Boost
C++ Libraries
...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
This is an older version of Boost and was released in 2018. The current version is 1.89.0.
To dynamically allocate storage with desired alignment, you can use the aligned_alloc function:
void*
storage =
boost::alignment::aligned_alloc(alignment,
size);
To deallocate storage allocated with the aligned_alloc
function, use the aligned_free
function:
boost::alignment::aligned_free(storage);
For C++ allocator aware code, you can use the aligned_allocator
class template for an allocator that respects over-alignment:
std::vector<int128_t, boost::alignment::aligned_allocator<int128_t>
> vector;
This template allows specifying minimum alignment for all dynamic allocations:
std::vector<double, boost::alignment::aligned_allocator<double, 64> >
vector;
To turn an allocator into an allocator that respects over-alignment, you can
use the aligned_allocator_adaptor
class template:
boost::alignment::aligned_allocator_adaptor<First> second(first);
This template allows specifying minimum alignment for all dynamic allocations:
boost::alignment::aligned_allocator_adaptor<First, 64> second(first);
For a deleter that can be paired with aligned_alloc,
you can use the aligned_delete
class:
std::unique_ptr<double, boost::alignment::aligned_delete>
pointer;
To advance a pointer to the next address with the desired alignment:
void*
pointer =
storage;
std::size_t space
= size;
void*
result =
boost::alignment::align(64, sizeof(double), pointer, space);
To obtain the alignment of a given type at compie time, you can use:
boost::alignment::alignment_of<int128_t>::value
If your compiler supports C++14 variable templates, you can also use:
boost::alignment::alignment_of_v<int128_t>
To inform the compiler about the alignment of a pointer, you can use:
BOOST_ALIGN_ASSUME_ALIGNED(pointer, 64)
To check alignment of a pointer you can use the is_aligned
function:
assert(boost::alignment::is_aligned(pointer, 64));