1 //=-- lsan_allocator.h ----------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of LeakSanitizer.
11 // Allocator for standalone LSan.
13 //===----------------------------------------------------------------------===//
15 #ifndef LSAN_ALLOCATOR_H
16 #define LSAN_ALLOCATOR_H
18 #include "sanitizer_common/sanitizer_allocator.h"
19 #include "sanitizer_common/sanitizer_common.h"
20 #include "sanitizer_common/sanitizer_internal_defs.h"
21 #include "lsan_common.h"
25 void *Allocate(const StackTrace
&stack
, uptr size
, uptr alignment
,
27 void Deallocate(void *p
);
28 void *Reallocate(const StackTrace
&stack
, void *p
, uptr new_size
,
30 uptr
GetMallocUsableSize(const void *p
);
32 template<typename Callable
>
33 void ForEachChunk(const Callable
&callback
);
35 void GetAllocatorCacheRange(uptr
*begin
, uptr
*end
);
36 void AllocatorThreadFinish();
37 void InitializeAllocator();
39 const bool kAlwaysClearMemory
= true;
41 struct ChunkMetadata
{
42 u8 allocated
: 8; // Must be first.
44 #if SANITIZER_WORDSIZE == 64
45 uptr requested_size
: 54;
47 uptr requested_size
: 32;
53 #if defined(__mips64) || defined(__aarch64__) || defined(__i386__) || \
55 static const uptr kRegionSizeLog
= 20;
56 static const uptr kNumRegions
= SANITIZER_MMAP_RANGE_SIZE
>> kRegionSizeLog
;
57 typedef TwoLevelByteMap
<(kNumRegions
>> 12), 1 << 12> ByteMap
;
60 static const uptr kSpaceBeg
= 0;
61 static const u64 kSpaceSize
= SANITIZER_MMAP_RANGE_SIZE
;
62 static const uptr kMetadataSize
= sizeof(ChunkMetadata
);
63 typedef __sanitizer::CompactSizeClassMap SizeClassMap
;
64 static const uptr kRegionSizeLog
= __lsan::kRegionSizeLog
;
65 typedef __lsan::ByteMap ByteMap
;
66 typedef NoOpMapUnmapCallback MapUnmapCallback
;
67 static const uptr kFlags
= 0;
69 typedef SizeClassAllocator32
<AP32
> PrimaryAllocator
;
70 #elif defined(__x86_64__) || defined(__powerpc64__)
71 # if defined(__powerpc64__)
72 const uptr kAllocatorSpace
= 0xa0000000000ULL
;
73 const uptr kAllocatorSize
= 0x20000000000ULL
; // 2T.
75 const uptr kAllocatorSpace
= 0x600000000000ULL
;
76 const uptr kAllocatorSize
= 0x40000000000ULL
; // 4T.
78 struct AP64
{ // Allocator64 parameters. Deliberately using a short name.
79 static const uptr kSpaceBeg
= kAllocatorSpace
;
80 static const uptr kSpaceSize
= kAllocatorSize
;
81 static const uptr kMetadataSize
= sizeof(ChunkMetadata
);
82 typedef DefaultSizeClassMap SizeClassMap
;
83 typedef NoOpMapUnmapCallback MapUnmapCallback
;
84 static const uptr kFlags
= 0;
87 typedef SizeClassAllocator64
<AP64
> PrimaryAllocator
;
89 typedef SizeClassAllocatorLocalCache
<PrimaryAllocator
> AllocatorCache
;
91 AllocatorCache
*GetAllocatorCache();
93 void *lsan_memalign(uptr alignment
, uptr size
, const StackTrace
&stack
);
94 void *lsan_malloc(uptr size
, const StackTrace
&stack
);
95 void lsan_free(void *p
);
96 void *lsan_realloc(void *p
, uptr size
, const StackTrace
&stack
);
97 void *lsan_calloc(uptr nmemb
, uptr size
, const StackTrace
&stack
);
98 void *lsan_valloc(uptr size
, const StackTrace
&stack
);
99 uptr
lsan_mz_size(const void *p
);
101 } // namespace __lsan
103 #endif // LSAN_ALLOCATOR_H