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
3 // found in the LICENSE file.
4
5 #include <utility>
6
7 #include "base/allocator/allocator_interception_mac.h"
8 #include "base/allocator/allocator_shim.h"
9 #include "base/allocator/malloc_zone_functions_mac.h"
10
11 namespace base {
12 namespace allocator {
13 namespace {
14
MallocImpl(const AllocatorDispatch *,size_t size,void * context)15 void* MallocImpl(const AllocatorDispatch*, size_t size, void* context) {
16 MallocZoneFunctions& functions = GetFunctionsForZone(context);
17 return functions.malloc(reinterpret_cast<struct _malloc_zone_t*>(context),
18 size);
19 }
20
CallocImpl(const AllocatorDispatch *,size_t n,size_t size,void * context)21 void* CallocImpl(const AllocatorDispatch*,
22 size_t n,
23 size_t size,
24 void* context) {
25 MallocZoneFunctions& functions = GetFunctionsForZone(context);
26 return functions.calloc(reinterpret_cast<struct _malloc_zone_t*>(context), n,
27 size);
28 }
29
MemalignImpl(const AllocatorDispatch *,size_t alignment,size_t size,void * context)30 void* MemalignImpl(const AllocatorDispatch*,
31 size_t alignment,
32 size_t size,
33 void* context) {
34 MallocZoneFunctions& functions = GetFunctionsForZone(context);
35 return functions.memalign(reinterpret_cast<struct _malloc_zone_t*>(context),
36 alignment, size);
37 }
38
ReallocImpl(const AllocatorDispatch *,void * ptr,size_t size,void * context)39 void* ReallocImpl(const AllocatorDispatch*,
40 void* ptr,
41 size_t size,
42 void* context) {
43 MallocZoneFunctions& functions = GetFunctionsForZone(context);
44 return functions.realloc(reinterpret_cast<struct _malloc_zone_t*>(context),
45 ptr, size);
46 }
47
FreeImpl(const AllocatorDispatch *,void * ptr,void * context)48 void FreeImpl(const AllocatorDispatch*, void* ptr, void* context) {
49 MallocZoneFunctions& functions = GetFunctionsForZone(context);
50 functions.free(reinterpret_cast<struct _malloc_zone_t*>(context), ptr);
51 }
52
GetSizeEstimateImpl(const AllocatorDispatch *,void * ptr,void * context)53 size_t GetSizeEstimateImpl(const AllocatorDispatch*, void* ptr, void* context) {
54 MallocZoneFunctions& functions = GetFunctionsForZone(context);
55 return functions.size(reinterpret_cast<struct _malloc_zone_t*>(context), ptr);
56 }
57
BatchMallocImpl(const AllocatorDispatch * self,size_t size,void ** results,unsigned num_requested,void * context)58 unsigned BatchMallocImpl(const AllocatorDispatch* self,
59 size_t size,
60 void** results,
61 unsigned num_requested,
62 void* context) {
63 MallocZoneFunctions& functions = GetFunctionsForZone(context);
64 return functions.batch_malloc(
65 reinterpret_cast<struct _malloc_zone_t*>(context), size, results,
66 num_requested);
67 }
68
BatchFreeImpl(const AllocatorDispatch * self,void ** to_be_freed,unsigned num_to_be_freed,void * context)69 void BatchFreeImpl(const AllocatorDispatch* self,
70 void** to_be_freed,
71 unsigned num_to_be_freed,
72 void* context) {
73 MallocZoneFunctions& functions = GetFunctionsForZone(context);
74 functions.batch_free(reinterpret_cast<struct _malloc_zone_t*>(context),
75 to_be_freed, num_to_be_freed);
76 }
77
FreeDefiniteSizeImpl(const AllocatorDispatch * self,void * ptr,size_t size,void * context)78 void FreeDefiniteSizeImpl(const AllocatorDispatch* self,
79 void* ptr,
80 size_t size,
81 void* context) {
82 MallocZoneFunctions& functions = GetFunctionsForZone(context);
83 functions.free_definite_size(
84 reinterpret_cast<struct _malloc_zone_t*>(context), ptr, size);
85 }
86
87 } // namespace
88
89 const AllocatorDispatch AllocatorDispatch::default_dispatch = {
90 &MallocImpl, /* alloc_function */
91 &MallocImpl, /* alloc_unchecked_function */
92 &CallocImpl, /* alloc_zero_initialized_function */
93 &MemalignImpl, /* alloc_aligned_function */
94 &ReallocImpl, /* realloc_function */
95 &FreeImpl, /* free_function */
96 &GetSizeEstimateImpl, /* get_size_estimate_function */
97 &BatchMallocImpl, /* batch_malloc_function */
98 &BatchFreeImpl, /* batch_free_function */
99 &FreeDefiniteSizeImpl, /* free_definite_size_function */
100 nullptr, /* aligned_malloc_function */
101 nullptr, /* aligned_realloc_function */
102 nullptr, /* aligned_free_function */
103 nullptr, /* next */
104 };
105
106 } // namespace allocator
107 } // namespace base
108