W Effective STL było całkiem sporo o tym jak napisać customowy allocator (i kiedy tego [nie] robić, ale Ty raczej nie masz wyboru), poszukaj, fajnie opisane.
Zawsze możesz też pójść na skróty i poszukać gotowców (tylko i tak musisz wiedzieć co się tam dzieje, oraz ufać autorowi że jest dobrze): na przykład http://www.josuttis.com/cppcode/allocator.html :
Kopiuj
#include <limits>
#include <iostream>
namespace MyLib {
template <class T>
class MyAlloc {
public:
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
template <class U>
struct rebind {
typedef MyAlloc<U> other;
};
pointer address (reference value) const {
return &value;
}
const_pointer address (const_reference value) const {
return &value;
}
MyAlloc() throw() {
}
MyAlloc(const MyAlloc&) throw() {
}
template <class U>
MyAlloc (const MyAlloc<U>&) throw() {
}
~MyAlloc() throw() {
}
size_type max_size () const throw() {
return std::numeric_limits<std::size_t>::max() / sizeof(T);
}
pointer allocate (size_type num, const void* = 0) {
std::cerr << "allocate " << num << " element(s)"
<< " of size " << sizeof(T) << std::endl;
pointer ret = (pointer)(::operator new(num*sizeof(T)));
std::cerr << " allocated at: " << (void*)ret << std::endl;
return ret;
}
void construct (pointer p, const T& value) {
new((void*)p)T(value);
}
void destroy (pointer p) {
p->~T();
}
void deallocate (pointer p, size_type num) {
std::cerr << "deallocate " << num << " element(s)"
<< " of size " << sizeof(T)
<< " at: " << (void*)p << std::endl;
::operator delete((void*)p);
}
};
template <class T1, class T2>
bool operator== (const MyAlloc<T1>&,
const MyAlloc<T2>&) throw() {
return true;
}
template <class T1, class T2>
bool operator!= (const MyAlloc<T1>&,
const MyAlloc<T2>&) throw() {
return false;
}
}
edit: z cppreference.com
W C++14 dochodzi jeszcze propagate_on_container_move_assignment
W C++17 prawdopodobnie dojdzieis_always_equal
Do C++11 wymaganiem było żeby wszystkie alokatory były stateless.