back
4 comments
It's relatively easy to make an allocator, it is really hard to make an efficient one that works well across a large variety of use cases. That's also why it can pay off big time if you know more about your use case than malloc does (and you usually do) to roll your own allocator.
Memory management is complicated (at least, doing it well is). It's a good thing to discover as well, because it should lead to an appreciation that malloc() isn't cheap, and there can be an awful lot of work behind every single call.

Besides, that code seems pretty well commented.

It's complicated, but there's quite a bit of wisdom embodied in it. It's well worth reading the code and comments carefully, if you're at all interested in memory management.
malloc is a generic allocator, it needs to worry about a lot of things that are not common. A simple allocator will work most of the time, but the devil is in the details.