...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
boost::container::allocator
// In header: <boost/container/allocator.hpp> template<typename T> class allocator { public: // types 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; typedef unspecified version; // member classes/structs/unions template<typename T2> struct rebind { // types typedef allocator< T2, Version, AllocationDisableMask > other; }; // construct/copy/destruct allocator() noexcept; allocator(const allocator &) noexcept; template<typename T2> allocator(const allocator< T2 > &) noexcept; // public member functions pointer allocate(size_type, const void * = 0); void deallocate(pointer, size_type) noexcept; size_type max_size() const noexcept; std::pair< pointer, bool > allocation_command(allocation_type, size_type, size_type, size_type &, pointer = pointer()); size_type size(pointer) const noexcept; pointer allocate_one(); void allocate_individual(std::size_t, multiallocation_chain &); void deallocate_one(pointer) noexcept; void deallocate_individual(multiallocation_chain &) noexcept; void allocate_many(size_type, std::size_t, multiallocation_chain &); void allocate_many(const size_type *, size_type, multiallocation_chain &); void deallocate_many(multiallocation_chain &) noexcept; // friend functions friend void swap(self_t &, self_t &) noexcept; friend bool operator==(const allocator &, const allocator &) noexcept; friend bool operator!=(const allocator &, const allocator &) noexcept; // private member functions std::pair< pointer, bool > priv_allocation_command(allocation_type, std::size_t, std::size_t, std::size_t &, void *); };
allocator
public member functionspointer allocate(size_type count, const void * hint = 0);
Allocates memory for an array of count elements. Throws std::bad_alloc if there is no enough memory If Version is 2, this allocated memory can only be deallocated with deallocate() or (for Version == 2) deallocate_many()
void deallocate(pointer ptr, size_type) noexcept;
Deallocates previously allocated memory. Never throws
size_type max_size() const noexcept;
Returns the maximum number of elements that could be allocated. Never throws
std::pair< pointer, bool > allocation_command(allocation_type command, size_type limit_size, size_type preferred_size, size_type & received_size, pointer reuse = pointer());
An advanced function that offers in-place expansion shrink to fit and new allocation capabilities. Memory allocated with this function can only be deallocated with deallocate() or deallocate_many(). This function is available only with Version == 2
size_type size(pointer p) const noexcept;
Returns maximum the number of objects the previously allocated memory pointed by p can hold. Memory must not have been allocated with allocate_one or allocate_individual. This function is available only with Version == 2
pointer allocate_one();
Allocates just one object. Memory allocated with this function must be deallocated only with deallocate_one(). Throws bad_alloc if there is no enough memory This function is available only with Version == 2
void allocate_individual(std::size_t num_elements, multiallocation_chain & chain);
Allocates many elements of size == 1. Elements must be individually deallocated with deallocate_one() This function is available only with Version == 2
void deallocate_one(pointer p) noexcept;
Deallocates memory previously allocated with allocate_one(). You should never use deallocate_one to deallocate memory allocated with other functions different from allocate_one() or allocate_individual.
void deallocate_individual(multiallocation_chain & chain) noexcept;
Deallocates memory allocated with allocate_one() or allocate_individual(). This function is available only with Version == 2
void allocate_many(size_type elem_size, std::size_t n_elements, multiallocation_chain & chain);
Allocates many elements of size elem_size. Elements must be individually deallocated with deallocate() This function is available only with Version == 2
void allocate_many(const size_type * elem_sizes, size_type n_elements, multiallocation_chain & chain);
Allocates n_elements elements, each one of size elem_sizes[i] Elements must be individually deallocated with deallocate() This function is available only with Version == 2
void deallocate_many(multiallocation_chain & chain) noexcept;
Deallocates several elements allocated by allocate_many(), allocate(), or allocation_command(). This function is available only with Version == 2
allocator
friend functionsfriend void swap(self_t &, self_t &) noexcept;
Swaps two allocators, does nothing because this allocator is stateless
friend bool operator==(const allocator &, const allocator &) noexcept;
An allocator always compares to true, as memory allocated with one instance can be deallocated by another instance
friend bool operator!=(const allocator &, const allocator &) noexcept;
An allocator always compares to false, as memory allocated with one instance can be deallocated by another instance