OLD | NEW |
---|---|
(Empty) | |
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 | |
5 #ifndef BASE_ALLOCATOR_MALLOC_ZONE_AGGREGATOR_MAC_H_ | |
6 #define BASE_ALLOCATOR_MALLOC_ZONE_AGGREGATOR_MAC_H_ | |
7 | |
8 #include <malloc/malloc.h> | |
9 #include <stddef.h> | |
10 | |
11 #include "base/base_export.h" | |
12 #include "base/synchronization/lock.h" | |
13 #include "third_party/apple_apsl/malloc.h" | |
14 | |
15 namespace base { | |
16 namespace allocator { | |
17 | |
18 typedef void* (*malloc_type)(struct _malloc_zone_t* zone, size_t size); | |
19 typedef void* (*calloc_type)(struct _malloc_zone_t* zone, | |
20 size_t num_items, | |
21 size_t size); | |
22 typedef void* (*valloc_type)(struct _malloc_zone_t* zone, size_t size); | |
23 typedef void (*free_type)(struct _malloc_zone_t* zone, void* ptr); | |
24 typedef void* (*realloc_type)(struct _malloc_zone_t* zone, | |
25 void* ptr, | |
26 size_t size); | |
27 typedef void* (*memalign_type)(struct _malloc_zone_t* zone, | |
28 size_t alignment, | |
29 size_t size); | |
30 typedef unsigned (*batch_malloc_type)(struct _malloc_zone_t* zone, | |
31 size_t size, | |
32 void** results, | |
33 unsigned num_requested); | |
34 typedef void (*batch_free_type)(struct _malloc_zone_t* zone, | |
35 void** to_be_freed, | |
36 unsigned num_to_be_freed); | |
37 typedef void (*free_definite_size_type)(struct _malloc_zone_t* zone, | |
38 void* ptr, | |
39 size_t size); | |
40 typedef size_t (*size_fn_type)(struct _malloc_zone_t* zone, const void* ptr); | |
41 | |
42 struct MallocZoneFunctions { | |
Primiano Tucci (use gerrit)
2017/02/22 12:25:39
TIL about the existence of ChromeMallocZone. Now m
erikchen
2017/02/22 21:03:10
The latter doesn't have a |context| parameter.
| |
43 MallocZoneFunctions(); | |
44 malloc_type malloc = nullptr; | |
45 calloc_type calloc = nullptr; | |
46 valloc_type valloc = nullptr; | |
47 free_type free = nullptr; | |
48 realloc_type realloc = nullptr; | |
49 memalign_type memalign = nullptr; | |
50 batch_malloc_type batch_malloc = nullptr; | |
51 batch_free_type batch_free = nullptr; | |
52 free_definite_size_type free_definite_size = nullptr; | |
53 size_fn_type size = nullptr; | |
54 ChromeMallocZone* context = nullptr; | |
55 }; | |
56 | |
57 void StoreZoneFunctions(ChromeMallocZone* zone, MallocZoneFunctions* functions); | |
58 | |
59 // This class stores all information about malloc zones before they are shimmed. | |
60 // This information needs to be accessed during dispatch back into the zone, and | |
61 // additional zones may be added later in the execution fo the program, so | |
62 // this class needs to be both thread-safe and high-performance. | |
63 // | |
64 // We begin by creating an array of MallocZoneFunctions of fixed size. We will | |
65 // never modify the container, which provides thread-safety to iterators. When | |
66 // we want to add a MallocZoneFunctions to the container, we: | |
67 // 1. Fill in all the fields. | |
68 // 2. Update the total zone count. | |
69 // 3. Insert a memory barrier. | |
70 // 4. Insert our shim. | |
71 // | |
72 // Each MallocZoneFunctions is uniquely identified by |context|, which is a | |
73 // pointer to the original malloc zone. When we wish to dispatch back to the | |
74 // original malloc zones, we iterate through the array, looking for a matching | |
75 // |context|. | |
76 // | |
77 // Most allocations go through the default allocator. We will ensure that the | |
78 // default allocator is stored as the first MallocZoneFunctions. | |
79 class BASE_EXPORT MallocZoneAggregator { | |
80 public: | |
81 static MallocZoneAggregator* GetMallocZoneAggregator(); | |
82 | |
83 MallocZoneAggregator(); | |
84 ~MallocZoneAggregator(); | |
85 | |
86 void* DispatchMallocToZone(void* zone, size_t size); | |
87 void* DispatchCallocToZone(void* zone, size_t num_items, size_t size); | |
88 void* DispatchVallocToZone(void* zone, size_t size); | |
89 void DispatchFreeToZone(void* zone, void* ptr); | |
90 void* DispatchReallocToZone(void* zone, void* ptr, size_t size); | |
91 void* DispatchMemalignToZone(void* zone, size_t alignment, size_t size); | |
92 unsigned DispatchBatchMallocToZone(void* zone, | |
93 size_t size, | |
94 void** results, | |
95 unsigned num_requested); | |
96 void DispatchBatchFreeToZone(void* zone, | |
97 void** to_be_freed, | |
98 unsigned num_to_be_freed); | |
99 void DispatchFreeDefiniteSizeToZone(void* zone, void* ptr, size_t size); | |
100 size_t DispatchGetSizeEstimateToZone(void* zone, void* ptr); | |
101 | |
102 void StoreZone(ChromeMallocZone* zone); | |
103 bool IsZoneAlreadyStored(ChromeMallocZone* zone); | |
104 | |
105 int GetZoneCount(); | |
106 static constexpr int kMaxZoneCount = 30; | |
107 | |
108 private: | |
109 bool IsZoneAlreadyStoredLockAcquired(ChromeMallocZone* zone); | |
110 MallocZoneFunctions& GetFunctionsForZone(void* zone); | |
111 | |
112 // All modifications to the MallocZoneAggregator are gated behind this lock. | |
113 // Dispatch to a malloc zone does not need to acquire this lock. | |
114 base::Lock lock_; | |
115 int zone_count_ = 0; | |
116 MallocZoneFunctions zones_[kMaxZoneCount]; | |
117 }; | |
118 | |
119 } // namespace allocator | |
120 } // namespace base | |
121 | |
122 #endif // BASE_ALLOCATOR_MALLOC_ZONE_AGGREGATOR_MAC_H_ | |
OLD | NEW |