Need for custom allocators library

M

mczard

Does anyone knows about some open-source custom allocators libarary? I
need one especially for fast allocations of elements of a fixed size.
This seems to be a very common problem, isn't it?
 
E

Erik Wikström

Does anyone knows about some open-source custom allocators libarary? I
need one especially for fast allocations of elements of a fixed size.
This seems to be a very common problem, isn't it?

I do not know of any specific library but if you search for pool
allocator you should be able to find something.
 
L

Lance Diduck

Does anyone knows about some open-source custom allocators libarary? I
need one especially for fast allocations of elements of a fixed size.
This seems to be a very common problem, isn't it?
It is a common problem, but no one seems to have a open source library
for this. I've looked everywhere for one, and finally just rolled my
own, which perhaps one day I will open source.
A trick is to create a node that looks like this:
template <class T> struct MyNode{
typedef
boost::aligned_storage<sizeof(T),boost::alignment_of<T>::value>
data_t;

};

Then creating an allocator that looks like
template <class T>struct Alloc{
void* allocate(size_t){
void* ret=0;
if(!m_nodes.empty()){
ret=m_nodes.top();
m_nodes.pop();
}else
ret = new MyNode<T>::data_t;
return ret;
}
void deallocate(void*p){
m_nodes.push(p);
}
~Alloc(){
while(!m_nodes.empty()){
delete mnodes.top();
m_nodes.pop();
}
}
std::stack<void*> m_nodes;
};
This isn't the fastest allocator in existence. That doesn't exist. But
it is way faster than just using built in new/delete, esp when you
recycle T's a lot. You should easily be able to extrapolate an
allocator from this code tuned to your application.
Lance
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,537
Latest member
BellCorone

Latest Threads

Top