1 //===-- sanitizer_allocator.h -----------------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // Specialized memory allocator for ThreadSanitizer, MemorySanitizer, etc.
10 //===----------------------------------------------------------------------===//
12 #ifndef SANITIZER_ALLOCATOR_H
13 #define SANITIZER_ALLOCATOR_H
15 #include "sanitizer_internal_defs.h"
16 #include "sanitizer_common.h"
17 #include "sanitizer_libc.h"
18 #include "sanitizer_list.h"
19 #include "sanitizer_mutex.h"
20 #include "sanitizer_lfstack.h"
21 #include "sanitizer_procmaps.h"
23 namespace __sanitizer
{
25 // Allows the tools to name their allocations appropriately.
26 extern const char *PrimaryAllocatorName
;
27 extern const char *SecondaryAllocatorName
;
29 // Since flags are immutable and allocator behavior can be changed at runtime
30 // (unit tests or ASan on Android are some examples), allocator_may_return_null
31 // flag value is cached here and can be altered later.
32 bool AllocatorMayReturnNull();
33 void SetAllocatorMayReturnNull(bool may_return_null
);
35 // Returns true if allocator detected OOM condition. Can be used to avoid memory
37 bool IsAllocatorOutOfMemory();
38 // Should be called by a particular allocator when OOM is detected.
39 void SetAllocatorOutOfMemory();
41 void PrintHintAllocatorCannotReturnNull();
43 // Allocators call these callbacks on mmap/munmap.
44 struct NoOpMapUnmapCallback
{
45 void OnMap(uptr p
, uptr size
) const { }
46 void OnUnmap(uptr p
, uptr size
) const { }
49 // Callback type for iterating over chunks.
50 typedef void (*ForEachChunkCallback
)(uptr chunk
, void *arg
);
52 INLINE u32
Rand(u32
*state
) { // ANSI C linear congruential PRNG.
53 return (*state
= *state
* 1103515245 + 12345) >> 16;
56 INLINE u32
RandN(u32
*state
, u32 n
) { return Rand(state
) % n
; } // [0, n)
59 INLINE
void RandomShuffle(T
*a
, u32 n
, u32
*rand_state
) {
61 u32 state
= *rand_state
;
62 for (u32 i
= n
- 1; i
> 0; i
--)
63 Swap(a
[i
], a
[RandN(&state
, i
+ 1)]);
67 #include "sanitizer_allocator_size_class_map.h"
68 #include "sanitizer_allocator_stats.h"
69 #include "sanitizer_allocator_primary64.h"
70 #include "sanitizer_allocator_bytemap.h"
71 #include "sanitizer_allocator_primary32.h"
72 #include "sanitizer_allocator_local_cache.h"
73 #include "sanitizer_allocator_secondary.h"
74 #include "sanitizer_allocator_combined.h"
76 } // namespace __sanitizer
78 #endif // SANITIZER_ALLOCATOR_H