OLD | NEW |
(Empty) | |
| 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 |
| 5 #ifndef BASE_ALLOCATOR_ALLOCATOR_SHIM_H_ |
| 6 #define BASE_ALLOCATOR_ALLOCATOR_SHIM_H_ |
| 7 |
| 8 #include <stddef.h> |
| 9 |
| 10 #include "base/base_export.h" |
| 11 |
| 12 namespace base { |
| 13 namespace allocator { |
| 14 |
| 15 // Allocator Shim API. Allows to to: |
| 16 // - Configure the behavior of the allocator (what to do on OOM failures). |
| 17 // - Install new hooks (AllocatorDispatch) in the allocator chain. |
| 18 |
| 19 // When this shim layer is enabled, the route of an allocation is as-follows: |
| 20 // |
| 21 // [allocator_shim_override_*.h] Intercept malloc() / operator new calls: |
| 22 // The override_* headers define the symbols required to intercept calls to |
| 23 // malloc() and operator new (if not overridden by specific C++ classes). |
| 24 // |
| 25 // [allocator_shim.cc] Routing allocation calls to the shim: |
| 26 // The headers above route the calls to the internal ShimMalloc(), ShimFree(), |
| 27 // ShimCppNew() etc. methods defined in allocator_shim.cc. |
| 28 // These methods will: (1) forward the allocation call to the front of the |
| 29 // AllocatorDispatch chain. (2) perform security hardenings (e.g., might |
| 30 // call std::new_handler on OOM failure). |
| 31 // |
| 32 // [allocator_shim_default_dispatch_to_*.cc] The AllocatorDispatch chain: |
| 33 // It is a singly linked list where each element is a struct with function |
| 34 // pointers (|malloc_function|, |free_function|, etc). Normally the chain |
| 35 // consists of a single AllocatorDispatch element, herein called |
| 36 // the "default dispatch", which is statically defined at build time and |
| 37 // ultimately routes the calls to the actual allocator defined by the build |
| 38 // config (tcmalloc, glibc, ...). |
| 39 // |
| 40 // It is possible to dynamically insert (and remove) further AllocatorDispatch |
| 41 // stages to the front of the chain, for debugging / profiling purposes. |
| 42 |
| 43 struct AllocatorDispatch { |
| 44 using AllocFn = void*(size_t size, const AllocatorDispatch* self); |
| 45 using AllocZeroInitializedFn = void*(size_t n, |
| 46 size_t size, |
| 47 const AllocatorDispatch* self); |
| 48 using AllocAlignedFn = void*(size_t alignment, |
| 49 size_t size, |
| 50 const AllocatorDispatch* self); |
| 51 using ReallocFn = void*(void* address, |
| 52 size_t size, |
| 53 const AllocatorDispatch* self); |
| 54 using FreeFn = void(void* address, const AllocatorDispatch* self); |
| 55 |
| 56 AllocFn* const alloc_function; |
| 57 AllocZeroInitializedFn* const alloc_zero_initialized_function; |
| 58 AllocAlignedFn* const alloc_aligned_function; |
| 59 ReallocFn* const realloc_function; |
| 60 FreeFn* const free_function; |
| 61 |
| 62 const AllocatorDispatch* next; |
| 63 |
| 64 // |default_dispatch| is statically defined by one (and only one) of the |
| 65 // allocator_shim_default_dispatch_to_*.cc files, depending on the build |
| 66 // configuration. |
| 67 static const AllocatorDispatch default_dispatch; |
| 68 }; |
| 69 |
| 70 // When true makes malloc behave like new, w.r.t calling the new_handler if |
| 71 // the allocation fails (see set_new_mode() in Windows). |
| 72 BASE_EXPORT void SetCallNewHandlerOnMallocFailure(bool value); |
| 73 |
| 74 // Allocates |size| bytes or returns nullptr. It does NOT call the new_handler, |
| 75 // regardless of SetCallNewHandlerOnMallocFailure(). |
| 76 BASE_EXPORT void* UncheckedAlloc(size_t size); |
| 77 |
| 78 // Inserts |dispatch| in front of the allocator chain. This method is NOT |
| 79 // thread-safe w.r.t concurrent invocations of Insert/RemoveAllocatorDispatch(). |
| 80 // The callers have the responsibility of linearizing the changes to the chain |
| 81 // (or more likely call these always on the same thread). |
| 82 BASE_EXPORT void InsertAllocatorDispatch(AllocatorDispatch* dispatch); |
| 83 |
| 84 // Removes |dispatch| from the allocator chain. |
| 85 // It is not safe to free |dispatch| after this call, as other threads might be |
| 86 // still derefercing it. In other words, make the AllocatorDispatch a static |
| 87 // variable in the caller and expect spurious calls even after this returns. |
| 88 BASE_EXPORT void RemoveAllocatorDispatch(AllocatorDispatch* dispatch); |
| 89 |
| 90 } // namespace allocator |
| 91 } // namespace base |
| 92 |
| 93 #endif // BASE_ALLOCATOR_ALLOCATOR_SHIM_H_ |
OLD | NEW |