//This will need to be made threadsafe at some point
template <class T>
class ObjectPool
{
public:
ObjectPool()
: _HunkAllocSize(1)
{
//Create a linked list for storage of pool elements. Precreate one item
}
ObjectPool(unsigned long num)
: _HunkAllocSize(num)
{
//Create a linked list for storage of pool elements, precreating. Manager
//Object is iniitailised with HunkAllocSize already set.
//Precreate HunkAllocSize objects. This is like specifying a starting pool.
}
T *New(void)
{
//Check to see if there are any free objects available
if (_FreeList.empty())
{
//No free objects Available. Create some more x HunkAllocSize
//This will precreate a few so we don''t call the costly creation
//often. Place all new items on the
}
else
{
//Free object available. Pass back pointer to one and move
//it to the used list.
}
}
void Delete (T *p)
{
//Return the object to the pool
}
void Flush(void)
{
//Clear the free list unallocated objects down to the standard pool size
for (unsigned int i=_HunkAllocSize; i<_FreeList.size(); i++)
{
//Get iterator to last item and delete.
}
}
void FlushAll(void)
{
//Clear the free list unallocated objects.
while(!_FreeList.empty())
{
//Get iterator to last itme and delete
}
}
void SetHunkAllocSize(unsigned long num)
{
_HunkAllocSize = num;
}
private:
unsigned long _HunkAllocSize;
list<ByteArray*> _FreeList;
list<ByteArray*> _UsedList;
};
Writing a Memory Manager with a new
I''m writing a memory manager that does precreation\reuse of objects to improve performance. I''ve listed my intentions in the source below. What I need to do however is work out a good way of passing values through my .New() to the objects that it creates. I''m betting that I''ll have to use some kind of templaty thing but can''t quite grasp how I''d pass multiple values to new in a generic manner.
Apart from that please feel free to critique the code and let me know if I''m walking down a blind alley... I''ve never done anything like this before...(The first template I''ve ever created was yesterday)
Many thanks
gimp
Chris Brodie
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement