Chromium Code Reviews| 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); | |
| 65 | 73 |
| 66 AllocFn* const alloc_function; | 74 AllocFn* const alloc_function; |
| 67 AllocZeroInitializedFn* const alloc_zero_initialized_function; | 75 AllocZeroInitializedFn* const alloc_zero_initialized_function; |
| 68 AllocAlignedFn* const alloc_aligned_function; | 76 AllocAlignedFn* const alloc_aligned_function; |
| 69 ReallocFn* const realloc_function; | 77 ReallocFn* const realloc_function; |
| 70 FreeFn* const free_function; | 78 FreeFn* const free_function; |
| 71 GetSizeEstimateFn* const get_size_estimate_function; | 79 GetSizeEstimateFn* const get_size_estimate_function; |
| 80 BatchMallocFn* const batch_malloc_function; | |
| 81 BatchFreeFn* const batch_free_function; | |
| 72 | 82 |
| 73 const AllocatorDispatch* next; | 83 const AllocatorDispatch* next; |
| 74 | 84 |
| 75 // |default_dispatch| is statically defined by one (and only one) of the | 85 // |default_dispatch| is statically defined by one (and only one) of the |
| 76 // allocator_shim_default_dispatch_to_*.cc files, depending on the build | 86 // allocator_shim_default_dispatch_to_*.cc files, depending on the build |
| 77 // configuration. | 87 // configuration. |
| 78 static const AllocatorDispatch default_dispatch; | 88 static const AllocatorDispatch default_dispatch; |
| 79 }; | 89 }; |
| 80 | 90 |
| 81 // When true makes malloc behave like new, w.r.t calling the new_handler if | 91 // 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). | 92 // the allocation fails (see set_new_mode() in Windows). |
| 83 BASE_EXPORT void SetCallNewHandlerOnMallocFailure(bool value); | 93 BASE_EXPORT void SetCallNewHandlerOnMallocFailure(bool value); |
| 84 | 94 |
| 85 // Allocates |size| bytes or returns nullptr. It does NOT call the new_handler, | 95 // Allocates |size| bytes or returns nullptr. It does NOT call the new_handler, |
| 86 // regardless of SetCallNewHandlerOnMallocFailure(). | 96 // regardless of SetCallNewHandlerOnMallocFailure(). |
| 87 BASE_EXPORT void* UncheckedAlloc(size_t size); | 97 BASE_EXPORT void* UncheckedAlloc(size_t size); |
| 88 | 98 |
| 89 // Inserts |dispatch| in front of the allocator chain. This method is | 99 // Inserts |dispatch| in front of the allocator chain. This method is |
| 90 // thread-safe w.r.t concurrent invocations of InsertAllocatorDispatch(). | 100 // thread-safe w.r.t concurrent invocations of InsertAllocatorDispatch(). |
| 91 // The callers have responsibility for inserting a single dispatch no more | 101 // The callers have responsibility for inserting a single dispatch no more |
| 92 // than once. | 102 // than once. |
| 93 BASE_EXPORT void InsertAllocatorDispatch(AllocatorDispatch* dispatch); | 103 BASE_EXPORT void InsertAllocatorDispatch(AllocatorDispatch* dispatch); |
| 94 | 104 |
| 95 // Test-only. Rationale: (1) lack of use cases; (2) dealing safely with a | 105 // 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 | 106 // removal of arbitrary elements from a singly linked list would require a lock |
| 97 // in malloc(), which we really don't want. | 107 // in malloc(), which we really don't want. |
| 98 BASE_EXPORT void RemoveAllocatorDispatchForTesting(AllocatorDispatch* dispatch); | 108 BASE_EXPORT void RemoveAllocatorDispatchForTesting(AllocatorDispatch* dispatch); |
| 99 | 109 |
| 110 #if defined(OS_MACOSX) | |
|
Primiano Tucci (use gerrit)
2017/01/28 05:10:02
Was this the argument of the discussion with dskib
erikchen
2017/01/31 02:21:22
No, the discussion was about whether the members b
| |
| 111 // On macOS, the allocator shim needs to be turned on during runtime. | |
| 112 BASE_EXPORT void InitializeAllocatorShim(); | |
| 113 #endif // defined(OS_MACOSX) | |
| 114 | |
| 100 } // namespace allocator | 115 } // namespace allocator |
| 101 } // namespace base | 116 } // namespace base |
| 102 | 117 |
| 103 #endif // BASE_ALLOCATOR_ALLOCATOR_SHIM_H_ | 118 #endif // BASE_ALLOCATOR_ALLOCATOR_SHIM_H_ |
| OLD | NEW |