I\'m working on a memory pool/memory allocator implementation and I am setting it up in a manor where only a special \"Client\" object type can draw from the pool.The client
With the help of Dieter Lücking I was able to figure out how to use my pool in operator new and operator delete
Here is the code for operator new:
void* ObjectBase::operator new(size_t size, Pool* pool)
{
if (pool!=nullptr) {
//use pool allocation
MemoryBlock** block = pool->Alloc(size+(sizeof(MemoryHeader)));
MemoryBlock* t = * block;
t = (MemoryBlock*)((unsigned char*)t+sizeof(MemoryHeader));
MemoryHeader* header = new(*block)MemoryHeader(pool);
header=nullptr;
return t;
}
else{
//use std allocation
void* temp = ::operator new(size);
if (temp!=nullptr) {
return temp;
}
else throw new std::bad_alloc;
}
}
Here is the code for operator delete
void ObjectBase::operator delete(void* memory)
{
MemoryBlock* temp = (MemoryBlock*)((unsigned char*)memory-sizeof(MemoryHeader));
MemoryHeader* header = static_cast(temp);
if (header->pool!=nullptr) {
if (header->pool->Free((MemoryBlock**)&header));
else
{
::operator delete(memory);
}
}
else{
::operator delete(memory);
}
}
I'm using the "Memory Header" idea that was suggested.
The code is also set up in a way that defaults to using a standard memory allocation call if for some reason the pool fails.
Thanks Again for your help.