1//===-- sanitizer_allocator.h -----------------------------------*- C++ -*-===// 2// 3// This file is distributed under the University of Illinois Open Source 4// License. See LICENSE.TXT for details. 5// 6//===----------------------------------------------------------------------===// 7// 8// Specialized memory allocator for ThreadSanitizer, MemorySanitizer, etc. 9// 10//===----------------------------------------------------------------------===// 11 12#ifndef SANITIZER_ALLOCATOR_H 13#define SANITIZER_ALLOCATOR_H 14 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" 22 23namespace __sanitizer { 24 25// Allows the tools to name their allocations appropriately. 26extern const char *PrimaryAllocatorName; 27extern const char *SecondaryAllocatorName; 28 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. 32bool AllocatorMayReturnNull(); 33void SetAllocatorMayReturnNull(bool may_return_null); 34 35// Returns true if allocator detected OOM condition. Can be used to avoid memory 36// hungry operations. 37bool IsAllocatorOutOfMemory(); 38// Should be called by a particular allocator when OOM is detected. 39void SetAllocatorOutOfMemory(); 40 41void PrintHintAllocatorCannotReturnNull(); 42 43// Allocators call these callbacks on mmap/munmap. 44struct NoOpMapUnmapCallback { 45 void OnMap(uptr p, uptr size) const { } 46 void OnUnmap(uptr p, uptr size) const { } 47}; 48 49// Callback type for iterating over chunks. 50typedef void (*ForEachChunkCallback)(uptr chunk, void *arg); 51 52INLINE u32 Rand(u32 *state) { // ANSI C linear congruential PRNG. 53 return (*state = *state * 1103515245 + 12345) >> 16; 54} 55 56INLINE u32 RandN(u32 *state, u32 n) { return Rand(state) % n; } // [0, n) 57 58template<typename T> 59INLINE void RandomShuffle(T *a, u32 n, u32 *rand_state) { 60 if (n <= 1) return; 61 u32 state = *rand_state; 62 for (u32 i = n - 1; i > 0; i--) 63 Swap(a[i], a[RandN(&state, i + 1)]); 64 *rand_state = state; 65} 66 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" 75 76} // namespace __sanitizer 77 78#endif // SANITIZER_ALLOCATOR_H 79