template<class T>
class Mem_root_allocator< T >
Mem_root_allocator is a C++ STL memory allocator based on MEM_ROOT.
No deallocation is done by this allocator. Calling the constructor and destructor on the supplied MEM_ROOT is the responsibility of the caller. Do not call Clear() or ~MEM_ROOT until the destructor of any objects using this allocator has completed. This includes iterators.
Example of use: vector<int, Mem_root_allocator<int> > v((Mem_root_allocator<int>(&mem_root)));
- Note
- allocate() throws std::bad_alloc() similarly to the default STL memory allocator. This is necessary - STL functions which allocate memory expect it. Otherwise these functions will try to use the memory, leading to seg faults if memory allocation was not successful.
-
This allocator cannot be used for std::basic_string with RHEL 6/7 because of this bug: https://bugzilla.redhat.com/show_bug.cgi?id=1546704 "Define _GLIBCXX_USE_CXX11_ABI gets ignored by gcc in devtoolset-7"
-
C++98 says that STL implementors can assume that allocator objects of the same type always compare equal. This will only be the case for two Mem_root_allocators that use the same MEM_ROOT. Care should be taken when this is not the case. Especially:
- Using list::splice() on two lists with allocators using two different MEM_ROOTs causes undefined behavior. Most implementations seem to give runtime errors in such cases.
- swap() on two collections with allocators using two different MEM_ROOTs is not well defined. At least some implementations also swap allocators, but this should not be depended on.