Let's Write a Malloc (2014)
danluu.com
danluu.com
It would have been nice to include a list of the types of changes that a modern malloc would make to this basic structure to make it faster or fix bugs.
https://journal.stuffwithstuff.com/2013/12/08/babys-first-ga...
As someone who really enjoys reinventing the wheel for fun, the final words are something of a mantra for me:
> Let me stress here that while this collector is simple, it isn’t a toy.
> There are a ton of optimizations you can build on top of this—in GCs and programming languages, optimization is 90% of the effort—but the core code here is a legitimate real GC.
> It’s very similar to the collectors that were in Ruby and Lua until recently.
> You can ship production code that uses something exactly like this.
> Now go build something awesome!
The results from past years were public and included the test case names like "PowersOfTwo" or "Primes". So instead of a generic allocator some of us optimized for the test suite :)
I managed to get one of the all time high scores by implementing a variable length integer encoding scheme that could fit powers of two, primes and other "interesting" numbers up to some size into one or two bytes.
It is very useful to at least scan through this before reading this post and have it in front of you as a reference.
[0] https://wiki-prog.infoprepa.epita.fr/images/0/04/Malloc_tuto... -- for example here; the link in the post is stale.
One of the biggest dangerous for the link's approach is partial interception, leading to conflicting libraries being used.
I collected some of my thoughts about allocation in a gist (originally on Reddit before it imploded): https://gist.github.com/o11c/6b08643335388bbab0228db763f9921...
(I didn't take that class, but have reviewed students' code for this assignment several times)
[edit] : the header file is better to understand how everything work : https://github.com/Jibus22/malloc/blob/main/srcs/includes/ma...
This is not a real malloc(3),but this is better than the OP to understand the bases.
Hint: Storing the free-list in the free space means that to free even more memory, you have to page in otherwise unused VM pages.
I used a doubly linked list of memory blocks. They are split into smaller blocks on allocation and merged with surrounding free blocks on deallocation.
Which browser are you using that makes this look bad?