2

There doesn't seem to be that many good descriptions that go into the specifics about how dlmalloc works. The sources I have come across so far mention dlmalloc, but then only goes on to explain what malloc() and free() are, rather than describing dlmalloc.

The Wikipedia description, on the other hand, was a bit hard for me to understand. http://en.wikipedia.org/wiki/Malloc#dlmalloc_and_its_derivatives

Can anyone explain the workings of dlmalloc, how to implement it, and any additional sources that could help?

Dark Templar
  • 6,223
  • 16
  • 46
  • 46

1 Answers1

2

If you ever don't understand an implementation you should check the source, if available. Here's a fairly extensive explanation which was mentioned in the source.

Can anyone explain the workings of dlmalloc, how to implement it, and any additional sources that could help?

If you want to use dlmalloc then download the source. Creating your own version of malloc is going to be very difficult and will take you days reading to understand dlmalloc's source.

If you want to learn how dynamic memory works then I suggest you create a memory pool/allocator which will teach you everything you need to know. All you need to do is allocate a large buffer and create functions which allocate/free parts of that buffer.

Pubby
  • 3,290
  • 1
  • 21
  • 26
  • I just have one quick question, though. Can dynamic memory ever "give memory back" to the system? I know that sbrk() is called whenever the heap needs to be extended when there are no suitable free blocks. However, I was wondering if the heap can be "shrinked" if there is a freed block at the very end of the heap, or if the highwater mark is there to stay until the whole process has been quit... – Dark Templar Oct 13 '11 at 18:40
  • 1
    @DarkTemplar Of course memory *can* be given back, but that doesn't mean that every implementation does so. sbrk() can be called to shrink the data segment, but as you said you'll need to make sure that no blocks are allocated at the end. – Pubby Oct 13 '11 at 18:51