Path: blob/main/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_allocator.h
35233 views
//===-- sanitizer_allocator.h -----------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Specialized memory allocator for ThreadSanitizer, MemorySanitizer, etc.9//10//===----------------------------------------------------------------------===//1112#ifndef SANITIZER_ALLOCATOR_H13#define SANITIZER_ALLOCATOR_H1415#include "sanitizer_common.h"16#include "sanitizer_flat_map.h"17#include "sanitizer_internal_defs.h"18#include "sanitizer_lfstack.h"19#include "sanitizer_libc.h"20#include "sanitizer_list.h"21#include "sanitizer_local_address_space_view.h"22#include "sanitizer_mutex.h"23#include "sanitizer_procmaps.h"24#include "sanitizer_type_traits.h"2526namespace __sanitizer {2728// Allows the tools to name their allocations appropriately.29extern const char *PrimaryAllocatorName;30extern const char *SecondaryAllocatorName;3132// Since flags are immutable and allocator behavior can be changed at runtime33// (unit tests or ASan on Android are some examples), allocator_may_return_null34// flag value is cached here and can be altered later.35bool AllocatorMayReturnNull();36void SetAllocatorMayReturnNull(bool may_return_null);3738// Returns true if allocator detected OOM condition. Can be used to avoid memory39// hungry operations.40bool IsAllocatorOutOfMemory();41// Should be called by a particular allocator when OOM is detected.42void SetAllocatorOutOfMemory();4344void PrintHintAllocatorCannotReturnNull();4546// Callback type for iterating over chunks.47typedef void (*ForEachChunkCallback)(uptr chunk, void *arg);4849inline u32 Rand(u32 *state) { // ANSI C linear congruential PRNG.50return (*state = *state * 1103515245 + 12345) >> 16;51}5253inline u32 RandN(u32 *state, u32 n) { return Rand(state) % n; } // [0, n)5455template<typename T>56inline void RandomShuffle(T *a, u32 n, u32 *rand_state) {57if (n <= 1) return;58u32 state = *rand_state;59for (u32 i = n - 1; i > 0; i--)60Swap(a[i], a[RandN(&state, i + 1)]);61*rand_state = state;62}6364struct NoOpMapUnmapCallback {65void OnMap(uptr p, uptr size) const {}66void OnMapSecondary(uptr p, uptr size, uptr user_begin,67uptr user_size) const {}68void OnUnmap(uptr p, uptr size) const {}69};7071#include "sanitizer_allocator_size_class_map.h"72#include "sanitizer_allocator_stats.h"73#include "sanitizer_allocator_primary64.h"74#include "sanitizer_allocator_primary32.h"75#include "sanitizer_allocator_local_cache.h"76#include "sanitizer_allocator_secondary.h"77#include "sanitizer_allocator_combined.h"7879bool IsRssLimitExceeded();80void SetRssLimitExceeded(bool limit_exceeded);8182} // namespace __sanitizer8384#endif // SANITIZER_ALLOCATOR_H858687