I recently played the development of a custom allocator based on a memory pool that is shared between multiple instances of a allocator.
It was assumed that the dispenser would be compatible with containers based on STL and Standard C ++, such as vector, deque, map, string, etc.
However, something in particular caused me some confusion. Various container implementations, such as std :: vector , std :: string , use Small Buffer Optimization β a stack-based allocation for small initial memory requirements.
For example, MSVC9.1 has the following member in the basic_string class:
union _Bxty { // storage for small buffer or pointer to larger one _Elem _Buf[_BUF_SIZE]; _Elem *_Ptr; char _Alias[_BUF_SIZE]; // to permit aliasing } _Bx;
I donβt see how, when creating instances of such containers, you can persuade the implementation only and always use the provided allocator and not use SBO. I ask, because one of the intentions of custom allocators was to use them in a shared memory context, where the amount of shared memory might be less; the SBO restriction some of the various implementations might use.
For example, I would like to have a situation where I can have two instances of std :: string, one per process, sharing a common block of memory, which may be less than or equal to the top of the SBO limit.
Perhaps related: May std :: vector use a little buffer optimization?
typedef std::vector<int,mysharedmemallocator> shmvtype; shmvtype v(2,0); //<-- if SBO then error as memory is allocated on //stack not via the allocator v[1] = 1234; //<-- if SBO then error as wrong piece of memory // is being modified.
Let's look at another example that is not based on shared memory, as this seems to complicate some things. Suppose I want to specialize my std :: basic_string or std :: vector, etc. Using a allocator that fills the memory that it allocates with the value 0xAB before presenting the pointer back to the caller for some reason other than a whim.
A container that specializes in this new allocator, but which also uses SBO, will not have SBO-based memory filled with the 0xAB pattern. For example:
typedef std::basic_string<char,myfillmemallocator> stype stype s; s.resize(2); assert(s[0] == 0xAB);