]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_allocator.h
MFV r328490: Update libfdt to github:f1879e1
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / sanitizer_allocator.h
1 //===-- sanitizer_allocator.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 //
10 // Specialized memory allocator for ThreadSanitizer, MemorySanitizer, etc.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef SANITIZER_ALLOCATOR_H
15 #define SANITIZER_ALLOCATOR_H
16
17 #include "sanitizer_internal_defs.h"
18 #include "sanitizer_common.h"
19 #include "sanitizer_libc.h"
20 #include "sanitizer_list.h"
21 #include "sanitizer_mutex.h"
22 #include "sanitizer_lfstack.h"
23 #include "sanitizer_procmaps.h"
24
25 namespace __sanitizer {
26
27 // Since flags are immutable and allocator behavior can be changed at runtime
28 // (unit tests or ASan on Android are some examples), allocator_may_return_null
29 // flag value is cached here and can be altered later.
30 bool AllocatorMayReturnNull();
31 void SetAllocatorMayReturnNull(bool may_return_null);
32
33 // Allocator failure handling policies:
34 // Implements AllocatorMayReturnNull policy, returns null when the flag is set,
35 // dies otherwise.
36 struct ReturnNullOrDieOnFailure {
37   static void *OnBadRequest();
38   static void *OnOOM();
39 };
40 // Always dies on the failure.
41 struct DieOnFailure {
42   static void NORETURN *OnBadRequest();
43   static void NORETURN *OnOOM();
44 };
45
46 // Returns true if allocator detected OOM condition. Can be used to avoid memory
47 // hungry operations. Set when AllocatorReturnNullOrDieOnOOM() is called.
48 bool IsAllocatorOutOfMemory();
49
50 // Allocators call these callbacks on mmap/munmap.
51 struct NoOpMapUnmapCallback {
52   void OnMap(uptr p, uptr size) const { }
53   void OnUnmap(uptr p, uptr size) const { }
54 };
55
56 // Callback type for iterating over chunks.
57 typedef void (*ForEachChunkCallback)(uptr chunk, void *arg);
58
59 INLINE u32 Rand(u32 *state) {  // ANSI C linear congruential PRNG.
60   return (*state = *state * 1103515245 + 12345) >> 16;
61 }
62
63 INLINE u32 RandN(u32 *state, u32 n) { return Rand(state) % n; }  // [0, n)
64
65 template<typename T>
66 INLINE void RandomShuffle(T *a, u32 n, u32 *rand_state) {
67   if (n <= 1) return;
68   for (u32 i = n - 1; i > 0; i--)
69     Swap(a[i], a[RandN(rand_state, i + 1)]);
70 }
71
72 #include "sanitizer_allocator_size_class_map.h"
73 #include "sanitizer_allocator_stats.h"
74 #include "sanitizer_allocator_primary64.h"
75 #include "sanitizer_allocator_bytemap.h"
76 #include "sanitizer_allocator_primary32.h"
77 #include "sanitizer_allocator_local_cache.h"
78 #include "sanitizer_allocator_secondary.h"
79 #include "sanitizer_allocator_combined.h"
80
81 } // namespace __sanitizer
82
83 #endif // SANITIZER_ALLOCATOR_H