1 //=-- lsan_allocator.h ----------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is a part of LeakSanitizer.
10 // Allocator for standalone LSan.
12 //===----------------------------------------------------------------------===//
14 #ifndef LSAN_ALLOCATOR_H
15 #define LSAN_ALLOCATOR_H
17 #include "sanitizer_common/sanitizer_allocator.h"
18 #include "sanitizer_common/sanitizer_common.h"
19 #include "sanitizer_common/sanitizer_internal_defs.h"
20 #include "lsan_common.h"
24 void *Allocate(const StackTrace
&stack
, uptr size
, uptr alignment
,
26 void Deallocate(void *p
);
27 void *Reallocate(const StackTrace
&stack
, void *p
, uptr new_size
,
29 uptr
GetMallocUsableSize(const void *p
);
31 template<typename Callable
>
32 void ForEachChunk(const Callable
&callback
);
34 void GetAllocatorCacheRange(uptr
*begin
, uptr
*end
);
35 void AllocatorThreadFinish();
36 void InitializeAllocator();
38 const bool kAlwaysClearMemory
= true;
40 struct ChunkMetadata
{
41 u8 allocated
: 8; // Must be first.
43 #if SANITIZER_WORDSIZE == 64
44 uptr requested_size
: 54;
46 uptr requested_size
: 32;
52 #if !SANITIZER_CAN_USE_ALLOCATOR64
53 template <typename AddressSpaceViewTy
>
55 static const uptr kSpaceBeg
= 0;
56 static const u64 kSpaceSize
= SANITIZER_MMAP_RANGE_SIZE
;
57 static const uptr kMetadataSize
= sizeof(ChunkMetadata
);
58 typedef __sanitizer::CompactSizeClassMap SizeClassMap
;
59 static const uptr kRegionSizeLog
= 20;
60 using AddressSpaceView
= AddressSpaceViewTy
;
61 typedef NoOpMapUnmapCallback MapUnmapCallback
;
62 static const uptr kFlags
= 0;
64 template <typename AddressSpaceView
>
65 using PrimaryAllocatorASVT
= SizeClassAllocator32
<AP32
<AddressSpaceView
>>;
66 using PrimaryAllocator
= PrimaryAllocatorASVT
<LocalAddressSpaceView
>;
68 # if SANITIZER_FUCHSIA || defined(__powerpc64__)
69 const uptr kAllocatorSpace
= ~(uptr
)0;
70 const uptr kAllocatorSize
= 0x40000000000ULL
; // 4T.
71 # elif SANITIZER_APPLE
72 const uptr kAllocatorSpace
= 0x600000000000ULL
;
73 const uptr kAllocatorSize
= 0x40000000000ULL
; // 4T.
75 const uptr kAllocatorSpace
= 0x500000000000ULL
;
76 const uptr kAllocatorSize
= 0x40000000000ULL
; // 4T.
78 template <typename AddressSpaceViewTy
>
79 struct AP64
{ // Allocator64 parameters. Deliberately using a short name.
80 static const uptr kSpaceBeg
= kAllocatorSpace
;
81 static const uptr kSpaceSize
= kAllocatorSize
;
82 static const uptr kMetadataSize
= sizeof(ChunkMetadata
);
83 typedef DefaultSizeClassMap SizeClassMap
;
84 typedef NoOpMapUnmapCallback MapUnmapCallback
;
85 static const uptr kFlags
= 0;
86 using AddressSpaceView
= AddressSpaceViewTy
;
89 template <typename AddressSpaceView
>
90 using PrimaryAllocatorASVT
= SizeClassAllocator64
<AP64
<AddressSpaceView
>>;
91 using PrimaryAllocator
= PrimaryAllocatorASVT
<LocalAddressSpaceView
>;
94 template <typename AddressSpaceView
>
95 using AllocatorASVT
= CombinedAllocator
<PrimaryAllocatorASVT
<AddressSpaceView
>>;
96 using Allocator
= AllocatorASVT
<LocalAddressSpaceView
>;
97 using AllocatorCache
= Allocator::AllocatorCache
;
99 Allocator::AllocatorCache
*GetAllocatorCache();
101 int lsan_posix_memalign(void **memptr
, uptr alignment
, uptr size
,
102 const StackTrace
&stack
);
103 void *lsan_aligned_alloc(uptr alignment
, uptr size
, const StackTrace
&stack
);
104 void *lsan_memalign(uptr alignment
, uptr size
, const StackTrace
&stack
);
105 void *lsan_malloc(uptr size
, const StackTrace
&stack
);
106 void lsan_free(void *p
);
107 void *lsan_realloc(void *p
, uptr size
, const StackTrace
&stack
);
108 void *lsan_reallocarray(void *p
, uptr nmemb
, uptr size
,
109 const StackTrace
&stack
);
110 void *lsan_calloc(uptr nmemb
, uptr size
, const StackTrace
&stack
);
111 void *lsan_valloc(uptr size
, const StackTrace
&stack
);
112 void *lsan_pvalloc(uptr size
, const StackTrace
&stack
);
113 uptr
lsan_mz_size(const void *p
);
115 } // namespace __lsan
117 #endif // LSAN_ALLOCATOR_H