| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"" |
| Date: |
28 Dec 2007 05:02:31 AM |
| Object: |
Need for custom allocators library |
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?
.
|
|
| User: "=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=" |
|
| Title: Re: Need for custom allocators library |
28 Dec 2007 06:29:45 AM |
|
|
On 2007-12-28 12:02, wrote:
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.
--
Erik Wikström
.
|
|
|
|
| User: "Lance Diduck" |
|
| Title: Re: Need for custom allocators library |
28 Dec 2007 08:42:58 AM |
|
|
On Dec 28, 6:02=A0am, wrote:
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=3D0;
if(!m_nodes.empty()){
ret=3Dm_nodes.top();
m_nodes.pop();
}else
ret =3D 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
.
|
|
|
|

|
Related Articles |
|
|