blob: 4bc8c11d94ebc0557b34675d387b4cb776f111a3 [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 Serebryanyab349192012-07-18 16:04:55 +0000155 uptr GetActuallyAllocatedSize(void *p) {
156 CHECK(PointerIsMine(p));
157 return SizeClassMap::Size(GetSizeClass(p));
158 }
159
Kostya Serebryany739b0de2012-07-06 14:32:00 +0000160 uptr ClassID(uptr size) { return SizeClassMap::ClassID(size); }
161
Kostya Serebryany41960462012-06-26 14:23:32 +0000162 void *GetMetaData(void *p) {
Kostya Serebryany278ccda2012-06-22 16:13:28 +0000163 uptr class_id = GetSizeClass(p);
164 uptr chunk_idx = GetChunkIdx(reinterpret_cast<uptr>(p), class_id);
Kostya Serebryany41960462012-06-26 14:23:32 +0000165 return reinterpret_cast<void*>(kSpaceBeg + (kRegionSize * (class_id + 1)) -
166 (1 + chunk_idx) * kMetadataSize);
Kostya Serebryany278ccda2012-06-22 16:13:28 +0000167 }
168
Kostya Serebryany100590f2012-06-25 14:53:49 +0000169 uptr TotalMemoryUsed() {
Kostya Serebryany5b014152012-06-22 13:00:50 +0000170 uptr res = 0;
171 for (uptr i = 0; i < kNumClasses; i++)
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000172 res += GetRegionInfo(i)->allocated_user;
Kostya Serebryany5b014152012-06-22 13:00:50 +0000173 return res;
174 }
175
176 // Test-only.
177 void TestOnlyUnmap() {
178 UnmapOrDie(reinterpret_cast<void*>(AllocBeg()), AllocSize());
179 }
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000180
Dmitry Vyukovc6936892012-08-15 14:25:08 +0000181 static uptr AllocBeg() { return kSpaceBeg - AdditionalSize(); }
182 static uptr AllocEnd() { return kSpaceBeg + kSpaceSize; }
183 static uptr AllocSize() { return kSpaceSize + AdditionalSize(); }
184
Kostya Serebryany5b014152012-06-22 13:00:50 +0000185 static const uptr kNumClasses = 256; // Power of two <= 256
Kostya Serebryanyd1e60942012-07-06 13:46:49 +0000186
187 private:
Dmitry Vyukovc6936892012-08-15 14:25:08 +0000188 COMPILER_CHECK(kSpaceBeg % kSpaceSize == 0);
Kostya Serebryany5b014152012-06-22 13:00:50 +0000189 COMPILER_CHECK(kNumClasses <= SizeClassMap::kNumClasses);
190 static const uptr kRegionSize = kSpaceSize / kNumClasses;
Kostya Serebryany278ccda2012-06-22 16:13:28 +0000191 COMPILER_CHECK((kRegionSize >> 32) > 0); // kRegionSize must be >= 2^32.
Kostya Serebryany5b014152012-06-22 13:00:50 +0000192 // Populate the free list with at most this number of bytes at once
193 // or with one element if its size is greater.
194 static const uptr kPopulateSize = 1 << 18;
195
Kostya Serebryany5b014152012-06-22 13:00:50 +0000196 struct RegionInfo {
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000197 SpinMutex mutex;
Kostya Serebryanyd1e60942012-07-06 13:46:49 +0000198 AllocatorFreeList free_list;
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000199 uptr allocated_user; // Bytes allocated for user memory.
200 uptr allocated_meta; // Bytes allocated for metadata.
Kostya Serebryanyd1e60942012-07-06 13:46:49 +0000201 char padding[kCacheLineSize - 3 * sizeof(uptr) - sizeof(AllocatorFreeList)];
Kostya Serebryany5b014152012-06-22 13:00:50 +0000202 };
203 COMPILER_CHECK(sizeof(RegionInfo) == kCacheLineSize);
204
Dmitry Vyukovc6936892012-08-15 14:25:08 +0000205 static uptr AdditionalSize() {
Kostya Serebryany100590f2012-06-25 14:53:49 +0000206 uptr res = sizeof(RegionInfo) * kNumClasses;
207 CHECK_EQ(res % kPageSize, 0);
208 return res;
209 }
Kostya Serebryany5b014152012-06-22 13:00:50 +0000210
211 RegionInfo *GetRegionInfo(uptr class_id) {
212 CHECK_LT(class_id, kNumClasses);
213 RegionInfo *regions = reinterpret_cast<RegionInfo*>(kSpaceBeg);
214 return &regions[-1 - class_id];
215 }
216
Kostya Serebryany278ccda2012-06-22 16:13:28 +0000217 uptr GetChunkIdx(uptr chunk, uptr class_id) {
218 u32 offset = chunk % kRegionSize;
219 // Here we divide by a non-constant. This is costly.
220 // We require that kRegionSize is at least 2^32 so that offset is 32-bit.
221 // We save 2x by using 32-bit div, but may need to use a 256-way switch.
222 return offset / (u32)SizeClassMap::Size(class_id);
223 }
224
Kostya Serebryany78e973f2012-07-06 09:26:01 +0000225 void PopulateFreeList(uptr class_id, RegionInfo *region) {
Kostya Serebryany5b014152012-06-22 13:00:50 +0000226 uptr size = SizeClassMap::Size(class_id);
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000227 uptr beg_idx = region->allocated_user;
Kostya Serebryany5b014152012-06-22 13:00:50 +0000228 uptr end_idx = beg_idx + kPopulateSize;
Kostya Serebryany78e973f2012-07-06 09:26:01 +0000229 region->free_list.clear();
Kostya Serebryany5b014152012-06-22 13:00:50 +0000230 uptr region_beg = kSpaceBeg + kRegionSize * class_id;
231 uptr idx = beg_idx;
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000232 uptr i = 0;
Kostya Serebryany5b014152012-06-22 13:00:50 +0000233 do { // do-while loop because we need to put at least one item.
234 uptr p = region_beg + idx;
Kostya Serebryanyd1e60942012-07-06 13:46:49 +0000235 region->free_list.push_front(reinterpret_cast<AllocatorListNode*>(p));
Kostya Serebryany5b014152012-06-22 13:00:50 +0000236 idx += size;
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000237 i++;
Kostya Serebryany5b014152012-06-22 13:00:50 +0000238 } while (idx < end_idx);
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000239 region->allocated_user += idx - beg_idx;
240 region->allocated_meta += i * kMetadataSize;
241 CHECK_LT(region->allocated_user + region->allocated_meta, kRegionSize);
Kostya Serebryany5b014152012-06-22 13:00:50 +0000242 }
243
244 void *AllocateBySizeClass(uptr class_id) {
245 CHECK_LT(class_id, kNumClasses);
246 RegionInfo *region = GetRegionInfo(class_id);
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000247 SpinMutexLock l(&region->mutex);
Kostya Serebryany78e973f2012-07-06 09:26:01 +0000248 if (region->free_list.empty()) {
249 PopulateFreeList(class_id, region);
Kostya Serebryany5b014152012-06-22 13:00:50 +0000250 }
Kostya Serebryany78e973f2012-07-06 09:26:01 +0000251 CHECK(!region->free_list.empty());
Kostya Serebryanyd1e60942012-07-06 13:46:49 +0000252 AllocatorListNode *node = region->free_list.front();
Kostya Serebryany78e973f2012-07-06 09:26:01 +0000253 region->free_list.pop_front();
Kostya Serebryany5b014152012-06-22 13:00:50 +0000254 return reinterpret_cast<void*>(node);
255 }
256
257 void DeallocateBySizeClass(void *p, uptr class_id) {
258 RegionInfo *region = GetRegionInfo(class_id);
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000259 SpinMutexLock l(&region->mutex);
Kostya Serebryanyd1e60942012-07-06 13:46:49 +0000260 region->free_list.push_front(reinterpret_cast<AllocatorListNode*>(p));
Kostya Serebryany5b014152012-06-22 13:00:50 +0000261 }
262};
263
Kostya Serebryanyd1e60942012-07-06 13:46:49 +0000264// Objects of this type should be used as local caches for SizeClassAllocator64.
265// Since the typical use of this class is to have one object per thread in TLS,
266// is has to be POD.
267template<const uptr kNumClasses, class SizeClassAllocator>
268struct SizeClassAllocatorLocalCache {
269 // Don't need to call Init if the object is a global (i.e. zero-initialized).
270 void Init() {
271 internal_memset(this, 0, sizeof(*this));
272 }
273
274 void *Allocate(SizeClassAllocator *allocator, uptr class_id) {
275 CHECK_LT(class_id, kNumClasses);
276 AllocatorFreeList *free_list = &free_lists_[class_id];
277 if (free_list->empty())
278 allocator->BulkAllocate(class_id, free_list);
279 CHECK(!free_list->empty());
280 void *res = free_list->front();
281 free_list->pop_front();
282 return res;
283 }
284
285 void Deallocate(SizeClassAllocator *allocator, uptr class_id, void *p) {
286 CHECK_LT(class_id, kNumClasses);
287 free_lists_[class_id].push_front(reinterpret_cast<AllocatorListNode*>(p));
288 }
289
290 void Drain(SizeClassAllocator *allocator) {
291 for (uptr i = 0; i < kNumClasses; i++) {
292 allocator->BulkDeallocate(i, &free_lists_[i]);
293 CHECK(free_lists_[i].empty());
294 }
295 }
296
297 // private:
298 AllocatorFreeList free_lists_[kNumClasses];
299};
300
Kostya Serebryany41960462012-06-26 14:23:32 +0000301// This class can (de)allocate only large chunks of memory using mmap/unmap.
302// The main purpose of this allocator is to cover large and rare allocation
303// sizes not covered by more efficient allocators (e.g. SizeClassAllocator64).
304// The result is always page-aligned.
305class LargeMmapAllocator {
306 public:
307 void Init() {
308 internal_memset(this, 0, sizeof(*this));
309 }
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000310 void *Allocate(uptr size, uptr alignment) {
311 CHECK_LE(alignment, kPageSize); // Not implemented. Do we need it?
Dmitry Vyukovc6936892012-08-15 14:25:08 +0000312 if (size + alignment + 2 * kPageSize < size)
313 return 0;
Kostya Serebryany41960462012-06-26 14:23:32 +0000314 uptr map_size = RoundUpMapSize(size);
315 void *map = MmapOrDie(map_size, "LargeMmapAllocator");
316 void *res = reinterpret_cast<void*>(reinterpret_cast<uptr>(map)
317 + kPageSize);
318 Header *h = GetHeader(res);
319 h->size = size;
320 {
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000321 SpinMutexLock l(&mutex_);
Kostya Serebryany41960462012-06-26 14:23:32 +0000322 h->next = list_;
323 h->prev = 0;
324 if (list_)
325 list_->prev = h;
326 list_ = h;
327 }
328 return res;
329 }
330
331 void Deallocate(void *p) {
332 Header *h = GetHeader(p);
333 uptr map_size = RoundUpMapSize(h->size);
334 {
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000335 SpinMutexLock l(&mutex_);
Kostya Serebryany41960462012-06-26 14:23:32 +0000336 Header *prev = h->prev;
337 Header *next = h->next;
338 if (prev)
339 prev->next = next;
340 if (next)
341 next->prev = prev;
342 if (h == list_)
343 list_ = next;
344 }
345 UnmapOrDie(h, map_size);
346 }
347
348 uptr TotalMemoryUsed() {
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000349 SpinMutexLock l(&mutex_);
Kostya Serebryany41960462012-06-26 14:23:32 +0000350 uptr res = 0;
351 for (Header *l = list_; l; l = l->next) {
352 res += RoundUpMapSize(l->size);
353 }
354 return res;
355 }
356
357 bool PointerIsMine(void *p) {
358 // Fast check.
359 if ((reinterpret_cast<uptr>(p) % kPageSize) != 0) return false;
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000360 SpinMutexLock l(&mutex_);
Kostya Serebryany41960462012-06-26 14:23:32 +0000361 for (Header *l = list_; l; l = l->next) {
362 if (GetUser(l) == p) return true;
363 }
364 return false;
365 }
366
Kostya Serebryanyab349192012-07-18 16:04:55 +0000367 uptr GetActuallyAllocatedSize(void *p) {
368 return RoundUpMapSize(GetHeader(p)->size) - kPageSize;
369 }
370
Kostya Serebryany41960462012-06-26 14:23:32 +0000371 // At least kPageSize/2 metadata bytes is available.
372 void *GetMetaData(void *p) {
373 return GetHeader(p) + 1;
374 }
375
376 private:
377 struct Header {
378 uptr size;
379 Header *next;
380 Header *prev;
381 };
382
383 Header *GetHeader(void *p) {
384 return reinterpret_cast<Header*>(reinterpret_cast<uptr>(p) - kPageSize);
385 }
386
387 void *GetUser(Header *h) {
388 return reinterpret_cast<void*>(reinterpret_cast<uptr>(h) + kPageSize);
389 }
390
391 uptr RoundUpMapSize(uptr size) {
392 return RoundUpTo(size, kPageSize) + kPageSize;
393 }
394
395 Header *list_;
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000396 SpinMutex mutex_;
Kostya Serebryany41960462012-06-26 14:23:32 +0000397};
398
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000399// This class implements a complete memory allocator by using two
400// internal allocators:
401// PrimaryAllocator is efficient, but may not allocate some sizes (alignments).
402// When allocating 2^x bytes it should return 2^x aligned chunk.
Kostya Serebryany739b0de2012-07-06 14:32:00 +0000403// PrimaryAllocator is used via a local AllocatorCache.
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000404// SecondaryAllocator can allocate anything, but is not efficient.
Kostya Serebryany739b0de2012-07-06 14:32:00 +0000405template <class PrimaryAllocator, class AllocatorCache,
Alexander Potapenkob4e9fd22012-07-08 15:00:06 +0000406 class SecondaryAllocator> // NOLINT
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000407class CombinedAllocator {
408 public:
409 void Init() {
410 primary_.Init();
411 secondary_.Init();
412 }
413
Kostya Serebryanyab349192012-07-18 16:04:55 +0000414 void *Allocate(AllocatorCache *cache, uptr size, uptr alignment,
415 bool cleared = false) {
Kostya Serebryanya415df62012-07-19 12:15:33 +0000416 // Returning 0 on malloc(0) may break a lot of code.
Dmitry Vyukovc6936892012-08-15 14:25:08 +0000417 if (size == 0)
418 size = 1;
419 if (size + alignment < size)
420 return 0;
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000421 if (alignment > 8)
422 size = RoundUpTo(size, alignment);
423 void *res;
424 if (primary_.CanAllocate(size, alignment))
Kostya Serebryany739b0de2012-07-06 14:32:00 +0000425 res = cache->Allocate(&primary_, primary_.ClassID(size));
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000426 else
427 res = secondary_.Allocate(size, alignment);
428 if (alignment > 8)
429 CHECK_EQ(reinterpret_cast<uptr>(res) & (alignment - 1), 0);
Dmitry Vyukovc6936892012-08-15 14:25:08 +0000430 if (cleared && res)
Kostya Serebryanyab349192012-07-18 16:04:55 +0000431 internal_memset(res, 0, size);
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000432 return res;
433 }
434
Kostya Serebryany739b0de2012-07-06 14:32:00 +0000435 void Deallocate(AllocatorCache *cache, void *p) {
Kostya Serebryanyab349192012-07-18 16:04:55 +0000436 if (!p) return;
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000437 if (primary_.PointerIsMine(p))
Kostya Serebryany739b0de2012-07-06 14:32:00 +0000438 cache->Deallocate(&primary_, primary_.GetSizeClass(p), p);
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000439 else
440 secondary_.Deallocate(p);
441 }
442
Kostya Serebryanyab349192012-07-18 16:04:55 +0000443 void *Reallocate(AllocatorCache *cache, void *p, uptr new_size,
444 uptr alignment) {
445 if (!p)
446 return Allocate(cache, new_size, alignment);
447 if (!new_size) {
448 Deallocate(cache, p);
449 return 0;
450 }
451 CHECK(PointerIsMine(p));
452 uptr old_size = GetActuallyAllocatedSize(p);
453 uptr memcpy_size = Min(new_size, old_size);
454 void *new_p = Allocate(cache, new_size, alignment);
455 if (new_p)
456 internal_memcpy(new_p, p, memcpy_size);
457 Deallocate(cache, p);
458 return new_p;
459 }
460
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000461 bool PointerIsMine(void *p) {
462 if (primary_.PointerIsMine(p))
463 return true;
464 return secondary_.PointerIsMine(p);
465 }
466
467 void *GetMetaData(void *p) {
468 if (primary_.PointerIsMine(p))
469 return primary_.GetMetaData(p);
470 return secondary_.GetMetaData(p);
471 }
472
Kostya Serebryanyab349192012-07-18 16:04:55 +0000473 uptr GetActuallyAllocatedSize(void *p) {
474 if (primary_.PointerIsMine(p))
475 return primary_.GetActuallyAllocatedSize(p);
476 return secondary_.GetActuallyAllocatedSize(p);
477 }
478
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000479 uptr TotalMemoryUsed() {
480 return primary_.TotalMemoryUsed() + secondary_.TotalMemoryUsed();
481 }
482
483 void TestOnlyUnmap() { primary_.TestOnlyUnmap(); }
484
Kostya Serebryany739b0de2012-07-06 14:32:00 +0000485 void SwallowCache(AllocatorCache *cache) {
486 cache->Drain(&primary_);
487 }
488
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000489 private:
490 PrimaryAllocator primary_;
491 SecondaryAllocator secondary_;
492};
493
Kostya Serebryany6e26fa92012-06-21 10:04:36 +0000494} // namespace __sanitizer
495
496#endif // SANITIZER_ALLOCATOR_H