Skip to content

Commit 1dfb5b6

Browse files
authored
Merge pull request #1075 from Noxybot/win_static_overrides
add support for static override on Windows
2 parents af8044b + 09e73bf commit 1dfb5b6

File tree

3 files changed

+209
-15
lines changed

3 files changed

+209
-15
lines changed

CMakeLists.txt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -771,13 +771,10 @@ if (MI_OVERRIDE)
771771
if (MI_BUILD_SHARED)
772772
target_compile_definitions(mimalloc PRIVATE MI_MALLOC_OVERRIDE)
773773
endif()
774-
if(NOT WIN32)
775-
# It is only possible to override malloc on Windows when building as a DLL.
776-
if (MI_BUILD_STATIC)
777-
target_compile_definitions(mimalloc-static PRIVATE MI_MALLOC_OVERRIDE)
778-
endif()
779-
if (MI_BUILD_OBJECT)
780-
target_compile_definitions(mimalloc-obj PRIVATE MI_MALLOC_OVERRIDE)
781-
endif()
774+
if (MI_BUILD_STATIC)
775+
target_compile_definitions(mimalloc-static PRIVATE MI_MALLOC_OVERRIDE)
776+
endif()
777+
if (MI_BUILD_OBJECT)
778+
target_compile_definitions(mimalloc-obj PRIVATE MI_MALLOC_OVERRIDE)
782779
endif()
783780
endif()

src/alloc-override.c

Lines changed: 195 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@ terms of the MIT license. A copy of the license can be found in the file
99
#error "this file should be included from 'alloc.c' (so aliases can work)"
1010
#endif
1111

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
1512

16-
#if defined(MI_MALLOC_OVERRIDE) && !(defined(_WIN32))
13+
#if defined(MI_MALLOC_OVERRIDE) && !defined(_DLL)
1714

1815
#if defined(__APPLE__)
1916
#include <AvailabilityMacros.h>
@@ -129,8 +126,197 @@ typedef void* mi_nothrow_t;
129126
};
130127

