Allocator (C++) - Requirements

Requirements

Any class that fulfills the allocator requirements can be used as an allocator. In particular, a class A capable of allocating memory for an object of type T must provide the types A::pointer, A::const_pointer, A::reference, A::const_reference, and A::value_type for generically declaring objects and references (or pointers) to objects of type T. It should also provide type A::size_type, an unsigned type which can represent the largest size for an object in the allocation model defined by A, and similarly, a signed integral A::difference_type that can represent the difference between any two pointers in the allocation model.

Although a conforming standard library implementation is allowed to assume that the allocator's A::pointer and A::const_pointer are simply typedefs for T* and T const*, library implementors are encouraged to support more general allocators.

An allocator, A, for objects of type T must have a member function with the signature A::pointer A::allocate(size_type n, A::const_pointer hint = 0). This function returns a pointer to the first element of a newly allocated array large enough to contain n objects of type T; only the memory is allocated, and the objects are not constructed. Moreover, an optional pointer argument (that points to an object already allocated by A) can be used as a hint to the implementation about where the new memory should be allocated in order to improve locality. However, the implementation is free to ignore the argument.

The corresponding void A::deallocate(A::pointer p, A::size_type n) member function accepts any pointer that was returned from a previous invocation of the A::allocate member function and the number of elements to deallocate (but not destruct).

The A::max_size member function returns the largest number of objects of type T that could be expected to be successfully allocated by an invocation of A::allocate; the value returned is typically A::size_type(-1) / sizeof(T). Also, the A::address member function returns an A::pointer denoting the address of an object, given an A::reference to it.

Object construction and destruction is performed separately from allocation and deallocation. The allocator is required to have two member functions, A::construct and A::destroy, which handles object construction and destruction, respectively. The semantics of the functions should be equivalent to the following:

template void A::construct(A::pointer p, A::const_reference t) { new ((void*) p) T(t); } template void A::destroy(A::pointer p){ ((T*)p)->~T; }

The above code uses the placement new syntax, and calls the destructor directly.

Allocators should be copy-constructible. An allocator for objects of type T can be constructed from an allocator for objects of type U. If an allocator, A, allocates a region of memory, R, then R can only be deallocated by an allocator that compares equal to A.

Allocators are required to supply a template class member template struct A::rebind { typedef A other; };, which enables the possibility of obtaining a related allocator, parametrized in terms of a different type. For example, given an allocator type IntAllocator for objects of type int, a related allocator type for objects of type long could be obtained using IntAllocator::rebind::other.

Read more about this topic:  Allocator (C++)