Hi guys! First post, most of the time only read. I'm taken a look at this article about custom allocators and trying to implement one. There a couple of things on the FreeListAllocator that i don't understand.
1) Knowing that total_size variable already takes into the consideration the header, I don't see sens of this, if fact never can be less than the size of header, same or bigger and only gets inside when is the same size. If it gets inside and the prev block is no null, the prev block now points to the next block of the actual block, leaving this block without any way to get to it.
if(free_block->size - total_size <= sizeof(AllocationHeader))
{
//Increase allocation size instead of creating a new FreeBlock
total_size = free_block->size;
if(prev_free_block != nullptr)
prev_free_block->next = free_block->next;
else
_free_blocks = free_block->next;
}
2) OK! We have a free block, now we create a new block that contains the remaining free memory, but next_block->next = free_block->next it will always point to null, there is no link is generated between them, in fact no list is generated, next_block should points to free_block.
else
{
//Else create a new FreeBlock containing remaining memory
FreeBlock* next_block = (FreeBlock*)( pointer_math::add(free_block, total_size) );
next_block->size = free_block->size - total_size;
next_block->next = free_block->next;
if(prev_free_block != nullptr)
prev_free_block->next = next_block;
else
_free_blocks = next_block;
}
3) At this point we have our aligned address, but the alignment offset only takes into consideration the allocation header. So depending on the offset size the header could override the block memory info (FreeBlock). An other problem that I'm seeing is that in any moment is marking this free block as occupied.
uptr aligned_address = (uptr)free_block + adjustment;
AllocationHeader* header = (AllocationHeader*)(aligned_address - sizeof(AllocationHeader));
header->size = total_size;
header->adjustment = adjustment;
PS: Sorry for my poor english