OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_ALLOCATOR_ALLOCATOR_SHIM_H_ | 5 #ifndef BASE_ALLOCATOR_ALLOCATOR_SHIM_H_ |
6 #define BASE_ALLOCATOR_ALLOCATOR_SHIM_H_ | 6 #define BASE_ALLOCATOR_ALLOCATOR_SHIM_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
| 11 #include "build/build_config.h" |
11 | 12 |
12 namespace base { | 13 namespace base { |
13 namespace allocator { | 14 namespace allocator { |
14 | 15 |
15 // Allocator Shim API. Allows to to: | 16 // Allocator Shim API. Allows to to: |
16 // - Configure the behavior of the allocator (what to do on OOM failures). | 17 // - Configure the behavior of the allocator (what to do on OOM failures). |
17 // - Install new hooks (AllocatorDispatch) in the allocator chain. | 18 // - Install new hooks (AllocatorDispatch) in the allocator chain. |
18 | 19 |
19 // When this shim layer is enabled, the route of an allocation is as-follows: | 20 // When this shim layer is enabled, the route of an allocation is as-follows: |
20 // | 21 // |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 using ReallocFn = void*(const AllocatorDispatch* self, | 56 using ReallocFn = void*(const AllocatorDispatch* self, |
56 void* address, | 57 void* address, |
57 size_t size); | 58 size_t size); |
58 using FreeFn = void(const AllocatorDispatch* self, void* address); | 59 using FreeFn = void(const AllocatorDispatch* self, void* address); |
59 // Returns the best available estimate for the actual amount of memory | 60 // Returns the best available estimate for the actual amount of memory |
60 // consumed by the allocation |address|. If possible, this should include | 61 // consumed by the allocation |address|. If possible, this should include |
61 // heap overhead or at least a decent estimate of the full cost of the | 62 // heap overhead or at least a decent estimate of the full cost of the |
62 // allocation. If no good estimate is possible, returns zero. | 63 // allocation. If no good estimate is possible, returns zero. |
63 using GetSizeEstimateFn = size_t(const AllocatorDispatch* self, | 64 using GetSizeEstimateFn = size_t(const AllocatorDispatch* self, |
64 void* address); | 65 void* address); |
| 66 using BatchMallocFn = unsigned(const AllocatorDispatch* self, |
| 67 size_t size, |
| 68 void** results, |
| 69 unsigned num_requested); |
| 70 using BatchFreeFn = void(const AllocatorDispatch* self, |
| 71 void** to_be_freed, |
| 72 unsigned num_to_be_freed); |
| 73 using FreeDefiniteSizeFn = void(const AllocatorDispatch* self, |
| 74 void* ptr, |
| 75 size_t size); |
65 | 76 |
66 AllocFn* const alloc_function; | 77 AllocFn* const alloc_function; |
67 AllocZeroInitializedFn* const alloc_zero_initialized_function; | 78 AllocZeroInitializedFn* const alloc_zero_initialized_function; |
68 AllocAlignedFn* const alloc_aligned_function; | 79 AllocAlignedFn* const alloc_aligned_function; |
69 ReallocFn* const realloc_function; | 80 ReallocFn* const realloc_function; |
70 FreeFn* const free_function; | 81 FreeFn* const free_function; |
71 GetSizeEstimateFn* const get_size_estimate_function; | 82 GetSizeEstimateFn* const get_size_estimate_function; |
| 83 BatchMallocFn* const batch_malloc_function; |
| 84 BatchFreeFn* const batch_free_function; |
| 85 FreeDefiniteSizeFn* const free_definite_size_function; |
72 | 86 |
73 const AllocatorDispatch* next; | 87 const AllocatorDispatch* next; |
74 | 88 |
75 // |default_dispatch| is statically defined by one (and only one) of the | 89 // |default_dispatch| is statically defined by one (and only one) of the |
76 // allocator_shim_default_dispatch_to_*.cc files, depending on the build | 90 // allocator_shim_default_dispatch_to_*.cc files, depending on the build |
77 // configuration. | 91 // configuration. |
78 static const AllocatorDispatch default_dispatch; | 92 static const AllocatorDispatch default_dispatch; |
79 }; | 93 }; |
80 | 94 |
81 // When true makes malloc behave like new, w.r.t calling the new_handler if | 95 // When true makes malloc behave like new, w.r.t calling the new_handler if |
82 // the allocation fails (see set_new_mode() in Windows). | 96 // the allocation fails (see set_new_mode() in Windows). |
83 BASE_EXPORT void SetCallNewHandlerOnMallocFailure(bool value); | 97 BASE_EXPORT void SetCallNewHandlerOnMallocFailure(bool value); |
84 | 98 |
85 // Allocates |size| bytes or returns nullptr. It does NOT call the new_handler, | 99 // Allocates |size| bytes or returns nullptr. It does NOT call the new_handler, |
86 // regardless of SetCallNewHandlerOnMallocFailure(). | 100 // regardless of SetCallNewHandlerOnMallocFailure(). |
87 BASE_EXPORT void* UncheckedAlloc(size_t size); | 101 BASE_EXPORT void* UncheckedAlloc(size_t size); |
88 | 102 |
89 // Inserts |dispatch| in front of the allocator chain. This method is | 103 // Inserts |dispatch| in front of the allocator chain. This method is |
90 // thread-safe w.r.t concurrent invocations of InsertAllocatorDispatch(). | 104 // thread-safe w.r.t concurrent invocations of InsertAllocatorDispatch(). |
91 // The callers have responsibility for inserting a single dispatch no more | 105 // The callers have responsibility for inserting a single dispatch no more |
92 // than once. | 106 // than once. |
93 BASE_EXPORT void InsertAllocatorDispatch(AllocatorDispatch* dispatch); | 107 BASE_EXPORT void InsertAllocatorDispatch(AllocatorDispatch* dispatch); |
94 | 108 |
95 // Test-only. Rationale: (1) lack of use cases; (2) dealing safely with a | 109 // Test-only. Rationale: (1) lack of use cases; (2) dealing safely with a |
96 // removal of arbitrary elements from a singly linked list would require a lock | 110 // removal of arbitrary elements from a singly linked list would require a lock |
97 // in malloc(), which we really don't want. | 111 // in malloc(), which we really don't want. |
98 BASE_EXPORT void RemoveAllocatorDispatchForTesting(AllocatorDispatch* dispatch); | 112 BASE_EXPORT void RemoveAllocatorDispatchForTesting(AllocatorDispatch* dispatch); |
99 | 113 |
| 114 #if defined(OS_MACOSX) |
| 115 // On macOS, the allocator shim needs to be turned on during runtime. |
| 116 BASE_EXPORT void InitializeAllocatorShim(); |
| 117 #endif // defined(OS_MACOSX) |
| 118 |
100 } // namespace allocator | 119 } // namespace allocator |
101 } // namespace base | 120 } // namespace base |
102 | 121 |
103 #endif // BASE_ALLOCATOR_ALLOCATOR_SHIM_H_ | 122 #endif // BASE_ALLOCATOR_ALLOCATOR_SHIM_H_ |
OLD | NEW |