1/*
2 * Copyright (C) 2014 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef Sizes_h
27#define Sizes_h
28
29#include "Algorithm.h"
30#include <algorithm>
31#include <cstdint>
32#include <cstddef>
33#include <limits>
34#include <type_traits>
35#include <chrono>
36
37namespace bmalloc {
38
39// Repository for malloc sizing constants and calculations.
40
41namespace Sizes {
42    static const size_t kB = 1024;
43    static const size_t MB = kB * kB;
44
45    static const size_t alignment = 8;
46    static const size_t alignmentMask = alignment - 1ul;
47
48    static const size_t superChunkSize = 32 * MB;
49
50    static const size_t smallMax = 256;
51    static const size_t smallLineSize = 512;
52    static const size_t smallLineMask = ~(smallLineSize - 1ul);
53
54    static const size_t smallChunkSize = superChunkSize / 4;
55    static const size_t smallChunkOffset = superChunkSize * 3 / 4;
56    static const size_t smallChunkMask = ~(smallChunkSize - 1ul);
57
58    static const size_t mediumMax = 1024;
59    static const size_t mediumLineSize = 2048;
60    static const size_t mediumLineMask = ~(mediumLineSize - 1ul);
61
62    static const size_t mediumChunkSize = superChunkSize / 4;
63    static const size_t mediumChunkOffset = superChunkSize * 2 / 4;
64    static const size_t mediumChunkMask = ~(mediumChunkSize - 1ul);
65
66    static const size_t largeChunkSize = superChunkSize / 2;
67    static const size_t largeChunkOffset = 0;
68    static const size_t largeChunkMask = ~(largeChunkSize - 1ul);
69
70    static const size_t largeAlignment = 64;
71    static const size_t largeMax = largeChunkSize * 99 / 100; // Plenty of room for metadata.
72    static const size_t largeMin = 1024;
73
74    static const size_t segregatedFreeListSearchDepth = 16;
75
76    static const uintptr_t typeMask = (superChunkSize - 1) & ~((superChunkSize / 4) - 1); // 4 taggable chunks
77    static const uintptr_t smallType = (superChunkSize + smallChunkOffset) & typeMask;
78    static const uintptr_t mediumType = (superChunkSize + mediumChunkOffset) & typeMask;
79    static const uintptr_t largeTypeMask = ~(mediumType & smallType);
80    static const uintptr_t smallOrMediumTypeMask = mediumType & smallType;
81    static const uintptr_t smallOrMediumSmallTypeMask = smallType ^ mediumType; // Only valid if object is known to be small or medium.
82
83    static const size_t deallocatorLogCapacity = 256;
84
85    static const size_t smallLineCacheCapacity = 16;
86    static const size_t mediumLineCacheCapacity = 8;
87
88    static const size_t smallAllocatorLogCapacity = 16;
89    static const size_t mediumAllocatorLogCapacity = 8;
90
91    static const std::chrono::milliseconds scavengeSleepDuration = std::chrono::milliseconds(512);
92
93    inline size_t smallSizeClassFor(size_t size)
94    {
95        static const size_t smallSizeClassMask = (smallMax / alignment) - 1;
96        return mask((size - 1ul) / alignment, smallSizeClassMask);
97    }
98};
99
100using namespace Sizes;
101
102} // namespace bmalloc
103
104#endif // Sizes_h
105