Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 1 | //=-- lsan_allocator.h ----------------------------------------------------===// |
| 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 | // |
| 10 | // This file is a part of LeakSanitizer. |
| 11 | // Allocator for standalone LSan. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LSAN_ALLOCATOR_H |
| 16 | #define LSAN_ALLOCATOR_H |
| 17 | |
Francis Ricci | d668a01 | 2017-03-27 14:07:50 +0000 | [diff] [blame] | 18 | #include "sanitizer_common/sanitizer_allocator.h" |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 19 | #include "sanitizer_common/sanitizer_common.h" |
| 20 | #include "sanitizer_common/sanitizer_internal_defs.h" |
Francis Ricci | d668a01 | 2017-03-27 14:07:50 +0000 | [diff] [blame] | 21 | #include "lsan_common.h" |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 22 | |
| 23 | namespace __lsan { |
| 24 | |
| 25 | void *Allocate(const StackTrace &stack, uptr size, uptr alignment, |
| 26 | bool cleared); |
| 27 | void Deallocate(void *p); |
| 28 | void *Reallocate(const StackTrace &stack, void *p, uptr new_size, |
| 29 | uptr alignment); |
Sergey Matveev | 08347ca | 2014-08-26 14:28:28 +0000 | [diff] [blame] | 30 | uptr GetMallocUsableSize(const void *p); |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 31 | |
| 32 | template<typename Callable> |
| 33 | void ForEachChunk(const Callable &callback); |
| 34 | |
| 35 | void GetAllocatorCacheRange(uptr *begin, uptr *end); |
| 36 | void AllocatorThreadFinish(); |
| 37 | void InitializeAllocator(); |
| 38 | |
Francis Ricci | 03b2a8e | 2017-04-11 20:05:02 +0000 | [diff] [blame] | 39 | const bool kAlwaysClearMemory = true; |
| 40 | |
Francis Ricci | d668a01 | 2017-03-27 14:07:50 +0000 | [diff] [blame] | 41 | struct ChunkMetadata { |
| 42 | u8 allocated : 8; // Must be first. |
| 43 | ChunkTag tag : 2; |
| 44 | #if SANITIZER_WORDSIZE == 64 |
| 45 | uptr requested_size : 54; |
| 46 | #else |
| 47 | uptr requested_size : 32; |
| 48 | uptr padding : 22; |
| 49 | #endif |
| 50 | u32 stack_trace_id; |
| 51 | }; |
| 52 | |
Maxim Ostapenko | de3b9a2 | 2017-04-11 14:58:26 +0000 | [diff] [blame] | 53 | #if defined(__mips64) || defined(__aarch64__) || defined(__i386__) || \ |
| 54 | defined(__arm__) |
Francis Ricci | d668a01 | 2017-03-27 14:07:50 +0000 | [diff] [blame] | 55 | static const uptr kRegionSizeLog = 20; |
| 56 | static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog; |
| 57 | typedef TwoLevelByteMap<(kNumRegions >> 12), 1 << 12> ByteMap; |
| 58 | typedef CompactSizeClassMap SizeClassMap; |
| 59 | typedef SizeClassAllocator32<0, SANITIZER_MMAP_RANGE_SIZE, |
| 60 | sizeof(ChunkMetadata), SizeClassMap, kRegionSizeLog, ByteMap> |
| 61 | PrimaryAllocator; |
Alex Shlyapnikov | 342586d | 2017-04-21 21:59:53 +0000 | [diff] [blame^] | 62 | #elif defined(__x86_64__) || defined(__powerpc64__) |
Francis Ricci | d668a01 | 2017-03-27 14:07:50 +0000 | [diff] [blame] | 63 | struct AP64 { // Allocator64 parameters. Deliberately using a short name. |
| 64 | static const uptr kSpaceBeg = 0x600000000000ULL; |
| 65 | static const uptr kSpaceSize = 0x40000000000ULL; // 4T. |
| 66 | static const uptr kMetadataSize = sizeof(ChunkMetadata); |
| 67 | typedef DefaultSizeClassMap SizeClassMap; |
| 68 | typedef NoOpMapUnmapCallback MapUnmapCallback; |
| 69 | static const uptr kFlags = 0; |
| 70 | }; |
| 71 | |
| 72 | typedef SizeClassAllocator64<AP64> PrimaryAllocator; |
| 73 | #endif |
| 74 | typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache; |
| 75 | |
| 76 | AllocatorCache *GetAllocatorCache(); |
Francis Ricci | 03b2a8e | 2017-04-11 20:05:02 +0000 | [diff] [blame] | 77 | |
| 78 | void *lsan_memalign(uptr alignment, uptr size, const StackTrace &stack); |
| 79 | void *lsan_malloc(uptr size, const StackTrace &stack); |
| 80 | void lsan_free(void *p); |
| 81 | void *lsan_realloc(void *p, uptr size, const StackTrace &stack); |
| 82 | void *lsan_calloc(uptr nmemb, uptr size, const StackTrace &stack); |
| 83 | void *lsan_valloc(uptr size, const StackTrace &stack); |
| 84 | uptr lsan_mz_size(const void *p); |
| 85 | |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 86 | } // namespace __lsan |
| 87 | |
| 88 | #endif // LSAN_ALLOCATOR_H |