By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,919 Members | 1,625 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,919 IT Pros & Developers. It's quick & easy.

looking for an allocator class

P: n/a
Does anybody have a good allocator class for allocating lots of small
objects in chunks ? Preferably one that is thread safe - or one that
can be easily made thread safe - I can add the locking myself.
Basically I'm working on some code where much of the time is spent in
allocating objects that are pretty small in size. Many, many of these
objects are allocated.

Aug 14 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Does anybody have a good allocator class for allocating lots of small
objects in chunks ? Preferably one that is thread safe - or one that
can be easily made thread safe - I can add the locking myself.
Basically I'm working on some code where much of the time is spent in
allocating objects that are pretty small in size. Many, many of these
objects are allocated.
alexandrescu's Modern C++ Design has a chapter on it.And probably in
the Loki library.Rest of the book is useful too.
There is also Boost Pool library for you to consider

Hurcan Solter

Aug 14 '07 #2

P: n/a
On Aug 14, 10:02 am, hurcan solter <hsol...@gmail.comwrote:
Does anybody have a good allocator class for allocating lots of small
objects in chunks ? Preferably one that is thread safe - or one that
can be easily made thread safe - I can add the locking myself.
Basically I'm working on some code where much of the time is spent in
allocating objects that are pretty small in size. Many, many of these
objects are allocated.

alexandrescu's Modern C++ Design has a chapter on it.And probably in
the Loki library.Rest of the book is useful too.
There is also Boost Pool library for you to consider

Hurcan Solter
Thanks. I'm checking out the Boost Library now. Suppose we do the
following:

std::vector<int, boost::pool_allocator<int v;
for (int i = 0; i < 10; ++i)
v.push_back(-1);

And the std::vector<intis implemented so that it starts out with
reserved
space of 1 and doubles space every time. So At the end of the above
loop
v will have size 10 and reserved size 16.

Does the boost allocator mean that there will memory arrays lieing
around
of size 1,2,4,8 that can be reused ?

Eg if you do this:

std::vector<int, boost::pool_allocator<int v;
for (int i = 0; i < 10; ++i)
v.push_back(-1);

std::vector<int, boost::pool_allocator<int v2;
for (int i = 0; i < 8; ++i)
v2.push_back(-1);

There will be no system call needed to get more memory -
v2 will just reuse the memory in the pool_allocator<int?



Aug 14 '07 #3

P: n/a
Thanks. I'm checking out the Boost Library now. Suppose we do the
following:

std::vector<int, boost::pool_allocator<int v;
for (int i = 0; i < 10; ++i)
v.push_back(-1);

And the std::vector<intis implemented so that it starts out with
reserved
space of 1 and doubles space every time. So At the end of the above
loop
v will have size 10 and reserved size 16.
well on my implementation(VC++ 8.0) it increases by 50%.So it goes
like
1,2,3,4,6,9,13 multiplied by size of whatever you put into it.Not
that
important anyway,they are free to choose as they please.
>
Does the boost allocator mean that there will memory arrays lieing
around
of size 1,2,4,8 that can be reused ?
vector will deallocate the previous array through the allocator,and
it
would probably return the memory to the pool ready for reuse.not
that
there will be single units of different sizes. it uses chunks of some
bytes
appropriate for integers. there may be enough free chunks to allocate
for
the next request.(previous allocation of 8*sizeof(int) bytes are
probably available)
if not, it would cause an allocation
>
Eg if you do this:

std::vector<int, boost::pool_allocator<int v;
for (int i = 0; i < 10; ++i)
v.push_back(-1);

std::vector<int, boost::pool_allocator<int v2;
for (int i = 0; i < 8; ++i)
v2.push_back(-1);

There will be no system call needed to get more memory -
v2 will just reuse the memory in the pool_allocator<int?
It depends on whether there will be enough contigious free memory
left over from whatever allocated for v (Note that v still holds
on memory). It may allocate more than what requested by the first
vector ,and return that extra memory for v2 if the requested size is
small enough to fit in.
Aug 15 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.