|
-
November 10th, 2006, 06:43 AM
#1
Memory pool
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.
-
November 10th, 2006, 06:57 AM
#2
Re: Memory pool
Did you check this article?
-
November 10th, 2006, 07:07 AM
#3
Re: Memory pool
No, I have not.
It seems to be very interesting. I'll take a look at it right now!
Thanks Marc.
-
November 10th, 2006, 07:15 AM
#4
Re: Memory pool
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.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
November 10th, 2006, 09:26 AM
#5
Re: Memory pool
Two points:
1. Heed SuperKoko's advice.
2. On Marc's link, there's a sub-link to boost::pool. If you go the route of using a pool, I would recommend this implementation.
Jeff
-
November 11th, 2006, 12:44 PM
#6
Re: Memory pool
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).
Code:
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.
Last edited by ltcmelo; March 31st, 2007 at 11:10 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|