allocator_shim.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_ALLOCATOR_ALLOCATOR_SHIM_H_
  5. #define BASE_ALLOCATOR_ALLOCATOR_SHIM_H_
  6. #include <stddef.h>
  7. #include "base/base_export.h"
  8. #include "build/build_config.h"
  9. namespace base {
  10. namespace allocator {
  11. // Allocator Shim API. Allows to:
  12. // - Configure the behavior of the allocator (what to do on OOM failures).
  13. // - Install new hooks (AllocatorDispatch) in the allocator chain.
  14. // When this shim layer is enabled, the route of an allocation is as-follows:
  15. //
  16. // [allocator_shim_override_*.h] Intercept malloc() / operator new calls:
  17. // The override_* headers define the symbols required to intercept calls to
  18. // malloc() and operator new (if not overridden by specific C++ classes).
  19. //
  20. // [allocator_shim.cc] Routing allocation calls to the shim:
  21. // The headers above route the calls to the internal ShimMalloc(), ShimFree(),
  22. // ShimCppNew() etc. methods defined in allocator_shim.cc.
  23. // These methods will: (1) forward the allocation call to the front of the
  24. // AllocatorDispatch chain. (2) perform security hardenings (e.g., might
  25. // call std::new_handler on OOM failure).
  26. //
  27. // [allocator_shim_default_dispatch_to_*.cc] The AllocatorDispatch chain:
  28. // It is a singly linked list where each element is a struct with function
  29. // pointers (|malloc_function|, |free_function|, etc). Normally the chain
  30. // consists of a single AllocatorDispatch element, herein called
  31. // the "default dispatch", which is statically defined at build time and
  32. // ultimately routes the calls to the actual allocator defined by the build
  33. // config (tcmalloc, glibc, ...).
  34. //
  35. // It is possible to dynamically insert further AllocatorDispatch stages
  36. // to the front of the chain, for debugging / profiling purposes.
  37. //
  38. // All the functions must be thread safe. The shim does not enforce any
  39. // serialization. This is to route to thread-aware allocators (e.g, tcmalloc)
  40. // wihout introducing unnecessary perf hits.
  41. struct AllocatorDispatch {
  42. using AllocFn = void*(const AllocatorDispatch* self,
  43. size_t size,
  44. void* context);
  45. using AllocUncheckedFn = void*(const AllocatorDispatch* self,
  46. size_t size,
  47. void* context);
  48. using AllocZeroInitializedFn = void*(const AllocatorDispatch* self,
  49. size_t n,
  50. size_t size,
  51. void* context);
  52. using AllocAlignedFn = void*(const AllocatorDispatch* self,
  53. size_t alignment,
  54. size_t size,
  55. void* context);
  56. using ReallocFn = void*(const AllocatorDispatch* self,
  57. void* address,
  58. size_t size,
  59. void* context);
  60. using FreeFn = void(const AllocatorDispatch* self,
  61. void* address,
  62. void* context);
  63. // Returns the best available estimate for the actual amount of memory
  64. // consumed by the allocation |address|. If possible, this should include
  65. // heap overhead or at least a decent estimate of the full cost of the
  66. // allocation. If no good estimate is possible, returns zero.
  67. using GetSizeEstimateFn = size_t(const AllocatorDispatch* self,
  68. void* address,
  69. void* context);
  70. using BatchMallocFn = unsigned(const AllocatorDispatch* self,
  71. size_t size,
  72. void** results,
  73. unsigned num_requested,
  74. void* context);
  75. using BatchFreeFn = void(const AllocatorDispatch* self,
  76. void** to_be_freed,
  77. unsigned num_to_be_freed,
  78. void* context);
  79. using FreeDefiniteSizeFn = void(const AllocatorDispatch* self,
  80. void* ptr,
  81. size_t size,
  82. void* context);
  83. using AlignedMallocFn = void*(const AllocatorDispatch* self,
  84. size_t size,
  85. size_t alignment,
  86. void* context);
  87. using AlignedReallocFn = void*(const AllocatorDispatch* self,
  88. void* address,
  89. size_t size,
  90. size_t alignment,
  91. void* context);
  92. using AlignedFreeFn = void(const AllocatorDispatch* self,
  93. void* address,
  94. void* context);
  95. AllocFn* const alloc_function;
  96. AllocUncheckedFn* const alloc_unchecked_function;
  97. AllocZeroInitializedFn* const alloc_zero_initialized_function;
  98. AllocAlignedFn* const alloc_aligned_function;
  99. ReallocFn* const realloc_function;
  100. FreeFn* const free_function;
  101. GetSizeEstimateFn* const get_size_estimate_function;
  102. // batch_malloc, batch_free, and free_definite_size are specific to the OSX
  103. // and iOS allocators.
  104. BatchMallocFn* const batch_malloc_function;
  105. BatchFreeFn* const batch_free_function;
  106. FreeDefiniteSizeFn* const free_definite_size_function;
  107. // _aligned_malloc, _aligned_realloc, and _aligned_free are specific to the
  108. // Windows allocator.
  109. AlignedMallocFn* const aligned_malloc_function;
  110. AlignedReallocFn* const aligned_realloc_function;
  111. AlignedFreeFn* const aligned_free_function;
  112. const AllocatorDispatch* next;
  113. // |default_dispatch| is statically defined by one (and only one) of the
  114. // allocator_shim_default_dispatch_to_*.cc files, depending on the build
  115. // configuration.
  116. static const AllocatorDispatch default_dispatch;
  117. };
  118. // When true makes malloc behave like new, w.r.t calling the new_handler if
  119. // the allocation fails (see set_new_mode() in Windows).
  120. BASE_EXPORT void SetCallNewHandlerOnMallocFailure(bool value);
  121. // Allocates |size| bytes or returns nullptr. It does NOT call the new_handler,
  122. // regardless of SetCallNewHandlerOnMallocFailure().
  123. BASE_EXPORT void* UncheckedAlloc(size_t size);
  124. // Inserts |dispatch| in front of the allocator chain. This method is
  125. // thread-safe w.r.t concurrent invocations of InsertAllocatorDispatch().
  126. // The callers have responsibility for inserting a single dispatch no more
  127. // than once.
  128. BASE_EXPORT void InsertAllocatorDispatch(AllocatorDispatch* dispatch);
  129. // Test-only. Rationale: (1) lack of use cases; (2) dealing safely with a
  130. // removal of arbitrary elements from a singly linked list would require a lock
  131. // in malloc(), which we really don't want.
  132. BASE_EXPORT void RemoveAllocatorDispatchForTesting(AllocatorDispatch* dispatch);
  133. #if defined(OS_APPLE)
  134. // On macOS, the allocator shim needs to be turned on during runtime.
  135. BASE_EXPORT void InitializeAllocatorShim();
  136. #endif // defined(OS_APPLE)
  137. } // namespace allocator
  138. } // namespace base
  139. #endif // BASE_ALLOCATOR_ALLOCATOR_SHIM_H_