allocator_shim_default_dispatch_to_linker_wrapped_symbols.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #include <malloc.h>
  5. #include "base/allocator/allocator_shim.h"
  6. #include "build/build_config.h"
  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. extern "C" {
  16. void* __real_malloc(size_t);
  17. void* __real_calloc(size_t, size_t);
  18. void* __real_realloc(void*, size_t);
  19. void* __real_memalign(size_t, size_t);
  20. void __real_free(void*);
  21. size_t __real_malloc_usable_size(void*);
  22. } // extern "C"
  23. namespace {
  24. using base::allocator::AllocatorDispatch;
  25. void* RealMalloc(const AllocatorDispatch*, size_t size, void* context) {
  26. return __real_malloc(size);
  27. }
  28. void* RealCalloc(const AllocatorDispatch*,
  29. size_t n,
  30. size_t size,
  31. void* context) {
  32. return __real_calloc(n, size);
  33. }
  34. void* RealRealloc(const AllocatorDispatch*,
  35. void* address,
  36. size_t size,
  37. void* context) {
  38. return __real_realloc(address, size);
  39. }
  40. void* RealMemalign(const AllocatorDispatch*,
  41. size_t alignment,
  42. size_t size,
  43. void* context) {
  44. return __real_memalign(alignment, size);
  45. }
  46. void RealFree(const AllocatorDispatch*, void* address, void* context) {
  47. __real_free(address);
  48. }
  49. size_t RealSizeEstimate(const AllocatorDispatch*,
  50. void* address,
  51. void* context) {
  52. return __real_malloc_usable_size(address);
  53. }
  54. } // namespace
  55. const AllocatorDispatch AllocatorDispatch::default_dispatch = {
  56. &RealMalloc, /* alloc_function */
  57. &RealMalloc, /* alloc_unchecked_function */
  58. &RealCalloc, /* alloc_zero_initialized_function */
  59. &RealMemalign, /* alloc_aligned_function */
  60. &RealRealloc, /* realloc_function */
  61. &RealFree, /* free_function */
  62. &RealSizeEstimate, /* get_size_estimate_function */
  63. nullptr, /* batch_malloc_function */
  64. nullptr, /* batch_free_function */
  65. nullptr, /* free_definite_size_function */
  66. nullptr, /* aligned_malloc_function */
  67. nullptr, /* aligned_realloc_function */
  68. nullptr, /* aligned_free_function */
  69. nullptr, /* next */
  70. };