
Hi Tomas
On Tue, Sep 25, 2012 at 7:09 PM, Graeme Russ graeme.russ@gmail.com wrote:
We should implement each of malloc(), free(), calloc(), and realloc().
Don't worry about reclaiming and reusing space with a proper free() implementation. Remember, all memory allocated on the early heap must be relocated anyway.
Maybe if you add a size_t value immediately before the allocated space which stores the block size. So:
size_t *bytes_ptr = ptr; bytes_ptr--; size_t bytes = *bytes_ptr;
gives you the block size
I've been thinking about this a bit more, and for the sake of 4 bytes, this additional 'size' member could be quite handy: - We effectively end up with a linked-list of allocated blocks - free() could set the high bit to tag the block as 'de-allocated' - When a block is relocated into the permanent heap, free() should be called on the source (early heap) block - We can call a 'cleanup' function after early heap is no longer needed and check that every block has the high bit set - We can re-use blocks by scanning for a tagged block with the same size (usefull for drivers that allocate temporary buffers which are always the same size) - If there are no early heaps with enough space for a given malloc operation, but there is a tagged block that is larger than the requested size, we can split tagged blocks
Remebering back to when I suggested a list of relocation helpers (one for each allocated block), I think we can implement that as an additional field in the block header (stretching the header to 8 bytes). This can come later.
Regards,
Graeme