How to create a dispenser for std :: map using an object pool

This is a continuation from stl allocator, a copy constructor of another type, rebind

I am using std :: map and I want the custom allocator to be able to reuse storage for internal nodes. The elements that are stored are pointers, so I'm not talking about reusing them, but only about the internal distributions for the map.

Basic requirements: different instances of the map cannot share the pool of objects, it must be unique for each instance.

I do not need a complete solution, I just would like to understand how to deal with the required copy constructor, which accepts distributors of a different type. I do not know how to manage the internal memory in this case.

+2
c ++
source share
1 answer

As you point out in another question, dispensers should not have any state. Use local storage or a pointer in each allocator object in the memory pool: allocators simply become the type interface for this pool.

struct MemoryPool { // none of this depends on the type of objects being allocated }; template<class T> struct MyAllocator { template<class U> struct rebind { typedef MyAllocator<U> other; }; MemoryPool *_pool; // copied to any allocator constructed template<class U> MyAllocator(MyAllocator const &other) : _pool(other._pool) {} // allocate, deallocate use _pool // construct, destruct deal with T }; 
+2
source share

All Articles