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 #include "base/allocator/allocator_shim_default_dispatch_to_zoned_malloc.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/allocator/allocator_shim.h" | |
10 #include "base/logging.h" | |
11 | |
12 namespace base { | |
13 namespace allocator { | |
14 | |
15 namespace { | |
16 | |
17 // This is the zone that the allocator shim will call to actually perform heap | |
18 // allocations. It should be populated with the original, unintercepted default | |
19 // malloc zone. | |
20 MallocZoneFunctions g_default_zone; | |
21 | |
22 template <class F, class... Args> | |
23 auto CallUnshimmed(F ChromeMallocZone::*m, Args... args) | |
Primiano Tucci (use gerrit)
2017/01/28 05:10:02
This is the part I am really missing here.
I am no
erikchen
2017/01/31 02:21:22
This is needed because ThreadHeapUsageTracker::Sta
DmitrySkiba
2017/01/31 02:33:37
Interesting. Originally I added this because in te
Primiano Tucci (use gerrit)
2017/01/31 17:40:05
Hmm. I can see how that would cause the test to fa
| |
24 -> decltype(std::declval<F>()(nullptr, args...)) { | |
25 struct _malloc_zone_t* zone = malloc_default_zone(); | |
26 F f = reinterpret_cast<ChromeMallocZone*>(zone)->*m; | |
27 return f(zone, args...); | |
28 } | |
29 | |
30 void* MallocImpl(const AllocatorDispatch*, size_t size) { | |
31 if (g_default_zone.malloc) | |
32 return g_default_zone.malloc(malloc_default_zone(), size); | |
33 return CallUnshimmed(&ChromeMallocZone::malloc, size); | |
34 } | |
35 | |
36 void* CallocImpl(const AllocatorDispatch*, size_t n, size_t size) { | |
37 if (g_default_zone.calloc) | |
38 return g_default_zone.calloc(malloc_default_zone(), n, size); | |
39 return CallUnshimmed(&ChromeMallocZone::calloc, n, size); | |
40 } | |
41 | |
42 void* MemalignImpl(const AllocatorDispatch*, size_t alignment, size_t size) { | |
43 if (g_default_zone.memalign) | |
44 return g_default_zone.memalign(malloc_default_zone(), alignment, size); | |
45 return CallUnshimmed(&ChromeMallocZone::memalign, alignment, size); | |
46 } | |
47 | |
48 void* ReallocImpl(const AllocatorDispatch*, void* ptr, size_t size) { | |
49 if (g_default_zone.realloc) | |
50 return g_default_zone.realloc(malloc_default_zone(), ptr, size); | |
51 return CallUnshimmed(&ChromeMallocZone::realloc, ptr, size); | |
52 } | |
53 | |
54 void FreeImpl(const AllocatorDispatch*, void* ptr) { | |
55 if (g_default_zone.free) { | |
56 g_default_zone.free(malloc_default_zone(), ptr); | |
57 return; | |
58 } | |
59 CallUnshimmed(&ChromeMallocZone::free, ptr); | |
60 } | |
61 | |
62 size_t GetSizeEstimateImpl(const AllocatorDispatch*, void* ptr) { | |
63 if (g_default_zone.size) { | |
64 return g_default_zone.size(malloc_default_zone(), ptr); | |
65 } | |
66 return CallUnshimmed(&ChromeMallocZone::size, ptr); | |
67 } | |
68 | |
69 unsigned BatchMallocImpl(const AllocatorDispatch* self, | |
70 size_t size, | |
71 void** results, | |
72 unsigned num_requested) { | |
73 if (g_default_zone.batch_malloc) | |
74 return g_default_zone.batch_malloc(malloc_default_zone(), size, results, | |
75 num_requested); | |
76 return CallUnshimmed(&ChromeMallocZone::batch_malloc, size, results, | |
77 num_requested); | |
78 } | |
79 | |
80 void BatchFreeImpl(const AllocatorDispatch* self, | |
81 void** to_be_freed, | |
82 unsigned num_to_be_freed) { | |
83 if (g_default_zone.batch_free) { | |
84 g_default_zone.batch_free(malloc_default_zone(), to_be_freed, | |
85 num_to_be_freed); | |
86 return; | |
87 } | |
88 | |
89 CallUnshimmed(&ChromeMallocZone::batch_free, to_be_freed, num_to_be_freed); | |
90 } | |
91 | |
92 } // namespace | |
93 | |
94 void SetDefaultDispatchMallocZoneFunctions(MallocZoneFunctions* functions) { | |
Primiano Tucci (use gerrit)
2017/01/28 05:10:02
this one should be just:
void InitializeDefaultDis
erikchen
2017/01/31 02:21:22
Done.
| |
95 g_default_zone.malloc = functions->malloc; | |
96 g_default_zone.calloc = functions->calloc; | |
97 g_default_zone.memalign = functions->memalign; | |
98 g_default_zone.realloc = functions->realloc; | |
99 g_default_zone.free = functions->free; | |
100 g_default_zone.size = functions->size; | |
101 g_default_zone.batch_malloc = functions->batch_malloc; | |
102 g_default_zone.batch_free = functions->batch_free; | |
103 } | |
104 | |
105 const AllocatorDispatch AllocatorDispatch::default_dispatch = { | |
106 &MallocImpl, /* alloc_function */ | |
107 &CallocImpl, /* alloc_zero_initialized_function */ | |
108 &MemalignImpl, /* alloc_aligned_function */ | |
109 &ReallocImpl, /* realloc_function */ | |
110 &FreeImpl, /* free_function */ | |
111 &GetSizeEstimateImpl, /* get_size_estimate_function */ | |
112 &BatchMallocImpl, /* batch_malloc_function */ | |
113 &BatchFreeImpl, /* batch_free_function */ | |
114 nullptr, /* next */ | |
115 }; | |
116 | |
117 } // namespace allocator | |
118 } // namespace base | |
OLD | NEW |