microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
billti/bloch

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

allocator/mimalloc-sys/mimalloc/src/alloc-override.c

297lines · modecode

1/* ----------------------------------------------------------------------------
2Copyright (c) 2018-2021, Microsoft Research, Daan Leijen
3This is free software; you can redistribute it and/or modify it under the
4terms of the MIT license. A copy of the license can be found in the file
5"LICENSE" at the root of this distribution.
6-----------------------------------------------------------------------------*/
7
8#if !defined(MI_IN_ALLOC_C)
9#error "this file should be included from 'alloc.c' (so aliases can work)"
10#endif
11
12#if defined(MI_MALLOC_OVERRIDE) && defined(_WIN32) && !(defined(MI_SHARED_LIB) && defined(_DLL))
13#error "It is only possible to override "malloc" on Windows when building as a DLL (and linking the C runtime as a DLL)"
14#endif
15
16#if defined(MI_MALLOC_OVERRIDE) && !(defined(_WIN32))
17
18#if defined(__APPLE__)
19#include <AvailabilityMacros.h>
20mi_decl_externc void vfree(void* p);
21mi_decl_externc size_t malloc_size(const void* p);
22mi_decl_externc size_t malloc_good_size(size_t size);
23#endif
24
25// helper definition for C override of C++ new
26typedef struct mi_nothrow_s { int _tag; } mi_nothrow_t;
27
28// ------------------------------------------------------
29// Override system malloc
30// ------------------------------------------------------
31
32#if (defined(__GNUC__) || defined(__clang__)) && !defined(__APPLE__) && !MI_TRACK_ENABLED
33 // gcc, clang: use aliasing to alias the exported function to one of our `mi_` functions
34 #if (defined(__GNUC__) && __GNUC__ >= 9)
35 #pragma GCC diagnostic ignored "-Wattributes" // or we get warnings that nodiscard is ignored on a forward
36 #define MI_FORWARD(fun) __attribute__((alias(#fun), used, visibility("default"), copy(fun)));
37 #else
38 #define MI_FORWARD(fun) __attribute__((alias(#fun), used, visibility("default")));
39 #endif
40 #define MI_FORWARD1(fun,x) MI_FORWARD(fun)
41 #define MI_FORWARD2(fun,x,y) MI_FORWARD(fun)
42 #define MI_FORWARD3(fun,x,y,z) MI_FORWARD(fun)
43 #define MI_FORWARD0(fun,x) MI_FORWARD(fun)
44 #define MI_FORWARD02(fun,x,y) MI_FORWARD(fun)
45#else
46 // otherwise use forwarding by calling our `mi_` function
47 #define MI_FORWARD1(fun,x) { return fun(x); }
48 #define MI_FORWARD2(fun,x,y) { return fun(x,y); }
49 #define MI_FORWARD3(fun,x,y,z) { return fun(x,y,z); }
50 #define MI_FORWARD0(fun,x) { fun(x); }
51 #define MI_FORWARD02(fun,x,y) { fun(x,y); }
52#endif
53
54
55#if defined(__APPLE__) && defined(MI_SHARED_LIB_EXPORT) && defined(MI_OSX_INTERPOSE)
56 // define MI_OSX_IS_INTERPOSED as we should not provide forwarding definitions for
57 // functions that are interposed (or the interposing does not work)
58 #define MI_OSX_IS_INTERPOSED
59
60 mi_decl_externc size_t mi_malloc_size_checked(void *p) {
61 if (!mi_is_in_heap_region(p)) return 0;
62 return mi_usable_size(p);
63 }
64
65 // use interposing so `DYLD_INSERT_LIBRARIES` works without `DYLD_FORCE_FLAT_NAMESPACE=1`
66 // See: <https://books.google.com/books?id=K8vUkpOXhN4C&pg=PA73>
67 struct mi_interpose_s {
68 const void* replacement;
69 const void* target;
70 };
71 #define MI_INTERPOSE_FUN(oldfun,newfun) { (const void*)&newfun, (const void*)&oldfun }
72 #define MI_INTERPOSE_MI(fun) MI_INTERPOSE_FUN(fun,mi_##fun)
73
74 __attribute__((used)) static struct mi_interpose_s _mi_interposes[] __attribute__((section("__DATA, __interpose"))) =
75 {
76 MI_INTERPOSE_MI(malloc),
77 MI_INTERPOSE_MI(calloc),
78 MI_INTERPOSE_MI(realloc),
79 MI_INTERPOSE_MI(strdup),
80 MI_INTERPOSE_MI(strndup),
81 MI_INTERPOSE_MI(realpath),
82 MI_INTERPOSE_MI(posix_memalign),
83 MI_INTERPOSE_MI(reallocf),
84 MI_INTERPOSE_MI(valloc),
85 MI_INTERPOSE_FUN(malloc_size,mi_malloc_size_checked),
86 MI_INTERPOSE_MI(malloc_good_size),
87 #if defined(MAC_OS_X_VERSION_10_15) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_15
88 MI_INTERPOSE_MI(aligned_alloc),
89 #endif
90 #ifdef MI_OSX_ZONE
91 // we interpose malloc_default_zone in alloc-override-osx.c so we can use mi_free safely
92 MI_INTERPOSE_MI(free),
93 MI_INTERPOSE_FUN(vfree,mi_free),
94 #else
95 // sometimes code allocates from default zone but deallocates using plain free :-( (like NxHashResizeToCapacity <https://github.com/nneonneo/osx-10.9-opensource/blob/master/objc4-551.1/runtime/hashtable2.mm>)
96 MI_INTERPOSE_FUN(free,mi_cfree), // use safe free that checks if pointers are from us
97 MI_INTERPOSE_FUN(vfree,mi_cfree),
98 #endif
99 };
100
101 #ifdef __cplusplus
102 extern "C" {
103 #endif
104 void _ZdlPv(void* p); // delete
105 void _ZdaPv(void* p); // delete[]
106 void _ZdlPvm(void* p, size_t n); // delete
107 void _ZdaPvm(void* p, size_t n); // delete[]
108 void* _Znwm(size_t n); // new
109 void* _Znam(size_t n); // new[]
110 void* _ZnwmRKSt9nothrow_t(size_t n, mi_nothrow_t tag); // new nothrow
111 void* _ZnamRKSt9nothrow_t(size_t n, mi_nothrow_t tag); // new[] nothrow
112 #ifdef __cplusplus
113 }
114 #endif
115 __attribute__((used)) static struct mi_interpose_s _mi_cxx_interposes[] __attribute__((section("__DATA, __interpose"))) =
116 {
117 MI_INTERPOSE_FUN(_ZdlPv,mi_free),
118 MI_INTERPOSE_FUN(_ZdaPv,mi_free),
119 MI_INTERPOSE_FUN(_ZdlPvm,mi_free_size),
120 MI_INTERPOSE_FUN(_ZdaPvm,mi_free_size),
121 MI_INTERPOSE_FUN(_Znwm,mi_new),
122 MI_INTERPOSE_FUN(_Znam,mi_new),
123 MI_INTERPOSE_FUN(_ZnwmRKSt9nothrow_t,mi_new_nothrow),
124 MI_INTERPOSE_FUN(_ZnamRKSt9nothrow_t,mi_new_nothrow),
125 };
126
127#elif defined(_MSC_VER)
128 // cannot override malloc unless using a dll.
129 // we just override new/delete which does work in a static library.
130#else
131 // On all other systems forward to our API
132 mi_decl_export void* malloc(size_t size) MI_FORWARD1(mi_malloc, size)
133 mi_decl_export void* calloc(size_t size, size_t n) MI_FORWARD2(mi_calloc, size, n)
134 mi_decl_export void* realloc(void* p, size_t newsize) MI_FORWARD2(mi_realloc, p, newsize)
135 mi_decl_export void free(void* p) MI_FORWARD0(mi_free, p)
136#endif
137
138#if (defined(__GNUC__) || defined(__clang__)) && !defined(__APPLE__)
139#pragma GCC visibility push(default)
140#endif
141
142// ------------------------------------------------------
143// Override new/delete
144// This is not really necessary as they usually call
145// malloc/free anyway, but it improves performance.
146// ------------------------------------------------------
147#ifdef __cplusplus
148 // ------------------------------------------------------
149 // With a C++ compiler we override the new/delete operators.
150 // see <https://en.cppreference.com/w/cpp/memory/new/operator_new>
151 // ------------------------------------------------------
152 #include <new>
153
154 #ifndef MI_OSX_IS_INTERPOSED
155 void operator delete(void* p) noexcept MI_FORWARD0(mi_free,p)
156 void operator delete[](void* p) noexcept MI_FORWARD0(mi_free,p)
157
158 void* operator new(std::size_t n) noexcept(false) MI_FORWARD1(mi_new,n)
159 void* operator new[](std::size_t n) noexcept(false) MI_FORWARD1(mi_new,n)
160
161 void* operator new (std::size_t n, const std::nothrow_t& tag) noexcept { MI_UNUSED(tag); return mi_new_nothrow(n); }
162 void* operator new[](std::size_t n, const std::nothrow_t& tag) noexcept { MI_UNUSED(tag); return mi_new_nothrow(n); }
163
164 #if (__cplusplus >= 201402L || _MSC_VER >= 1916)
165 void operator delete (void* p, std::size_t n) noexcept MI_FORWARD02(mi_free_size,p,n)
166 void operator delete[](void* p, std::size_t n) noexcept MI_FORWARD02(mi_free_size,p,n)
167 #endif
168 #endif
169
170 #if (__cplusplus > 201402L && defined(__cpp_aligned_new)) && (!defined(__GNUC__) || (__GNUC__ > 5))
171 void operator delete (void* p, std::align_val_t al) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
172 void operator delete[](void* p, std::align_val_t al) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
173 void operator delete (void* p, std::size_t n, std::align_val_t al) noexcept { mi_free_size_aligned(p, n, static_cast<size_t>(al)); };
174 void operator delete[](void* p, std::size_t n, std::align_val_t al) noexcept { mi_free_size_aligned(p, n, static_cast<size_t>(al)); };
175 void operator delete (void* p, std::align_val_t al, const std::nothrow_t&) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
176 void operator delete[](void* p, std::align_val_t al, const std::nothrow_t&) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
177
178 void* operator new( std::size_t n, std::align_val_t al) noexcept(false) { return mi_new_aligned(n, static_cast<size_t>(al)); }
179 void* operator new[]( std::size_t n, std::align_val_t al) noexcept(false) { return mi_new_aligned(n, static_cast<size_t>(al)); }
180 void* operator new (std::size_t n, std::align_val_t al, const std::nothrow_t&) noexcept { return mi_new_aligned_nothrow(n, static_cast<size_t>(al)); }
181 void* operator new[](std::size_t n, std::align_val_t al, const std::nothrow_t&) noexcept { return mi_new_aligned_nothrow(n, static_cast<size_t>(al)); }
182 #endif
183
184#elif (defined(__GNUC__) || defined(__clang__))
185 // ------------------------------------------------------
186 // Override by defining the mangled C++ names of the operators (as
187 // used by GCC and CLang).
188 // See <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling>
189 // ------------------------------------------------------
190
191 void _ZdlPv(void* p) MI_FORWARD0(mi_free,p) // delete
192 void _ZdaPv(void* p) MI_FORWARD0(mi_free,p) // delete[]
193 void _ZdlPvm(void* p, size_t n) MI_FORWARD02(mi_free_size,p,n)
194 void _ZdaPvm(void* p, size_t n) MI_FORWARD02(mi_free_size,p,n)
195 void _ZdlPvSt11align_val_t(void* p, size_t al) { mi_free_aligned(p,al); }
196 void _ZdaPvSt11align_val_t(void* p, size_t al) { mi_free_aligned(p,al); }
197 void _ZdlPvmSt11align_val_t(void* p, size_t n, size_t al) { mi_free_size_aligned(p,n,al); }
198 void _ZdaPvmSt11align_val_t(void* p, size_t n, size_t al) { mi_free_size_aligned(p,n,al); }
199
200 #if (MI_INTPTR_SIZE==8)
201 void* _Znwm(size_t n) MI_FORWARD1(mi_new,n) // new 64-bit
202 void* _Znam(size_t n) MI_FORWARD1(mi_new,n) // new[] 64-bit
203 void* _ZnwmRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_nothrow(n); }
204 void* _ZnamRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_nothrow(n); }
205 void* _ZnwmSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al)
206 void* _ZnamSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al)
207 void* _ZnwmSt11align_val_tRKSt9nothrow_t(size_t n, size_t al, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_aligned_nothrow(n,al); }
208 void* _ZnamSt11align_val_tRKSt9nothrow_t(size_t n, size_t al, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_aligned_nothrow(n,al); }
209 #elif (MI_INTPTR_SIZE==4)
210 void* _Znwj(size_t n) MI_FORWARD1(mi_new,n) // new 64-bit
211 void* _Znaj(size_t n) MI_FORWARD1(mi_new,n) // new[] 64-bit
212 void* _ZnwjRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_nothrow(n); }
213 void* _ZnajRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_nothrow(n); }
214 void* _ZnwjSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al)
215 void* _ZnajSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al)
216 void* _ZnwjSt11align_val_tRKSt9nothrow_t(size_t n, size_t al, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_aligned_nothrow(n,al); }
217 void* _ZnajSt11align_val_tRKSt9nothrow_t(size_t n, size_t al, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_aligned_nothrow(n,al); }
218 #else
219 #error "define overloads for new/delete for this platform (just for performance, can be skipped)"
220 #endif
221#endif // __cplusplus
222
223// ------------------------------------------------------
224// Further Posix & Unix functions definitions
225// ------------------------------------------------------
226
227#ifdef __cplusplus
228extern "C" {
229#endif
230
231#ifndef MI_OSX_IS_INTERPOSED
232 // Forward Posix/Unix calls as well
233 void* reallocf(void* p, size_t newsize) MI_FORWARD2(mi_reallocf,p,newsize)
234 size_t malloc_size(const void* p) MI_FORWARD1(mi_usable_size,p)
235 #if !defined(__ANDROID__) && !defined(__FreeBSD__)
236 size_t malloc_usable_size(void *p) MI_FORWARD1(mi_usable_size,p)
237 #else
238 size_t malloc_usable_size(const void *p) MI_FORWARD1(mi_usable_size,p)
239 #endif
240
241 // No forwarding here due to aliasing/name mangling issues
242 void* valloc(size_t size) { return mi_valloc(size); }
243 void vfree(void* p) { mi_free(p); }
244 size_t malloc_good_size(size_t size) { return mi_malloc_good_size(size); }
245 int posix_memalign(void** p, size_t alignment, size_t size) { return mi_posix_memalign(p, alignment, size); }
246
247 // `aligned_alloc` is only available when __USE_ISOC11 is defined.
248 // Note: it seems __USE_ISOC11 is not defined in musl (and perhaps other libc's) so we only check
249 // for it if using glibc.
250 // Note: Conda has a custom glibc where `aligned_alloc` is declared `static inline` and we cannot
251 // override it, but both _ISOC11_SOURCE and __USE_ISOC11 are undefined in Conda GCC7 or GCC9.
252 // Fortunately, in the case where `aligned_alloc` is declared as `static inline` it
253 // uses internally `memalign`, `posix_memalign`, or `_aligned_malloc` so we can avoid overriding it ourselves.
254 #if !defined(__GLIBC__) || __USE_ISOC11
255 void* aligned_alloc(size_t alignment, size_t size) { return mi_aligned_alloc(alignment, size); }
256 #endif
257#endif
258
259// no forwarding here due to aliasing/name mangling issues
260void cfree(void* p) { mi_free(p); }
261void* pvalloc(size_t size) { return mi_pvalloc(size); }
262void* reallocarray(void* p, size_t count, size_t size) { return mi_reallocarray(p, count, size); }
263int reallocarr(void* p, size_t count, size_t size) { return mi_reallocarr(p, count, size); }
264void* memalign(size_t alignment, size_t size) { return mi_memalign(alignment, size); }
265void* _aligned_malloc(size_t alignment, size_t size) { return mi_aligned_alloc(alignment, size); }
266
267#if defined(__wasi__)
268 // forward __libc interface (see PR #667)
269 void* __libc_malloc(size_t size) MI_FORWARD1(mi_malloc, size)
270 void* __libc_calloc(size_t count, size_t size) MI_FORWARD2(mi_calloc, count, size)
271 void* __libc_realloc(void* p, size_t size) MI_FORWARD2(mi_realloc, p, size)
272 void __libc_free(void* p) MI_FORWARD0(mi_free, p)
273 void* __libc_memalign(size_t alignment, size_t size) { return mi_memalign(alignment, size); }
274
275#elif defined(__GLIBC__) && defined(__linux__)
276 // forward __libc interface (needed for glibc-based Linux distributions)
277 void* __libc_malloc(size_t size) MI_FORWARD1(mi_malloc,size)
278 void* __libc_calloc(size_t count, size_t size) MI_FORWARD2(mi_calloc,count,size)
279 void* __libc_realloc(void* p, size_t size) MI_FORWARD2(mi_realloc,p,size)
280 void __libc_free(void* p) MI_FORWARD0(mi_free,p)
281 void __libc_cfree(void* p) MI_FORWARD0(mi_free,p)
282
283 void* __libc_valloc(size_t size) { return mi_valloc(size); }
284 void* __libc_pvalloc(size_t size) { return mi_pvalloc(size); }
285 void* __libc_memalign(size_t alignment, size_t size) { return mi_memalign(alignment,size); }
286 int __posix_memalign(void** p, size_t alignment, size_t size) { return mi_posix_memalign(p,alignment,size); }
287#endif
288
289#ifdef __cplusplus
290}
291#endif
292
293#if (defined(__GNUC__) || defined(__clang__)) && !defined(__APPLE__)
294#pragma GCC visibility pop
295#endif
296
297#endif // MI_MALLOC_OVERRIDE && !_WIN32