OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 #include "base/allocator/allocator_shim_default_dispatch_to_mac_zoned_malloc.h" | 5 #include "base/allocator/allocator_shim_default_dispatch_to_mac_zoned_malloc.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/allocator/allocator_interception_mac.h" | 9 #include "base/allocator/allocator_interception_mac.h" |
10 #include "base/allocator/allocator_shim.h" | 10 #include "base/allocator/allocator_shim.h" |
11 #include "base/logging.h" | 11 #include "base/allocator/malloc_zone_functions_mac.h" |
12 | 12 |
13 namespace base { | 13 namespace base { |
14 namespace allocator { | 14 namespace allocator { |
15 | |
16 namespace { | 15 namespace { |
17 | 16 |
18 // This is the zone that the allocator shim will call to actually perform heap | |
19 // allocations. It should be populated with the original, unintercepted default | |
20 // malloc zone. | |
21 MallocZoneFunctions g_default_zone; | |
22 | |
23 void* MallocImpl(const AllocatorDispatch*, size_t size, void* context) { | 17 void* MallocImpl(const AllocatorDispatch*, size_t size, void* context) { |
24 return g_default_zone.malloc( | 18 MallocZoneFunctions& functions = GetFunctionsForZone(context); |
25 reinterpret_cast<struct _malloc_zone_t*>(context), size); | 19 return functions.malloc(reinterpret_cast<struct _malloc_zone_t*>(context), |
| 20 size); |
26 } | 21 } |
27 | 22 |
28 void* CallocImpl(const AllocatorDispatch*, | 23 void* CallocImpl(const AllocatorDispatch*, |
29 size_t n, | 24 size_t n, |
30 size_t size, | 25 size_t size, |
31 void* context) { | 26 void* context) { |
32 return g_default_zone.calloc( | 27 MallocZoneFunctions& functions = GetFunctionsForZone(context); |
33 reinterpret_cast<struct _malloc_zone_t*>(context), n, size); | 28 return functions.calloc(reinterpret_cast<struct _malloc_zone_t*>(context), n, |
| 29 size); |
34 } | 30 } |
35 | 31 |
36 void* MemalignImpl(const AllocatorDispatch*, | 32 void* MemalignImpl(const AllocatorDispatch*, |
37 size_t alignment, | 33 size_t alignment, |
38 size_t size, | 34 size_t size, |
39 void* context) { | 35 void* context) { |
40 return g_default_zone.memalign( | 36 MallocZoneFunctions& functions = GetFunctionsForZone(context); |
41 reinterpret_cast<struct _malloc_zone_t*>(context), alignment, size); | 37 return functions.memalign(reinterpret_cast<struct _malloc_zone_t*>(context), |
| 38 alignment, size); |
42 } | 39 } |
43 | 40 |
44 void* ReallocImpl(const AllocatorDispatch*, | 41 void* ReallocImpl(const AllocatorDispatch*, |
45 void* ptr, | 42 void* ptr, |
46 size_t size, | 43 size_t size, |
47 void* context) { | 44 void* context) { |
48 return g_default_zone.realloc( | 45 MallocZoneFunctions& functions = GetFunctionsForZone(context); |
49 reinterpret_cast<struct _malloc_zone_t*>(context), ptr, size); | 46 return functions.realloc(reinterpret_cast<struct _malloc_zone_t*>(context), |
| 47 ptr, size); |
50 } | 48 } |
51 | 49 |
52 void FreeImpl(const AllocatorDispatch*, void* ptr, void* context) { | 50 void FreeImpl(const AllocatorDispatch*, void* ptr, void* context) { |
53 g_default_zone.free(reinterpret_cast<struct _malloc_zone_t*>(context), ptr); | 51 MallocZoneFunctions& functions = GetFunctionsForZone(context); |
| 52 functions.free(reinterpret_cast<struct _malloc_zone_t*>(context), ptr); |
54 } | 53 } |
55 | 54 |
56 size_t GetSizeEstimateImpl(const AllocatorDispatch*, void* ptr, void* context) { | 55 size_t GetSizeEstimateImpl(const AllocatorDispatch*, void* ptr, void* context) { |
57 return g_default_zone.size(reinterpret_cast<struct _malloc_zone_t*>(context), | 56 MallocZoneFunctions& functions = GetFunctionsForZone(context); |
58 ptr); | 57 return functions.size(reinterpret_cast<struct _malloc_zone_t*>(context), ptr); |
59 } | 58 } |
60 | 59 |
61 unsigned BatchMallocImpl(const AllocatorDispatch* self, | 60 unsigned BatchMallocImpl(const AllocatorDispatch* self, |
62 size_t size, | 61 size_t size, |
63 void** results, | 62 void** results, |
64 unsigned num_requested, | 63 unsigned num_requested, |
65 void* context) { | 64 void* context) { |
66 return g_default_zone.batch_malloc( | 65 MallocZoneFunctions& functions = GetFunctionsForZone(context); |
| 66 return functions.batch_malloc( |
67 reinterpret_cast<struct _malloc_zone_t*>(context), size, results, | 67 reinterpret_cast<struct _malloc_zone_t*>(context), size, results, |
68 num_requested); | 68 num_requested); |
69 } | 69 } |
70 | 70 |
71 void BatchFreeImpl(const AllocatorDispatch* self, | 71 void BatchFreeImpl(const AllocatorDispatch* self, |
72 void** to_be_freed, | 72 void** to_be_freed, |
73 unsigned num_to_be_freed, | 73 unsigned num_to_be_freed, |
74 void* context) { | 74 void* context) { |
75 g_default_zone.batch_free(reinterpret_cast<struct _malloc_zone_t*>(context), | 75 MallocZoneFunctions& functions = GetFunctionsForZone(context); |
76 to_be_freed, num_to_be_freed); | 76 functions.batch_free(reinterpret_cast<struct _malloc_zone_t*>(context), |
| 77 to_be_freed, num_to_be_freed); |
77 } | 78 } |
78 | 79 |
79 void FreeDefiniteSizeImpl(const AllocatorDispatch* self, | 80 void FreeDefiniteSizeImpl(const AllocatorDispatch* self, |
80 void* ptr, | 81 void* ptr, |
81 size_t size, | 82 size_t size, |
82 void* context) { | 83 void* context) { |
83 g_default_zone.free_definite_size( | 84 MallocZoneFunctions& functions = GetFunctionsForZone(context); |
| 85 functions.free_definite_size( |
84 reinterpret_cast<struct _malloc_zone_t*>(context), ptr, size); | 86 reinterpret_cast<struct _malloc_zone_t*>(context), ptr, size); |
85 } | 87 } |
86 | 88 |
87 } // namespace | 89 } // namespace |
88 | 90 |
89 void InitializeDefaultDispatchToMacAllocator() { | 91 void InitializeDefaultDispatchToMacAllocator() { |
90 StoreFunctionsForDefaultZone(&g_default_zone); | 92 StoreFunctionsForAllZones(); |
91 } | 93 } |
92 | 94 |
93 const AllocatorDispatch AllocatorDispatch::default_dispatch = { | 95 const AllocatorDispatch AllocatorDispatch::default_dispatch = { |
94 &MallocImpl, /* alloc_function */ | 96 &MallocImpl, /* alloc_function */ |
95 &CallocImpl, /* alloc_zero_initialized_function */ | 97 &CallocImpl, /* alloc_zero_initialized_function */ |
96 &MemalignImpl, /* alloc_aligned_function */ | 98 &MemalignImpl, /* alloc_aligned_function */ |
97 &ReallocImpl, /* realloc_function */ | 99 &ReallocImpl, /* realloc_function */ |
98 &FreeImpl, /* free_function */ | 100 &FreeImpl, /* free_function */ |
99 &GetSizeEstimateImpl, /* get_size_estimate_function */ | 101 &GetSizeEstimateImpl, /* get_size_estimate_function */ |
100 &BatchMallocImpl, /* batch_malloc_function */ | 102 &BatchMallocImpl, /* batch_malloc_function */ |
101 &BatchFreeImpl, /* batch_free_function */ | 103 &BatchFreeImpl, /* batch_free_function */ |
102 &FreeDefiniteSizeImpl, /* free_definite_size_function */ | 104 &FreeDefiniteSizeImpl, /* free_definite_size_function */ |
103 nullptr, /* next */ | 105 nullptr, /* next */ |
104 }; | 106 }; |
105 | 107 |
106 } // namespace allocator | 108 } // namespace allocator |
107 } // namespace base | 109 } // namespace base |
OLD | NEW |