Chromium Code Reviews| 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 <malloc.h> | 7 #include <malloc.h> |
| 8 | 8 |
| 9 // This is required for posix_memalign | |
| 10 #if defined(OS_AIX) | |
| 11 #include <stdlib.h> | |
| 12 #endif | |
| 13 | |
| 9 // This translation unit defines a default dispatch for the allocator shim which | 14 // This translation unit defines a default dispatch for the allocator shim which |
| 10 // routes allocations to libc functions. | 15 // routes allocations to libc functions. |
| 11 // The code here is strongly inspired from tcmalloc's libc_override_glibc.h. | 16 // The code here is strongly inspired from tcmalloc's libc_override_glibc.h. |
| 12 | 17 |
| 13 extern "C" { | 18 extern "C" { |
| 14 void* __libc_malloc(size_t size); | 19 void* __libc_malloc(size_t size); |
| 15 void* __libc_calloc(size_t n, size_t size); | 20 void* __libc_calloc(size_t n, size_t size); |
| 16 void* __libc_realloc(void* address, size_t size); | 21 void* __libc_realloc(void* address, size_t size); |
| 17 void* __libc_memalign(size_t alignment, size_t size); | 22 void* __libc_memalign(size_t alignment, size_t size); |
| 18 void __libc_free(void* ptr); | 23 void __libc_free(void* ptr); |
| 19 } // extern "C" | 24 } // extern "C" |
| 20 | 25 |
| 21 namespace { | 26 namespace { |
| 22 | 27 |
| 23 using base::allocator::AllocatorDispatch; | 28 using base::allocator::AllocatorDispatch; |
| 24 | 29 |
| 25 void* GlibcMalloc(const AllocatorDispatch*, size_t size, void* context) { | 30 void* GlibcMalloc(const AllocatorDispatch*, size_t size, void* context) { |
| 31 #if defined(OS_AIX) | |
| 32 return malloc(size); | |
|
Primiano Tucci (use gerrit)
2017/04/13 09:59:14
this is just going to cause loops in the malloc co
| |
| 33 #else | |
| 26 return __libc_malloc(size); | 34 return __libc_malloc(size); |
| 35 #endif | |
| 27 } | 36 } |
| 28 | 37 |
| 29 void* GlibcCalloc(const AllocatorDispatch*, | 38 void* GlibcCalloc(const AllocatorDispatch*, |
| 30 size_t n, | 39 size_t n, |
| 31 size_t size, | 40 size_t size, |
| 32 void* context) { | 41 void* context) { |
| 42 #if defined(OS_AIX) | |
| 43 return calloc(n, size); | |
| 44 #else | |
| 33 return __libc_calloc(n, size); | 45 return __libc_calloc(n, size); |
| 46 #endif | |
| 34 } | 47 } |
| 35 | 48 |
| 36 void* GlibcRealloc(const AllocatorDispatch*, | 49 void* GlibcRealloc(const AllocatorDispatch*, |
| 37 void* address, | 50 void* address, |
| 38 size_t size, | 51 size_t size, |
| 39 void* context) { | 52 void* context) { |
| 53 #if defined(OS_AIX) | |
| 54 return realloc(address, size); | |
| 55 #else | |
| 40 return __libc_realloc(address, size); | 56 return __libc_realloc(address, size); |
| 57 #endif | |
| 41 } | 58 } |
| 42 | 59 |
| 43 void* GlibcMemalign(const AllocatorDispatch*, | 60 void* GlibcMemalign(const AllocatorDispatch*, |
| 44 size_t alignment, | 61 size_t alignment, |
| 45 size_t size, | 62 size_t size, |
| 46 void* context) { | 63 void* context) { |
| 64 #if defined(OS_AIX) | |
| 65 void* p; | |
| 66 if (posix_memalign(&p, alignment, size) == 0) | |
| 67 return p; | |
| 68 else | |
| 69 return NULL; | |
| 70 #else | |
| 47 return __libc_memalign(alignment, size); | 71 return __libc_memalign(alignment, size); |
| 72 #endif | |
| 48 } | 73 } |
| 49 | 74 |
| 50 void GlibcFree(const AllocatorDispatch*, void* address, void* context) { | 75 void GlibcFree(const AllocatorDispatch*, void* address, void* context) { |
| 51 __libc_free(address); | 76 #if defined(OS_AIX) |
| 77 return free(address); | |
| 78 #else | |
| 79 return __libc_free(address); | |
| 80 #endif | |
| 52 } | 81 } |
| 53 | 82 |
| 54 size_t GlibcGetSizeEstimate(const AllocatorDispatch*, | 83 size_t GlibcGetSizeEstimate(const AllocatorDispatch*, |
| 55 void* address, | 84 void* address, |
| 56 void* context) { | 85 void* context) { |
| 57 // TODO(siggi, primiano): malloc_usable_size may need redirection in the | 86 // TODO(siggi, primiano): malloc_usable_size may need redirection in the |
| 58 // presence of interposing shims that divert allocations. | 87 // presence of interposing shims that divert allocations. |
| 88 #if defined(OS_AIX) | |
| 89 return 0; | |
| 90 #else | |
| 59 return malloc_usable_size(address); | 91 return malloc_usable_size(address); |
| 92 #endif | |
| 60 } | 93 } |
| 61 | 94 |
| 62 } // namespace | 95 } // namespace |
| 63 | 96 |
| 64 const AllocatorDispatch AllocatorDispatch::default_dispatch = { | 97 const AllocatorDispatch AllocatorDispatch::default_dispatch = { |
| 65 &GlibcMalloc, /* alloc_function */ | 98 &GlibcMalloc, /* alloc_function */ |
| 66 &GlibcCalloc, /* alloc_zero_initialized_function */ | 99 &GlibcCalloc, /* alloc_zero_initialized_function */ |
| 67 &GlibcMemalign, /* alloc_aligned_function */ | 100 &GlibcMemalign, /* alloc_aligned_function */ |
| 68 &GlibcRealloc, /* realloc_function */ | 101 &GlibcRealloc, /* realloc_function */ |
| 69 &GlibcFree, /* free_function */ | 102 &GlibcFree, /* free_function */ |
| 70 &GlibcGetSizeEstimate, /* get_size_estimate_function */ | 103 &GlibcGetSizeEstimate, /* get_size_estimate_function */ |
| 71 nullptr, /* batch_malloc_function */ | 104 nullptr, /* batch_malloc_function */ |
| 72 nullptr, /* batch_free_function */ | 105 nullptr, /* batch_free_function */ |
| 73 nullptr, /* free_definite_size_function */ | 106 nullptr, /* free_definite_size_function */ |
| 74 nullptr, /* next */ | 107 nullptr, /* next */ |
| 75 }; | 108 }; |
| OLD | NEW |