Click to See Complete Forum and Search --> : Memory pool


ltcmelo
November 10th, 2006, 05:43 AM
Hi all.
I'd like to implement my own memory management system.
My application allocates/deallocates small pieces of memory during its run. The idea is to make most of the allocation at once in the beginning (and allocates more whenever it's necessary), and deallocate to a pool, instead of returning it to the O.S. So, whenever I need more memory, I could get from this pool.
This will be used for STL containers, which means I'm going to have to implement my own allocators too.
I've found very scattered information about this on the web. Can anyone give a nice reference on this topic?

Thanks.

Marc G
November 10th, 2006, 05:57 AM
Did you check this article (http://www.codeguru.com/Cpp/Cpp/cpp_mfc/stl/article.php/c4079/)?

ltcmelo
November 10th, 2006, 06:07 AM
No, I have not.
It seems to be very interesting. I'll take a look at it right now!

Thanks Marc.

SuperKoko
November 10th, 2006, 06:15 AM
Beware: You should use memory pools only if you get a real performance gain and that it doesn't use too much RAM.
Otherwise, it's a bad idea to steal memory you don't need.

jfaust
November 10th, 2006, 08:26 AM
Two points:

1. Heed SuperKoko's advice.
2. On Marc's link, there's a sub-link to boost::pool (http://www.boost.org/libs/pool/doc/index.html). If you go the route of using a pool, I would recommend this implementation.

Jeff

ltcmelo
November 11th, 2006, 11:44 AM
Hi all!
I found a logical error whenever the grow() function is called (variable 'l' was not being assigned back). So, I made small changes in function allocate and everything seems to be working now!!! Here is my version (inside ss_storage.h).


T* allocate()
{
link *l;
if (head_ == 0) grow();
l = head_;
head_ = head_->next_;
return reinterpret_cast<T *>(l);
}


I tested this code under MS VS .NET 8 (2005) and GNU C++ compiler 3.0.4, 3.3.5 and 3.4.4.