low_level_alloc.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. #ifndef ABSL_BASE_INTERNAL_LOW_LEVEL_ALLOC_H_
  16. #define ABSL_BASE_INTERNAL_LOW_LEVEL_ALLOC_H_
  17. // A simple thread-safe memory allocator that does not depend on
  18. // mutexes or thread-specific data. It is intended to be used
  19. // sparingly, and only when malloc() would introduce an unwanted
  20. // dependency, such as inside the heap-checker, or the Mutex
  21. // implementation.
  22. // IWYU pragma: private, include "base/low_level_alloc.h"
  23. #include <sys/types.h>
  24. #include <cstdint>
  25. #include "absl/base/attributes.h"
  26. #include "absl/base/config.h"
  27. // LowLevelAlloc requires that the platform support low-level
  28. // allocation of virtual memory. Platforms lacking this cannot use
  29. // LowLevelAlloc.
  30. #ifdef ABSL_LOW_LEVEL_ALLOC_MISSING
  31. #error ABSL_LOW_LEVEL_ALLOC_MISSING cannot be directly set
  32. #elif !defined(ABSL_HAVE_MMAP) && !defined(_WIN32)
  33. #define ABSL_LOW_LEVEL_ALLOC_MISSING 1
  34. #endif
  35. // Using LowLevelAlloc with kAsyncSignalSafe isn't supported on Windows or
  36. // asm.js / WebAssembly.
  37. // See https://kripken.github.io/emscripten-site/docs/porting/pthreads.html
  38. // for more information.
  39. #ifdef ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING
  40. #error ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING cannot be directly set
  41. #elif defined(_WIN32) || defined(__asmjs__) || defined(__wasm__) || \
  42. defined(__hexagon__)
  43. #define ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING 1
  44. #endif
  45. #include <cstddef>
  46. #include "absl/base/port.h"
  47. namespace absl {
  48. ABSL_NAMESPACE_BEGIN
  49. namespace base_internal {
  50. class LowLevelAlloc {
  51. public:
  52. struct Arena; // an arena from which memory may be allocated
  53. // Returns a pointer to a block of at least "request" bytes
  54. // that have been newly allocated from the specific arena.
  55. // for Alloc() call the DefaultArena() is used.
  56. // Returns 0 if passed request==0.
  57. // Does not return 0 under other circumstances; it crashes if memory
  58. // is not available.
  59. static void *Alloc(size_t request) ABSL_ATTRIBUTE_SECTION(malloc_hook);
  60. static void *AllocWithArena(size_t request, Arena *arena)
  61. ABSL_ATTRIBUTE_SECTION(malloc_hook);
  62. // Deallocates a region of memory that was previously allocated with
  63. // Alloc(). Does nothing if passed 0. "s" must be either 0,
  64. // or must have been returned from a call to Alloc() and not yet passed to
  65. // Free() since that call to Alloc(). The space is returned to the arena
  66. // from which it was allocated.
  67. static void Free(void *s) ABSL_ATTRIBUTE_SECTION(malloc_hook);
  68. // ABSL_ATTRIBUTE_SECTION(malloc_hook) for Alloc* and Free
  69. // are to put all callers of MallocHook::Invoke* in this module
  70. // into special section,
  71. // so that MallocHook::GetCallerStackTrace can function accurately.
  72. // Create a new arena.
  73. // The root metadata for the new arena is allocated in the
  74. // meta_data_arena; the DefaultArena() can be passed for meta_data_arena.
  75. // These values may be ored into flags:
  76. enum {
  77. // Report calls to Alloc() and Free() via the MallocHook interface.
  78. // Set in the DefaultArena.
  79. kCallMallocHook = 0x0001,
  80. #ifndef ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING
  81. // Make calls to Alloc(), Free() be async-signal-safe. Not set in
  82. // DefaultArena(). Not supported on all platforms.
  83. kAsyncSignalSafe = 0x0002,
  84. #endif
  85. };
  86. // Construct a new arena. The allocation of the underlying metadata honors
  87. // the provided flags. For example, the call NewArena(kAsyncSignalSafe)
  88. // is itself async-signal-safe, as well as generatating an arena that provides
  89. // async-signal-safe Alloc/Free.
  90. static Arena *NewArena(uint32_t flags);
  91. // Destroys an arena allocated by NewArena and returns true,
  92. // provided no allocated blocks remain in the arena.
  93. // If allocated blocks remain in the arena, does nothing and
  94. // returns false.
  95. // It is illegal to attempt to destroy the DefaultArena().
  96. static bool DeleteArena(Arena *arena);
  97. // The default arena that always exists.
  98. static Arena *DefaultArena();
  99. private:
  100. LowLevelAlloc(); // no instances
  101. };
  102. } // namespace base_internal
  103. ABSL_NAMESPACE_END
  104. } // namespace absl
  105. #endif // ABSL_BASE_INTERNAL_LOW_LEVEL_ALLOC_H_