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/allocator/malloc_zone_functions_mac.h" | |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 | 13 |
13 namespace base { | 14 namespace base { |
14 namespace allocator { | 15 namespace allocator { |
15 | |
16 namespace { | 16 namespace { |
17 | 17 |
18 // This is the zone that the allocator shim will call to actually perform heap | 18 MallocZoneFunctions& GetFunctionsForZone(void* zone) { |
Primiano Tucci (use gerrit)
2017/02/23 00:40:17
IMHO it makes more sense for this to be an inline
erikchen
2017/02/23 01:43:14
inline function in malloc_zone_functions_mac.h doe
Primiano Tucci (use gerrit)
2017/02/23 11:19:41
sorry, I really meant "allow us to not have to acc
erikchen
2017/02/23 20:06:44
Done.
| |
19 // allocations. It should be populated with the original, unintercepted default | 19 for (unsigned int i = 0; i < arraysize(g_malloc_zones); ++i) { |
20 // malloc zone. | 20 if (g_malloc_zones[i].context == zone) |
21 MallocZoneFunctions g_default_zone; | 21 return g_malloc_zones[i]; |
22 } | |
23 IMMEDIATE_CRASH(); | |
Primiano Tucci (use gerrit)
2017/02/23 00:40:18
out of curiosity, did you use this because the com
erikchen
2017/02/23 01:43:14
yes
Primiano Tucci (use gerrit)
2017/02/23 11:19:41
got it, sg
| |
24 } | |
22 | 25 |
23 void* MallocImpl(const AllocatorDispatch*, size_t size, void* context) { | 26 void* MallocImpl(const AllocatorDispatch*, size_t size, void* context) { |
24 return g_default_zone.malloc( | 27 MallocZoneFunctions& functions = GetFunctionsForZone(context); |
25 reinterpret_cast<struct _malloc_zone_t*>(context), size); | 28 return functions.malloc(reinterpret_cast<struct _malloc_zone_t*>(context), |
29 size); | |
26 } | 30 } |
27 | 31 |
28 void* CallocImpl(const AllocatorDispatch*, | 32 void* CallocImpl(const AllocatorDispatch*, |
29 size_t n, | 33 size_t n, |
30 size_t size, | 34 size_t size, |
31 void* context) { | 35 void* context) { |
32 return g_default_zone.calloc( | 36 MallocZoneFunctions& functions = GetFunctionsForZone(context); |
33 reinterpret_cast<struct _malloc_zone_t*>(context), n, size); | 37 return functions.calloc(reinterpret_cast<struct _malloc_zone_t*>(context), n, |
38 size); | |
34 } | 39 } |
35 | 40 |
36 void* MemalignImpl(const AllocatorDispatch*, | 41 void* MemalignImpl(const AllocatorDispatch*, |
37 size_t alignment, | 42 size_t alignment, |
38 size_t size, | 43 size_t size, |
39 void* context) { | 44 void* context) { |
40 return g_default_zone.memalign( | 45 MallocZoneFunctions& functions = GetFunctionsForZone(context); |
41 reinterpret_cast<struct _malloc_zone_t*>(context), alignment, size); | 46 return functions.memalign(reinterpret_cast<struct _malloc_zone_t*>(context), |
47 alignment, size); | |
42 } | 48 } |
43 | 49 |
44 void* ReallocImpl(const AllocatorDispatch*, | 50 void* ReallocImpl(const AllocatorDispatch*, |
45 void* ptr, | 51 void* ptr, |
46 size_t size, | 52 size_t size, |
47 void* context) { | 53 void* context) { |
48 return g_default_zone.realloc( | 54 MallocZoneFunctions& functions = GetFunctionsForZone(context); |
49 reinterpret_cast<struct _malloc_zone_t*>(context), ptr, size); | 55 return functions.realloc(reinterpret_cast<struct _malloc_zone_t*>(context), |
56 ptr, size); | |
50 } | 57 } |
51 | 58 |
52 void FreeImpl(const AllocatorDispatch*, void* ptr, void* context) { | 59 void FreeImpl(const AllocatorDispatch*, void* ptr, void* context) { |
53 g_default_zone.free(reinterpret_cast<struct _malloc_zone_t*>(context), ptr); | 60 MallocZoneFunctions& functions = GetFunctionsForZone(context); |
61 functions.free(reinterpret_cast<struct _malloc_zone_t*>(context), ptr); | |
54 } | 62 } |
55 | 63 |
56 size_t GetSizeEstimateImpl(const AllocatorDispatch*, void* ptr, void* context) { | 64 size_t GetSizeEstimateImpl(const AllocatorDispatch*, void* ptr, void* context) { |
57 return g_default_zone.size(reinterpret_cast<struct _malloc_zone_t*>(context), | 65 MallocZoneFunctions& functions = GetFunctionsForZone(context); |
58 ptr); | 66 return functions.size(reinterpret_cast<struct _malloc_zone_t*>(context), ptr); |
59 } | 67 } |
60 | 68 |
61 unsigned BatchMallocImpl(const AllocatorDispatch* self, | 69 unsigned BatchMallocImpl(const AllocatorDispatch* self, |
62 size_t size, | 70 size_t size, |
63 void** results, | 71 void** results, |
64 unsigned num_requested, | 72 unsigned num_requested, |
65 void* context) { | 73 void* context) { |
66 return g_default_zone.batch_malloc( | 74 MallocZoneFunctions& functions = GetFunctionsForZone(context); |
75 return functions.batch_malloc( | |
67 reinterpret_cast<struct _malloc_zone_t*>(context), size, results, | 76 reinterpret_cast<struct _malloc_zone_t*>(context), size, results, |
68 num_requested); | 77 num_requested); |
69 } | 78 } |
70 | 79 |
71 void BatchFreeImpl(const AllocatorDispatch* self, | 80 void BatchFreeImpl(const AllocatorDispatch* self, |
72 void** to_be_freed, | 81 void** to_be_freed, |
73 unsigned num_to_be_freed, | 82 unsigned num_to_be_freed, |
74 void* context) { | 83 void* context) { |
75 g_default_zone.batch_free(reinterpret_cast<struct _malloc_zone_t*>(context), | 84 MallocZoneFunctions& functions = GetFunctionsForZone(context); |
76 to_be_freed, num_to_be_freed); | 85 functions.batch_free(reinterpret_cast<struct _malloc_zone_t*>(context), |
86 to_be_freed, num_to_be_freed); | |
77 } | 87 } |
78 | 88 |
79 void FreeDefiniteSizeImpl(const AllocatorDispatch* self, | 89 void FreeDefiniteSizeImpl(const AllocatorDispatch* self, |
80 void* ptr, | 90 void* ptr, |
81 size_t size, | 91 size_t size, |
82 void* context) { | 92 void* context) { |
83 g_default_zone.free_definite_size( | 93 MallocZoneFunctions& functions = GetFunctionsForZone(context); |
94 functions.free_definite_size( | |
84 reinterpret_cast<struct _malloc_zone_t*>(context), ptr, size); | 95 reinterpret_cast<struct _malloc_zone_t*>(context), ptr, size); |
85 } | 96 } |
86 | 97 |
87 } // namespace | 98 } // namespace |
88 | 99 |
89 void InitializeDefaultDispatchToMacAllocator() { | 100 void InitializeDefaultDispatchToMacAllocator() { |
90 StoreFunctionsForDefaultZone(&g_default_zone); | 101 StoreFunctionsForAllZones(); |
91 } | 102 } |
92 | 103 |
93 const AllocatorDispatch AllocatorDispatch::default_dispatch = { | 104 const AllocatorDispatch AllocatorDispatch::default_dispatch = { |
94 &MallocImpl, /* alloc_function */ | 105 &MallocImpl, /* alloc_function */ |
95 &CallocImpl, /* alloc_zero_initialized_function */ | 106 &CallocImpl, /* alloc_zero_initialized_function */ |
96 &MemalignImpl, /* alloc_aligned_function */ | 107 &MemalignImpl, /* alloc_aligned_function */ |
97 &ReallocImpl, /* realloc_function */ | 108 &ReallocImpl, /* realloc_function */ |
98 &FreeImpl, /* free_function */ | 109 &FreeImpl, /* free_function */ |
99 &GetSizeEstimateImpl, /* get_size_estimate_function */ | 110 &GetSizeEstimateImpl, /* get_size_estimate_function */ |
100 &BatchMallocImpl, /* batch_malloc_function */ | 111 &BatchMallocImpl, /* batch_malloc_function */ |
101 &BatchFreeImpl, /* batch_free_function */ | 112 &BatchFreeImpl, /* batch_free_function */ |
102 &FreeDefiniteSizeImpl, /* free_definite_size_function */ | 113 &FreeDefiniteSizeImpl, /* free_definite_size_function */ |
103 nullptr, /* next */ | 114 nullptr, /* next */ |
104 }; | 115 }; |
105 | 116 |
106 } // namespace allocator | 117 } // namespace allocator |
107 } // namespace base | 118 } // namespace base |
OLD | NEW |