
dear boost users, boost offers in lib pool the class object_pool.
as is stated in the docs, it allows for allocation with O(1) but
deallocation need O(n) time. i suppose it has to search the internal
list for the position of the object ro be freed.
but this could be speed up a lot by using a stack of free objects. if
one would like to allocate an object, a pointer to a free memory cell is
popped from the stack, if the object should be returned / freed to the
pool, that objects pointer is pushed onto the stack.
of course this causes memory overhead, but, for example if one would
like to implement a really large pool of small objects for a particle
system, then O(n) deallocation is prohibitive.
so my question is why boost does not offer an alternative version of
object_pool with both O(1) alloc and dealloc ?
below is my version of a stack based object pool with O(1) both alloc
and dealloc. i really do not like my implementation, but i did not know
how to do better...
template<class T> class Smallobjects_Pool
{
protected:
class Node // a node of a double linked list
{
public:
Node() {prev=next=NULL; idx=-1; }
Node* prev;
Node* next;
int idx; // index to the objects - vector
T obj;
};
//Node* next(int cur) { return objects[cur].next;}
//Node* root() { return _root; }
//Node* last() { return _last; }
protected:
Node* _root;
Node* _last;
vector<Node> objects;
vector<int> free_objects;
public:
T & operator[](int i){return objects[i].obj;}
bool empty() { return _root == NULL;}
Smallobjects_Pool(int initial_size)
{
_root=NULL;
_last=NULL;
objects.resize(initial_size);
free_objects.reserve(initial_size);
for(unsigned int a=0;a