• 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 libc functions.
9 // The code here is strongly inspired from tcmalloc's libc_override_glibc.h.
10 
11 extern "C" {
12 void* __libc_malloc(size_t size);
13 void* __libc_calloc(size_t n, size_t size);
14 void* __libc_realloc(void* address, size_t size);
15 void* __libc_memalign(size_t alignment, size_t size);
16 void __libc_free(void* ptr);
17 }  // extern "C"
18 
19 namespace {
20 
21 using base::allocator::AllocatorDispatch;
22 
GlibcMalloc(const AllocatorDispatch *,size_t size)23 void* GlibcMalloc(const AllocatorDispatch*, size_t size) {
24   return __libc_malloc(size);
25 }
26 
GlibcCalloc(const AllocatorDispatch *,size_t n,size_t size)27 void* GlibcCalloc(const AllocatorDispatch*, size_t n, size_t size) {
28   return __libc_calloc(n, size);
29 }
30 
GlibcRealloc(const AllocatorDispatch *,void * address,size_t size)31 void* GlibcRealloc(const AllocatorDispatch*, void* address, size_t size) {
32   return __libc_realloc(address, size);
33 }
34 
GlibcMemalign(const AllocatorDispatch *,size_t alignment,size_t size)35 void* GlibcMemalign(const AllocatorDispatch*, size_t alignment, size_t size) {
36   return __libc_memalign(alignment, size);
37 }
38 
GlibcFree(const AllocatorDispatch *,void * address)39 void GlibcFree(const AllocatorDispatch*, void* address) {
40   __libc_free(address);
41 }
42 
43 }  // namespace
44 
45 const AllocatorDispatch AllocatorDispatch::default_dispatch = {
46     &GlibcMalloc,   /* alloc_function */
47     &GlibcCalloc,   /* alloc_zero_initialized_function */
48     &GlibcMemalign, /* alloc_aligned_function */
49     &GlibcRealloc,  /* realloc_function */
50     &GlibcFree,     /* free_function */
51     nullptr,        /* next */
52 };
53