Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(183)

Side by Side Diff: base/allocator/allocator_shim_override_mac_symbols.h

Issue 2601573002: mac: Hook up allocator shim. (Closed)
Patch Set: Clean up. Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
Primiano Tucci (use gerrit) 2017/01/17 17:09:59 s/2016/2017/ happy new year :) also I think this f
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // This header defines symbols to override the same functions in the Visual C++
6 // CRT implementation.
7
8 #ifdef BASE_ALLOCATOR_ALLOCATOR_SHIM_OVERRIDE_MAC_SYMBOLS_H_
9 #error This header is meant to be included only once by allocator_shim.cc
10 #endif
11 #define BASE_ALLOCATOR_ALLOCATOR_SHIM_OVERRIDE_MAC_SYMBOLS_H_
12
13 #include <pthread.h>
14 #include <mach/mach.h>
15 #include <mach/mach_vm.h>
16 #include <malloc/malloc.h>
17
18 #include "base/logging.h"
19 #include "base/mac/mach_logging.h"
20 #include "base/process/memory.h"
21 #include "third_party/apple_apsl/malloc.h"
22
23 namespace {
24
25 using base::allocator::AllocatorDispatch;
26
27 ChromeMallocZone g_unshimmed_zone;
28
29 template <class F, class... Args>
30 auto CallUnshimmed(F ChromeMallocZone::*m, Args... args)
31 -> decltype(std::declval<F>()(nullptr, args...)) {
32 F f = g_unshimmed_zone.*m
33 ? g_unshimmed_zone.*m
34 : reinterpret_cast<ChromeMallocZone*>(malloc_default_zone())->*m;
35 return f(malloc_default_zone(), args...);
36 }
37
38 void* MallocImpl(const AllocatorDispatch*, size_t size) {
39 return CallUnshimmed(&ChromeMallocZone::malloc, size);
40 }
41
42 void* CallocImpl(const AllocatorDispatch*, size_t n, size_t size) {
43 return CallUnshimmed(&ChromeMallocZone::calloc, n, size);
44 }
45
46 void* MemalignImpl(const AllocatorDispatch*, size_t alignment, size_t size) {
47 ChromeMallocZone* defaul_zone =
48 reinterpret_cast<ChromeMallocZone*>(malloc_default_zone());
49 if (defaul_zone->version >= 5) {
50 return CallUnshimmed(&ChromeMallocZone::memalign, alignment, size);
51 } else if (alignment == GetCachedPageSize()) {
52 return CallUnshimmed(&ChromeMallocZone::valloc, size);
53 } else {
54 CHECK(false) << "memalign() is not implemented.";
55 return nullptr;
56 }
57 }
58
59 void* ReallocImpl(const AllocatorDispatch*, void* ptr, size_t size) {
60 return CallUnshimmed(&ChromeMallocZone::realloc, ptr, size);
61 }
62
63 void FreeImpl(const AllocatorDispatch*, void* ptr) {
64 CallUnshimmed(&ChromeMallocZone::free, ptr);
65 }
66
67 size_t GetSizeEstimateImpl(const AllocatorDispatch*, void* ptr) {
68 return CallUnshimmed(&ChromeMallocZone::size, ptr);
69 }
70
71 unsigned BatchMallocImpl(const AllocatorDispatch* self,
72 size_t size,
73 void** results,
74 unsigned num_requested) {
75 return CallUnshimmed(&ChromeMallocZone::batch_malloc, size, results,
76 num_requested);
77 }
78
79 void BatchFreeImpl(const AllocatorDispatch* self,
80 void** to_be_freed,
81 unsigned num_to_be_freed) {
82 CallUnshimmed(&ChromeMallocZone::batch_free, to_be_freed, num_to_be_freed);
83 }
84
85 void ShimZoneCallbacks(ChromeMallocZone* zone) {
Primiano Tucci (use gerrit) 2017/01/17 17:09:59 just for clarify I think should be called Initiali
86 zone->size = [](malloc_zone_t* zone, const void* ptr) -> size_t {
87 return ShimGetSizeEstimate(ptr);
88 };
89 zone->malloc = [](malloc_zone_t* zone, size_t size) -> void* {
90 return ShimMalloc(size);
91 };
92 zone->calloc = [](malloc_zone_t* zone, size_t n, size_t size) -> void* {
93 return ShimCalloc(n, size);
94 };
95 zone->valloc = [](malloc_zone_t* zone, size_t size) -> void* {
96 return ShimValloc(size);
97 };
98 zone->free = [](malloc_zone_t* zone, void* ptr) { ShimFree(ptr); };
99 zone->realloc = [](malloc_zone_t* zone, void* ptr, size_t size) -> void* {
100 return ShimRealloc(ptr, size);
101 };
102 zone->batch_malloc = [](struct _malloc_zone_t* zone, size_t size,
103 void** results, unsigned num_requested) -> unsigned {
104 return ShimBatchMalloc(size, results, num_requested);
105 };
106 zone->batch_free = [](struct _malloc_zone_t* zone, void** to_be_freed,
107 unsigned num_to_be_freed) -> void {
108 ShimBatchFree(to_be_freed, num_to_be_freed);
109 };
110
111 if (zone->version >= 5) {
112 zone->memalign = [](malloc_zone_t* zone, size_t alignment,
113 size_t size) -> void* {
114 return ShimMemalign(alignment, size);
115 };
116 }
117 if (zone->version >= 6) {
118 zone->free_definite_size = [](malloc_zone_t* zone, void* ptr, size_t size) {
119 ShimFree(ptr);
120 };
121 }
122 }
123
124 void InitializeAllocatorShim() {
125 ChromeMallocZone* default_zone =
126 reinterpret_cast<ChromeMallocZone*>(malloc_default_zone());
127
128 mach_vm_address_t default_reprotection_start = 0;
129 mach_vm_size_t default_reprotection_length = 0;
130 vm_prot_t default_reprotection_value = VM_PROT_NONE;
131 base::DeprotectMallocZone(default_zone, &default_reprotection_start,
132 &default_reprotection_length,
133 &default_reprotection_value);
134
135 CHECK(default_zone->size && default_zone->malloc && default_zone->calloc &&
136 default_zone->valloc && default_zone->free && default_zone->realloc)
137 << "Failed to get system allocation functions.";
138 g_unshimmed_zone.size = default_zone->size;
139 g_unshimmed_zone.malloc = default_zone->malloc;
140 g_unshimmed_zone.calloc = default_zone->calloc;
141 g_unshimmed_zone.valloc = default_zone->valloc;
142 g_unshimmed_zone.free = default_zone->free;
143 g_unshimmed_zone.realloc = default_zone->realloc;
144 g_unshimmed_zone.batch_malloc = default_zone->batch_malloc;
145 g_unshimmed_zone.batch_free = default_zone->batch_free;
146
147 g_unshimmed_zone.version = default_zone->version;
148 if (default_zone->version >= 5) {
149 g_unshimmed_zone.memalign = default_zone->memalign;
150 }
151 if (default_zone->version >= 6) {
152 g_unshimmed_zone.free_definite_size = default_zone->free_definite_size;
153 }
154
155 ShimZoneCallbacks(default_zone);
156
157 if (default_reprotection_start) {
158 kern_return_t result = mach_vm_protect(
159 mach_task_self(), default_reprotection_start,
160 default_reprotection_length, false, default_reprotection_value);
161 MACH_CHECK(result == KERN_SUCCESS, result) << "mach_vm_protect";
162 }
163 }
164
165 __attribute__((constructor)) void InitializeAllocatorShimAtDlOpen() {
166 InitializeAllocatorShim();
Primiano Tucci (use gerrit) 2017/01/17 17:09:59 Hmm this: - will cause the static initializers che
167 }
168
169 } // namespace
170
171 const AllocatorDispatch AllocatorDispatch::default_dispatch = {
172 &MallocImpl, /* alloc_function */
173 &CallocImpl, /* alloc_zero_initialized_function */
174 &MemalignImpl, /* alloc_aligned_function */
175 &ReallocImpl, /* realloc_function */
176 &FreeImpl, /* free_function */
177 &GetSizeEstimateImpl, /* get_size_estimate_function */
178 &BatchMallocImpl, /* batch_malloc_function */
179 &BatchFreeImpl, /* batch_free_function */
180 nullptr, /* next */
181 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698