Reputation: 1394
I would like to be able to pass to the STL containers (vector, unordered_map, etc) the memory pool from which they need to allocate. I've found this question but it does not address the specific problem I'm facing. I already have a working custom allocator that I can specify in the container declaration but could not figure out a way to pass the address for the allocator to use internally (via the placement new operator), from the application. Basically, I would like to go
From:
std::vector<int, myCustomAllocator<int>> myVector;
to:
void* pool = getMemoryPoolAddress();
std::vector<int, myCustomAllocator<int>/*Specify memory pool somehow*/> myVector;
How can I pass pool
to my allocator?
Upvotes: 0
Views: 2349
Reputation: 16256
Standard library allocator is stateless (see CppCon 2015: Andrei Alexandrescu “std::allocator is to Allocation what std::vector is to Vexation” for context). This means that your concrete allocator type can have only one state (monostate) - global one, or static
in C++.
So the question you linked contains the answer to your question:
class MyPoolAlloc {
public:
static MyPool *pMyPool;
...
};
MyPool* MyPoolAlloc<T>::pMyPool = NULL;
It's how you can specify your pool for your allocator type.
Upvotes: 2