blob: 966c128e4ecf1290630a640e0a795e0121a398d3 [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 Serebryany739b0de2012-07-06 14:32:00 +0000155 uptr ClassID(uptr size) { return SizeClassMap::ClassID(size); }
156
Kostya Serebryany41960462012-06-26 14:23:32 +0000157 void *GetMetaData(void *p) {
Kostya Serebryany278ccda2012-06-22 16:13:28 +0000158 uptr class_id = GetSizeClass(p);
159 uptr chunk_idx = GetChunkIdx(reinterpret_cast<uptr>(p), class_id);
Kostya Serebryany41960462012-06-26 14:23:32 +0000160 return reinterpret_cast<void*>(kSpaceBeg + (kRegionSize * (class_id + 1)) -
161 (1 + chunk_idx) * kMetadataSize);
Kostya Serebryany278ccda2012-06-22 16:13:28 +0000162 }
163
Kostya Serebryany100590f2012-06-25 14:53:49 +0000164 uptr TotalMemoryUsed() {
Kostya Serebryany5b014152012-06-22 13:00:50 +0000165 uptr res = 0;
166 for (uptr i = 0; i < kNumClasses; i++)
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000167 res += GetRegionInfo(i)->allocated_user;
Kostya Serebryany5b014152012-06-22 13:00:50 +0000168 return res;
169 }
170
171 // Test-only.
172 void TestOnlyUnmap() {
173 UnmapOrDie(reinterpret_cast<void*>(AllocBeg()), AllocSize());
174 }
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000175
Kostya Serebryany5b014152012-06-22 13:00:50 +0000176 static const uptr kNumClasses = 256; // Power of two <= 256
Kostya Serebryanyd1e60942012-07-06 13:46:49 +0000177
178 private:
Kostya Serebryany5b014152012-06-22 13:00:50 +0000179 COMPILER_CHECK(kNumClasses <= SizeClassMap::kNumClasses);
180 static const uptr kRegionSize = kSpaceSize / kNumClasses;
Kostya Serebryany278ccda2012-06-22 16:13:28 +0000181 COMPILER_CHECK((kRegionSize >> 32) > 0); // kRegionSize must be >= 2^32.
Kostya Serebryany5b014152012-06-22 13:00:50 +0000182 // Populate the free list with at most this number of bytes at once
183 // or with one element if its size is greater.
184 static const uptr kPopulateSize = 1 << 18;
185
Kostya Serebryany5b014152012-06-22 13:00:50 +0000186 struct RegionInfo {
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000187 SpinMutex mutex;
Kostya Serebryanyd1e60942012-07-06 13:46:49 +0000188 AllocatorFreeList free_list;
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000189 uptr allocated_user; // Bytes allocated for user memory.
190 uptr allocated_meta; // Bytes allocated for metadata.
Kostya Serebryanyd1e60942012-07-06 13:46:49 +0000191 char padding[kCacheLineSize - 3 * sizeof(uptr) - sizeof(AllocatorFreeList)];
Kostya Serebryany5b014152012-06-22 13:00:50 +0000192 };
193 COMPILER_CHECK(sizeof(RegionInfo) == kCacheLineSize);
194
Kostya Serebryanyaad697e2012-06-25 14:58:17 +0000195 uptr AdditionalSize() {
Kostya Serebryany100590f2012-06-25 14:53:49 +0000196 uptr res = sizeof(RegionInfo) * kNumClasses;
197 CHECK_EQ(res % kPageSize, 0);
198 return res;
199 }
Kostya Serebryany5b014152012-06-22 13:00:50 +0000200 uptr AllocBeg() { return kSpaceBeg - AdditionalSize(); }
201 uptr AllocSize() { return kSpaceSize + AdditionalSize(); }
202
203 RegionInfo *GetRegionInfo(uptr class_id) {
204 CHECK_LT(class_id, kNumClasses);
205 RegionInfo *regions = reinterpret_cast<RegionInfo*>(kSpaceBeg);
206 return &regions[-1 - class_id];
207 }
208
Kostya Serebryany278ccda2012-06-22 16:13:28 +0000209 uptr GetChunkIdx(uptr chunk, uptr class_id) {
210 u32 offset = chunk % kRegionSize;
211 // Here we divide by a non-constant. This is costly.
212 // We require that kRegionSize is at least 2^32 so that offset is 32-bit.
213 // We save 2x by using 32-bit div, but may need to use a 256-way switch.
214 return offset / (u32)SizeClassMap::Size(class_id);
215 }
216
Kostya Serebryany78e973f2012-07-06 09:26:01 +0000217 void PopulateFreeList(uptr class_id, RegionInfo *region) {
Kostya Serebryany5b014152012-06-22 13:00:50 +0000218 uptr size = SizeClassMap::Size(class_id);
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000219 uptr beg_idx = region->allocated_user;
Kostya Serebryany5b014152012-06-22 13:00:50 +0000220 uptr end_idx = beg_idx + kPopulateSize;
Kostya Serebryany78e973f2012-07-06 09:26:01 +0000221 region->free_list.clear();
Kostya Serebryany5b014152012-06-22 13:00:50 +0000222 uptr region_beg = kSpaceBeg + kRegionSize * class_id;
223 uptr idx = beg_idx;
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000224 uptr i = 0;
Kostya Serebryany5b014152012-06-22 13:00:50 +0000225 do { // do-while loop because we need to put at least one item.
226 uptr p = region_beg + idx;
Kostya Serebryanyd1e60942012-07-06 13:46:49 +0000227 region->free_list.push_front(reinterpret_cast<AllocatorListNode*>(p));
Kostya Serebryany5b014152012-06-22 13:00:50 +0000228 idx += size;
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000229 i++;
Kostya Serebryany5b014152012-06-22 13:00:50 +0000230 } while (idx < end_idx);
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000231 region->allocated_user += idx - beg_idx;
232 region->allocated_meta += i * kMetadataSize;
233 CHECK_LT(region->allocated_user + region->allocated_meta, kRegionSize);
Kostya Serebryany5b014152012-06-22 13:00:50 +0000234 }
235
236 void *AllocateBySizeClass(uptr class_id) {
237 CHECK_LT(class_id, kNumClasses);
238 RegionInfo *region = GetRegionInfo(class_id);
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000239 SpinMutexLock l(&region->mutex);
Kostya Serebryany78e973f2012-07-06 09:26:01 +0000240 if (region->free_list.empty()) {
241 PopulateFreeList(class_id, region);
Kostya Serebryany5b014152012-06-22 13:00:50 +0000242 }
Kostya Serebryany78e973f2012-07-06 09:26:01 +0000243 CHECK(!region->free_list.empty());
Kostya Serebryanyd1e60942012-07-06 13:46:49 +0000244 AllocatorListNode *node = region->free_list.front();
Kostya Serebryany78e973f2012-07-06 09:26:01 +0000245 region->free_list.pop_front();
Kostya Serebryany5b014152012-06-22 13:00:50 +0000246 return reinterpret_cast<void*>(node);
247 }
248
249 void DeallocateBySizeClass(void *p, uptr class_id) {
250 RegionInfo *region = GetRegionInfo(class_id);
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000251 SpinMutexLock l(&region->mutex);
Kostya Serebryanyd1e60942012-07-06 13:46:49 +0000252 region->free_list.push_front(reinterpret_cast<AllocatorListNode*>(p));
Kostya Serebryany5b014152012-06-22 13:00:50 +0000253 }
254};
255
Kostya Serebryanyd1e60942012-07-06 13:46:49 +0000256// Objects of this type should be used as local caches for SizeClassAllocator64.
257// Since the typical use of this class is to have one object per thread in TLS,
258// is has to be POD.
259template<const uptr kNumClasses, class SizeClassAllocator>
260struct SizeClassAllocatorLocalCache {
261 // Don't need to call Init if the object is a global (i.e. zero-initialized).
262 void Init() {
263 internal_memset(this, 0, sizeof(*this));
264 }
265
266 void *Allocate(SizeClassAllocator *allocator, uptr class_id) {
267 CHECK_LT(class_id, kNumClasses);
268 AllocatorFreeList *free_list = &free_lists_[class_id];
269 if (free_list->empty())
270 allocator->BulkAllocate(class_id, free_list);
271 CHECK(!free_list->empty());
272 void *res = free_list->front();
273 free_list->pop_front();
274 return res;
275 }
276
277 void Deallocate(SizeClassAllocator *allocator, uptr class_id, void *p) {
278 CHECK_LT(class_id, kNumClasses);
279 free_lists_[class_id].push_front(reinterpret_cast<AllocatorListNode*>(p));
280 }
281
282 void Drain(SizeClassAllocator *allocator) {
283 for (uptr i = 0; i < kNumClasses; i++) {
284 allocator->BulkDeallocate(i, &free_lists_[i]);
285 CHECK(free_lists_[i].empty());
286 }
287 }
288
289 // private:
290 AllocatorFreeList free_lists_[kNumClasses];
291};
292
Kostya Serebryany41960462012-06-26 14:23:32 +0000293// This class can (de)allocate only large chunks of memory using mmap/unmap.
294// The main purpose of this allocator is to cover large and rare allocation
295// sizes not covered by more efficient allocators (e.g. SizeClassAllocator64).
296// The result is always page-aligned.
297class LargeMmapAllocator {
298 public:
299 void Init() {
300 internal_memset(this, 0, sizeof(*this));
301 }
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000302 void *Allocate(uptr size, uptr alignment) {
303 CHECK_LE(alignment, kPageSize); // Not implemented. Do we need it?
Kostya Serebryany41960462012-06-26 14:23:32 +0000304 uptr map_size = RoundUpMapSize(size);
305 void *map = MmapOrDie(map_size, "LargeMmapAllocator");
306 void *res = reinterpret_cast<void*>(reinterpret_cast<uptr>(map)
307 + kPageSize);
308 Header *h = GetHeader(res);
309 h->size = size;
310 {
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000311 SpinMutexLock l(&mutex_);
Kostya Serebryany41960462012-06-26 14:23:32 +0000312 h->next = list_;
313 h->prev = 0;
314 if (list_)
315 list_->prev = h;
316 list_ = h;
317 }
318 return res;
319 }
320
321 void Deallocate(void *p) {
322 Header *h = GetHeader(p);
323 uptr map_size = RoundUpMapSize(h->size);
324 {
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000325 SpinMutexLock l(&mutex_);
Kostya Serebryany41960462012-06-26 14:23:32 +0000326 Header *prev = h->prev;
327 Header *next = h->next;
328 if (prev)
329 prev->next = next;
330 if (next)
331 next->prev = prev;
332 if (h == list_)
333 list_ = next;
334 }
335 UnmapOrDie(h, map_size);
336 }
337
338 uptr TotalMemoryUsed() {
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000339 SpinMutexLock l(&mutex_);
Kostya Serebryany41960462012-06-26 14:23:32 +0000340 uptr res = 0;
341 for (Header *l = list_; l; l = l->next) {
342 res += RoundUpMapSize(l->size);
343 }
344 return res;
345 }
346
347 bool PointerIsMine(void *p) {
348 // Fast check.
349 if ((reinterpret_cast<uptr>(p) % kPageSize) != 0) return false;
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000350 SpinMutexLock l(&mutex_);
Kostya Serebryany41960462012-06-26 14:23:32 +0000351 for (Header *l = list_; l; l = l->next) {
352 if (GetUser(l) == p) return true;
353 }
354 return false;
355 }
356
357 // At least kPageSize/2 metadata bytes is available.
358 void *GetMetaData(void *p) {
359 return GetHeader(p) + 1;
360 }
361
362 private:
363 struct Header {
364 uptr size;
365 Header *next;
366 Header *prev;
367 };
368
369 Header *GetHeader(void *p) {
370 return reinterpret_cast<Header*>(reinterpret_cast<uptr>(p) - kPageSize);
371 }
372
373 void *GetUser(Header *h) {
374 return reinterpret_cast<void*>(reinterpret_cast<uptr>(h) + kPageSize);
375 }
376
377 uptr RoundUpMapSize(uptr size) {
378 return RoundUpTo(size, kPageSize) + kPageSize;
379 }
380
381 Header *list_;
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000382 SpinMutex mutex_;
Kostya Serebryany41960462012-06-26 14:23:32 +0000383};
384
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000385// This class implements a complete memory allocator by using two
386// internal allocators:
387// PrimaryAllocator is efficient, but may not allocate some sizes (alignments).
388// When allocating 2^x bytes it should return 2^x aligned chunk.
Kostya Serebryany739b0de2012-07-06 14:32:00 +0000389// PrimaryAllocator is used via a local AllocatorCache.
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000390// SecondaryAllocator can allocate anything, but is not efficient.
Kostya Serebryany739b0de2012-07-06 14:32:00 +0000391template <class PrimaryAllocator, class AllocatorCache,
392 class SecondaryAllocator>
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000393class CombinedAllocator {
394 public:
395 void Init() {
396 primary_.Init();
397 secondary_.Init();
398 }
399
Kostya Serebryany739b0de2012-07-06 14:32:00 +0000400 void *Allocate(AllocatorCache *cache, uptr size, uptr alignment) {
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000401 CHECK_GT(size, 0);
402 if (alignment > 8)
403 size = RoundUpTo(size, alignment);
404 void *res;
405 if (primary_.CanAllocate(size, alignment))
Kostya Serebryany739b0de2012-07-06 14:32:00 +0000406 res = cache->Allocate(&primary_, primary_.ClassID(size));
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000407 else
408 res = secondary_.Allocate(size, alignment);
409 if (alignment > 8)
410 CHECK_EQ(reinterpret_cast<uptr>(res) & (alignment - 1), 0);
411 return res;
412 }
413
Kostya Serebryany739b0de2012-07-06 14:32:00 +0000414 void Deallocate(AllocatorCache *cache, void *p) {
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000415 if (primary_.PointerIsMine(p))
Kostya Serebryany739b0de2012-07-06 14:32:00 +0000416 cache->Deallocate(&primary_, primary_.GetSizeClass(p), p);
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000417 else
418 secondary_.Deallocate(p);
419 }
420
421 bool PointerIsMine(void *p) {
422 if (primary_.PointerIsMine(p))
423 return true;
424 return secondary_.PointerIsMine(p);
425 }
426
427 void *GetMetaData(void *p) {
428 if (primary_.PointerIsMine(p))
429 return primary_.GetMetaData(p);
430 return secondary_.GetMetaData(p);
431 }
432
433 uptr TotalMemoryUsed() {
434 return primary_.TotalMemoryUsed() + secondary_.TotalMemoryUsed();
435 }
436
437 void TestOnlyUnmap() { primary_.TestOnlyUnmap(); }
438
Kostya Serebryany739b0de2012-07-06 14:32:00 +0000439 void SwallowCache(AllocatorCache *cache) {
440 cache->Drain(&primary_);
441 }
442
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000443 private:
444 PrimaryAllocator primary_;
445 SecondaryAllocator secondary_;
446};
447
Kostya Serebryany6e26fa92012-06-21 10:04:36 +0000448} // namespace __sanitizer
449
450#endif // SANITIZER_ALLOCATOR_H