malloc_zone_functions_mac.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #ifndef BASE_ALLOCATOR_MALLOC_ZONE_FUNCTIONS_MAC_H_
  5. #define BASE_ALLOCATOR_MALLOC_ZONE_FUNCTIONS_MAC_H_
  6. #include <malloc/malloc.h>
  7. #include <stddef.h>
  8. #include "base/base_export.h"
  9. #include "base/immediate_crash.h"
  10. #include "third_party/apple_apsl/malloc.h"
  11. namespace base {
  12. namespace allocator {
  13. typedef void* (*malloc_type)(struct _malloc_zone_t* zone, size_t size);
  14. typedef void* (*calloc_type)(struct _malloc_zone_t* zone,
  15. size_t num_items,
  16. size_t size);
  17. typedef void* (*valloc_type)(struct _malloc_zone_t* zone, size_t size);
  18. typedef void (*free_type)(struct _malloc_zone_t* zone, void* ptr);
  19. typedef void* (*realloc_type)(struct _malloc_zone_t* zone,
  20. void* ptr,
  21. size_t size);
  22. typedef void* (*memalign_type)(struct _malloc_zone_t* zone,
  23. size_t alignment,
  24. size_t size);
  25. typedef unsigned (*batch_malloc_type)(struct _malloc_zone_t* zone,
  26. size_t size,
  27. void** results,
  28. unsigned num_requested);
  29. typedef void (*batch_free_type)(struct _malloc_zone_t* zone,
  30. void** to_be_freed,
  31. unsigned num_to_be_freed);
  32. typedef void (*free_definite_size_type)(struct _malloc_zone_t* zone,
  33. void* ptr,
  34. size_t size);
  35. typedef size_t (*size_fn_type)(struct _malloc_zone_t* zone, const void* ptr);
  36. struct MallocZoneFunctions {
  37. malloc_type malloc;
  38. calloc_type calloc;
  39. valloc_type valloc;
  40. free_type free;
  41. realloc_type realloc;
  42. memalign_type memalign;
  43. batch_malloc_type batch_malloc;
  44. batch_free_type batch_free;
  45. free_definite_size_type free_definite_size;
  46. size_fn_type size;
  47. const ChromeMallocZone* context;
  48. };
  49. BASE_EXPORT void StoreZoneFunctions(const ChromeMallocZone* zone,
  50. MallocZoneFunctions* functions);
  51. static constexpr int kMaxZoneCount = 30;
  52. BASE_EXPORT extern MallocZoneFunctions g_malloc_zones[kMaxZoneCount];
  53. // The array g_malloc_zones stores all information about malloc zones before
  54. // they are shimmed. This information needs to be accessed during dispatch back
  55. // into the zone, and additional zones may be added later in the execution fo
  56. // the program, so the array needs to be both thread-safe and high-performance.
  57. //
  58. // We begin by creating an array of MallocZoneFunctions of fixed size. We will
  59. // never modify the container, which provides thread-safety to iterators. When
  60. // we want to add a MallocZoneFunctions to the container, we:
  61. // 1. Fill in all the fields.
  62. // 2. Update the total zone count.
  63. // 3. Insert a memory barrier.
  64. // 4. Insert our shim.
  65. //
  66. // Each MallocZoneFunctions is uniquely identified by |context|, which is a
  67. // pointer to the original malloc zone. When we wish to dispatch back to the
  68. // original malloc zones, we iterate through the array, looking for a matching
  69. // |context|.
  70. //
  71. // Most allocations go through the default allocator. We will ensure that the
  72. // default allocator is stored as the first MallocZoneFunctions.
  73. //
  74. // Returns whether the zone was successfully stored.
  75. BASE_EXPORT bool StoreMallocZone(ChromeMallocZone* zone);
  76. BASE_EXPORT bool IsMallocZoneAlreadyStored(ChromeMallocZone* zone);
  77. BASE_EXPORT bool DoesMallocZoneNeedReplacing(
  78. ChromeMallocZone* zone,
  79. const MallocZoneFunctions* functions);
  80. BASE_EXPORT int GetMallocZoneCountForTesting();
  81. BASE_EXPORT void ClearAllMallocZonesForTesting();
  82. inline MallocZoneFunctions& GetFunctionsForZone(void* zone) {
  83. for (unsigned int i = 0; i < kMaxZoneCount; ++i) {
  84. if (g_malloc_zones[i].context == zone)
  85. return g_malloc_zones[i];
  86. }
  87. IMMEDIATE_CRASH();
  88. }
  89. } // namespace allocator
  90. } // namespace base
  91. #endif // BASE_ALLOCATOR_MALLOC_ZONE_FUNCTIONS_MAC_H_