2 * Copyright (C) 2014 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #include "Algorithm.h"
34 #include <type_traits>
39 // Repository for malloc sizing constants and calculations.
42 static const size_t kB = 1024;
43 static const size_t MB = kB * kB;
45 static const size_t alignment = 8;
46 static const size_t alignmentMask = alignment - 1ul;
48 static const size_t superChunkSize = 32 * MB;
50 static const size_t smallMax = 256;
51 static const size_t smallLineSize = 512;
52 static const size_t smallLineMask = ~(smallLineSize - 1ul);
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);
58 static const size_t mediumMax = 1024;
59 static const size_t mediumLineSize = 2048;
60 static const size_t mediumLineMask = ~(mediumLineSize - 1ul);
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);
66 static const size_t largeChunkSize = superChunkSize / 2;
67 static const size_t largeChunkOffset = 0;
68 static const size_t largeChunkMask = ~(largeChunkSize - 1ul);
70 static const size_t largeAlignment = 64;
71 static const size_t largeAlignmentShift = 6;
72 static_assert(1 << largeAlignmentShift == largeAlignment, "largeAlignmentShift be log2(largeAlignment).");
73 static const size_t largeMax = largeChunkSize * 99 / 100; // Plenty of room for metadata.
74 static const size_t largeMin = mediumMax;
76 static const size_t segregatedFreeListSearchDepth = 16;
78 static const uintptr_t typeMask = (superChunkSize - 1) & ~((superChunkSize / 4) - 1); // 4 taggable chunks
79 static const uintptr_t smallType = (superChunkSize + smallChunkOffset) & typeMask;
80 static const uintptr_t mediumType = (superChunkSize + mediumChunkOffset) & typeMask;
81 static const uintptr_t largeTypeMask = ~(mediumType & smallType);
82 static const uintptr_t smallOrMediumTypeMask = mediumType & smallType;
83 static const uintptr_t smallOrMediumSmallTypeMask = smallType ^ mediumType; // Only valid if object is known to be small or medium.
85 static const size_t deallocatorLogCapacity = 256;
87 static const size_t smallLineCacheCapacity = 16;
88 static const size_t mediumLineCacheCapacity = 8;
90 static const std::chrono::milliseconds scavengeSleepDuration = std::chrono::milliseconds(512);
92 inline size_t smallSizeClassFor(size_t size)
94 static const size_t smallSizeClassMask = (smallMax / alignment) - 1;
95 return mask((size - 1ul) / alignment, smallSizeClassMask);
98 inline size_t mediumSizeClassFor(size_t size)
100 static const size_t mediumSizeClassMask = (mediumMax / alignment) - 1;
101 return mask((size - 1ul) / alignment, mediumSizeClassMask);
105 using namespace Sizes;
107 } // namespace bmalloc