by pizzabearman · 5y ago · 0 comments
On windows I'd expect the default allocator to be a black box, but I might be wrong.
For garbage collection I strongly recommend this book (on top of the source code of your gc if available!) https://gchandbook.org/
The UCRT is at least "source available" on Windows, up to a point, and distributed with the Windows SDK. The release heap codepath is a bit boring:
malloc: C:\Program Files (x86)\Windows Kits\10\Source\10.0.19041.0\ucrt\heap\malloc.cpp
_malloc_base: C:\Program Files (x86)\Windows Kits\10\Source\10.0.19041.0\ucrt\heap\malloc_base.cpp
HeapAlloc: (kernel32.dll alias for ntdll.dll!RtlAllocateHeap() on my machine)
The debug codepath is a bit more interesting: malloc: C:\Program Files (x86)\Windows Kits\10\Source\10.0.19041.0\ucrt\heap\malloc.cpp
_malloc_dbg: C:\Program Files (x86)\Windows Kits\10\Source\10.0.19041.0\ucrt\heap\debug_heap.cpp
heap_alloc_dbg: C:\Program Files (x86)\Windows Kits\10\Source\10.0.19041.0\ucrt\heap\debug_heap.cpp
heap_alloc_dbg_internal: C:\Program Files (x86)\Windows Kits\10\Source\10.0.19041.0\ucrt\heap\debug_heap.cpp
HeapAlloc
HeapAlloc itself is a bit more of a black box (AFAIK), and contains a lot of the fun details about the actual process of heap allocation - although there's a bunch of hooks, debug functions, documentation, articles, alternative implementations (ReactOS), etc.It's trivial to replace malloc/free with my_malloc/my_free - and integrating libraries that replace malloc/free as-is without renaming also tends to be straightforward. In C++, you can overload new/delete to use my_* with little hassle, or placement new to instantiate classes on previously allocated memory directly.
Meanwhile, C# and Java provide absolutely no means of creating instances of their classes via anything other than their built-in GCs. You can't just distribute a .exe or .jar with a replaced GC - instead, you need to create/distribute/install an entirely new runtime, and even that doesn't really provide any sane means of having multiple GCs living side by side. This is all theoretically technically possible, but orders of magnitude more work.