OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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.h" | 5 #include "base/allocator/allocator_shim.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 | 8 |
9 #include <new> | 9 #include <new> |
10 | 10 |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 size = (size + GetCachedPageSize() - 1) & ~(GetCachedPageSize() - 1); | 234 size = (size + GetCachedPageSize() - 1) & ~(GetCachedPageSize() - 1); |
235 } | 235 } |
236 return ShimMemalign(GetCachedPageSize(), size); | 236 return ShimMemalign(GetCachedPageSize(), size); |
237 } | 237 } |
238 | 238 |
239 void ShimFree(void* address) { | 239 void ShimFree(void* address) { |
240 const allocator::AllocatorDispatch* const chain_head = GetChainHead(); | 240 const allocator::AllocatorDispatch* const chain_head = GetChainHead(); |
241 return chain_head->free_function(chain_head, address); | 241 return chain_head->free_function(chain_head, address); |
242 } | 242 } |
243 | 243 |
| 244 size_t ShimGetSizeEstimate(const void* address) { |
| 245 const allocator::AllocatorDispatch* const chain_head = GetChainHead(); |
| 246 return chain_head->get_size_estimate_function(chain_head, |
| 247 const_cast<void*>(address)); |
| 248 } |
| 249 |
| 250 unsigned ShimBatchMalloc(size_t size, void** results, unsigned num_requested) { |
| 251 const allocator::AllocatorDispatch* const chain_head = GetChainHead(); |
| 252 return chain_head->batch_malloc_function(chain_head, size, results, |
| 253 num_requested); |
| 254 } |
| 255 |
| 256 void ShimBatchFree(void** to_be_freed, unsigned num_to_be_freed) { |
| 257 const allocator::AllocatorDispatch* const chain_head = GetChainHead(); |
| 258 return chain_head->batch_free_function(chain_head, to_be_freed, |
| 259 num_to_be_freed); |
| 260 } |
| 261 |
| 262 void ShimFreeDefiniteSize(void* ptr, size_t size) { |
| 263 const allocator::AllocatorDispatch* const chain_head = GetChainHead(); |
| 264 return chain_head->free_definite_size_function(chain_head, ptr, size); |
| 265 } |
| 266 |
244 } // extern "C" | 267 } // extern "C" |
245 | 268 |
246 #if !defined(OS_WIN) | 269 #if !defined(OS_WIN) && !defined(OS_MACOSX) |
247 // Cpp symbols (new / delete) should always be routed through the shim layer | 270 // Cpp symbols (new / delete) should always be routed through the shim layer |
248 // except on Windows where the malloc intercept is deep enough that it also | 271 // except on Windows and macOS where the malloc intercept is deep enough that it |
249 // catches the cpp calls. | 272 // also catches the cpp calls. |
250 #include "base/allocator/allocator_shim_override_cpp_symbols.h" | 273 #include "base/allocator/allocator_shim_override_cpp_symbols.h" |
251 #endif | 274 #endif |
252 | 275 |
253 #if defined(OS_ANDROID) | 276 #if defined(OS_ANDROID) |
254 // Android does not support symbol interposition. The way malloc symbols are | 277 // Android does not support symbol interposition. The way malloc symbols are |
255 // intercepted on Android is by using link-time -wrap flags. | 278 // intercepted on Android is by using link-time -wrap flags. |
256 #include "base/allocator/allocator_shim_override_linker_wrapped_symbols.h" | 279 #include "base/allocator/allocator_shim_override_linker_wrapped_symbols.h" |
257 #elif defined(OS_WIN) | 280 #elif defined(OS_WIN) |
258 // On Windows we use plain link-time overriding of the CRT symbols. | 281 // On Windows we use plain link-time overriding of the CRT symbols. |
259 #include "base/allocator/allocator_shim_override_ucrt_symbols_win.h" | 282 #include "base/allocator/allocator_shim_override_ucrt_symbols_win.h" |
| 283 #elif defined(OS_MACOSX) |
| 284 #include "base/allocator/allocator_shim_default_dispatch_to_mac_zoned_malloc.h" |
| 285 #include "base/allocator/allocator_shim_override_mac_symbols.h" |
260 #else | 286 #else |
261 #include "base/allocator/allocator_shim_override_libc_symbols.h" | 287 #include "base/allocator/allocator_shim_override_libc_symbols.h" |
262 #endif | 288 #endif |
263 | 289 |
264 // In the case of tcmalloc we also want to plumb into the glibc hooks | 290 // In the case of tcmalloc we also want to plumb into the glibc hooks |
265 // to avoid that allocations made in glibc itself (e.g., strdup()) get | 291 // to avoid that allocations made in glibc itself (e.g., strdup()) get |
266 // accidentally performed on the glibc heap instead of the tcmalloc one. | 292 // accidentally performed on the glibc heap instead of the tcmalloc one. |
267 #if defined(USE_TCMALLOC) | 293 #if defined(USE_TCMALLOC) |
268 #include "base/allocator/allocator_shim_override_glibc_weak_symbols.h" | 294 #include "base/allocator/allocator_shim_override_glibc_weak_symbols.h" |
269 #endif | 295 #endif |
270 | 296 |
| 297 #if defined(OS_MACOSX) |
| 298 namespace base { |
| 299 namespace allocator { |
| 300 void InitializeAllocatorShim() { |
| 301 // Prepares the default dispatch. After the intercepted malloc calls have |
| 302 // traversed the shim this will route them to the default malloc zone. |
| 303 InitializeDefaultDispatchToMacAllocator(); |
| 304 |
| 305 // This replaces the default malloc zone, causing calls to malloc & friends |
| 306 // from the codebase to be routed to ShimMalloc() above. |
| 307 OverrideMacSymbols(); |
| 308 } |
| 309 } // namespace allocator |
| 310 } // namespace base |
| 311 #endif |
| 312 |
271 // Cross-checks. | 313 // Cross-checks. |
272 | 314 |
273 #if defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 315 #if defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
274 #error The allocator shim should not be compiled when building for memory tools. | 316 #error The allocator shim should not be compiled when building for memory tools. |
275 #endif | 317 #endif |
276 | 318 |
277 #if (defined(__GNUC__) && defined(__EXCEPTIONS)) || \ | 319 #if (defined(__GNUC__) && defined(__EXCEPTIONS)) || \ |
278 (defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS) | 320 (defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS) |
279 #error This code cannot be used when exceptions are turned on. | 321 #error This code cannot be used when exceptions are turned on. |
280 #endif | 322 #endif |
OLD | NEW |