2 (c) 2014-2015 Glen Joseph Fernandes
3 <glenjofe -at- gmail.com>
5 Distributed under the Boost Software
7 http://boost.org/LICENSE_1_0.txt
13 [copyright 2014-2016 Glen Joseph Fernandes]
14 [authors [Fernandes, Glen]]
16 [license Distributed under the Boost Software License, Version 1.0.]
19 [section Introduction]
21 This library provides an alignment function, aligned allocation and
22 deallocation functions, an aligned allocator, an aligned allocator
23 adaptor, an aligned deleter, a type trait to query alignment
24 requirements, a macro to hint pointer alignment, and a function to
25 verify pointer value alignment.
27 [table The Boost.Align Library
32 [Pointer alignment function]
34 [ [`align_up`, `align_down`]
35 [Pointer and integral alignment functions]
37 [ [`aligned_alloc`, `aligned_free`]
38 [Aligned allocation and deallocation functions]
40 [ [`aligned_allocator`]
41 [Alignment aware allocator]
43 [ [`aligned_allocator_adaptor`]
44 [Alignment aware allocator adaptor]
47 [Deleter for deallocation of aligned allocations]
50 [Trait to query alignment requirement of a type]
53 [Macro for static pointer alignment hint]
56 [Pointer and integral alignment checking]
64 [heading Dynamic allocation]
66 C++11 added the ability to specify increased alignment (over-alignment)
67 for class types. Unfortunately, `::operator new` allocation functions,
68 `new` expressions, and the default allocator, `std::allocator`, do not
69 support dynamic memory allocation of over-aligned data. This library
70 provides allocation functions, allocators, allocator adaptors, and
71 deleters, that are alignment aware.
73 [table Boost.Align solutions
77 [ [`::operator new(std::size_t, const std::no_throw_t&)`]
78 [`aligned_alloc(std::size_t, std::size_t)`]
80 [ [`::operator delete(void*)`]
81 [`aligned_free(void*)`]
83 [ [`std::allocator<T>`]
84 [`aligned_allocator<T>`]
87 [`aligned_allocator_adaptor<Allocator>`]
89 [ [`std::default_delete<T>`]
94 [heading Alignment functions]
96 C++11 provided `std::align` in the standard library to align a pointer
97 value. Unfortunately some C++ standard library implementations do not
98 support it yet (libstdc++ as far as gcc 4.8.0) and other standard
99 library implementations implement it incorrectly (dinkumware in msvc
100 11.0). This library provides it for those implementations and also for
101 C++03 compilers where it is equally useful.
103 [heading Alignment traits]
105 C++11 provided the `std::alignment_of` trait in the standard library to
106 query the alignment requirement of a type. Unfortunately some C++
107 standard library vendors do not implement it in an entirely standard
108 conforming manner, such as for array types (libc++ as far as clang
109 3.4). Other vendor implementations report incorrect values for certain
110 types, such as pointer to members (msvc 14.0). This library provides it
111 for those implementations and also for C++03 compilers where it is
114 [heading Alignment hints]
116 Allocating aligned memory is sometimes not enough to ensure that
117 optimal code is generated. Developers use specific compiler intrinsics
118 to notify the compiler of a given alignment property of a memory block.
119 This library provides a macro to abstract that functionality for
120 compilers with the appropriate intrinsics.
122 [heading Alignment testing]
124 This library provides a function to test the alignment of a pointer
125 value. It is generally useful in assertions to validate that memory is
134 The alignment function can be used to find the first address of a given
135 alignment value within a given buffer of a given size. It adjusts the
136 pointer provided, returns that value, and decreases space by the amount
137 advanced, if the alignment succeeds, provided sufficient space in the
138 buffer. Otherwise it yields a null pointer to indicate failure due to
142 #include <boost/align/align.hpp>
143 #include <boost/align/alignment_of.hpp>
146 struct alignas(16) type {
150 void use(void* ptr, std::size_t size)
152 auto p = boost::alignment::align(16, sizeof(type), ptr, size);
154 auto q = ::new(p) type;
168 [section:aligned_alloc aligned_alloc and aligned_free]
170 Consider these functions alignment enabled versions of `std::malloc`,
171 `std::free` or `::operator new(std::size_t, const std::no_throw_t&)`,
172 `::operator delete(void*)`. The aligned allocation function allocates
173 space with the specified size and alignment. The aligned deallocation
174 function can then deallocate this space.
177 #include <boost/align/aligned_alloc.hpp>
181 void* p = boost::alignment::aligned_alloc(16, 100);
183 boost::alignment::aligned_free(p);
190 [section aligned_allocator]
192 Consider this class template a superior version of the default
193 allocator, `std::allocator`, because it can be used with types that are
197 #include <boost/align/aligned_allocator.hpp>
200 struct alignas(16) type {
207 boost::alignment::aligned_allocator<type> > v;
212 The optional template parameter of this class allows specifying a
213 minimum alignment to use for allocations. The default minimum alignment
217 #include <boost/align/aligned_allocator.hpp>
222 std::vector<char, boost::alignment::
223 aligned_allocator<char, 16> > v;
230 [section aligned_allocator_adaptor]
232 This class template can turn any existing allocator type, C++11 or
233 C++03, stateful or stateless, into one that supports types which are
237 #include <boost/align/aligned_allocator_adaptor.hpp>
240 template<class T, class Allocator>
247 std::vector<T, boost::alignment::
248 aligned_allocator_adaptor<Allocator> > v;
251 struct alignas(16) type {
257 utility<type, std::allocator<type> > u;
262 The optional template parameter of this class allows specifying a
263 minimum alignment to use for allocations. The default minimum alignment
267 #include <boost/align/aligned_allocator_adaptor.hpp>
270 template<class T, class Allocator, std::size_t Alignment>
277 std::vector<T, boost::alignment::
278 aligned_allocator_adaptor<Allocator, Alignment> > v;
283 utility<char, std::allocator<char>, 16> u;
290 [section aligned_delete]
292 Consider this class an alignment aware version of the class template
293 `std::default_delete`. It is a deleter that destroys the object and
294 then deallocates space using our aligned deallocation function. It
295 should be used with constructed objects that were allocated with our
296 aligned allocation function and is useful with deleter enabled types
297 like `std::unique_ptr`.
300 #include <boost/align/aligned_delete.hpp>
303 struct alignas(16) type {
309 void* p = boost::alignment::aligned_alloc(16, sizeof(type));
311 type* q = ::new(p) type;
312 std::unique_ptr<type, boost::alignment::aligned_delete>(q);
319 [section alignment_of]
321 This type trait can be used to query the alignment requirement of a
322 type at compile time.
325 #include <boost/align/alignment_of.hpp>
326 #include <type_traits>
336 std::aligned_storage_t<sizeof(T),
337 boost::alignment::alignment_of<T>::value> v;
340 struct alignas(16) type {
353 [section assume_aligned]
355 This macro is used to notify the compiler of a given pointer variable's
356 alignment. It is useful for guide optimizing compilers into vectorizing
357 or applying other compiler specific, alignment related optimizations.
360 #include <boost/align/assume_aligned.hpp>
362 void use(double* array, std::size_t size)
364 BOOST_ALIGN_ASSUME_ALIGNED(array, 16);
365 for (std::size_t i = 0; i < size; i++) {
372 alignas(16) double d[4] { };
381 This function is used to compare the alignment of a pointer. It is
382 useful in assertions that validate a pointer value is aligned on a
386 #include <boost/align/is_aligned.hpp>
391 assert(boost::alignment::is_aligned(ptr, 16));
396 alignas(16) char c[64];
407 [section:aligned_ptr aligned_ptr and make_aligned]
409 This example presents an alternative to `std::unique_ptr` for objects
410 allocated with the aligned allocation function. It is defined simply by
411 providing an alias template which uses `std::unique_ptr` with our
412 aligned deleter in place of the default `std::default_delete` deleter.
413 It also presents an alternative to `std::make_unique` for the creation
414 of these aligned unique pointer objects. It is implemented using our
415 aligned allocation function.
417 [heading Implementation]
420 #include <boost/align/aligned_alloc.hpp>
421 #include <boost/align/aligned_delete.hpp>
422 #include <boost/align/alignment_of.hpp>
426 using aligned_ptr = std::unique_ptr<T,
427 boost::alignment::aligned_delete>;
429 template<class T, class... Args>
430 inline aligned_ptr<T> make_aligned(Args&&... args)
432 auto p = boost::alignment::aligned_alloc(boost::
433 alignment::alignment_of<T>::value, sizeof(T));
435 throw std::bad_alloc();
438 auto q = ::new(p) T(std::forward<Args>(args)...);
439 return aligned_ptr<T>(q);
441 boost::alignment::aligned_free(p);
450 struct alignas(16) type {
456 auto p = make_aligned<type>();
463 [section aligned_vector]
465 This example presents an alternative to `std::vector` that can be used
466 with over-aligned types, and allows specifying a minimum alignment. It
467 is defined simply by providing an alias template which uses
468 `std::vector` with our aligned allocator.
470 [heading Implementation]
473 #include <boost/align/aligned_allocator.hpp>
476 template<class T, std::size_t Alignment = 1>
477 using aligned_vector = std::vector<T,
478 boost::alignment::aligned_allocator<T, Alignment> >;
491 aligned_vector<char, cache_line> v;
504 The alignment function is used to obtain a pointer to the first address
505 within the specified buffer that is a multiple of the specified
506 alignment value. This function exists in the C++11 standard library but
507 is provided in this library for those C++11 and C++03 library
508 implementations which do not yet support it.
510 [heading:synopsis Header <boost/align/align.hpp>]
514 namespace alignment {
515 void* align(std::size_t alignment, std::size_t size, void*& ptr,
521 [heading:align Function align]
524 void* align(std::size_t alignment, std::size_t size, void*& ptr,
528 [*Effects:] If it is possible to fit `size` bytes of storage aligned
529 by `alignment` into the buffer pointed to by `ptr` with length
530 `space`, the function updates `ptr` to point to the first possible
531 address of such storage and decreases `space` by the number of bytes
532 used for alignment. Otherwise, the function does nothing.
536 [`alignment` shall be a fundamental alignment value or an extended
537 alignment value, and shall be a power of two]
538 [`ptr` shall point to contiguous storage of at least `space` bytes]
541 [*Returns:] A null pointer if the requested aligned buffer would not
542 fit into the available space, otherwise the adjusted value of `ptr`.
544 [*Note:] The function updates its `ptr` and `space` arguments so that
545 it can be called repeatedly with possibly different `alignment` and
546 `size` arguments for the same buffer.
550 [section:align_down align_down]
552 The directional alignment functions can be used with pointers or
553 integral values to align down. This functionality is not yet provided by
556 [heading:synopsis Header <boost/align/align_down.hpp>]
560 namespace alignment {
561 constexpr std::size_t align_down(std::size_t value, std::size_t
564 void* align_down(void* ptr, std::size_t alignment) noexcept;
569 [heading:align_down Function align_down]
572 void* align_down(void* ptr, std::size_t alignment) noexcept;
577 [`ptr` shall point to contiguous storage with sufficient space
579 [`alignment` shall be a power of two]
582 [*Returns:] A pointer value at or before `ptr` that is at least
583 `alignment` bytes aligned.
587 [section:align_up align_up]
589 The directional alignment functions can be used with pointers or
590 integral values to align up. This functionality is not yet provided by
593 [heading:synopsis Header <boost/align/align_up.hpp>]
597 namespace alignment {
598 constexpr std::size_t align_up(std::size_t value, std::size_t
601 void* align_up(void* ptr, std::size_t alignment) noexcept;
606 [heading:align_up Function align_up]
609 void* align_up(void* ptr, std::size_t alignment) noexcept;
614 [`ptr` shall point to contiguous storage with sufficient space
616 [`alignment` shall be a power of two]
619 [*Returns:] A pointer value at or after `ptr` that is at least
620 `alignment` bytes aligned.
624 [section:aligned_alloc aligned_alloc and aligned_free]
626 The aligned allocation function is a replacement for
627 `::operator new(std::size_t, const std::no_throw_t&)` that allows
628 requesting aligned memory. The deallocation function replaces the
629 corresponding `::operator delete(void*)` function. This functionality
630 is not yet provided by the C++ standard.
632 [heading:synopsis Header <boost/align/aligned_alloc.hpp>]
636 namespace alignment {
637 void* aligned_alloc(std::size_t alignment, std::size_t size);
639 void aligned_free(void* ptr);
644 [heading:aligned_alloc Function aligned_alloc]
647 void* aligned_alloc(std::size_t alignment, std::size_t size);
650 [*Effects:] Allocates space for an object whose alignment is
651 specified by `alignment`, whose size is specified by `size`, and
652 whose value is indeterminate. The value of `alignment` shall be a
655 [*Requires:] `alignment` shall be a power of two.
657 [*Returns:] A null pointer or a pointer to the allocated space.
659 [*Note:] On certain platforms, the alignment may be rounded up to
660 `alignof(void*)` and the space allocated may be slightly larger than
661 `size` bytes, by an additional `sizeof(void*)` and `alignment - 1`
664 [heading:aligned_free Function aligned_free]
667 void aligned_free(void* ptr);
670 [*Effects:] Causes the space pointed to by `ptr` to be deallocated,
671 that is, made available for further allocation. If `ptr` is a null
672 pointer, no action occurs. Otherwise, if the argument does not match
673 a pointer earlier returned by the `aligned_alloc` function, or if the
674 space has been deallocated by a call to `aligned_free`, the behavior
677 [*Requires:] `ptr` is a null pointer or a pointer earlier returned by
678 the `aligned_alloc` function that has not been deallocated by a call
681 [*Returns:] The `aligned_free` function returns no value.
685 [section aligned_allocator]
687 The aligned allocator is a replacement for the default allocator,
688 `std::allocator`, that supports value types which are over-aligned. It
689 also allows specifying a minimum alignment value used for all
690 allocations, via the optional template parameter. An alignment aware
691 allocator is not yet provided by the C++ standard.
693 [tip Using the aligned allocator with a minimum alignment value is
694 generally only suitable with containers that are not node-based such
695 as `std::vector`. With node-based containers, such as list, the node
696 object would have the minimum alignment instead of the value type
699 [heading:synopsis Header <boost/align/aligned_allocator.hpp>]
703 namespace alignment {
704 template<class T, std::size_t Alignment = 1>
705 class aligned_allocator;
707 template<std::size_t Alignment>
708 class aligned_allocator<void, Alignment>;
710 template<class T1, class T2, std::size_t Alignment>
711 bool operator==(const aligned_allocator<T1,
712 Alignment>&, const aligned_allocator<T2,
713 Alignment>&) noexcept;
715 template<class T1, class T2, std::size_t Alignment>
716 bool operator!=(const aligned_allocator<T1,
717 Alignment>&, const aligned_allocator<T2,
718 Alignment>&) noexcept;
723 [heading:aligned_allocator Class template aligned_allocator]
726 template<class T, std::size_t Alignment = 1>
727 class aligned_allocator {
729 typedef T value_type;
731 typedef const T* const_pointer;
732 typedef void* void_pointer;
733 typedef const void* const_void_pointer;
734 typedef std::size_t size_type;
735 typedef std::ptrdiff_t difference_type;
736 typedef T& reference;
737 typedef const T& const_reference;
741 typedef aligned_allocator<U, Alignment> other;
744 aligned_allocator() = default;
747 aligned_allocator(const aligned_allocator<U, Alignment>&) noexcept;
749 pointer address(reference value) const noexcept;
751 const_pointer address(const_reference value) const noexcept;
753 pointer allocate(size_type size, const_void_pointer = 0);
755 void deallocate(pointer ptr, size_type);
757 size_type max_size() const noexcept;
759 template<class U, class... Args>
760 void construct(U* ptr, Args&&... args);
763 void destroy(U* ptr);
766 template<std::size_t Alignment>
767 class aligned_allocator<void, Alignment> {
769 typedef void value_type;
770 typedef void* pointer;
771 typedef const void* const_pointer;
775 typedef aligned_allocator<U, Alignment> other;
782 Except for the destructor, member functions of the aligned allocator
783 shall not introduce data races as a result of concurrent calls to those
784 member functions from different threads. Calls to these functions that
785 allocate or deallocate a particular unit of storage shall occur in a
786 single total order, and each such deallocation call shall happen before
787 the next allocation (if any) in this order.
790 pointer address(reference value) const noexcept;
793 [*Returns:] The actual address of the object referenced by `value`,
794 even in the presence of an overloaded `operator&`.
797 const_pointer address(const_reference value) const noexcept;
800 [*Returns:] The actual address of the object referenced by `value`,
801 even in the presence of an overloaded `operator&`.
804 pointer allocate(size_type size, const_void_pointer = 0);
807 [*Returns:] A pointer to the initial element of an array of storage
808 of size `n * sizeof(T)`, aligned on the maximum of the minimum
809 alignment specified and the alignment of objects of type `T`.
811 [*Remark:] The storage is obtained by calling
812 `aligned_alloc(std::size_t, std::size_t)`.
814 [*Throws:] `std::bad_alloc` if the storage cannot be obtained.
817 void deallocate(pointer ptr, size_type);
820 [*Requires:] `ptr` shall be a pointer value obtained from
823 [*Effects:] Deallocates the storage referenced by `ptr`.
825 [*Remark:] Uses `alignment::aligned_free(void*)`.
828 size_type max_size() const noexcept;
831 [*Returns:] The largest value `N` for which the call `allocate(N)`
835 template<class U, class... Args>
836 void construct(U* ptr, Args&&... args);
839 [*Effects:] `::new((void*)ptr) U(std::forward<Args>(args)...)`
843 void destroy(U* ptr);
846 [*Effects:] `ptr->~U()`
851 template<class T1, class T2, std::size_t Alignment>
852 bool operator==(const aligned_allocator<T1,
853 Alignment>&, const aligned_allocator<T2,
854 Alignment>&) noexcept;
860 template<class T1, class T2, std::size_t Alignment>
861 bool operator!=(const aligned_allocator<T1,
862 Alignment>&, const aligned_allocator<T2,
863 Alignment>&) noexcept;
870 [section aligned_allocator_adaptor]
872 The aligned allocator adaptor can turn any existing C++03 or C++11
873 allocator into one that supports value types that are over-aligned. It
874 also allows specifying a minimum alignment value used for all
875 allocations, via the optional template parameter. An alignment aware
876 allocator adaptor is not yet provided by the C++ standard.
878 [tip This adaptor can be used with a C++11 allocator whose pointer
879 type is a smart pointer but the adaptor can choose to expose only raw
882 [heading:synopsis Header <boost/align/aligned_allocator_adaptor.hpp>]
886 namespace alignment {
887 template<class Allocator, std::size_t Alignment = 1>
888 class aligned_allocator_adaptor;
890 template<class A1, class A2, std::size_t Alignment>
891 bool operator==(const aligned_allocator_adaptor<A1,
892 Alignment>& a1, const aligned_allocator_adaptor<A2,
893 Alignment>& a2) noexcept;
895 template<class A1, class A2, std::size_t Alignment>
896 bool operator!=(const aligned_allocator_adaptor<A1,
897 Alignment>& a1, const aligned_allocator_adaptor<A2,
898 Alignment>& a2) noexcept;
903 [heading:aligned_allocator_adaptor Class template
904 aligned_allocator_adaptor]
907 template<class Allocator, std::size_t Alignment = 1>
908 class aligned_allocator_adaptor
910 typedef std::allocator_traits<Allocator> Traits; // exposition
913 typedef typename Traits::value_type value_type;
914 typedef typename Traits::size_type size_type;
915 typedef value_type* pointer;
916 typedef const value_type* const_pointer;
917 typedef void* void_pointer;
918 typedef const void* const_void_pointer;
919 typedef std::ptrdiff_t difference_type;
923 typedef aligned_allocator_adaptor<typename Traits::template
924 rebind_alloc<U>, Alignment> other;
927 aligned_allocator_adaptor() = default;
930 explicit aligned_allocator_adaptor(A&& alloc) noexcept;
933 aligned_allocator_adaptor(const aligned_allocator_adaptor<U,
934 Alignment>& other) noexcept;
936 Allocator& base() noexcept;
938 const Allocator& base() const noexcept;
940 pointer allocate(size_type size);
942 pointer allocate(size_type size, const_void_pointer hint);
944 void deallocate(pointer ptr, size_type size);
948 [heading Constructors]
951 aligned_allocator_adaptor() = default;
954 [*Effects:] Value-initializes the `Allocator` base class.
958 explicit aligned_allocator_adaptor(A&& alloc) noexcept;
961 [*Requires:] `Allocator` shall be constructible from `A`.
963 [*Effects:] Initializes the `Allocator` base class with
964 `std::forward<A>(alloc)`.
968 aligned_allocator_adaptor(const aligned_allocator_adaptor<U,
969 Alignment>& other) noexcept;
972 [*Requires:] `Allocator` shall be constructible from `A`.
974 [*Effects:] Initializes the `Allocator` base class with
980 Allocator& base() noexcept;
983 [*Returns:] `static_cast<Allocator&>(*this)`
986 const Allocator& base() const noexcept;
989 [*Returns:] `static_cast<const Allocator&>(*this)`
992 pointer allocate(size_type size);
995 [*Returns:] A pointer to the initial element of an array of storage
996 of size `n * sizeof(value_type)`, aligned on the maximum of the
997 minimum alignment specified and the alignment of objects of type
1000 [*Remark:] The storage is obtained by calling `A2::allocate` on an
1001 object `a2`, where `a2` of type `A2` is a rebound copy of `base()`
1002 where its `value_type` is unspecified.
1004 [*Throws:] Throws an exception thrown from `A2::allocate` if the
1005 storage cannot be obtained.
1008 pointer allocate(size_type size, const_void_pointer hint);
1011 [*Requires:] `hint` is a value obtained by calling `allocate()` on
1012 any equivalent aligned allocator adaptor object, or else `nullptr`.
1014 [*Returns:] A pointer to the initial element of an array of storage
1015 of size `n * sizeof(value_type)`, aligned on the maximum of the
1016 minimum alignment specified and the alignment of objects of type
1019 [*Remark:] The storage is obtained by calling `A2::allocate` on an
1020 object `a2`, where `a2` of type `A2` is a rebound copy of `base()`
1021 where its `value_type` is unspecified.
1023 [*Throws:] Throws an exception thrown from `A2::allocate` if the
1024 storage cannot be obtained.
1027 void deallocate(pointer ptr, size_type size);
1032 [`ptr` shall be a pointer value obtained from `allocate()`]
1033 [`size` shall equal the value passed as the first argument to the
1034 invocation of `allocate()` which returned `ptr`]
1037 [*Effects:] Deallocates the storage referenced by `ptr`.
1039 [*Note:] Uses `A2::deallocate` on an object `a2`, where `a2` of
1040 type `A2` is a rebound copy of `base()` where its `value_type` is
1046 template<class A1, class A2, std::size_t Alignment>
1047 bool operator==(const aligned_allocator_adaptor<A1,
1048 Alignment>& a1, const aligned_allocator_adaptor<A2,
1049 Alignment>& a2) noexcept;
1052 [*Returns:] `a1.base() == a2.base()`
1055 template<class A1, class A2, std::size_t Alignment>
1056 bool operator!=(const aligned_allocator_adaptor<A1,
1057 Alignment>& a1, const aligned_allocator_adaptor<A2,
1058 Alignment>& a2) noexcept;
1061 [*Returns:] `!(a1 == a2)`
1065 [section aligned_delete]
1067 The aligned deleter class is convenient utility for destroying and
1068 then deallocating the constructed objects that were allocated using
1069 aligned allocation function provided in this library. It serves as
1070 a replacement for the `std::default_delete` class for this case.
1072 [heading:synopsis Header <boost/align/aligned_delete.hpp>]
1076 namespace alignment {
1077 class aligned_delete;
1082 [heading:aligned_delete Class aligned_delete]
1085 class aligned_delete {
1088 void operator()(T* ptr) const noexcept(noexcept(ptr->~T()));
1096 void operator()(T* ptr) const noexcept(noexcept(ptr->~T()));
1099 [*Effects:] Calls `~T()` on `ptr` to destroy the object and then
1100 calls `alignment::aligned_free` on `ptr` to free the allocated
1103 [*Note:] If `T` is an incomplete type, the program is ill-formed.
1107 [section alignment_of]
1109 The alignment type trait is used to query the alignment requirement of
1110 a type at compile time. It is provided by the C++11 standard library
1111 but is provided in this library for C++11 and C++03 implementations
1112 that do not provide this functionality.
1114 [heading:synopsis Header <boost/align/alignment_of.hpp>]
1118 namespace alignment {
1120 struct alignment_of;
1123 constexpr std::size_t alignment_of_v = alignment_of<T>::value;
1128 [heading:alignment_of Type trait alignment_of]
1132 struct alignment_of;
1135 [*Value:] The alignment requirement of the type `T` as an integral
1136 constant of type `std::size_t`. When `T` is a reference array type,
1137 the value shall be the alignment of the referenced type. When `T` is
1138 an array type, the value shall be the alignment of the element type.
1140 [*Requires:] `T` shall be a complete object type, or an array
1141 thereof, or a reference to one of those types.
1145 [section assume_aligned]
1147 The alignment hint macro can be used to inform the compiler of the
1148 alignment of a memory block, to enable vectorizing or other compiler
1149 specific alignment related optimizations.
1151 [heading:synopsis Header <boost/align/assume_aligned.hpp>]
1154 BOOST_ALIGN_ASSUME_ALIGNED(ptr, alignment)
1157 [heading:assume_aligned Macro BOOST_ALIGN_ASSUME_ALIGNED]
1160 BOOST_ALIGN_ASSUME_ALIGNED(ptr, alignment)
1165 [`alignment` shall be a power of two]
1166 [`ptr` shall be mutable]
1169 [*Effect:] `ptr` may be modified in an implementation specific way to
1170 inform the compiler of its alignment.
1174 [section is_aligned]
1176 The alignment validation function indicates whether or not an address
1177 is a multiple of the specified alignment value. It is generally useful
1178 in assertions to verify memory is correctly aligned. This functionality
1179 is not yet provided by the C++ standard.
1181 [heading:synopsis Header <boost/align/is_aligned.hpp>]
1185 namespace alignment {
1186 constexpr bool is_aligned(std::size_t value, std::size_t
1187 alignment) noexcept;
1189 bool is_aligned(const void* ptr, std::size_t alignment) noexcept;
1194 [heading:is_aligned Function is_aligned]
1197 bool is_aligned(const void* ptr, std::size_t alignment) noexcept;
1200 [*Requires:] `alignment` shall be a power of two.
1202 [*Returns:] `true` if the value of `ptr` is aligned on the boundary
1203 specified by `alignment`, otherwise `false`.
1209 [section Vocabulary]
1211 [heading:basic_align \[basic.align\]]
1213 Object types have *alignment requirements* which place restrictions on
1214 the addresses at which an object of that type may be allocated. An
1215 *alignment* is an implementation-defined integer value representing the
1216 number of bytes between successive addresses at which a given object
1217 can be allocated. An object type imposes an alignment requirement on
1218 every object of that type; stricter alignment can be requested using
1219 the alignment specifier.
1221 A *fundamental alignment* is represented by an alignment less than or
1222 equal to the greatest alignment supported by the implementation in all
1223 contexts, which is equal to `alignof(std::max_align_t)`. The alignment
1224 required for a type might be different when it is used as the type of a
1225 complete object and when it is used as the type of a subobject.
1229 struct B { long double d; };
1230 struct D : virtual B { char c; };
1232 When `D` is the type of a complete object, it will have a subobject
1233 of type `B`, so it must be aligned appropriately for a
1234 `long double`. If `D` appears as a subobject of another object that
1235 also has `B` as a virtual base class, the `B` subobject might be
1236 part of a different subobject, reducing the alignment requirements
1237 on the `D` subobject.]
1239 The result of the `alignof` operator reflects the alignment requirement
1240 of the type in the complete-object case.
1242 An *extended alignment* is represented by an alignment greater than
1243 `alignof(std::max_align_t)`. It is implementation-defined whether any
1244 extended alignments are supported and the contexts in which they are
1245 supported. A type having an extended alignment requirement is an
1246 *over-aligned type*.
1248 [note Every over-aligned type is or contains a class type to which
1249 extended alignment applies (possibly through a non-static data
1252 Alignments are represented as values of the type `std::size_t`. Valid
1253 alignments include only those values returned by an `alignof`
1254 expression for the fundamental types plus an additional
1255 implementation-defined set of values, which may be empty. Every
1256 alignment value shall be a non-negative integral power of two.
1258 Alignments have an order from *weaker* to *stronger* or *stricter*
1259 alignments. Stricter alignments have larger alignment values. An
1260 address that satisfies an alignment requirement also satisfies any
1261 weaker valid alignment requirement.
1263 The alignment requirement of a complete type can be queried using an
1264 `alignof` expression. Furthermore, the types `char`, `signed char`,
1265 and `unsigned char` shall have the weakest alignment requirement.
1267 [note This enables the character types to be used as the underlying
1268 type for an aligned memory area.]
1270 Comparing alignments is meaningful and provides the obvious results:
1272 * Two alignments are equal when their numeric values are equal.
1273 * Two alignments are different when their numeric values are not equal.
1274 * When an alignment is larger than another it represents a stricter
1277 [note The runtime pointer alignment function can be used to obtain an
1278 aligned pointer within a buffer; the aligned-storage templates in
1279 the library can be used to obtain aligned storage.]
1281 If a request for a specific extended alignment in a specific context is
1282 not supported by an implementation, the program is ill-formed.
1283 Additionally, a request for runtime allocation of dynamic storage for
1284 which the requested alignment cannot be honored shall be treated as an
1289 [section Compatibility]
1291 Boost.Align has been tested with GCC, Clang, Visual C++, and Intel C++,
1292 on Linux, Windows, and OS X. It supports any conforming C++11 or C++03
1295 [table Compilers Tested
1334 [section Acknowledgments]
1336 Glen Fernandes authored the library and contributed it to the Boost C++
1339 [heading Library Reviews]
1341 Thank you to the following developers who reviewed the design, code,
1342 examples, tests, or documentation.
1353 Thank you to Ahmed Charles who served as review manager for the formal
1354 review of the library.
1356 [heading Contributions]
1358 Thank you to Joel Falcou and Charly Chevalier for both your contribution
1365 [heading Boost 1.61]
1367 * Added functions for aligning up and aligning down of pointer and
1370 [heading Boost 1.59]
1372 * Joel Falcou and Charly Chevalier together contributed the alignment
1375 [heading Boost 1.56]
1377 * Glen Fernandes implemented the Boost.Align library.