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