Boost logo

Boost :

Subject: Re: [boost] [pool] Objects of varying sizes
From: Rowan James (rowanj_at_[hidden])
Date: 2012-06-27 09:52:38


On 27 Jun 2012, at 02:19, Robert Dailey wrote:
> On Mon, Jun 25, 2012 at 6:48 PM, Klaim - Joël Lamotte <mjklaim_at_[hidden]>wrote:
>
>> Hi,
>>
>> don't look at the object_pool, look at pool. It provide only allocation and
>> destruction methods, given a size at runtime.
>> That way you can wrap it with a template function that give the size of the
>> object you want to create to the pool for it to allocate memory for you.
>
>
> Thanks for the response. I am reviewing the boost.pool reference here:
> http://www.boost.org/doc/libs/1_49_0/libs/pool/doc/html/boost/pool.html
>
> Based on that, I do not see a way to allocate blocks of varying sizes. The
> documentation makes it sound like I have to set size_type to the largest
> object that will be allocated, which I have no reasonable way of knowing (I
> have hundreds of objects in my inheritance hierarchy, each with varying
> size, and no way to calculate the largest in a maintainable way).
>
> The malloc() method does not take any parameters.
>
> Am I misunderstanding you? Could you please point me in the right
> direction? Thanks again for your help.
>

Sorry for the late response, I've created a couple of code snippets that seem to do something like what I understand you're after.

My first reaction on hearing your question was to create a distinct boost::object_pool instance for each class type. Whenever you construct an object of a given type, just use the pool for that object's construct method:

https://gist.github.com/3004098

This strategy is straightforward, scalable, and can be integrated fairly readily with varying amounts of intrusiveness into the class hierarchy - but will require at least some level of coordination from the class authors or an exhaustive list of classes at some other point. Adding them to a namespace can be quite simple, and done in each class' .hpp file such that whenever you can construct an Example, you can call

MyPools::Example.construct();

--
Secondly, and more directly along these lines of discussion; the pool_allocator (i.e. std::allocator) implementation provides:
static pointer allocate(size_type n);
… which could be used with placement new to wrap your construction however you liked without being intrusive into the constructed class.
https://gist.github.com/3004102
--
Of course, I'm not handling the cleanup stage of this; which I expect is a very important consideration.  I hope this helps :-)
Regards,
    Rowan James

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk