blob: 0bac2374202dd5b81f61a66cbd58c80e620fc0a4 [file] [log] [blame]
Kostya Serebryany6e26fa92012-06-21 10:04:36 +00001//===-- 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"
Kostya Serebryany41960462012-06-26 14:23:32 +000022#include "sanitizer_libc.h"
Kostya Serebryany6e26fa92012-06-21 10:04:36 +000023
24namespace __sanitizer {
25
Kostya Serebryany5b014152012-06-22 13:00:50 +000026// Maps size class id to size and back.
Kostya Serebryany6e26fa92012-06-21 10:04:36 +000027class DefaultSizeClassMap {
28 private:
29 // Here we use a spline composed of 5 polynomials of oder 1.
30 // The first size class is l0, then the classes go with step s0
31 // untill they reach l1, after which they go with step s1 and so on.
32 // Steps should be powers of two for cheap division.
33 // The size of the last size class should be a power of two.
34 // There should be at most 256 size classes.
35 static const uptr l0 = 1 << 4;
36 static const uptr l1 = 1 << 9;
37 static const uptr l2 = 1 << 12;
38 static const uptr l3 = 1 << 15;
39 static const uptr l4 = 1 << 18;
40 static const uptr l5 = 1 << 21;
41
42 static const uptr s0 = 1 << 4;
43 static const uptr s1 = 1 << 6;
44 static const uptr s2 = 1 << 9;
45 static const uptr s3 = 1 << 12;
46 static const uptr s4 = 1 << 15;
47
48 static const uptr u0 = 0 + (l1 - l0) / s0;
49 static const uptr u1 = u0 + (l2 - l1) / s1;
50 static const uptr u2 = u1 + (l3 - l2) / s2;
51 static const uptr u3 = u2 + (l4 - l3) / s3;
52 static const uptr u4 = u3 + (l5 - l4) / s4;
53
54 public:
55 static const uptr kNumClasses = u4 + 1;
56 static const uptr kMaxSize = l5;
Kostya Serebryany5b014152012-06-22 13:00:50 +000057 static const uptr kMinSize = l0;
Kostya Serebryany6e26fa92012-06-21 10:04:36 +000058
59 COMPILER_CHECK(kNumClasses <= 256);
60 COMPILER_CHECK((kMaxSize & (kMaxSize - 1)) == 0);
61
Kostya Serebryany5b014152012-06-22 13:00:50 +000062 static uptr Size(uptr class_id) {
63 if (class_id <= u0) return l0 + s0 * (class_id - 0);
64 if (class_id <= u1) return l1 + s1 * (class_id - u0);
65 if (class_id <= u2) return l2 + s2 * (class_id - u1);
66 if (class_id <= u3) return l3 + s3 * (class_id - u2);
67 if (class_id <= u4) return l4 + s4 * (class_id - u3);
Kostya Serebryany6e26fa92012-06-21 10:04:36 +000068 return 0;
69 }
Kostya Serebryany5b014152012-06-22 13:00:50 +000070 static uptr ClassID(uptr size) {
Kostya Serebryany6e26fa92012-06-21 10:04:36 +000071 if (size <= l1) return 0 + (size - l0 + s0 - 1) / s0;
72 if (size <= l2) return u0 + (size - l1 + s1 - 1) / s1;
73 if (size <= l3) return u1 + (size - l2 + s2 - 1) / s2;
74 if (size <= l4) return u2 + (size - l3 + s3 - 1) / s3;
75 if (size <= l5) return u3 + (size - l4 + s4 - 1) / s4;
76 return 0;
77 }
78};
79
Kostya Serebryany5b014152012-06-22 13:00:50 +000080// Space: a portion of address space of kSpaceSize bytes starting at
81// a fixed address (kSpaceBeg). Both constants are powers of two and
82// kSpaceBeg is kSpaceSize-aligned.
83//
84// Region: a part of Space dedicated to a single size class.
85// There are kNumClasses Regions of equal size.
86//
87// UserChunk: a piece of memory returned to user.
88// MetaChunk: kMetadataSize bytes of metadata associated with a UserChunk.
89//
90// A Region looks like this:
91// UserChunk1 ... UserChunkN <gap> MetaChunkN ... MetaChunk1
92template <const uptr kSpaceBeg, const uptr kSpaceSize,
93 const uptr kMetadataSize, class SizeClassMap>
94class SizeClassAllocator64 {
95 public:
96 void Init() {
97 CHECK_EQ(AllocBeg(), reinterpret_cast<uptr>(MmapFixedNoReserve(
98 AllocBeg(), AllocSize())));
99 }
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000100
101 bool CanAllocate(uptr size, uptr alignment) {
102 return size <= SizeClassMap::kMaxSize &&
103 alignment <= SizeClassMap::kMaxSize;
104 }
105
106 void *Allocate(uptr size, uptr alignment) {
107 CHECK(CanAllocate(size, alignment));
Kostya Serebryany5b014152012-06-22 13:00:50 +0000108 return AllocateBySizeClass(SizeClassMap::ClassID(size));
109 }
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000110
Kostya Serebryany5b014152012-06-22 13:00:50 +0000111 void Deallocate(void *p) {
Kostya Serebryany100590f2012-06-25 14:53:49 +0000112 CHECK(PointerIsMine(p));
Kostya Serebryany5b014152012-06-22 13:00:50 +0000113 DeallocateBySizeClass(p, GetSizeClass(p));
114 }
115 bool PointerIsMine(void *p) {
116 return reinterpret_cast<uptr>(p) / kSpaceSize == kSpaceBeg / kSpaceSize;
117 }
118 uptr GetSizeClass(void *p) {
119 return (reinterpret_cast<uptr>(p) / kRegionSize) % kNumClasses;
120 }
121
Kostya Serebryany41960462012-06-26 14:23:32 +0000122 void *GetMetaData(void *p) {
Kostya Serebryany278ccda2012-06-22 16:13:28 +0000123 uptr class_id = GetSizeClass(p);
124 uptr chunk_idx = GetChunkIdx(reinterpret_cast<uptr>(p), class_id);
Kostya Serebryany41960462012-06-26 14:23:32 +0000125 return reinterpret_cast<void*>(kSpaceBeg + (kRegionSize * (class_id + 1)) -
126 (1 + chunk_idx) * kMetadataSize);
Kostya Serebryany278ccda2012-06-22 16:13:28 +0000127 }
128
Kostya Serebryany100590f2012-06-25 14:53:49 +0000129 uptr TotalMemoryUsed() {
Kostya Serebryany5b014152012-06-22 13:00:50 +0000130 uptr res = 0;
131 for (uptr i = 0; i < kNumClasses; i++)
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000132 res += GetRegionInfo(i)->allocated_user;
Kostya Serebryany5b014152012-06-22 13:00:50 +0000133 return res;
134 }
135
136 // Test-only.
137 void TestOnlyUnmap() {
138 UnmapOrDie(reinterpret_cast<void*>(AllocBeg()), AllocSize());
139 }
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000140
Kostya Serebryany5b014152012-06-22 13:00:50 +0000141 private:
142 static const uptr kNumClasses = 256; // Power of two <= 256
143 COMPILER_CHECK(kNumClasses <= SizeClassMap::kNumClasses);
144 static const uptr kRegionSize = kSpaceSize / kNumClasses;
Kostya Serebryany278ccda2012-06-22 16:13:28 +0000145 COMPILER_CHECK((kRegionSize >> 32) > 0); // kRegionSize must be >= 2^32.
Kostya Serebryany5b014152012-06-22 13:00:50 +0000146 // Populate the free list with at most this number of bytes at once
147 // or with one element if its size is greater.
148 static const uptr kPopulateSize = 1 << 18;
149
150 struct LifoListNode {
151 LifoListNode *next;
152 };
153
154 struct RegionInfo {
155 uptr mutex; // FIXME
156 LifoListNode *free_list;
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000157 uptr allocated_user; // Bytes allocated for user memory.
158 uptr allocated_meta; // Bytes allocated for metadata.
Kostya Serebryany6bbb5142012-06-25 14:31:59 +0000159 char padding[kCacheLineSize - 4 * sizeof(uptr)];
Kostya Serebryany5b014152012-06-22 13:00:50 +0000160 };
161 COMPILER_CHECK(sizeof(RegionInfo) == kCacheLineSize);
162
Kostya Serebryanyaad697e2012-06-25 14:58:17 +0000163 uptr AdditionalSize() {
Kostya Serebryany100590f2012-06-25 14:53:49 +0000164 uptr res = sizeof(RegionInfo) * kNumClasses;
165 CHECK_EQ(res % kPageSize, 0);
166 return res;
167 }
Kostya Serebryany5b014152012-06-22 13:00:50 +0000168 uptr AllocBeg() { return kSpaceBeg - AdditionalSize(); }
169 uptr AllocSize() { return kSpaceSize + AdditionalSize(); }
170
171 RegionInfo *GetRegionInfo(uptr class_id) {
172 CHECK_LT(class_id, kNumClasses);
173 RegionInfo *regions = reinterpret_cast<RegionInfo*>(kSpaceBeg);
174 return &regions[-1 - class_id];
175 }
176
177 void PushLifoList(LifoListNode **list, LifoListNode *node) {
178 node->next = *list;
179 *list = node;
180 }
181
182 LifoListNode *PopLifoList(LifoListNode **list) {
183 LifoListNode *res = *list;
Kostya Serebryany100590f2012-06-25 14:53:49 +0000184 *list = res->next;
Kostya Serebryany5b014152012-06-22 13:00:50 +0000185 return res;
186 }
187
Kostya Serebryany278ccda2012-06-22 16:13:28 +0000188 uptr GetChunkIdx(uptr chunk, uptr class_id) {
189 u32 offset = chunk % kRegionSize;
190 // Here we divide by a non-constant. This is costly.
191 // We require that kRegionSize is at least 2^32 so that offset is 32-bit.
192 // We save 2x by using 32-bit div, but may need to use a 256-way switch.
193 return offset / (u32)SizeClassMap::Size(class_id);
194 }
195
Kostya Serebryany5b014152012-06-22 13:00:50 +0000196 LifoListNode *PopulateFreeList(uptr class_id, RegionInfo *region) {
197 uptr size = SizeClassMap::Size(class_id);
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000198 uptr beg_idx = region->allocated_user;
Kostya Serebryany5b014152012-06-22 13:00:50 +0000199 uptr end_idx = beg_idx + kPopulateSize;
200 LifoListNode *res = 0;
201 uptr region_beg = kSpaceBeg + kRegionSize * class_id;
202 uptr idx = beg_idx;
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000203 uptr i = 0;
Kostya Serebryany5b014152012-06-22 13:00:50 +0000204 do { // do-while loop because we need to put at least one item.
205 uptr p = region_beg + idx;
206 PushLifoList(&res, reinterpret_cast<LifoListNode*>(p));
207 idx += size;
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000208 i++;
Kostya Serebryany5b014152012-06-22 13:00:50 +0000209 } while (idx < end_idx);
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000210 region->allocated_user += idx - beg_idx;
211 region->allocated_meta += i * kMetadataSize;
212 CHECK_LT(region->allocated_user + region->allocated_meta, kRegionSize);
Kostya Serebryany5b014152012-06-22 13:00:50 +0000213 return res;
214 }
215
216 void *AllocateBySizeClass(uptr class_id) {
217 CHECK_LT(class_id, kNumClasses);
218 RegionInfo *region = GetRegionInfo(class_id);
219 // FIXME: Lock region->mutex;
220 if (!region->free_list) {
221 region->free_list = PopulateFreeList(class_id, region);
222 }
223 CHECK_NE(region->free_list, 0);
224 LifoListNode *node = PopLifoList(&region->free_list);
225 return reinterpret_cast<void*>(node);
226 }
227
228 void DeallocateBySizeClass(void *p, uptr class_id) {
229 RegionInfo *region = GetRegionInfo(class_id);
230 // FIXME: Lock region->mutex;
231 PushLifoList(&region->free_list, reinterpret_cast<LifoListNode*>(p));
232 }
233};
234
Kostya Serebryany41960462012-06-26 14:23:32 +0000235// This class can (de)allocate only large chunks of memory using mmap/unmap.
236// The main purpose of this allocator is to cover large and rare allocation
237// sizes not covered by more efficient allocators (e.g. SizeClassAllocator64).
238// The result is always page-aligned.
239class LargeMmapAllocator {
240 public:
241 void Init() {
242 internal_memset(this, 0, sizeof(*this));
243 }
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000244 void *Allocate(uptr size, uptr alignment) {
245 CHECK_LE(alignment, kPageSize); // Not implemented. Do we need it?
Kostya Serebryany41960462012-06-26 14:23:32 +0000246 uptr map_size = RoundUpMapSize(size);
247 void *map = MmapOrDie(map_size, "LargeMmapAllocator");
248 void *res = reinterpret_cast<void*>(reinterpret_cast<uptr>(map)
249 + kPageSize);
250 Header *h = GetHeader(res);
251 h->size = size;
252 {
253 // FIXME: lock
254 h->next = list_;
255 h->prev = 0;
256 if (list_)
257 list_->prev = h;
258 list_ = h;
259 }
260 return res;
261 }
262
263 void Deallocate(void *p) {
264 Header *h = GetHeader(p);
265 uptr map_size = RoundUpMapSize(h->size);
266 {
267 // FIXME: lock
268 Header *prev = h->prev;
269 Header *next = h->next;
270 if (prev)
271 prev->next = next;
272 if (next)
273 next->prev = prev;
274 if (h == list_)
275 list_ = next;
276 }
277 UnmapOrDie(h, map_size);
278 }
279
280 uptr TotalMemoryUsed() {
281 // FIXME: lock
282 uptr res = 0;
283 for (Header *l = list_; l; l = l->next) {
284 res += RoundUpMapSize(l->size);
285 }
286 return res;
287 }
288
289 bool PointerIsMine(void *p) {
290 // Fast check.
291 if ((reinterpret_cast<uptr>(p) % kPageSize) != 0) return false;
292 // FIXME: lock
293 for (Header *l = list_; l; l = l->next) {
294 if (GetUser(l) == p) return true;
295 }
296 return false;
297 }
298
299 // At least kPageSize/2 metadata bytes is available.
300 void *GetMetaData(void *p) {
301 return GetHeader(p) + 1;
302 }
303
304 private:
305 struct Header {
306 uptr size;
307 Header *next;
308 Header *prev;
309 };
310
311 Header *GetHeader(void *p) {
312 return reinterpret_cast<Header*>(reinterpret_cast<uptr>(p) - kPageSize);
313 }
314
315 void *GetUser(Header *h) {
316 return reinterpret_cast<void*>(reinterpret_cast<uptr>(h) + kPageSize);
317 }
318
319 uptr RoundUpMapSize(uptr size) {
320 return RoundUpTo(size, kPageSize) + kPageSize;
321 }
322
323 Header *list_;
324 uptr lock_; // FIXME
325};
326
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000327// This class implements a complete memory allocator by using two
328// internal allocators:
329// PrimaryAllocator is efficient, but may not allocate some sizes (alignments).
330// When allocating 2^x bytes it should return 2^x aligned chunk.
331// SecondaryAllocator can allocate anything, but is not efficient.
332template <class PrimaryAllocator, class SecondaryAllocator>
333class CombinedAllocator {
334 public:
335 void Init() {
336 primary_.Init();
337 secondary_.Init();
338 }
339
340 void *Allocate(uptr size, uptr alignment) {
341 CHECK_GT(size, 0);
342 if (alignment > 8)
343 size = RoundUpTo(size, alignment);
344 void *res;
345 if (primary_.CanAllocate(size, alignment))
346 res = primary_.Allocate(size, alignment);
347 else
348 res = secondary_.Allocate(size, alignment);
349 if (alignment > 8)
350 CHECK_EQ(reinterpret_cast<uptr>(res) & (alignment - 1), 0);
351 return res;
352 }
353
354 void Deallocate(void *p) {
355 if (primary_.PointerIsMine(p))
356 primary_.Deallocate(p);
357 else
358 secondary_.Deallocate(p);
359 }
360
361 bool PointerIsMine(void *p) {
362 if (primary_.PointerIsMine(p))
363 return true;
364 return secondary_.PointerIsMine(p);
365 }
366
367 void *GetMetaData(void *p) {
368 if (primary_.PointerIsMine(p))
369 return primary_.GetMetaData(p);
370 return secondary_.GetMetaData(p);
371 }
372
373 uptr TotalMemoryUsed() {
374 return primary_.TotalMemoryUsed() + secondary_.TotalMemoryUsed();
375 }
376
377 void TestOnlyUnmap() { primary_.TestOnlyUnmap(); }
378
379 private:
380 PrimaryAllocator primary_;
381 SecondaryAllocator secondary_;
382};
383
Kostya Serebryany6e26fa92012-06-21 10:04:36 +0000384} // namespace __sanitizer
385
386#endif // SANITIZER_ALLOCATOR_H