...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
boost::alignment::aligned_allocator
// In header: <boost/align/aligned_allocator.hpp> template<typename T, std::size_t Alignment> class aligned_allocator { public: // types typedef T value_type; typedef T * pointer; typedef const T * const_pointer; typedef void * void_pointer; typedef const void * const_void_pointer; typedef std::size_t size_type; typedef std::ptrdiff_t difference_type; typedef T & reference; typedef const T & const_reference; // member classes/structs/unions template<typename U> struct rebind { // types typedef aligned_allocator< U, Alignment > other; }; // construct/copy/destruct aligned_allocator(); template<typename U> aligned_allocator(const aligned_allocator< U, Alignment > &) noexcept; // public member functions pointer address(reference) const noexcept; const_pointer address(const_reference) const noexcept; pointer allocate(size_type, const_void_pointer = 0); void deallocate(pointer, size_type); BOOST_CONSTEXPR size_type max_size() const noexcept; template<typename U, class... Args> void construct(U *, Args &&...); template<typename U> void construct(U *); template<typename U> void destroy(U *); };
Class template aligned_allocator.
Note: Except for the destructor, member functions of the aligned allocator shall not introduce data races as a result of concurrent calls to those member functions from different threads. Calls to these functions that allocate or deallocate a particular unit of storage shall occur in a single total order, and each such deallocation call shall happen before the next allocation (if any) in this order.
Note | |
---|---|
Specifying minimum alignment is generally only suitable for containers such as vector and undesirable with other, node-based, containers. For node-based containers, such as list, the node object would have the minimum alignment specified instead of the value type object. |
typename T
std::size_t Alignment
Is the minimum alignment to specify for allocations, if it is larger than the alignment of the value type. It shall be a power of two.
aligned_allocator
public
construct/copy/destructaligned_allocator();
template<typename U> aligned_allocator(const aligned_allocator< U, Alignment > &) noexcept;
aligned_allocator
public member functionspointer address(reference value) const noexcept;
Returns: |
The actual address of the object referenced by |
const_pointer address(const_reference value) const noexcept;
Returns: |
The actual address of the object referenced by |
pointer allocate(size_type size, const_void_pointer = 0);
Throw: Throws std::bad_alloc
if the storage cannot be obtained.
Note: The storage is obtained by calling aligned_alloc(std::size_t, std::size_t)
.
Returns: |
A pointer to the initial element of an array of storage of size |
void deallocate(pointer ptr, size_type);
Deallocates the storage referenced by ptr
.
Note: Uses alignment::aligned_free(void*)
.
Parameters: |
|
BOOST_CONSTEXPR size_type max_size() const noexcept;
Returns: |
The largest value |
template<typename U, class... Args> void construct(U * ptr, Args &&... args);
Calls global new((void*)ptr) U(std::forward<Args>(args)...)
.
template<typename U> void construct(U * ptr);
Calls global new((void*)ptr) U()
.
template<typename U> void destroy(U * ptr);
Calls ptr->~U()
.