Boost logo

Boost Users :

Subject: [Boost-users] Combing 2 memory pools
From: Michael Kennard (mkennard_at_[hidden])
Date: 2010-02-25 19:54:32


Short version:
I'm using the boost pool library for allocation of a linked list of
small objects. Now I want to append one linked list onto the end of
another. Thus I also want to combine the memory pools. I don't see
this functionality in the interface documentation. Is there a way to
easily combine two memory pools?

Longer version:
The list is a member of a class which manages it and has methods for
creating and deleting list elements. The memory pool is also a member
of this class. Before using the memory pool, each list element was
allocated individually and deleted individually. With the pool,
deletion is easier because you just destroy the pool instead of
deleting each list element individually. Merging two lists together
before I used the pool was just a matter of appending one linked list
onto the end of the other (easy, and code not shown). But now that I'm
using the pool I've got to figure out how to merge the two pools into
one.

Here's a simplified version of the code (which I haven't tried to compile):

struct element
{
  int m_data;
  element* m_next;
};

class MyList
{
public:
  MyList(); // constructor

  void NewElement (int data)
  {
    element* new_element = static_cast<element*>(m_pool.malloc());
    new_element->m_data = data;

    // add to beginning of list
    new_element->next = m_list;
    m_list = new_element;
  }

  element* m_list;
  boost::pool m_pool;
};

-- 
Michael Kennard
Aquaveo (www.aquaveo.com)

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net