CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Memory pool

  1. #1
    Join Date
    Jan 2006
    Location
    Belo Horizonte, Brazil
    Posts
    405

    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.

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Memory pool

    Did you check this article?
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Jan 2006
    Location
    Belo Horizonte, Brazil
    Posts
    405

    Re: Memory pool

    No, I have not.
    It seems to be very interesting. I'll take a look at it right now!

    Thanks Marc.

  4. #4
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    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()!

  5. #5
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582

    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

  6. #6
    Join Date
    Jan 2006
    Location
    Belo Horizonte, Brazil
    Posts
    405

    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
  •  





Click Here to Expand Forum to Full Width

Featured