Search code examples
boostpoolboost-pool

boost pool with specified element size, and specified initial # of element?


boost::pool<> constructor takes "element size" parameter.
boost::object_pool constructor takes "initial # of element" parameter.

I want to create pool with "element size S" and "initial N of element".
Is this possible with boost::pool ?

Thank you


Solution

  • You can do that with object_pool; it infers the size of its element based on the ElementType template parameter, so there is no need to specify a size explicitly. You can specify the requested number of chunks (your 'N') as an additional constructor parameter.

    Update based on OP comments:

    From the boost::pool source:

    explicit pool(const size_type nrequested_size,
        const size_type nnext_size = 32)
    

    So you can just do this:

    boost::pool<> p(8 * sizeof(int), 64);
    

    If you want a pool that returns chunks of size 8 ints, and makes an initial allocation of 64 * 8 ints. After you exceed the initial allocation of chunks, storage will be doubled.