Stateless_allocator is a C++ STL memory allocator skeleton based on Malloc_allocator, which assumes that a global free function can be used to allocate and deallocate memory, so that no state need to be kept by the allocator object.
More...
template<class T, class ALLOC_FUN, class DEALLOC_FUN = My_free_functor>
class Stateless_allocator< T, ALLOC_FUN, DEALLOC_FUN >
Stateless_allocator is a C++ STL memory allocator skeleton based on Malloc_allocator, which assumes that a global free function can be used to allocate and deallocate memory, so that no state need to be kept by the allocator object.
The allocation and deallocation functions must be provided as callable types (aka functors) which have no state and can be default constructed.
Example usage:
struct My_psi_key_alloc
{
void* operator(size_t s)()
{
return my_malloc(My_psi_key, s, MYF(MY_WME | ME_FATALERROR));
}
};
template <class T>
using My_psi_key_allocator =
Stateless_allocator<T, My_psi_key_alloc>;
template < template<class T> class Allocator >
using default_string=
std::basic_string<char, std::char_traits<char>, Allocator<char> >;
typedef default_string<My_psi_key_allocator> My_psi_key_str;
My_psi_key_str x("foobar");
Since a Stateless_allocator instance is always default-constructible, it can also be used to create instances of std::basic_string, even with compilers that have this libstd++ bug: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56437 "basic_string
assumes that allocators are default-constructible".
- 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.