131128
#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.
129+
_Check_return_ _Ret_maybenull_ _Post_writable_byte_size_(_Size)
130+
_ACRTIMP _CRTALLOCATOR
131+
void* __cdecl _expand(
132+
_Pre_notnull_ void* _Block,
133+
_In_ _CRT_GUARDOVERFLOW size_t _Size
134+
)
135+
{
136+
return mi_expand(_Block, _Size);
137+
}
138+
_Check_return_
139+
_ACRTIMP
140+
size_t __cdecl _msize_base(
141+
_Pre_notnull_ void* _Block
142+
) _CRT_NOEXCEPT
143+
{
144+
return mi_malloc_size(_Block);
145+
}
146+
_Check_return_
147+
_ACRTIMP _CRT_HYBRIDPATCHABLE
148+
size_t __cdecl _msize(
149+
_Pre_notnull_ void* _Block
150+
)
151+
{
152+
return mi_malloc_size(_Block);
153+
}
154+
_ACRTIMP
155+
void __cdecl _free_base(
156+
_Pre_maybenull_ _Post_invalid_ void* _Block
157+
)
158+
{
159+
mi_free(_Block);
160+
}
161+
_ACRTIMP _CRT_HYBRIDPATCHABLE
162+
void __cdecl free(
163+
_Pre_maybenull_ _Post_invalid_ void* _Block
164+
)
165+
{
166+
mi_free(_Block);
167+
}
168+
_Check_return_ _Ret_maybenull_ _Post_writable_byte_size_(_Size)
169+
_ACRTIMP _CRTALLOCATOR _CRT_JIT_INTRINSIC _CRTRESTRICT _CRT_HYBRIDPATCHABLE
170+
void* __cdecl malloc(
171+
_In_ _CRT_GUARDOVERFLOW size_t _Size
172+
)
173+
{
174+
return mi_malloc(_Size);
175+
}
176+
_Check_return_ _Ret_maybenull_ _Post_writable_byte_size_(_Size)
177+
_ACRTIMP _CRTALLOCATOR _CRTRESTRICT
178+
void* __cdecl _malloc_base(
179+
_In_ size_t _Size
180+
)
181+
{
182+
return mi_malloc(_Size);
183+
}
184+
_Success_(return != 0) _Check_return_ _Ret_maybenull_ _Post_writable_byte_size_(_Size)
185+
_ACRTIMP _CRTALLOCATOR _CRTRESTRICT
186+
void* __cdecl _realloc_base(
187+
_Pre_maybenull_ _Post_invalid_ void* _Block,
188+
_In_ size_t _Size
189+
)
190+
{
191+
return mi_realloc(_Block, _Size);
192+
}
193+
_Success_(return != 0) _Check_return_ _Ret_maybenull_ _Post_writable_byte_size_(_Size)
194+
_ACRTIMP _CRTALLOCATOR _CRTRESTRICT _CRT_HYBRIDPATCHABLE
195+
void* __cdecl realloc(
196+
_Pre_maybenull_ _Post_invalid_ void* _Block,
197+
_In_ _CRT_GUARDOVERFLOW size_t _Size
198+
)
199+
{
200+
return mi_realloc(_Block, _Size);
201+
}
202+
_Check_return_ _Ret_maybenull_ _Post_writable_byte_size_(_Count * _Size)
203+
_ACRTIMP _CRTALLOCATOR _CRTRESTRICT
204+
void* __cdecl _calloc_base(
205+
_In_ size_t _Count,
206+
_In_ size_t _Size
207+
)
208+
{
209+
return mi_calloc(_Count, _Size);
210+
}
211+
_Check_return_ _Ret_maybenull_ _Post_writable_byte_size_(_Count * _Size)
212+
_ACRTIMP _CRT_JIT_INTRINSIC _CRTALLOCATOR _CRTRESTRICT
213+
void* __cdecl calloc(
214+
_In_ _CRT_GUARDOVERFLOW size_t _Count,
215+
_In_ _CRT_GUARDOVERFLOW size_t _Size
216+
)
217+
{
218+
return mi_calloc(_Count, _Size);
219+
}
220+
_Success_(return != 0) _Check_return_ _Ret_maybenull_ _Post_writable_byte_size_(_Count * _Size)
221+
_ACRTIMP _CRTALLOCATOR _CRTRESTRICT
222+
void* __cdecl _recalloc_base(
223+
_Pre_maybenull_ _Post_invalid_ void* _Block,
224+
_In_ size_t _Count,
225+
_In_ size_t _Size
226+
)
227+
{
228+
return mi_recalloc(_Block, _Count, _Size);
229+
}
230+
_Success_(return != 0) _Check_return_ _Ret_maybenull_ _Post_writable_byte_size_(_Count * _Size)
231+
_ACRTIMP _CRTALLOCATOR _CRTRESTRICT
232+
void* __cdecl _recalloc(
233+
_Pre_maybenull_ _Post_invalid_ void* _Block,
234+
_In_ _CRT_GUARDOVERFLOW size_t _Count,
235+
_In_ _CRT_GUARDOVERFLOW size_t _Size
236+
)
237+
{
238+
return mi_recalloc(_Block, _Count, _Size);
239+
}
240+
_ACRTIMP
241+
void __cdecl _aligned_free(
242+
_Pre_maybenull_ _Post_invalid_ void* _Block
243+
)
244+
{
245+
mi_free(_Block);
246+
}
247+
_Check_return_ _Ret_maybenull_ _Post_writable_byte_size_(_Size)
248+
_ACRTIMP _CRTALLOCATOR _CRTRESTRICT
249+
void* __cdecl _aligned_malloc(
250+
_In_ _CRT_GUARDOVERFLOW size_t _Size,
251+
_In_ size_t _Alignment
252+
)
253+
{
254+
return mi_malloc_aligned(_Size, _Alignment);
255+
}
256+
_Check_return_ _Ret_maybenull_ _Post_writable_byte_size_(_Size)
257+
_ACRTIMP _CRTALLOCATOR _CRTRESTRICT
258+
void* __cdecl _aligned_offset_malloc(
259+
_In_ _CRT_GUARDOVERFLOW size_t _Size,
260+
_In_ size_t _Alignment,
261+
_In_ size_t _Offset
262+
)
263+
{
264+
return mi_malloc_aligned_at(_Size, _Alignment, _Offset);
265+
}
266+
_Check_return_
267+
_ACRTIMP
268+
size_t __cdecl _aligned_msize(
269+
_Pre_notnull_ void* _Block,
270+
_In_ size_t _Alignment,
271+
_In_ size_t _Offset
272+
)
273+
{
274+
return mi_malloc_size(_Block);
275+
}
276+
_Success_(return != 0) _Check_return_ _Ret_maybenull_ _Post_writable_byte_size_(_Size)
277+
_ACRTIMP _CRTALLOCATOR _CRTRESTRICT
278+
void* __cdecl _aligned_offset_realloc(
279+
_Pre_maybenull_ _Post_invalid_ void* _Block,
280+
_In_ _CRT_GUARDOVERFLOW size_t _Size,
281+
_In_ size_t _Alignment,
282+
_In_ size_t _Offset
283+
)
284+
{
285+
return mi_realloc_aligned_at(_Block, _Size, _Alignment, _Offset);
286+
}
287+
_Success_(return != 0) _Check_return_ _Ret_maybenull_ _Post_writable_byte_size_(_Count * _Size)
288+
_ACRTIMP _CRTALLOCATOR _CRTRESTRICT
289+
void* __cdecl _aligned_offset_recalloc(
290+
_Pre_maybenull_ _Post_invalid_ void* _Block,
291+
_In_ _CRT_GUARDOVERFLOW size_t _Count,
292+
_In_ _CRT_GUARDOVERFLOW size_t _Size,
293+
_In_ size_t _Alignment,
294+
_In_ size_t _Offset
295+
)
296+
{
297+
return mi_recalloc_aligned_at(_Block, _Count, _Size, _Alignment, _Offset);
298+
}
299+
_Success_(return != 0) _Check_return_ _Ret_maybenull_ _Post_writable_byte_size_(_Size)
300+
_ACRTIMP _CRTALLOCATOR _CRTRESTRICT
301+
void* __cdecl _aligned_realloc(
302+
_Pre_maybenull_ _Post_invalid_ void* _Block,
303+
_In_ _CRT_GUARDOVERFLOW size_t _Size,
304+
_In_ size_t _Alignment
305+
)
306+
{
307+
return mi_realloc_aligned(_Block, _Size, _Alignment);
308+
}
309+
_Success_(return != 0) _Check_return_ _Ret_maybenull_ _Post_writable_byte_size_(_Count * _Size)
310+
_ACRTIMP _CRTALLOCATOR _CRTRESTRICT
311+
void* __cdecl _aligned_recalloc(
312+
_Pre_maybenull_ _Post_invalid_ void* _Block,
313+
_In_ _CRT_GUARDOVERFLOW size_t _Count,
314+
_In_ _CRT_GUARDOVERFLOW size_t _Size,
315+
_In_ size_t _Alignment
316+
)
317+
{
318+
return mi_recalloc_aligned(_Block, _Count, _Size, _Alignment);
319+
}
134320
#else
135321
// On all other systems forward allocation primitives to our API
136322
mi_decl_export void* malloc(size_t size) MI_FORWARD1(mi_malloc, size)
@@ -278,7 +464,9 @@ extern "C" {
278464
void cfree(void* p) { mi_free(p); }
279465
void* pvalloc(size_t size) { return mi_pvalloc(size); }
280466
void* memalign(size_t alignment, size_t size) { return mi_memalign(alignment, size); }
467+
#if !defined(_WIN32)
281468
void* _aligned_malloc(size_t alignment, size_t size) { return mi_aligned_alloc(alignment, size); }
469+
#endif
282470
void* reallocarray(void* p, size_t count, size_t size) { return mi_reallocarray(p, count, size); }
283471
// some systems define reallocarr so mark it as a weak symbol (#751)
284472
mi_decl_weak int reallocarr(void* p, size_t count, size_t size) { return mi_reallocarr(p, count, size); }
@@ -313,4 +501,4 @@ mi_decl_weak int reallocarr(void* p, size_t count, size_t size) { return mi_r
313501
#pragma GCC visibility pop
314502
#endif
315503

316-
#endif // MI_MALLOC_OVERRIDE && !_WIN32
504+
#endif // MI_MALLOC_OVERRIDE

test/main-override.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,16 @@
88
int main() {
99
mi_version(); // ensure mimalloc library is linked
1010
void* p1 = malloc(78);
11+
if (!mi_is_in_heap_region(p1)) {
12+
printf("p1: malloc failed to allocate in heap region\n");
13+
return 1;
14+
}
15+
1116
void* p2 = malloc(24);
17+
if (!mi_is_in_heap_region(p2)) {
18+
printf("p2: malloc failed to allocate in heap region\n");
19+
return 1;
20+
}
1221
free(p1);
1322
p1 = malloc(8);
1423
//char* s = strdup("hello\n");

0 commit comments

Comments
 (0)