Boost logo

Boost Users :

Subject: Re: [Boost-users] Object pools?
From: Ryan McConnehey (mccorywork_at_[hidden])
Date: 2010-02-17 02:44:22


Michael Caisse wrote:
> Ryan McConnehey wrote:
>> I recently had to create a class that managed a pool of objects for
>> the user. Instead of the user creating the object themselves they
>> would ask the class for an object. The class would hand the user a
>> boost::shared_ptr of the object. When the boost::shared_ptr needed
>> to destroy the object it handed it back to the class to reuse. The
>> class is designed for those areas that need a lot of the same object
>> but not all at the same time. This class has been useful for the
>> boost::asio library where the async_receive and async_write need the
>> buffer to be valid until the callback occurs. Other people at my
>> work have shown interest and I'm looking at needing to improve the
>> class to make it more general. Instead of spending time doing the
>> improvements I would like to know if the boost libraries have
>> anything like what I described. I'll looked through the descriptions
>> but haven't seen anything that comes close.
>>
>> Ryan
>
> Hi Ryan -
>
> Did you take a look at Boost.Pool?
>
> http://www.boost.org/doc/libs/1_42_0/libs/pool/doc/index.html
>
> regards -
>
I have taken a look at Boost.Pool. I didn't think it meet my
requirements. If I understand Boost.Pool correctly, when malloc is
called the pool creates the size of the item desired and passes a
pointer to the user. The pool still retains ownership of the resource.
This would be how when pool is destroyed it can delete all items that
had been malloc()'ed.

The pool I'm talking about gives the user complete ownership of the
item, upon "malloc()'ed", in the form of a boost::shared_ptr. Only when
the user is done with the item is it return to the pool. If the pool is
destroyed before the item is returned the item is still valid until the
user is done with it. Once the user is done with the item and the pool
is already destroyed then the item is also destroyed. (I checked my
original post and realized that the requirement of the pool being
destroyed and the object still being valid was not mentioned. I apologize.)

Ryan


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