• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // All the functions must be thred safe. The shim does not enforce any
44 // serialization. This is to route to thread-aware allocators (e.g, tcmalloc)
45 // wihout introducing unnecessary perf hits.
46 
47 struct AllocatorDispatch {
48   using AllocFn = void*(const AllocatorDispatch* self, size_t size);
49   using AllocZeroInitializedFn = void*(const AllocatorDispatch* self,
50                                        size_t n,
51                                        size_t size);
52   using AllocAlignedFn = void*(const AllocatorDispatch* self,
53                                size_t alignment,
54                                size_t size);
55   using ReallocFn = void*(const AllocatorDispatch* self,
56                           void* address,
57                           size_t size);
58   using FreeFn = void(const AllocatorDispatch* self, void* address);
59 
60   AllocFn* const alloc_function;
61   AllocZeroInitializedFn* const alloc_zero_initialized_function;
62   AllocAlignedFn* const alloc_aligned_function;
63   ReallocFn* const realloc_function;
64   FreeFn* const free_function;
65 
66   const AllocatorDispatch* next;
67 
68   // |default_dispatch| is statically defined by one (and only one) of the
69   // allocator_shim_default_dispatch_to_*.cc files, depending on the build
70   // configuration.
71   static const AllocatorDispatch default_dispatch;
72 };
73 
74 // When true makes malloc behave like new, w.r.t calling the new_handler if
75 // the allocation fails (see set_new_mode() in Windows).
76 BASE_EXPORT void SetCallNewHandlerOnMallocFailure(bool value);
77 
78 // Allocates |size| bytes or returns nullptr. It does NOT call the new_handler,
79 // regardless of SetCallNewHandlerOnMallocFailure().
80 BASE_EXPORT void* UncheckedAlloc(size_t size);
81 
82 // Inserts |dispatch| in front of the allocator chain. This method is NOT
83 // thread-safe w.r.t concurrent invocations of InsertAllocatorDispatch().
84 // The callers have the responsibility of linearizing the changes to the chain
85 // (or more likely call these always on the same thread).
86 BASE_EXPORT void InsertAllocatorDispatch(AllocatorDispatch* dispatch);
87 
88 // Test-only. Rationale: (1) lack of use cases; (2) dealing safely with a
89 // removal of arbitrary elements from a singly linked list would require a lock
90 // in malloc(), which we really don't want.
91 BASE_EXPORT void RemoveAllocatorDispatchForTesting(AllocatorDispatch* dispatch);
92 
93 }  // namespace allocator
94 }  // namespace base
95 
96 #endif  // BASE_ALLOCATOR_ALLOCATOR_SHIM_H_
97