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