0

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?

Vectorizer
  • 1,076
  • 9
  • 24

1 Answers1

2

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.

Andriy Tylychko
  • 15,967
  • 6
  • 64
  • 112