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