Page 1 of 1

multiple heaps with dlmalloc

Posted: Fri Mar 24, 2006 6:39 pm
by carbonBased
For ease of development, I'm currently using Doug Lea's malloc as my byte granularity memory allocator.

I'm curious, however, if anybody has hacked this allocator to allow for multiple heaps to allow something like:

void *memoryAllocate(int heapNum, int size);

Where heapNum references various different heaps (perhaps one for basic OS structures, one for shared memory, etc).

I haven't actually looked into what might be required, yet. I'm just curious to know if its been done before, and to perhaps get some advice on it, before actually attempting it.

Thanks,
Jeff

Re:multiple heaps with dlmalloc

Posted: Sat Mar 25, 2006 5:35 am
by JoeKayzA
Hi!

I've once implemented heap support for multiple, seperate heaps. Probably, your heap is described by a set of variables (base address, size, pointer to first non-free block, pointer to first free block...). All you have to do is to pack these into a structure, and then use an instance of this structure for every heap you initialise. When you call malloc, pass it a pointer to such a structure to identify the heap you want to allocate memory from (the 'int heapnum' would work as well, but you'll need a global table of all heaps then). This is how I did it before, for now I've switched to c++, where I will implement heaps as classes. The basic idea is the same, however.

cheers Joe