sanitizer_allocator.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. struct NoOpMapUnmapCallback {
  54. void OnMap(uptr p, uptr size) const {}
  55. void OnMapSecondary(uptr p, uptr size, uptr user_begin,
  56. uptr user_size) const {}
  57. void OnUnmap(uptr p, uptr size) const {}
  58. };
  59. #include "sanitizer_allocator_size_class_map.h"
  60. #include "sanitizer_allocator_stats.h"
  61. #include "sanitizer_allocator_primary64.h"
  62. #include "sanitizer_allocator_primary32.h"
  63. #include "sanitizer_allocator_local_cache.h"
  64. #include "sanitizer_allocator_secondary.h"
  65. #include "sanitizer_allocator_combined.h"
  66. bool IsRssLimitExceeded();
  67. void SetRssLimitExceeded(bool limit_exceeded);
  68. } // namespace __sanitizer
  69. #endif // SANITIZER_ALLOCATOR_H