Hacker News new | ask | show | jobs
by nkurz 4219 days ago
Great tutorial, Dan! Since it sounds like you are planning to continue the series, I have a few thoughts on potential directions to go with it.

First, a link to Doug Lea's classic malloc page might be a good addition to the resources section. His dlmalloc() is the basis for GCC's current ptmalloc. His code is wonderfully clear and commented, both for the implementation and the rationale behind it: http://g.oswego.edu/dl/html/malloc.html

Second, I wonder if it would make sense to jump straight to using mmap() instead of the classic brk()/sbrk(). I think it's no more complicated, has more uses elsewhere, is conceptually more portable, and allows multiple arena's to be added in a straightforward way. Are there advantages I'm not seeing to sticking to the ancient ways?

Last, on the debugging side, I think you might want to start with an introduction to Valgrind rather than gdb. It's a much easier learning curve, and even for an expert it's often the better tool for the memory allocation type bugs that are going to be most common here. Alternatively (or additionally) some examples of the more modern Address Sanitizer that's now in GCC and CLang would be slick: https://code.google.com/p/address-sanitizer/wiki/AddressSani...

1 comments

> Second, I wonder if it would make sense to jump straight to using mmap() instead of the classic brk()/sbrk().

Yes, it will. brk/sbrk are terrible ways to allocate anything but more stack. Use mmap instead, both for more control over the layout in memory, and for more portability.

Perhaps you know: on Linux, how do mmap() and brk() differ as far as page initialization? Does brk() conceptually include the effects of MAP_POPULATE? Do both of them cause virtual to physical memory mapping at the time of the call?
I'm not sure as to the specifics, because brk is a strict subset of mmap; however, I would be surprised if it doesn't use the full VM substructure to allocate the mapping immediately.

I would amend my statement before to say that, if you're looking into memory allocation, conceptually, mmap is where everything happens these days. brk is kept for backwards compatibility.

On the systems I've seen, brk makes a lazy allocation, i.e. it doesn't allocate physical memory until you write to a page.