sanitizer_allocator.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //===-- sanitizer_allocator.h -----------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Specialized memory allocator for ThreadSanitizer, MemorySanitizer, etc.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef SANITIZER_ALLOCATOR_H
  13. #define SANITIZER_ALLOCATOR_H
  14. #include "sanitizer_common.h"
  15. #include "sanitizer_flat_map.h"
  16. #include "sanitizer_internal_defs.h"
  17. #include "sanitizer_lfstack.h"
  18. #include "sanitizer_libc.h"
  19. #include "sanitizer_list.h"
  20. #include "sanitizer_local_address_space_view.h"
  21. #include "sanitizer_mutex.h"
  22. #include "sanitizer_procmaps.h"
  23. #include "sanitizer_type_traits.h"
  24. namespace __sanitizer {
  25. // Allows the tools to name their allocations appropriately.
  26. extern const char *PrimaryAllocatorName;
  27. extern const char *SecondaryAllocatorName;
  28. // Since flags are immutable and allocator behavior can be changed at runtime
  29. // (unit tests or ASan on Android are some examples), allocator_may_return_null
  30. // flag value is cached here and can be altered later.
  31. bool AllocatorMayReturnNull();
  32. void SetAllocatorMayReturnNull(bool may_return_null);
  33. // Returns true if allocator detected OOM condition. Can be used to avoid memory
  34. // hungry operations.
  35. bool IsAllocatorOutOfMemory();
  36. // Should be called by a particular allocator when OOM is detected.
  37. void SetAllocatorOutOfMemory();
  38. void PrintHintAllocatorCannotReturnNull();
  39. // Callback type for iterating over chunks.
  40. typedef void (*ForEachChunkCallback)(uptr chunk, void *arg);
  41. inline u32 Rand(u32 *state) { // ANSI C linear congruential PRNG.
  42. return (*state = *state * 1103515245 + 12345) >> 16;
  43. }
  44. inline u32 RandN(u32 *state, u32 n) { return Rand(state) % n; } // [0, n)
  45. template<typename T>
  46. inline void RandomShuffle(T *a, u32 n, u32 *rand_state) {
  47. if (n <= 1) return;
  48. u32 state = *rand_state;
  49. for (u32 i = n - 1; i > 0; i--)
  50. Swap(a[i], a[RandN(&state, i + 1)]);
  51. *rand_state = state;
  52. }
  53. #include "sanitizer_allocator_size_class_map.h"
  54. #include "sanitizer_allocator_stats.h"
  55. #include "sanitizer_allocator_primary64.h"
  56. #include "sanitizer_allocator_primary32.h"
  57. #include "sanitizer_allocator_local_cache.h"
  58. #include "sanitizer_allocator_secondary.h"
  59. #include "sanitizer_allocator_combined.h"
  60. } // namespace __sanitizer
  61. #endif // SANITIZER_ALLOCATOR_H