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 3// found in the LICENSE file. 4 5#include "base/allocator/allocator_shim.h" 6 7#include <malloc.h> 8 9// This translation unit defines a default dispatch for the allocator shim which 10// routes allocations to libc functions. 11// The code here is strongly inspired from tcmalloc's libc_override_glibc.h. 12 13extern "C" { 14void* __libc_malloc(size_t size); 15void* __libc_calloc(size_t n, size_t size); 16void* __libc_realloc(void* address, size_t size); 17void* __libc_memalign(size_t alignment, size_t size); 18void __libc_free(void* ptr); 19} // extern "C" 20 21namespace { 22 23using base::allocator::AllocatorDispatch; 24 25void* GlibcMalloc(const AllocatorDispatch*, size_t size, void* context) { 26 return __libc_malloc(size); 27} 28 29void* GlibcCalloc(const AllocatorDispatch*, 30 size_t n, 31 size_t size, 32 void* context) { 33 return __libc_calloc(n, size); 34} 35 36void* GlibcRealloc(const AllocatorDispatch*, 37 void* address, 38 size_t size, 39 void* context) { 40 return __libc_realloc(address, size); 41} 42 43void* GlibcMemalign(const AllocatorDispatch*, 44 size_t alignment, 45 size_t size, 46 void* context) { 47 return __libc_memalign(alignment, size); 48} 49 50void GlibcFree(const AllocatorDispatch*, void* address, void* context) { 51 __libc_free(address); 52} 53 54size_t GlibcGetSizeEstimate(const AllocatorDispatch*, 55 void* address, 56 void* context) { 57 // TODO(siggi, primiano): malloc_usable_size may need redirection in the 58 // presence of interposing shims that divert allocations. 59 return malloc_usable_size(address); 60} 61 62} // namespace 63 64const AllocatorDispatch AllocatorDispatch::default_dispatch = { 65 &GlibcMalloc, /* alloc_function */ 66 &GlibcCalloc, /* alloc_zero_initialized_function */ 67 &GlibcMemalign, /* alloc_aligned_function */ 68 &GlibcRealloc, /* realloc_function */ 69 &GlibcFree, /* free_function */ 70 &GlibcGetSizeEstimate, /* get_size_estimate_function */ 71 nullptr, /* batch_malloc_function */ 72 nullptr, /* batch_free_function */ 73 nullptr, /* free_definite_size_function */ 74 nullptr, /* next */ 75}; 76