Home | History | Annotate | Line # | Download | only in sanitizer_common
      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_common.h"
     18 #include "sanitizer_internal_defs.h"
     19 #include "sanitizer_lfstack.h"
     20 #include "sanitizer_libc.h"
     21 #include "sanitizer_list.h"
     22 #include "sanitizer_local_address_space_view.h"
     23 #include "sanitizer_mutex.h"
     24 #include "sanitizer_procmaps.h"
     25 #include "sanitizer_type_traits.h"
     26 
     27 namespace __sanitizer {
     28 
     29 // Allows the tools to name their allocations appropriately.
     30 extern const char *PrimaryAllocatorName;
     31 extern const char *SecondaryAllocatorName;
     32 
     33 // Since flags are immutable and allocator behavior can be changed at runtime
     34 // (unit tests or ASan on Android are some examples), allocator_may_return_null
     35 // flag value is cached here and can be altered later.
     36 bool AllocatorMayReturnNull();
     37 void SetAllocatorMayReturnNull(bool may_return_null);
     38 
     39 // Returns true if allocator detected OOM condition. Can be used to avoid memory
     40 // hungry operations.
     41 bool IsAllocatorOutOfMemory();
     42 // Should be called by a particular allocator when OOM is detected.
     43 void SetAllocatorOutOfMemory();
     44 
     45 void PrintHintAllocatorCannotReturnNull();
     46 
     47 // Allocators call these callbacks on mmap/munmap.
     48 struct NoOpMapUnmapCallback {
     49   void OnMap(uptr p, uptr size) const { }
     50   void OnUnmap(uptr p, uptr size) const { }
     51 };
     52 
     53 // Callback type for iterating over chunks.
     54 typedef void (*ForEachChunkCallback)(uptr chunk, void *arg);
     55 
     56 INLINE u32 Rand(u32 *state) {  // ANSI C linear congruential PRNG.
     57   return (*state = *state * 1103515245 + 12345) >> 16;
     58 }
     59 
     60 INLINE u32 RandN(u32 *state, u32 n) { return Rand(state) % n; }  // [0, n)
     61 
     62 template<typename T>
     63 INLINE void RandomShuffle(T *a, u32 n, u32 *rand_state) {
     64   if (n <= 1) return;
     65   u32 state = *rand_state;
     66   for (u32 i = n - 1; i > 0; i--)
     67     Swap(a[i], a[RandN(&state, i + 1)]);
     68   *rand_state = state;
     69 }
     70 
     71 #include "sanitizer_allocator_size_class_map.h"
     72 #include "sanitizer_allocator_stats.h"
     73 #include "sanitizer_allocator_primary64.h"
     74 #include "sanitizer_allocator_bytemap.h"
     75 #include "sanitizer_allocator_primary32.h"
     76 #include "sanitizer_allocator_local_cache.h"
     77 #include "sanitizer_allocator_secondary.h"
     78 #include "sanitizer_allocator_combined.h"
     79 
     80 } // namespace __sanitizer
     81 
     82 #endif // SANITIZER_ALLOCATOR_H
     83