allocator_shim_default_dispatch_to_mac_zoned_malloc.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2017 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 <utility>
  5. #include "base/allocator/allocator_interception_mac.h"
  6. #include "base/allocator/allocator_shim.h"
  7. #include "base/allocator/malloc_zone_functions_mac.h"
  8. namespace base {
  9. namespace allocator {
  10. namespace {
  11. void* MallocImpl(const AllocatorDispatch*, size_t size, void* context) {
  12. MallocZoneFunctions& functions = GetFunctionsForZone(context);
  13. return functions.malloc(reinterpret_cast<struct _malloc_zone_t*>(context),
  14. size);
  15. }
  16. void* CallocImpl(const AllocatorDispatch*,
  17. size_t n,
  18. size_t size,
  19. void* context) {
  20. MallocZoneFunctions& functions = GetFunctionsForZone(context);
  21. return functions.calloc(reinterpret_cast<struct _malloc_zone_t*>(context), n,
  22. size);
  23. }
  24. void* MemalignImpl(const AllocatorDispatch*,
  25. size_t alignment,
  26. size_t size,
  27. void* context) {
  28. MallocZoneFunctions& functions = GetFunctionsForZone(context);
  29. return functions.memalign(reinterpret_cast<struct _malloc_zone_t*>(context),
  30. alignment, size);
  31. }
  32. void* ReallocImpl(const AllocatorDispatch*,
  33. void* ptr,
  34. size_t size,
  35. void* context) {
  36. MallocZoneFunctions& functions = GetFunctionsForZone(context);
  37. return functions.realloc(reinterpret_cast<struct _malloc_zone_t*>(context),
  38. ptr, size);
  39. }
  40. void FreeImpl(const AllocatorDispatch*, void* ptr, void* context) {
  41. MallocZoneFunctions& functions = GetFunctionsForZone(context);
  42. functions.free(reinterpret_cast<struct _malloc_zone_t*>(context), ptr);
  43. }
  44. size_t GetSizeEstimateImpl(const AllocatorDispatch*, void* ptr, void* context) {
  45. MallocZoneFunctions& functions = GetFunctionsForZone(context);
  46. return functions.size(reinterpret_cast<struct _malloc_zone_t*>(context), ptr);
  47. }
  48. unsigned BatchMallocImpl(const AllocatorDispatch* self,
  49. size_t size,
  50. void** results,
  51. unsigned num_requested,
  52. void* context) {
  53. MallocZoneFunctions& functions = GetFunctionsForZone(context);
  54. return functions.batch_malloc(
  55. reinterpret_cast<struct _malloc_zone_t*>(context), size, results,
  56. num_requested);
  57. }
  58. void BatchFreeImpl(const AllocatorDispatch* self,
  59. void** to_be_freed,
  60. unsigned num_to_be_freed,
  61. void* context) {
  62. MallocZoneFunctions& functions = GetFunctionsForZone(context);
  63. functions.batch_free(reinterpret_cast<struct _malloc_zone_t*>(context),
  64. to_be_freed, num_to_be_freed);
  65. }
  66. void FreeDefiniteSizeImpl(const AllocatorDispatch* self,
  67. void* ptr,
  68. size_t size,
  69. void* context) {
  70. MallocZoneFunctions& functions = GetFunctionsForZone(context);
  71. functions.free_definite_size(
  72. reinterpret_cast<struct _malloc_zone_t*>(context), ptr, size);
  73. }
  74. } // namespace
  75. const AllocatorDispatch AllocatorDispatch::default_dispatch = {
  76. &MallocImpl, /* alloc_function */
  77. &MallocImpl, /* alloc_unchecked_function */
  78. &CallocImpl, /* alloc_zero_initialized_function */
  79. &MemalignImpl, /* alloc_aligned_function */
  80. &ReallocImpl, /* realloc_function */
  81. &FreeImpl, /* free_function */
  82. &GetSizeEstimateImpl, /* get_size_estimate_function */
  83. &BatchMallocImpl, /* batch_malloc_function */
  84. &BatchFreeImpl, /* batch_free_function */
  85. &FreeDefiniteSizeImpl, /* free_definite_size_function */
  86. nullptr, /* aligned_malloc_function */
  87. nullptr, /* aligned_realloc_function */
  88. nullptr, /* aligned_free_function */
  89. nullptr, /* next */
  90. };
  91. } // namespace allocator
  92. } // namespace base