blob: ca2321ce587d503f15b969a9780720a244cab10b [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
Kostya Serebryany5b014152012-06-22 13:00:50 +0000181 static const uptr kNumClasses = 256; // Power of two <= 256
Kostya Serebryanyd1e60942012-07-06 13:46:49 +0000182
183 private:
Kostya Serebryany5b014152012-06-22 13:00:50 +0000184 COMPILER_CHECK(kNumClasses <= SizeClassMap::kNumClasses);
185 static const uptr kRegionSize = kSpaceSize / kNumClasses;
Kostya Serebryany278ccda2012-06-22 16:13:28 +0000186 COMPILER_CHECK((kRegionSize >> 32) > 0); // kRegionSize must be >= 2^32.
Kostya Serebryany5b014152012-06-22 13:00:50 +0000187 // Populate the free list with at most this number of bytes at once
188 // or with one element if its size is greater.
189 static const uptr kPopulateSize = 1 << 18;
190
Kostya Serebryany5b014152012-06-22 13:00:50 +0000191 struct RegionInfo {
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000192 SpinMutex mutex;
Kostya Serebryanyd1e60942012-07-06 13:46:49 +0000193 AllocatorFreeList free_list;
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000194 uptr allocated_user; // Bytes allocated for user memory.
195 uptr allocated_meta; // Bytes allocated for metadata.
Kostya Serebryanyd1e60942012-07-06 13:46:49 +0000196 char padding[kCacheLineSize - 3 * sizeof(uptr) - sizeof(AllocatorFreeList)];
Kostya Serebryany5b014152012-06-22 13:00:50 +0000197 };
198 COMPILER_CHECK(sizeof(RegionInfo) == kCacheLineSize);
199
Kostya Serebryanyaad697e2012-06-25 14:58:17 +0000200 uptr AdditionalSize() {
Kostya Serebryany100590f2012-06-25 14:53:49 +0000201 uptr res = sizeof(RegionInfo) * kNumClasses;
202 CHECK_EQ(res % kPageSize, 0);
203 return res;
204 }
Kostya Serebryany5b014152012-06-22 13:00:50 +0000205 uptr AllocBeg() { return kSpaceBeg - AdditionalSize(); }
206 uptr AllocSize() { return kSpaceSize + AdditionalSize(); }
207
208 RegionInfo *GetRegionInfo(uptr class_id) {
209 CHECK_LT(class_id, kNumClasses);
210 RegionInfo *regions = reinterpret_cast<RegionInfo*>(kSpaceBeg);
211 return &regions[-1 - class_id];
212 }
213
Kostya Serebryany278ccda2012-06-22 16:13:28 +0000214 uptr GetChunkIdx(uptr chunk, uptr class_id) {
215 u32 offset = chunk % kRegionSize;
216 // Here we divide by a non-constant. This is costly.
217 // We require that kRegionSize is at least 2^32 so that offset is 32-bit.
218 // We save 2x by using 32-bit div, but may need to use a 256-way switch.
219 return offset / (u32)SizeClassMap::Size(class_id);
220 }
221
Kostya Serebryany78e973f2012-07-06 09:26:01 +0000222 void PopulateFreeList(uptr class_id, RegionInfo *region) {
Kostya Serebryany5b014152012-06-22 13:00:50 +0000223 uptr size = SizeClassMap::Size(class_id);
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000224 uptr beg_idx = region->allocated_user;
Kostya Serebryany5b014152012-06-22 13:00:50 +0000225 uptr end_idx = beg_idx + kPopulateSize;
Kostya Serebryany78e973f2012-07-06 09:26:01 +0000226 region->free_list.clear();
Kostya Serebryany5b014152012-06-22 13:00:50 +0000227 uptr region_beg = kSpaceBeg + kRegionSize * class_id;
228 uptr idx = beg_idx;
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000229 uptr i = 0;
Kostya Serebryany5b014152012-06-22 13:00:50 +0000230 do { // do-while loop because we need to put at least one item.
231 uptr p = region_beg + idx;
Kostya Serebryanyd1e60942012-07-06 13:46:49 +0000232 region->free_list.push_front(reinterpret_cast<AllocatorListNode*>(p));
Kostya Serebryany5b014152012-06-22 13:00:50 +0000233 idx += size;
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000234 i++;
Kostya Serebryany5b014152012-06-22 13:00:50 +0000235 } while (idx < end_idx);
Kostya Serebryanyf299f702012-06-25 04:12:49 +0000236 region->allocated_user += idx - beg_idx;
237 region->allocated_meta += i * kMetadataSize;
238 CHECK_LT(region->allocated_user + region->allocated_meta, kRegionSize);
Kostya Serebryany5b014152012-06-22 13:00:50 +0000239 }
240
241 void *AllocateBySizeClass(uptr class_id) {
242 CHECK_LT(class_id, kNumClasses);
243 RegionInfo *region = GetRegionInfo(class_id);
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000244 SpinMutexLock l(&region->mutex);
Kostya Serebryany78e973f2012-07-06 09:26:01 +0000245 if (region->free_list.empty()) {
246 PopulateFreeList(class_id, region);
Kostya Serebryany5b014152012-06-22 13:00:50 +0000247 }
Kostya Serebryany78e973f2012-07-06 09:26:01 +0000248 CHECK(!region->free_list.empty());
Kostya Serebryanyd1e60942012-07-06 13:46:49 +0000249 AllocatorListNode *node = region->free_list.front();
Kostya Serebryany78e973f2012-07-06 09:26:01 +0000250 region->free_list.pop_front();
Kostya Serebryany5b014152012-06-22 13:00:50 +0000251 return reinterpret_cast<void*>(node);
252 }
253
254 void DeallocateBySizeClass(void *p, uptr class_id) {
255 RegionInfo *region = GetRegionInfo(class_id);
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000256 SpinMutexLock l(&region->mutex);
Kostya Serebryanyd1e60942012-07-06 13:46:49 +0000257 region->free_list.push_front(reinterpret_cast<AllocatorListNode*>(p));
Kostya Serebryany5b014152012-06-22 13:00:50 +0000258 }
259};
260
Kostya Serebryanyd1e60942012-07-06 13:46:49 +0000261// Objects of this type should be used as local caches for SizeClassAllocator64.
262// Since the typical use of this class is to have one object per thread in TLS,
263// is has to be POD.
264template<const uptr kNumClasses, class SizeClassAllocator>
265struct SizeClassAllocatorLocalCache {
266 // Don't need to call Init if the object is a global (i.e. zero-initialized).
267 void Init() {
268 internal_memset(this, 0, sizeof(*this));
269 }
270
271 void *Allocate(SizeClassAllocator *allocator, uptr class_id) {
272 CHECK_LT(class_id, kNumClasses);
273 AllocatorFreeList *free_list = &free_lists_[class_id];
274 if (free_list->empty())
275 allocator->BulkAllocate(class_id, free_list);
276 CHECK(!free_list->empty());
277 void *res = free_list->front();
278 free_list->pop_front();
279 return res;
280 }
281
282 void Deallocate(SizeClassAllocator *allocator, uptr class_id, void *p) {
283 CHECK_LT(class_id, kNumClasses);
284 free_lists_[class_id].push_front(reinterpret_cast<AllocatorListNode*>(p));
285 }
286
287 void Drain(SizeClassAllocator *allocator) {
288 for (uptr i = 0; i < kNumClasses; i++) {
289 allocator->BulkDeallocate(i, &free_lists_[i]);
290 CHECK(free_lists_[i].empty());
291 }
292 }
293
294 // private:
295 AllocatorFreeList free_lists_[kNumClasses];
296};
297
Kostya Serebryany41960462012-06-26 14:23:32 +0000298// This class can (de)allocate only large chunks of memory using mmap/unmap.
299// The main purpose of this allocator is to cover large and rare allocation
300// sizes not covered by more efficient allocators (e.g. SizeClassAllocator64).
301// The result is always page-aligned.
302class LargeMmapAllocator {
303 public:
304 void Init() {
305 internal_memset(this, 0, sizeof(*this));
306 }
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000307 void *Allocate(uptr size, uptr alignment) {
308 CHECK_LE(alignment, kPageSize); // Not implemented. Do we need it?
Kostya Serebryany41960462012-06-26 14:23:32 +0000309 uptr map_size = RoundUpMapSize(size);
310 void *map = MmapOrDie(map_size, "LargeMmapAllocator");
311 void *res = reinterpret_cast<void*>(reinterpret_cast<uptr>(map)
312 + kPageSize);
313 Header *h = GetHeader(res);
314 h->size = size;
315 {
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000316 SpinMutexLock l(&mutex_);
Kostya Serebryany41960462012-06-26 14:23:32 +0000317 h->next = list_;
318 h->prev = 0;
319 if (list_)
320 list_->prev = h;
321 list_ = h;
322 }
323 return res;
324 }
325
326 void Deallocate(void *p) {
327 Header *h = GetHeader(p);
328 uptr map_size = RoundUpMapSize(h->size);
329 {
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000330 SpinMutexLock l(&mutex_);
Kostya Serebryany41960462012-06-26 14:23:32 +0000331 Header *prev = h->prev;
332 Header *next = h->next;
333 if (prev)
334 prev->next = next;
335 if (next)
336 next->prev = prev;
337 if (h == list_)
338 list_ = next;
339 }
340 UnmapOrDie(h, map_size);
341 }
342
343 uptr TotalMemoryUsed() {
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000344 SpinMutexLock l(&mutex_);
Kostya Serebryany41960462012-06-26 14:23:32 +0000345 uptr res = 0;
346 for (Header *l = list_; l; l = l->next) {
347 res += RoundUpMapSize(l->size);
348 }
349 return res;
350 }
351
352 bool PointerIsMine(void *p) {
353 // Fast check.
354 if ((reinterpret_cast<uptr>(p) % kPageSize) != 0) return false;
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000355 SpinMutexLock l(&mutex_);
Kostya Serebryany41960462012-06-26 14:23:32 +0000356 for (Header *l = list_; l; l = l->next) {
357 if (GetUser(l) == p) return true;
358 }
359 return false;
360 }
361
Kostya Serebryanyab349192012-07-18 16:04:55 +0000362 uptr GetActuallyAllocatedSize(void *p) {
363 return RoundUpMapSize(GetHeader(p)->size) - kPageSize;
364 }
365
Kostya Serebryany41960462012-06-26 14:23:32 +0000366 // At least kPageSize/2 metadata bytes is available.
367 void *GetMetaData(void *p) {
368 return GetHeader(p) + 1;
369 }
370
371 private:
372 struct Header {
373 uptr size;
374 Header *next;
375 Header *prev;
376 };
377
378 Header *GetHeader(void *p) {
379 return reinterpret_cast<Header*>(reinterpret_cast<uptr>(p) - kPageSize);
380 }
381
382 void *GetUser(Header *h) {
383 return reinterpret_cast<void*>(reinterpret_cast<uptr>(h) + kPageSize);
384 }
385
386 uptr RoundUpMapSize(uptr size) {
387 return RoundUpTo(size, kPageSize) + kPageSize;
388 }
389
390 Header *list_;
Dmitry Vyukovb462dfc2012-07-02 06:54:24 +0000391 SpinMutex mutex_;
Kostya Serebryany41960462012-06-26 14:23:32 +0000392};
393
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000394// This class implements a complete memory allocator by using two
395// internal allocators:
396// PrimaryAllocator is efficient, but may not allocate some sizes (alignments).
397// When allocating 2^x bytes it should return 2^x aligned chunk.
Kostya Serebryany739b0de2012-07-06 14:32:00 +0000398// PrimaryAllocator is used via a local AllocatorCache.
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000399// SecondaryAllocator can allocate anything, but is not efficient.
Kostya Serebryany739b0de2012-07-06 14:32:00 +0000400template <class PrimaryAllocator, class AllocatorCache,
Alexander Potapenkob4e9fd22012-07-08 15:00:06 +0000401 class SecondaryAllocator> // NOLINT
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000402class CombinedAllocator {
403 public:
404 void Init() {
405 primary_.Init();
406 secondary_.Init();
407 }
408
Kostya Serebryanyab349192012-07-18 16:04:55 +0000409 void *Allocate(AllocatorCache *cache, uptr size, uptr alignment,
410 bool cleared = false) {
411 if (size == 0) return 0;
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000412 CHECK_GT(size, 0);
413 if (alignment > 8)
414 size = RoundUpTo(size, alignment);
415 void *res;
416 if (primary_.CanAllocate(size, alignment))
Kostya Serebryany739b0de2012-07-06 14:32:00 +0000417 res = cache->Allocate(&primary_, primary_.ClassID(size));
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000418 else
419 res = secondary_.Allocate(size, alignment);
420 if (alignment > 8)
421 CHECK_EQ(reinterpret_cast<uptr>(res) & (alignment - 1), 0);
Kostya Serebryanyab349192012-07-18 16:04:55 +0000422 if (cleared)
423 internal_memset(res, 0, size);
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000424 return res;
425 }
426
Kostya Serebryany739b0de2012-07-06 14:32:00 +0000427 void Deallocate(AllocatorCache *cache, void *p) {
Kostya Serebryanyab349192012-07-18 16:04:55 +0000428 if (!p) return;
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000429 if (primary_.PointerIsMine(p))
Kostya Serebryany739b0de2012-07-06 14:32:00 +0000430 cache->Deallocate(&primary_, primary_.GetSizeClass(p), p);
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000431 else
432 secondary_.Deallocate(p);
433 }
434
Kostya Serebryanyab349192012-07-18 16:04:55 +0000435 void *Reallocate(AllocatorCache *cache, void *p, uptr new_size,
436 uptr alignment) {
437 if (!p)
438 return Allocate(cache, new_size, alignment);
439 if (!new_size) {
440 Deallocate(cache, p);
441 return 0;
442 }
443 CHECK(PointerIsMine(p));
444 uptr old_size = GetActuallyAllocatedSize(p);
445 uptr memcpy_size = Min(new_size, old_size);
446 void *new_p = Allocate(cache, new_size, alignment);
447 if (new_p)
448 internal_memcpy(new_p, p, memcpy_size);
449 Deallocate(cache, p);
450 return new_p;
451 }
452
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000453 bool PointerIsMine(void *p) {
454 if (primary_.PointerIsMine(p))
455 return true;
456 return secondary_.PointerIsMine(p);
457 }
458
459 void *GetMetaData(void *p) {
460 if (primary_.PointerIsMine(p))
461 return primary_.GetMetaData(p);
462 return secondary_.GetMetaData(p);
463 }
464
Kostya Serebryanyab349192012-07-18 16:04:55 +0000465 uptr GetActuallyAllocatedSize(void *p) {
466 if (primary_.PointerIsMine(p))
467 return primary_.GetActuallyAllocatedSize(p);
468 return secondary_.GetActuallyAllocatedSize(p);
469 }
470
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000471 uptr TotalMemoryUsed() {
472 return primary_.TotalMemoryUsed() + secondary_.TotalMemoryUsed();
473 }
474
475 void TestOnlyUnmap() { primary_.TestOnlyUnmap(); }
476
Kostya Serebryany739b0de2012-07-06 14:32:00 +0000477 void SwallowCache(AllocatorCache *cache) {
478 cache->Drain(&primary_);
479 }
480
Kostya Serebryany92afdb62012-06-29 15:35:18 +0000481 private:
482 PrimaryAllocator primary_;
483 SecondaryAllocator secondary_;
484};
485
Kostya Serebryany6e26fa92012-06-21 10:04:36 +0000486} // namespace __sanitizer
487
488#endif // SANITIZER_ALLOCATOR_H