Writing a Memory Allocator (2019)
dmitrysoshnikov.com
dmitrysoshnikov.com
You can build a jemalloc in about 10 lines roughly.
And if you want, you know, performance, it needs to know something about threads, pre-allocating chunks to assign to threads, and batching free ops per thread.
Most of what you can find about writing allocators is written by people who don't actually know how; and actually useful allocators are made by people who don't write tutorials about it.
What happens with the object afterwards is not relevant.
Edit: wrong, I re-checked. The solution to this is that marking an object as free is lock-free. The freed objects do not immediately change thread pools.
Using an anonymous map provides address space randomization by default (for the raw pool location), whereas brk/sbrk extends a limit and because of this are predictable.
you may implement custom sbrk via mmap easily
It's quite simple and extensively commented.
[0] https://web.archive.org/web/20190126213344/https://www.drdob...
[1] https://github.com/dlang/dmd/blob/025caed79/src/dmd/root/rme...
This is one of the reasons why people write custom allocators - to suit a specific purpose where the allocation pattern is known well in advance.
Debugging it is great fun as well.
Toying with allocators is one of my favorite programming pastimes for some reason.