I am currently implementing my own pool allocator to store n chunks of the same size in one big block of memory. I am linking all the chunks together using a *next
pointer stored in the struct chunk like this
struct Chunk{
Chunk* next;
};
so I would expect to make a linked list like this given that i have a variable num_chunks which stores the number of chunks in the block
Chunk* allocate_block(size_t chunk_size){
alloc_pointer = (Chunk*) malloc(chunk_size * num_chunks);
Chunk* chunk = alloc_pointer;
for (int i = 0; i < num_chunks; ++i){
/*
I need to solve the problem of how to
link all these chunks together.
So I know that I have to work using the next pointer.
This next pointer must point to an address chunk_size
away from the current pointer and so on so forth.
So basically:
chunk -> next = alloc_pointer + chunk_size
and chunk is going to be this chunk -> next on the
successive call.
*/
chunk -> next = chunk + chunk_size;
chunk = chunk -> next;}
chunk -> next = nullptr;
return chunk;
}
However looking at a blog post I have this implementation which makes sense but still do not understand why mine should be wrong
/**
* Allocates a new block from OS.
*
* Returns a Chunk pointer set to the beginning of the block.
*/
Chunk *PoolAllocator::allocateBlock(size_t chunkSize) {
cout << "\nAllocating block (" << mChunksPerBlock << " chunks):\n\n";
size_t blockSize = mChunksPerBlock * chunkSize;
// The first chunk of the new block.
Chunk *blockBegin = reinterpret_cast<Chunk *>(malloc(blockSize));
// Once the block is allocated, we need to chain all
// the chunks in this block:
Chunk *chunk = blockBegin;
for (int i = 0; i < mChunksPerBlock - 1; ++i) {
chunk->next =
reinterpret_cast<Chunk *>(reinterpret_cast<char *>(chunk) + chunkSize);
chunk = chunk->next;
}
chunk->next = nullptr;
return blockBegin;
}
I don’t really understand why I should convert the type of chunk to char and then add that to the size of the chunk. Thanks in advance