mimalloc icon indicating copy to clipboard operation
mimalloc copied to clipboard

Static override of malloc-like functions on Windows

Open Noxybot opened this issue 8 months ago • 1 comments

Hi @daanx, just a general question - why mimalloc doesn't provide overrides for malloc-like function on Windows when built as an object file. Specifically, this code block:

#elif defined(_MSC_VER)
  // cannot override malloc unless using a dll.
  // we just override new/delete which does work in a static library.
#else

Could override publicly available functions from ucrt/corecrt_malloc.h, like

_Check_return_ _Ret_maybenull_ _Post_writable_byte_size_(_Size)
_ACRTIMP _CRTALLOCATOR _CRT_JIT_INTRINSIC _CRTRESTRICT _CRT_HYBRIDPATCHABLE
void* __cdecl malloc(
    _In_ _CRT_GUARDOVERFLOW size_t _Size
    )
{
    return mi_malloc(_Size);
}

Any caveats in such approach? It seems to work just fine in our scenario. (When we link mimalloc as an object file + using static CRT) Thanks!

Noxybot avatar Apr 14 '25 22:04 Noxybot

Example: https://github.com/microsoft/mimalloc/pull/1075

Noxybot avatar Apr 15 '25 04:04 Noxybot