• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // This translation unit defines a default dispatch for the allocator shim which
8 // routes allocations to the original libc functions when using the link-time
9 // -Wl,-wrap,malloc approach (see README.md).
10 // The __real_X functions here are special symbols that the linker will relocate
11 // against the real "X" undefined symbol, so that __real_malloc becomes the
12 // equivalent of what an undefined malloc symbol reference would have been.
13 // This is the counterpart of allocator_shim_override_linker_wrapped_symbols.h,
14 // which routes the __wrap_X functions into the shim.
15 
16 extern "C" {
17 void* __real_malloc(size_t);
18 void* __real_calloc(size_t, size_t);
19 void* __real_realloc(void*, size_t);
20 void* __real_memalign(size_t, size_t);
21 void* __real_free(void*);
22 }  // extern "C"
23 
24 namespace {
25 
26 using base::allocator::AllocatorDispatch;
27 
RealMalloc(const AllocatorDispatch *,size_t size)28 void* RealMalloc(const AllocatorDispatch*, size_t size) {
29   return __real_malloc(size);
30 }
31 
RealCalloc(const AllocatorDispatch *,size_t n,size_t size)32 void* RealCalloc(const AllocatorDispatch*, size_t n, size_t size) {
33   return __real_calloc(n, size);
34 }
35 
RealRealloc(const AllocatorDispatch *,void * address,size_t size)36 void* RealRealloc(const AllocatorDispatch*, void* address, size_t size) {
37   return __real_realloc(address, size);
38 }
39 
RealMemalign(const AllocatorDispatch *,size_t alignment,size_t size)40 void* RealMemalign(const AllocatorDispatch*, size_t alignment, size_t size) {
41   return __real_memalign(alignment, size);
42 }
43 
RealFree(const AllocatorDispatch *,void * address)44 void RealFree(const AllocatorDispatch*, void* address) {
45   __real_free(address);
46 }
47 
48 }  // namespace
49 
50 const AllocatorDispatch AllocatorDispatch::default_dispatch = {
51     &RealMalloc,   /* alloc_function */
52     &RealCalloc,   /* alloc_zero_initialized_function */
53     &RealMemalign, /* alloc_aligned_function */
54     &RealRealloc,  /* realloc_function */
55     &RealFree,     /* free_function */
56     nullptr,       /* next */
57 };
58