c++boostobject-lifetimeintrusive-containers

Can I back a boost intrusive collection using boost pool as storage?


I understand that boost intrusive collections ultimately store references to the objects and thus that objects need their own lifetime management.

I was wondering if I can simply use boost pool to manage that lifetime. When I want to store a new object in boost intrusive list, could I just allocate an object from boost pool and store it within the list? Then when I delete from the list then I deallocate using boost pool.


Solution

  • The answer is yes.

    It's also not very typical.

    If you want to control when and where memory is allocated, you use a pool.

    If you want to decouple the memory layout of your datastructure and it's semantics, you use an intrusive container.

    So, there is a sweet spot, but it would look more like:


    Loose remarks:


    [1] any issues surrounding vector reallocation are usually prevented by reserving enough capacity up front. If you do, you will realize this is very very similar to a fixed-size pool allocator, but with the added guarantee of zero fragmentation. If you didn't need the latter, you could do a list<T, pool_allocator<T> > so you get locality of reference but stable references on insertion/deletion. Etc. etc.