Chromium Code Reviews| 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 further AllocatorDispatch stages | |
| 41 // to the front of the chain, for debugging / profiling purposes. | |
| 42 | |
| 43 struct AllocatorDispatch { | |
| 44 using AllocFn = void*(size_t size, const AllocatorDispatch* self); | |
|
Nico
2016/03/08 03:08:42
why make self the last arg?
mention that these fu
Primiano Tucci (use gerrit)
2016/03/08 20:54:05
Is the question:
Q1) why having |self| at all?
Q2)
Nico
2016/03/09 05:34:25
Q3 – seems a bit more common (languages with expli
Primiano Tucci (use gerrit)
2016/03/09 10:51:59
Alright, point taken. Moved as 1st argument.
| |
| 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 InsertAllocatorDispatch(). | |
| 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); | |
|
Nico
2016/03/08 03:08:42
removing a dispatch object is not possible?
Primiano Tucci (use gerrit)
2016/03/08 20:54:05
I removed it after a discussion that with chrisha:
| |
| 83 | |
| 84 } // namespace allocator | |
| 85 } // namespace base | |
| 86 | |
| 87 #endif // BASE_ALLOCATOR_ALLOCATOR_SHIM_H_ | |
| OLD | NEW |