low_level_alloc.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #define ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING 1
  43. #endif
  44. #include <cstddef>
  45. #include "absl/base/port.h"
  46. namespace absl {
  47. ABSL_NAMESPACE_BEGIN
  48. namespace base_internal {
  49. class LowLevelAlloc {
  50. public:
  51. struct Arena; // an arena from which memory may be allocated
  52. // Returns a pointer to a block of at least "request" bytes
  53. // that have been newly allocated from the specific arena.
  54. // for Alloc() call the DefaultArena() is used.
  55. // Returns 0 if passed request==0.
  56. // Does not return 0 under other circumstances; it crashes if memory
  57. // is not available.
  58. static void *Alloc(size_t request) ABSL_ATTRIBUTE_SECTION(malloc_hook);
  59. static void *AllocWithArena(size_t request, Arena *arena)
  60. ABSL_ATTRIBUTE_SECTION(malloc_hook);
  61. // Deallocates a region of memory that was previously allocated with
  62. // Alloc(). Does nothing if passed 0. "s" must be either 0,
  63. // or must have been returned from a call to Alloc() and not yet passed to
  64. // Free() since that call to Alloc(). The space is returned to the arena
  65. // from which it was allocated.
  66. static void Free(void *s) ABSL_ATTRIBUTE_SECTION(malloc_hook);
  67. // ABSL_ATTRIBUTE_SECTION(malloc_hook) for Alloc* and Free
  68. // are to put all callers of MallocHook::Invoke* in this module
  69. // into special section,
  70. // so that MallocHook::GetCallerStackTrace can function accurately.
  71. // Create a new arena.
  72. // The root metadata for the new arena is allocated in the
  73. // meta_data_arena; the DefaultArena() can be passed for meta_data_arena.
  74. // These values may be ored into flags:
  75. enum {
  76. // Report calls to Alloc() and Free() via the MallocHook interface.
  77. // Set in the DefaultArena.
  78. kCallMallocHook = 0x0001,
  79. #ifndef ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING
  80. // Make calls to Alloc(), Free() be async-signal-safe. Not set in
  81. // DefaultArena(). Not supported on all platforms.
  82. kAsyncSignalSafe = 0x0002,
  83. #endif
  84. };
  85. // Construct a new arena. The allocation of the underlying metadata honors
  86. // the provided flags. For example, the call NewArena(kAsyncSignalSafe)
  87. // is itself async-signal-safe, as well as generatating an arena that provides
  88. // async-signal-safe Alloc/Free.
  89. static Arena *NewArena(uint32_t flags);
  90. // Destroys an arena allocated by NewArena and returns true,
  91. // provided no allocated blocks remain in the arena.
  92. // If allocated blocks remain in the arena, does nothing and
  93. // returns false.
  94. // It is illegal to attempt to destroy the DefaultArena().
  95. static bool DeleteArena(Arena *arena);
  96. // The default arena that always exists.
  97. static Arena *DefaultArena();
  98. private:
  99. LowLevelAlloc(); // no instances
  100. };
  101. } // namespace base_internal
  102. ABSL_NAMESPACE_END
  103. } // namespace absl
  104. #endif // ABSL_BASE_INTERNAL_LOW_LEVEL_ALLOC_H_