microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
92f542e70a6ca4eadffcee9e2e42c59c8f02df49

Branches

Tags

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

Clone

HTTPS

Download ZIP

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

316lines · modeblame

91589c3aIan Davis2 years ago1/* ----------------------------------------------------------------------------
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
ce21e681Ian Davis1 years ago26typedef void* mi_nothrow_t;
91589c3aIan Davis2 years ago27
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
60mi_decl_externc size_t mi_malloc_size_checked(void *p) {
61if (!mi_is_in_heap_region(p)) return 0;
62return 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>
67struct mi_interpose_s {
68const void* replacement;
69const 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
ce21e681Ian Davis1 years ago74#define MI_INTERPOSE_DECLS(name) __attribute__((used)) static struct mi_interpose_s name[] __attribute__((section("__DATA, __interpose")))
75
76MI_INTERPOSE_DECLS(_mi_interposes) =
91589c3aIan Davis2 years ago77{
78MI_INTERPOSE_MI(malloc),
79MI_INTERPOSE_MI(calloc),
80MI_INTERPOSE_MI(realloc),
81MI_INTERPOSE_MI(strdup),
82MI_INTERPOSE_MI(realpath),
83MI_INTERPOSE_MI(posix_memalign),
84MI_INTERPOSE_MI(reallocf),
85MI_INTERPOSE_MI(valloc),
86MI_INTERPOSE_FUN(malloc_size,mi_malloc_size_checked),
87MI_INTERPOSE_MI(malloc_good_size),
88#ifdef MI_OSX_ZONE
89// we interpose malloc_default_zone in alloc-override-osx.c so we can use mi_free safely
90MI_INTERPOSE_MI(free),
91MI_INTERPOSE_FUN(vfree,mi_free),
92#else
93// 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>)
94MI_INTERPOSE_FUN(free,mi_cfree), // use safe free that checks if pointers are from us
95MI_INTERPOSE_FUN(vfree,mi_cfree),
96#endif
97};
ce21e681Ian Davis1 years ago98MI_INTERPOSE_DECLS(_mi_interposes_10_7) __OSX_AVAILABLE(10.7) = {
99MI_INTERPOSE_MI(strndup),
100};
101MI_INTERPOSE_DECLS(_mi_interposes_10_15) __OSX_AVAILABLE(10.15) = {
102MI_INTERPOSE_MI(aligned_alloc),
103};
91589c3aIan Davis2 years ago104
105#ifdef __cplusplus
106extern "C" {
107#endif
108void _ZdlPv(void* p); // delete
109void _ZdaPv(void* p); // delete[]
110void _ZdlPvm(void* p, size_t n); // delete
111void _ZdaPvm(void* p, size_t n); // delete[]
112void* _Znwm(size_t n); // new
113void* _Znam(size_t n); // new[]
114void* _ZnwmRKSt9nothrow_t(size_t n, mi_nothrow_t tag); // new nothrow
115void* _ZnamRKSt9nothrow_t(size_t n, mi_nothrow_t tag); // new[] nothrow
116#ifdef __cplusplus
117}
118#endif
119__attribute__((used)) static struct mi_interpose_s _mi_cxx_interposes[] __attribute__((section("__DATA, __interpose"))) =
120{
121MI_INTERPOSE_FUN(_ZdlPv,mi_free),
122MI_INTERPOSE_FUN(_ZdaPv,mi_free),
123MI_INTERPOSE_FUN(_ZdlPvm,mi_free_size),
124MI_INTERPOSE_FUN(_ZdaPvm,mi_free_size),
125MI_INTERPOSE_FUN(_Znwm,mi_new),
126MI_INTERPOSE_FUN(_Znam,mi_new),
127MI_INTERPOSE_FUN(_ZnwmRKSt9nothrow_t,mi_new_nothrow),
128MI_INTERPOSE_FUN(_ZnamRKSt9nothrow_t,mi_new_nothrow),
129};
130
131#elif defined(_MSC_VER)
132// cannot override malloc unless using a dll.
133// we just override new/delete which does work in a static library.
134#else
ce21e681Ian Davis1 years ago135// On all other systems forward allocation primitives to our API
91589c3aIan Davis2 years ago136mi_decl_export void* malloc(size_t size) MI_FORWARD1(mi_malloc, size)
137mi_decl_export void* calloc(size_t size, size_t n) MI_FORWARD2(mi_calloc, size, n)
138mi_decl_export void* realloc(void* p, size_t newsize) MI_FORWARD2(mi_realloc, p, newsize)
ce21e681Ian Davis1 years ago139mi_decl_export void free(void* p) MI_FORWARD0(mi_free, p)
140// In principle we do not need to forward `strdup`/`strndup` but on some systems these do not use `malloc` internally (but a more primitive call)
141// We only override if `strdup` is not a macro (as on some older libc's, see issue #885)
142#if !defined(strdup)
143mi_decl_export char* strdup(const char* str) MI_FORWARD1(mi_strdup, str)
144#endif
145#if !defined(strndup) && (!defined(__APPLE__) || (defined(MAC_OS_X_VERSION_10_7) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7))
146mi_decl_export char* strndup(const char* str, size_t n) MI_FORWARD2(mi_strndup, str, n)
147#endif
91589c3aIan Davis2 years ago148#endif
149
150#if (defined(__GNUC__) || defined(__clang__)) && !defined(__APPLE__)
151#pragma GCC visibility push(default)
152#endif
153
154// ------------------------------------------------------
155// Override new/delete
156// This is not really necessary as they usually call
157// malloc/free anyway, but it improves performance.
158// ------------------------------------------------------
159#ifdef __cplusplus
160// ------------------------------------------------------
161// With a C++ compiler we override the new/delete operators.
162// see <https://en.cppreference.com/w/cpp/memory/new/operator_new>
163// ------------------------------------------------------
164#include <new>
165
166#ifndef MI_OSX_IS_INTERPOSED
167void operator delete(void* p) noexcept MI_FORWARD0(mi_free,p)
168void operator delete[](void* p) noexcept MI_FORWARD0(mi_free,p)
169
170void* operator new(std::size_t n) noexcept(false) MI_FORWARD1(mi_new,n)
171void* operator new[](std::size_t n) noexcept(false) MI_FORWARD1(mi_new,n)
172
173void* operator new (std::size_t n, const std::nothrow_t& tag) noexcept { MI_UNUSED(tag); return mi_new_nothrow(n); }
174void* operator new[](std::size_t n, const std::nothrow_t& tag) noexcept { MI_UNUSED(tag); return mi_new_nothrow(n); }
175
176#if (__cplusplus >= 201402L || _MSC_VER >= 1916)
177void operator delete (void* p, std::size_t n) noexcept MI_FORWARD02(mi_free_size,p,n)
178void operator delete[](void* p, std::size_t n) noexcept MI_FORWARD02(mi_free_size,p,n)
179#endif
180#endif
181
182#if (__cplusplus > 201402L && defined(__cpp_aligned_new)) && (!defined(__GNUC__) || (__GNUC__ > 5))
183void operator delete (void* p, std::align_val_t al) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
184void operator delete[](void* p, std::align_val_t al) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
185void 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)); };
186void 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)); };
187void operator delete (void* p, std::align_val_t al, const std::nothrow_t&) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
188void operator delete[](void* p, std::align_val_t al, const std::nothrow_t&) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
189
190void* operator new( std::size_t n, std::align_val_t al) noexcept(false) { return mi_new_aligned(n, static_cast<size_t>(al)); }
191void* operator new[]( std::size_t n, std::align_val_t al) noexcept(false) { return mi_new_aligned(n, static_cast<size_t>(al)); }
192void* 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)); }
193void* 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)); }
194#endif
195
196#elif (defined(__GNUC__) || defined(__clang__))
197// ------------------------------------------------------
198// Override by defining the mangled C++ names of the operators (as
199// used by GCC and CLang).
200// See <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling>
201// ------------------------------------------------------
202
203void _ZdlPv(void* p) MI_FORWARD0(mi_free,p) // delete
204void _ZdaPv(void* p) MI_FORWARD0(mi_free,p) // delete[]
205void _ZdlPvm(void* p, size_t n) MI_FORWARD02(mi_free_size,p,n)
206void _ZdaPvm(void* p, size_t n) MI_FORWARD02(mi_free_size,p,n)
ce21e681Ian Davis1 years ago207
91589c3aIan Davis2 years ago208void _ZdlPvSt11align_val_t(void* p, size_t al) { mi_free_aligned(p,al); }
209void _ZdaPvSt11align_val_t(void* p, size_t al) { mi_free_aligned(p,al); }
210void _ZdlPvmSt11align_val_t(void* p, size_t n, size_t al) { mi_free_size_aligned(p,n,al); }
211void _ZdaPvmSt11align_val_t(void* p, size_t n, size_t al) { mi_free_size_aligned(p,n,al); }
212
ce21e681Ian Davis1 years ago213void _ZdlPvRKSt9nothrow_t(void* p, mi_nothrow_t tag) { MI_UNUSED(tag); mi_free(p); } // operator delete(void*, std::nothrow_t const&)
214void _ZdaPvRKSt9nothrow_t(void* p, mi_nothrow_t tag) { MI_UNUSED(tag); mi_free(p); } // operator delete[](void*, std::nothrow_t const&)
215void _ZdlPvSt11align_val_tRKSt9nothrow_t(void* p, size_t al, mi_nothrow_t tag) { MI_UNUSED(tag); mi_free_aligned(p,al); } // operator delete(void*, std::align_val_t, std::nothrow_t const&)
216void _ZdaPvSt11align_val_tRKSt9nothrow_t(void* p, size_t al, mi_nothrow_t tag) { MI_UNUSED(tag); mi_free_aligned(p,al); } // operator delete[](void*, std::align_val_t, std::nothrow_t const&)
217
91589c3aIan Davis2 years ago218#if (MI_INTPTR_SIZE==8)
219void* _Znwm(size_t n) MI_FORWARD1(mi_new,n) // new 64-bit
220void* _Znam(size_t n) MI_FORWARD1(mi_new,n) // new[] 64-bit
221void* _ZnwmRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_nothrow(n); }
222void* _ZnamRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_nothrow(n); }
223void* _ZnwmSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al)
224void* _ZnamSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al)
225void* _ZnwmSt11align_val_tRKSt9nothrow_t(size_t n, size_t al, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_aligned_nothrow(n,al); }
226void* _ZnamSt11align_val_tRKSt9nothrow_t(size_t n, size_t al, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_aligned_nothrow(n,al); }
227#elif (MI_INTPTR_SIZE==4)
228void* _Znwj(size_t n) MI_FORWARD1(mi_new,n) // new 64-bit
229void* _Znaj(size_t n) MI_FORWARD1(mi_new,n) // new[] 64-bit
230void* _ZnwjRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_nothrow(n); }
231void* _ZnajRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_nothrow(n); }
232void* _ZnwjSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al)
233void* _ZnajSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al)
234void* _ZnwjSt11align_val_tRKSt9nothrow_t(size_t n, size_t al, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_aligned_nothrow(n,al); }
235void* _ZnajSt11align_val_tRKSt9nothrow_t(size_t n, size_t al, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_aligned_nothrow(n,al); }
236#else
237#error "define overloads for new/delete for this platform (just for performance, can be skipped)"
238#endif
239#endif // __cplusplus
240
241// ------------------------------------------------------
242// Further Posix & Unix functions definitions
243// ------------------------------------------------------
244
245#ifdef __cplusplus
246extern "C" {
247#endif
248
249#ifndef MI_OSX_IS_INTERPOSED
250// Forward Posix/Unix calls as well
251void* reallocf(void* p, size_t newsize) MI_FORWARD2(mi_reallocf,p,newsize)
252size_t malloc_size(const void* p) MI_FORWARD1(mi_usable_size,p)
ce21e681Ian Davis1 years ago253#if !defined(__ANDROID__) && !defined(__FreeBSD__) && !defined(__DragonFly__)
91589c3aIan Davis2 years ago254size_t malloc_usable_size(void *p) MI_FORWARD1(mi_usable_size,p)
255#else
256size_t malloc_usable_size(const void *p) MI_FORWARD1(mi_usable_size,p)
257#endif
258
259// No forwarding here due to aliasing/name mangling issues
260void* valloc(size_t size) { return mi_valloc(size); }
261void vfree(void* p) { mi_free(p); }
262size_t malloc_good_size(size_t size) { return mi_malloc_good_size(size); }
263int posix_memalign(void** p, size_t alignment, size_t size) { return mi_posix_memalign(p, alignment, size); }
264
265// `aligned_alloc` is only available when __USE_ISOC11 is defined.
266// Note: it seems __USE_ISOC11 is not defined in musl (and perhaps other libc's) so we only check
267// for it if using glibc.
268// Note: Conda has a custom glibc where `aligned_alloc` is declared `static inline` and we cannot
269// override it, but both _ISOC11_SOURCE and __USE_ISOC11 are undefined in Conda GCC7 or GCC9.
270// Fortunately, in the case where `aligned_alloc` is declared as `static inline` it
271// uses internally `memalign`, `posix_memalign`, or `_aligned_malloc` so we can avoid overriding it ourselves.
272#if !defined(__GLIBC__) || __USE_ISOC11
273void* aligned_alloc(size_t alignment, size_t size) { return mi_aligned_alloc(alignment, size); }
274#endif
275#endif
276
277// no forwarding here due to aliasing/name mangling issues
278void cfree(void* p) { mi_free(p); }
279void* pvalloc(size_t size) { return mi_pvalloc(size); }
280void* memalign(size_t alignment, size_t size) { return mi_memalign(alignment, size); }
281void* _aligned_malloc(size_t alignment, size_t size) { return mi_aligned_alloc(alignment, size); }
ce21e681Ian Davis1 years ago282void* reallocarray(void* p, size_t count, size_t size) { return mi_reallocarray(p, count, size); }
283// some systems define reallocarr so mark it as a weak symbol (#751)
284mi_decl_weak int reallocarr(void* p, size_t count, size_t size) { return mi_reallocarr(p, count, size); }
91589c3aIan Davis2 years ago285
286#if defined(__wasi__)
287// forward __libc interface (see PR #667)
288void* __libc_malloc(size_t size) MI_FORWARD1(mi_malloc, size)
289void* __libc_calloc(size_t count, size_t size) MI_FORWARD2(mi_calloc, count, size)
290void* __libc_realloc(void* p, size_t size) MI_FORWARD2(mi_realloc, p, size)
291void __libc_free(void* p) MI_FORWARD0(mi_free, p)
292void* __libc_memalign(size_t alignment, size_t size) { return mi_memalign(alignment, size); }
293
ce21e681Ian Davis1 years ago294#elif defined(__linux__)
295// forward __libc interface (needed for glibc-based and musl-based Linux distributions)
91589c3aIan Davis2 years ago296void* __libc_malloc(size_t size) MI_FORWARD1(mi_malloc,size)
297void* __libc_calloc(size_t count, size_t size) MI_FORWARD2(mi_calloc,count,size)
298void* __libc_realloc(void* p, size_t size) MI_FORWARD2(mi_realloc,p,size)
299void __libc_free(void* p) MI_FORWARD0(mi_free,p)
300void __libc_cfree(void* p) MI_FORWARD0(mi_free,p)
301
302void* __libc_valloc(size_t size) { return mi_valloc(size); }
303void* __libc_pvalloc(size_t size) { return mi_pvalloc(size); }
304void* __libc_memalign(size_t alignment, size_t size) { return mi_memalign(alignment,size); }
305int __posix_memalign(void** p, size_t alignment, size_t size) { return mi_posix_memalign(p,alignment,size); }
306#endif
307
308#ifdef __cplusplus
309}
310#endif
311
312#if (defined(__GNUC__) || defined(__clang__)) && !defined(__APPLE__)
313#pragma GCC visibility pop
314#endif
315
316#endif // MI_MALLOC_OVERRIDE && !_WIN32