Kostya Serebryany | 6e26fa9 | 2012-06-21 10:04:36 +0000 | [diff] [blame] | 1 | //===-- sanitizer_allocator64.h ---------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // Specialized allocator which works only in 64-bit address space. |
| 10 | // To be used by ThreadSanitizer, MemorySanitizer and possibly other tools. |
| 11 | // The main feature of this allocator is that the header is located far away |
| 12 | // from the user memory region, so that the tool does not use extra shadow |
| 13 | // for the header. |
| 14 | // |
| 15 | // Status: not yet ready. |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | #ifndef SANITIZER_ALLOCATOR_H |
| 18 | #define SANITIZER_ALLOCATOR_H |
| 19 | |
| 20 | #include "sanitizer_common.h" |
| 21 | #include "sanitizer_internal_defs.h" |
| 22 | |
| 23 | namespace __sanitizer { |
| 24 | |
Kostya Serebryany | 5b01415 | 2012-06-22 13:00:50 +0000 | [diff] [blame^] | 25 | // Maps size class id to size and back. |
Kostya Serebryany | 6e26fa9 | 2012-06-21 10:04:36 +0000 | [diff] [blame] | 26 | class DefaultSizeClassMap { |
| 27 | private: |
| 28 | // Here we use a spline composed of 5 polynomials of oder 1. |
| 29 | // The first size class is l0, then the classes go with step s0 |
| 30 | // untill they reach l1, after which they go with step s1 and so on. |
| 31 | // Steps should be powers of two for cheap division. |
| 32 | // The size of the last size class should be a power of two. |
| 33 | // There should be at most 256 size classes. |
| 34 | static const uptr l0 = 1 << 4; |
| 35 | static const uptr l1 = 1 << 9; |
| 36 | static const uptr l2 = 1 << 12; |
| 37 | static const uptr l3 = 1 << 15; |
| 38 | static const uptr l4 = 1 << 18; |
| 39 | static const uptr l5 = 1 << 21; |
| 40 | |
| 41 | static const uptr s0 = 1 << 4; |
| 42 | static const uptr s1 = 1 << 6; |
| 43 | static const uptr s2 = 1 << 9; |
| 44 | static const uptr s3 = 1 << 12; |
| 45 | static const uptr s4 = 1 << 15; |
| 46 | |
| 47 | static const uptr u0 = 0 + (l1 - l0) / s0; |
| 48 | static const uptr u1 = u0 + (l2 - l1) / s1; |
| 49 | static const uptr u2 = u1 + (l3 - l2) / s2; |
| 50 | static const uptr u3 = u2 + (l4 - l3) / s3; |
| 51 | static const uptr u4 = u3 + (l5 - l4) / s4; |
| 52 | |
| 53 | public: |
| 54 | static const uptr kNumClasses = u4 + 1; |
| 55 | static const uptr kMaxSize = l5; |
Kostya Serebryany | 5b01415 | 2012-06-22 13:00:50 +0000 | [diff] [blame^] | 56 | static const uptr kMinSize = l0; |
Kostya Serebryany | 6e26fa9 | 2012-06-21 10:04:36 +0000 | [diff] [blame] | 57 | |
| 58 | COMPILER_CHECK(kNumClasses <= 256); |
| 59 | COMPILER_CHECK((kMaxSize & (kMaxSize - 1)) == 0); |
| 60 | |
Kostya Serebryany | 5b01415 | 2012-06-22 13:00:50 +0000 | [diff] [blame^] | 61 | static uptr Size(uptr class_id) { |
| 62 | if (class_id <= u0) return l0 + s0 * (class_id - 0); |
| 63 | if (class_id <= u1) return l1 + s1 * (class_id - u0); |
| 64 | if (class_id <= u2) return l2 + s2 * (class_id - u1); |
| 65 | if (class_id <= u3) return l3 + s3 * (class_id - u2); |
| 66 | if (class_id <= u4) return l4 + s4 * (class_id - u3); |
Kostya Serebryany | 6e26fa9 | 2012-06-21 10:04:36 +0000 | [diff] [blame] | 67 | return 0; |
| 68 | } |
Kostya Serebryany | 5b01415 | 2012-06-22 13:00:50 +0000 | [diff] [blame^] | 69 | static uptr ClassID(uptr size) { |
Kostya Serebryany | 6e26fa9 | 2012-06-21 10:04:36 +0000 | [diff] [blame] | 70 | if (size <= l1) return 0 + (size - l0 + s0 - 1) / s0; |
| 71 | if (size <= l2) return u0 + (size - l1 + s1 - 1) / s1; |
| 72 | if (size <= l3) return u1 + (size - l2 + s2 - 1) / s2; |
| 73 | if (size <= l4) return u2 + (size - l3 + s3 - 1) / s3; |
| 74 | if (size <= l5) return u3 + (size - l4 + s4 - 1) / s4; |
| 75 | return 0; |
| 76 | } |
| 77 | }; |
| 78 | |
Kostya Serebryany | 5b01415 | 2012-06-22 13:00:50 +0000 | [diff] [blame^] | 79 | // Space: a portion of address space of kSpaceSize bytes starting at |
| 80 | // a fixed address (kSpaceBeg). Both constants are powers of two and |
| 81 | // kSpaceBeg is kSpaceSize-aligned. |
| 82 | // |
| 83 | // Region: a part of Space dedicated to a single size class. |
| 84 | // There are kNumClasses Regions of equal size. |
| 85 | // |
| 86 | // UserChunk: a piece of memory returned to user. |
| 87 | // MetaChunk: kMetadataSize bytes of metadata associated with a UserChunk. |
| 88 | // |
| 89 | // A Region looks like this: |
| 90 | // UserChunk1 ... UserChunkN <gap> MetaChunkN ... MetaChunk1 |
| 91 | template <const uptr kSpaceBeg, const uptr kSpaceSize, |
| 92 | const uptr kMetadataSize, class SizeClassMap> |
| 93 | class SizeClassAllocator64 { |
| 94 | public: |
| 95 | void Init() { |
| 96 | CHECK_EQ(AllocBeg(), reinterpret_cast<uptr>(MmapFixedNoReserve( |
| 97 | AllocBeg(), AllocSize()))); |
| 98 | } |
| 99 | void *Allocate(uptr size) { |
| 100 | CHECK_LE(size, SizeClassMap::kMaxSize); |
| 101 | return AllocateBySizeClass(SizeClassMap::ClassID(size)); |
| 102 | } |
| 103 | void Deallocate(void *p) { |
| 104 | DeallocateBySizeClass(p, GetSizeClass(p)); |
| 105 | } |
| 106 | bool PointerIsMine(void *p) { |
| 107 | return reinterpret_cast<uptr>(p) / kSpaceSize == kSpaceBeg / kSpaceSize; |
| 108 | } |
| 109 | uptr GetSizeClass(void *p) { |
| 110 | return (reinterpret_cast<uptr>(p) / kRegionSize) % kNumClasses; |
| 111 | } |
| 112 | |
| 113 | uptr TotalMemoryUsedIncludingFreeLists() { |
| 114 | uptr res = 0; |
| 115 | for (uptr i = 0; i < kNumClasses; i++) |
| 116 | res += GetRegionInfo(i)->allocated; |
| 117 | return res; |
| 118 | } |
| 119 | |
| 120 | // Test-only. |
| 121 | void TestOnlyUnmap() { |
| 122 | UnmapOrDie(reinterpret_cast<void*>(AllocBeg()), AllocSize()); |
| 123 | } |
| 124 | private: |
| 125 | static const uptr kNumClasses = 256; // Power of two <= 256 |
| 126 | COMPILER_CHECK(kNumClasses <= SizeClassMap::kNumClasses); |
| 127 | static const uptr kRegionSize = kSpaceSize / kNumClasses; |
| 128 | // Populate the free list with at most this number of bytes at once |
| 129 | // or with one element if its size is greater. |
| 130 | static const uptr kPopulateSize = 1 << 18; |
| 131 | |
| 132 | struct LifoListNode { |
| 133 | LifoListNode *next; |
| 134 | }; |
| 135 | |
| 136 | struct RegionInfo { |
| 137 | uptr mutex; // FIXME |
| 138 | LifoListNode *free_list; |
| 139 | uptr allocated; |
| 140 | char padding[kCacheLineSize - |
| 141 | sizeof(mutex) - sizeof(free_list) - sizeof(allocated)]; |
| 142 | }; |
| 143 | COMPILER_CHECK(sizeof(RegionInfo) == kCacheLineSize); |
| 144 | |
| 145 | uptr AdditionalSize() { return sizeof(RegionInfo) * kNumClasses; } |
| 146 | uptr AllocBeg() { return kSpaceBeg - AdditionalSize(); } |
| 147 | uptr AllocSize() { return kSpaceSize + AdditionalSize(); } |
| 148 | |
| 149 | RegionInfo *GetRegionInfo(uptr class_id) { |
| 150 | CHECK_LT(class_id, kNumClasses); |
| 151 | RegionInfo *regions = reinterpret_cast<RegionInfo*>(kSpaceBeg); |
| 152 | return ®ions[-1 - class_id]; |
| 153 | } |
| 154 | |
| 155 | void PushLifoList(LifoListNode **list, LifoListNode *node) { |
| 156 | node->next = *list; |
| 157 | *list = node; |
| 158 | } |
| 159 | |
| 160 | LifoListNode *PopLifoList(LifoListNode **list) { |
| 161 | LifoListNode *res = *list; |
| 162 | *list = (*list)->next; |
| 163 | return res; |
| 164 | } |
| 165 | |
| 166 | LifoListNode *PopulateFreeList(uptr class_id, RegionInfo *region) { |
| 167 | uptr size = SizeClassMap::Size(class_id); |
| 168 | uptr beg_idx = region->allocated; |
| 169 | uptr end_idx = beg_idx + kPopulateSize; |
| 170 | LifoListNode *res = 0; |
| 171 | uptr region_beg = kSpaceBeg + kRegionSize * class_id; |
| 172 | uptr idx = beg_idx; |
| 173 | do { // do-while loop because we need to put at least one item. |
| 174 | uptr p = region_beg + idx; |
| 175 | PushLifoList(&res, reinterpret_cast<LifoListNode*>(p)); |
| 176 | idx += size; |
| 177 | } while (idx < end_idx); |
| 178 | CHECK_LT(idx, kRegionSize); |
| 179 | region->allocated += idx - beg_idx; |
| 180 | return res; |
| 181 | } |
| 182 | |
| 183 | void *AllocateBySizeClass(uptr class_id) { |
| 184 | CHECK_LT(class_id, kNumClasses); |
| 185 | RegionInfo *region = GetRegionInfo(class_id); |
| 186 | // FIXME: Lock region->mutex; |
| 187 | if (!region->free_list) { |
| 188 | region->free_list = PopulateFreeList(class_id, region); |
| 189 | } |
| 190 | CHECK_NE(region->free_list, 0); |
| 191 | LifoListNode *node = PopLifoList(®ion->free_list); |
| 192 | return reinterpret_cast<void*>(node); |
| 193 | } |
| 194 | |
| 195 | void DeallocateBySizeClass(void *p, uptr class_id) { |
| 196 | RegionInfo *region = GetRegionInfo(class_id); |
| 197 | // FIXME: Lock region->mutex; |
| 198 | PushLifoList(®ion->free_list, reinterpret_cast<LifoListNode*>(p)); |
| 199 | } |
| 200 | }; |
| 201 | |
Kostya Serebryany | 6e26fa9 | 2012-06-21 10:04:36 +0000 | [diff] [blame] | 202 | } // namespace __sanitizer |
| 203 | |
| 204 | #endif // SANITIZER_ALLOCATOR_H |