Hi all
I am trying to implement the Fixed Size Buffer Pattern (Douglass, "RT Design Patterns", ch. 6). Briefly, it is a matter of implementing a number of fixed-size preallocated heaps and a Heap Manager that keeps track of heaps, and then overload global opertors new() and delete().
To ensure that I am only using one Heap Manager, I wish to make this class a Singleton.
Global operator new() is implemented as
1 2 3 4
|
void* operator new(size_t size)
{
return HeapManager::getInstance()->allocate(size);
}
|
The HeapManager::getInstance() is implemented in the standard way:
1 2 3 4 5
|
HeapManager* HeapManager::getInstance()
{
if(!pInstance) pInstance = ::new HeapManager();
return pInstance;
}
|
And the constructor for class HeapManager is empty
1 2 3
|
HeapManager::HeapManager()
{
}
|
This is where the fun starts...when I access the HeapManager the first time, HeapManager::pInstance() is initialized using new() in HeapManager::getInstance(). However, this initialization uses global operator new() which is overloaded to use the HeapManager, whose pInstance is not yet initialized, so new() is called, so pInstance is initialized, and so forth and so on.
The question, of course, is: Is there a way to break this vicious circle?
TIA,
/Troels