blob: 679e07dfa63b210978261f1892c9edcdde06218d [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 Serebryany78e973f2012-07-06 09:26:01 +000023#include "sanitizer_list.h"
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +000024#include "sanitizer_mutex.h"
Kostya Serebryany6e26fa92012-06-21 10:04:36 +000025
26namespace __sanitizer {
27
Kostya Serebryany5b014152012-06-22 13:00:50 +000028// Maps size class id to size and back.
Kostya Serebryany6e26fa92012-06-21 10:04:36 +000029class DefaultSizeClassMap {
30 private:
31 // Here we use a spline composed of 5 polynomials of oder 1.
32 // The first size class is l0, then the classes go with step s0
33 // untill they reach l1, after which they go with step s1 and so on.
34 // Steps should be powers of two for cheap division.
35 // The size of the last size class should be a power of two.
36 // There should be at most 256 size classes.
37 static const uptr l0 = 1 << 4;
38 static const uptr l1 = 1 << 9;
39 static const uptr l2 = 1 << 12;
40 static const uptr l3 = 1 << 15;
41 static const uptr l4 = 1 << 18;
42 static const uptr l5 = 1 << 21;
43
44 static const uptr s0 = 1 << 4;
45 static const uptr s1 = 1 << 6;
46 static const uptr s2 = 1 << 9;
47 static const uptr s3 = 1 << 12;
48 static const uptr s4 = 1 << 15;
49
50 static const uptr u0 = 0 + (l1 - l0) / s0;
51 static const uptr u1 = u0 + (l2 - l1) / s1;
52 static const uptr u2 = u1 + (l3 - l2) / s2;
53 static const uptr u3 = u2 + (l4 - l3) / s3;
54 static const uptr u4 = u3 + (l5 - l4) / s4;
55
56 public:
57 static const uptr kNumClasses = u4 + 1;
58 static const uptr kMaxSize = l5;
Kostya Serebryany5b014152012-06-22 13:00:50 +000059 static const uptr kMinSize = l0;
Kostya Serebryany6e26fa92012-06-21 10:04:36 +000060
61 COMPILER_CHECK(kNumClasses <= 256);
62 COMPILER_CHECK((kMaxSize & (kMaxSize - 1)) == 0);
63
Kostya Serebryany5b014152012-06-22 13:00:50 +000064 static uptr Size(uptr class_id) {
65 if (class_id <= u0) return l0 + s0 * (class_id - 0);
66 if (class_id <= u1) return l1 + s1 * (class_id - u0);
67 if (class_id <= u2) return l2 + s2 * (class_id - u1);
68 if (class_id <= u3) return l3 + s3 * (class_id - u2);
69 if (class_id <= u4) return l4 + s4 * (class_id - u3);
Kostya Serebryany6e26fa92012-06-21 10:04:36 +000070 return 0;
71 }
Kostya Serebryany5b014152012-06-22 13:00:50 +000072 static uptr ClassID(uptr size) {
Kostya Serebryany6e26fa92012-06-21 10:04:36 +000073 if (size <= l1) return 0 + (size - l0 + s0 - 1) / s0;
74 if (size <= l2) return u0 + (size - l1 + s1 - 1) / s1;
75 if (size <= l3) return u1 + (size - l2 + s2 - 1) / s2;
76 if (size <= l4) return u2 + (size - l3 + s3 - 1) / s3;
77 if (size <= l5) return u3 + (size - l4 + s4 - 1) / s4;
78 return 0;
79 }
80};
81
Kostya Serebryanyd1e60942012-07-06 13:46:49 +000082struct AllocatorListNode {
83 AllocatorListNode *next;
84};
85
86typedef IntrusiveList<AllocatorListNode> AllocatorFreeList;
87
88
Kostya Serebryany5b014152012-06-22 13:00:50 +000089// Space: a portion of address space of kSpaceSize bytes starting at
90// a fixed address (kSpaceBeg). Both constants are powers of two and
91// kSpaceBeg is kSpaceSize-aligned.
92//
93// Region: a part of Space dedicated to a single size class.
94// There are kNumClasses Regions of equal size.
95//
96// UserChunk: a piece of memory returned to user.
97// MetaChunk: kMetadataSize bytes of metadata associated with a UserChunk.
98//
99// A Region looks like this:
100// UserChunk1 ... UserChunkN <gap> MetaChunkN ... MetaChunk1
101template <const uptr kSpaceBeg, const uptr kSpaceSize,
102 const uptr kMetadataSize, class SizeClassMap>
103class SizeClassAllocator64 {
104 public:
105 void Init() {
106 CHECK_EQ(AllocBeg(), reinterpret_cast<uptr>(MmapFixedNoReserve(
107 AllocBeg(), AllocSize())));
108 }
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000109
110 bool CanAllocate(uptr size, uptr alignment) {
111 return size <= SizeClassMap::kMaxSize &&
112 alignment <= SizeClassMap::kMaxSize;
113 }
114
115 void *Allocate(uptr size, uptr alignment) {
116 CHECK(CanAllocate(size, alignment));
Kostya Serebryany5b014152012-06-22 13:00:50 +0000117 return AllocateBySizeClass(SizeClassMap::ClassID(size));
118 }
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000119
Kostya Serebryany5b014152012-06-22 13:00:50 +0000120 void Deallocate(void *p) {
Kostya Serebryany100590f2012-06-25 14:53:49 +0000121 CHECK(PointerIsMine(p));
Kostya Serebryany5b014152012-06-22 13:00:50 +0000122 DeallocateBySizeClass(p, GetSizeClass(p));
123 }
Kostya Serebryanyd1e60942012-07-06 13:46:49 +0000124
125 // Allocate several chunks of the given class_id.
126 void BulkAllocate(uptr class_id, AllocatorFreeList *free_list) {
127 CHECK_LT(class_id, kNumClasses);
128 RegionInfo *region = GetRegionInfo(class_id);
129 SpinMutexLock l(&region->mutex);
130 if (region->free_list.empty()) {
131 PopulateFreeList(class_id, region);
132 }
133 CHECK(!region->free_list.empty());
134 // Just take as many chunks as we have in the free list now.
135 // FIXME: this might be too much.
136 free_list->append_front(&region->free_list);
137 CHECK(region->free_list.empty());
138 }
139
140 // Swallow the entire free_list for the given class_id.
141 void BulkDeallocate(uptr class_id, AllocatorFreeList *free_list) {
142 CHECK_LT(class_id, kNumClasses);
143 RegionInfo *region = GetRegionInfo(class_id);
144 SpinMutexLock l(&region->mutex);
145 region->free_list.append_front(free_list);
146 }
147
Kostya Serebryany5b014152012-06-22 13:00:50 +0000148 bool PointerIsMine(void *p) {
149 return reinterpret_cast<uptr>(p) / kSpaceSize == kSpaceBeg / kSpaceSize;
150 }
151 uptr GetSizeClass(void *p) {
152 return (reinterpret_cast<uptr>(p) / kRegionSize) % kNumClasses;
153 }
154
Kostya Serebryany41960462012-06-26 14:23:32 +0000155 void *GetMetaData(void *p) {
Kostya Serebryany278ccda2012-06-22 16:13:28 +0000156 uptr class_id = GetSizeClass(p);
157 uptr chunk_idx = GetChunkIdx(reinterpret_cast<uptr>(p), class_id);
Kostya Serebryany41960462012-06-26 14:23:32 +0000158 return reinterpret_cast<void*>(kSpaceBeg + (kRegionSize * (class_id + 1)) -
159 (1 + chunk_idx) * kMetadataSize);
Kostya Serebryany278ccda2012-06-22 16:13:28 +0000160 }
161
Kostya Serebryany100590f2012-06-25 14:53:49 +0000162 uptr TotalMemoryUsed() {
Kostya Serebryany5b014152012-06-22 13:00:50 +0000163 uptr res = 0;
164 for (uptr i = 0; i < kNumClasses; i++)
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000165 res += GetRegionInfo(i)->allocated_user;
Kostya Serebryany5b014152012-06-22 13:00:50 +0000166 return res;
167 }
168
169 // Test-only.
170 void TestOnlyUnmap() {
171 UnmapOrDie(reinterpret_cast<void*>(AllocBeg()), AllocSize());
172 }
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000173
Kostya Serebryany5b014152012-06-22 13:00:50 +0000174 static const uptr kNumClasses = 256; // Power of two <= 256
Kostya Serebryanyd1e60942012-07-06 13:46:49 +0000175
176 private:
Kostya Serebryany5b014152012-06-22 13:00:50 +0000177 COMPILER_CHECK(kNumClasses <= SizeClassMap::kNumClasses);
178 static const uptr kRegionSize = kSpaceSize / kNumClasses;
Kostya Serebryany278ccda2012-06-22 16:13:28 +0000179 COMPILER_CHECK((kRegionSize >> 32) > 0); // kRegionSize must be >= 2^32.
Kostya Serebryany5b014152012-06-22 13:00:50 +0000180 // Populate the free list with at most this number of bytes at once
181 // or with one element if its size is greater.
182 static const uptr kPopulateSize = 1 << 18;
183
Kostya Serebryany5b014152012-06-22 13:00:50 +0000184 struct RegionInfo {
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000185 SpinMutex mutex;
Kostya Serebryanyd1e60942012-07-06 13:46:49 +0000186 AllocatorFreeList free_list;
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000187 uptr allocated_user; // Bytes allocated for user memory.
188 uptr allocated_meta; // Bytes allocated for metadata.
Kostya Serebryanyd1e60942012-07-06 13:46:49 +0000189 char padding[kCacheLineSize - 3 * sizeof(uptr) - sizeof(AllocatorFreeList)];
Kostya Serebryany5b014152012-06-22 13:00:50 +0000190 };
191 COMPILER_CHECK(sizeof(RegionInfo) == kCacheLineSize);
192
Kostya Serebryanyaad697e2012-06-25 14:58:17 +0000193 uptr AdditionalSize() {
Kostya Serebryany100590f2012-06-25 14:53:49 +0000194 uptr res = sizeof(RegionInfo) * kNumClasses;
195 CHECK_EQ(res % kPageSize, 0);
196 return res;
197 }
Kostya Serebryany5b014152012-06-22 13:00:50 +0000198 uptr AllocBeg() { return kSpaceBeg - AdditionalSize(); }
199 uptr AllocSize() { return kSpaceSize + AdditionalSize(); }
200
201 RegionInfo *GetRegionInfo(uptr class_id) {
202 CHECK_LT(class_id, kNumClasses);
203 RegionInfo *regions = reinterpret_cast<RegionInfo*>(kSpaceBeg);
204 return &regions[-1 - class_id];
205 }
206
Kostya Serebryany278ccda2012-06-22 16:13:28 +0000207 uptr GetChunkIdx(uptr chunk, uptr class_id) {
208 u32 offset = chunk % kRegionSize;
209 // Here we divide by a non-constant. This is costly.
210 // We require that kRegionSize is at least 2^32 so that offset is 32-bit.
211 // We save 2x by using 32-bit div, but may need to use a 256-way switch.
212 return offset / (u32)SizeClassMap::Size(class_id);
213 }
214
Kostya Serebryany78e973f2012-07-06 09:26:01 +0000215 void PopulateFreeList(uptr class_id, RegionInfo *region) {
Kostya Serebryany5b014152012-06-22 13:00:50 +0000216 uptr size = SizeClassMap::Size(class_id);
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000217 uptr beg_idx = region->allocated_user;
Kostya Serebryany5b014152012-06-22 13:00:50 +0000218 uptr end_idx = beg_idx + kPopulateSize;
Kostya Serebryany78e973f2012-07-06 09:26:01 +0000219 region->free_list.clear();
Kostya Serebryany5b014152012-06-22 13:00:50 +0000220 uptr region_beg = kSpaceBeg + kRegionSize * class_id;
221 uptr idx = beg_idx;
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000222 uptr i = 0;
Kostya Serebryany5b014152012-06-22 13:00:50 +0000223 do { // do-while loop because we need to put at least one item.
224 uptr p = region_beg + idx;
Kostya Serebryanyd1e60942012-07-06 13:46:49 +0000225 region->free_list.push_front(reinterpret_cast<AllocatorListNode*>(p));
Kostya Serebryany5b014152012-06-22 13:00:50 +0000226 idx += size;
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000227 i++;
Kostya Serebryany5b014152012-06-22 13:00:50 +0000228 } while (idx < end_idx);
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000229 region->allocated_user += idx - beg_idx;
230 region->allocated_meta += i * kMetadataSize;
231 CHECK_LT(region->allocated_user + region->allocated_meta, kRegionSize);
Kostya Serebryany5b014152012-06-22 13:00:50 +0000232 }
233
234 void *AllocateBySizeClass(uptr class_id) {
235 CHECK_LT(class_id, kNumClasses);
236 RegionInfo *region = GetRegionInfo(class_id);
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000237 SpinMutexLock l(&region->mutex);
Kostya Serebryany78e973f2012-07-06 09:26:01 +0000238 if (region->free_list.empty()) {
239 PopulateFreeList(class_id, region);
Kostya Serebryany5b014152012-06-22 13:00:50 +0000240 }
Kostya Serebryany78e973f2012-07-06 09:26:01 +0000241 CHECK(!region->free_list.empty());
Kostya Serebryanyd1e60942012-07-06 13:46:49 +0000242 AllocatorListNode *node = region->free_list.front();
Kostya Serebryany78e973f2012-07-06 09:26:01 +0000243 region->free_list.pop_front();
Kostya Serebryany5b014152012-06-22 13:00:50 +0000244 return reinterpret_cast<void*>(node);
245 }
246
247 void DeallocateBySizeClass(void *p, uptr class_id) {
248 RegionInfo *region = GetRegionInfo(class_id);
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000249 SpinMutexLock l(&region->mutex);
Kostya Serebryanyd1e60942012-07-06 13:46:49 +0000250 region->free_list.push_front(reinterpret_cast<AllocatorListNode*>(p));
Kostya Serebryany5b014152012-06-22 13:00:50 +0000251 }
252};
253
Kostya Serebryanyd1e60942012-07-06 13:46:49 +0000254// Objects of this type should be used as local caches for SizeClassAllocator64.
255// Since the typical use of this class is to have one object per thread in TLS,
256// is has to be POD.
257template<const uptr kNumClasses, class SizeClassAllocator>
258struct SizeClassAllocatorLocalCache {
259 // Don't need to call Init if the object is a global (i.e. zero-initialized).
260 void Init() {
261 internal_memset(this, 0, sizeof(*this));
262 }
263
264 void *Allocate(SizeClassAllocator *allocator, uptr class_id) {
265 CHECK_LT(class_id, kNumClasses);
266 AllocatorFreeList *free_list = &free_lists_[class_id];
267 if (free_list->empty())
268 allocator->BulkAllocate(class_id, free_list);
269 CHECK(!free_list->empty());
270 void *res = free_list->front();
271 free_list->pop_front();
272 return res;
273 }
274
275 void Deallocate(SizeClassAllocator *allocator, uptr class_id, void *p) {
276 CHECK_LT(class_id, kNumClasses);
277 free_lists_[class_id].push_front(reinterpret_cast<AllocatorListNode*>(p));
278 }
279
280 void Drain(SizeClassAllocator *allocator) {
281 for (uptr i = 0; i < kNumClasses; i++) {
282 allocator->BulkDeallocate(i, &free_lists_[i]);
283 CHECK(free_lists_[i].empty());
284 }
285 }
286
287 // private:
288 AllocatorFreeList free_lists_[kNumClasses];
289};
290
Kostya Serebryany41960462012-06-26 14:23:32 +0000291// This class can (de)allocate only large chunks of memory using mmap/unmap.
292// The main purpose of this allocator is to cover large and rare allocation
293// sizes not covered by more efficient allocators (e.g. SizeClassAllocator64).
294// The result is always page-aligned.
295class LargeMmapAllocator {
296 public:
297 void Init() {
298 internal_memset(this, 0, sizeof(*this));
299 }
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000300 void *Allocate(uptr size, uptr alignment) {
301 CHECK_LE(alignment, kPageSize); // Not implemented. Do we need it?
Kostya Serebryany41960462012-06-26 14:23:32 +0000302 uptr map_size = RoundUpMapSize(size);
303 void *map = MmapOrDie(map_size, "LargeMmapAllocator");
304 void *res = reinterpret_cast<void*>(reinterpret_cast<uptr>(map)
305 + kPageSize);
306 Header *h = GetHeader(res);
307 h->size = size;
308 {
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000309 SpinMutexLock l(&mutex_);
Kostya Serebryany41960462012-06-26 14:23:32 +0000310 h->next = list_;
311 h->prev = 0;
312 if (list_)
313 list_->prev = h;
314 list_ = h;
315 }
316 return res;
317 }
318
319 void Deallocate(void *p) {
320 Header *h = GetHeader(p);
321 uptr map_size = RoundUpMapSize(h->size);
322 {
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000323 SpinMutexLock l(&mutex_);
Kostya Serebryany41960462012-06-26 14:23:32 +0000324 Header *prev = h->prev;
325 Header *next = h->next;
326 if (prev)
327 prev->next = next;
328 if (next)
329 next->prev = prev;
330 if (h == list_)
331 list_ = next;
332 }
333 UnmapOrDie(h, map_size);
334 }
335
336 uptr TotalMemoryUsed() {
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000337 SpinMutexLock l(&mutex_);
Kostya Serebryany41960462012-06-26 14:23:32 +0000338 uptr res = 0;
339 for (Header *l = list_; l; l = l->next) {
340 res += RoundUpMapSize(l->size);
341 }
342 return res;
343 }
344
345 bool PointerIsMine(void *p) {
346 // Fast check.
347 if ((reinterpret_cast<uptr>(p) % kPageSize) != 0) return false;
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000348 SpinMutexLock l(&mutex_);
Kostya Serebryany41960462012-06-26 14:23:32 +0000349 for (Header *l = list_; l; l = l->next) {
350 if (GetUser(l) == p) return true;
351 }
352 return false;
353 }
354
355 // At least kPageSize/2 metadata bytes is available.
356 void *GetMetaData(void *p) {
357 return GetHeader(p) + 1;
358 }
359
360 private:
361 struct Header {
362 uptr size;
363 Header *next;
364 Header *prev;
365 };
366
367 Header *GetHeader(void *p) {
368 return reinterpret_cast<Header*>(reinterpret_cast<uptr>(p) - kPageSize);
369 }
370
371 void *GetUser(Header *h) {
372 return reinterpret_cast<void*>(reinterpret_cast<uptr>(h) + kPageSize);
373 }
374
375 uptr RoundUpMapSize(uptr size) {
376 return RoundUpTo(size, kPageSize) + kPageSize;
377 }
378
379 Header *list_;
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000380 SpinMutex mutex_;
Kostya Serebryany41960462012-06-26 14:23:32 +0000381};
382
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000383// This class implements a complete memory allocator by using two
384// internal allocators:
385// PrimaryAllocator is efficient, but may not allocate some sizes (alignments).
386// When allocating 2^x bytes it should return 2^x aligned chunk.
387// SecondaryAllocator can allocate anything, but is not efficient.
388template <class PrimaryAllocator, class SecondaryAllocator>
389class CombinedAllocator {
390 public:
391 void Init() {
392 primary_.Init();
393 secondary_.Init();
394 }
395
396 void *Allocate(uptr size, uptr alignment) {
397 CHECK_GT(size, 0);
398 if (alignment > 8)
399 size = RoundUpTo(size, alignment);
400 void *res;
401 if (primary_.CanAllocate(size, alignment))
402 res = primary_.Allocate(size, alignment);
403 else
404 res = secondary_.Allocate(size, alignment);
405 if (alignment > 8)
406 CHECK_EQ(reinterpret_cast<uptr>(res) & (alignment - 1), 0);
407 return res;
408 }
409
410 void Deallocate(void *p) {
411 if (primary_.PointerIsMine(p))
412 primary_.Deallocate(p);
413 else
414 secondary_.Deallocate(p);
415 }
416
417 bool PointerIsMine(void *p) {
418 if (primary_.PointerIsMine(p))
419 return true;
420 return secondary_.PointerIsMine(p);
421 }
422
423 void *GetMetaData(void *p) {
424 if (primary_.PointerIsMine(p))
425 return primary_.GetMetaData(p);
426 return secondary_.GetMetaData(p);
427 }
428
429 uptr TotalMemoryUsed() {
430 return primary_.TotalMemoryUsed() + secondary_.TotalMemoryUsed();
431 }
432
433 void TestOnlyUnmap() { primary_.TestOnlyUnmap(); }
434
435 private:
436 PrimaryAllocator primary_;
437 SecondaryAllocator secondary_;
438};
439
Kostya Serebryany6e26fa92012-06-21 10:04:36 +0000440} // namespace __sanitizer
441
442#endif // SANITIZER_ALLOCATOR_H