]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/align/doc/align.qbk
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / align / doc / align.qbk
1 [/
2 (c) 2014-2015 Glen Joseph Fernandes
3 <glenjofe -at- gmail.com>
4
5 Distributed under the Boost Software
6 License, Version 1.0.
7 http://boost.org/LICENSE_1_0.txt
8 ]
9
10 [library Boost.Align
11 [quickbook 1.6]
12 [id align]
13 [copyright 2014-2016 Glen Joseph Fernandes]
14 [authors [Fernandes, Glen]]
15 [dirname align]
16 [license Distributed under the Boost Software License, Version 1.0.]
17 ]
18
19 [section Introduction]
20
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.
26
27 [table The Boost.Align Library
28 [ [Component]
29 [Description]
30 ]
31 [ [`align`]
32 [Pointer alignment function]
33 ]
34 [ [`align_up`, `align_down`]
35 [Pointer and integral alignment functions]
36 ]
37 [ [`aligned_alloc`, `aligned_free`]
38 [Aligned allocation and deallocation functions]
39 ]
40 [ [`aligned_allocator`]
41 [Alignment aware allocator]
42 ]
43 [ [`aligned_allocator_adaptor`]
44 [Alignment aware allocator adaptor]
45 ]
46 [ [`aligned_delete`]
47 [Deleter for deallocation of aligned allocations]
48 ]
49 [ [`alignment_of`]
50 [Trait to query alignment requirement of a type]
51 ]
52 [ [`assume_aligned`]
53 [Macro for static pointer alignment hint]
54 ]
55 [ [`is_aligned`]
56 [Pointer and integral alignment checking]
57 ]
58 ]
59
60 [endsect]
61
62 [section Rationale]
63
64 [heading Dynamic allocation]
65
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.
72
73 [table Boost.Align solutions
74 [ [Problem]
75 [Solution]
76 ]
77 [ [`::operator new(std::size_t, const std::no_throw_t&)`]
78 [`aligned_alloc(std::size_t, std::size_t)`]
79 ]
80 [ [`::operator delete(void*)`]
81 [`aligned_free(void*)`]
82 ]
83 [ [`std::allocator<T>`]
84 [`aligned_allocator<T>`]
85 ]
86 [ [`Allocator`]
87 [`aligned_allocator_adaptor<Allocator>`]
88 ]
89 [ [`std::default_delete<T>`]
90 [`aligned_delete`]
91 ]
92 ]
93
94 [heading Alignment functions]
95
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.
102
103 [heading Alignment traits]
104
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
112 equally useful.
113
114 [heading Alignment hints]
115
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.
121
122 [heading Alignment testing]
123
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
126 correctly aligned.
127
128 [endsect]
129
130 [section Tutorial]
131
132 [section align]
133
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
139 insufficient space.
140
141 ``
142 #include <boost/align/align.hpp>
143 #include <boost/align/alignment_of.hpp>
144 #include <new>
145
146 struct alignas(16) type {
147 float data[4];
148 };
149
150 void use(void* ptr, std::size_t size)
151 {
152 auto p = boost::alignment::align(16, sizeof(type), ptr, size);
153 if (p) {
154 auto q = ::new(p) type;
155 q->~type();
156 }
157 }
158
159 int main()
160 {
161 char c[64];
162 use(c, sizeof c);
163 }
164 ``
165
166 [endsect]
167
168 [section:aligned_alloc aligned_alloc and aligned_free]
169
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.
175
176 ``
177 #include <boost/align/aligned_alloc.hpp>
178
179 int main()
180 {
181 void* p = boost::alignment::aligned_alloc(16, 100);
182 if (p) {
183 boost::alignment::aligned_free(p);
184 }
185 }
186 ``
187
188 [endsect]
189
190 [section aligned_allocator]
191
192 Consider this class template a superior version of the default
193 allocator, `std::allocator`, because it can be used with types that are
194 over-aligned.
195
196 ``
197 #include <boost/align/aligned_allocator.hpp>
198 #include <vector>
199
200 struct alignas(16) type {
201 float data[4];
202 };
203
204 int main()
205 {
206 std::vector<type,
207 boost::alignment::aligned_allocator<type> > v;
208 v.emplace_back();
209 }
210 ``
211
212 The optional template parameter of this class allows specifying a
213 minimum alignment to use for allocations. The default minimum alignment
214 is 1.
215
216 ``
217 #include <boost/align/aligned_allocator.hpp>
218 #include <vector>
219
220 int main()
221 {
222 std::vector<char, boost::alignment::
223 aligned_allocator<char, 16> > v;
224 v.emplace_back();
225 }
226 ``
227
228 [endsect]
229
230 [section aligned_allocator_adaptor]
231
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
234 over-aligned.
235
236 ``
237 #include <boost/align/aligned_allocator_adaptor.hpp>
238 #include <vector>
239
240 template<class T, class Allocator>
241 class utility {
242 public:
243 void add() {
244 v.emplace_back();
245 }
246 private:
247 std::vector<T, boost::alignment::
248 aligned_allocator_adaptor<Allocator> > v;
249 };
250
251 struct alignas(16) type {
252 float data[4];
253 };
254
255 int main()
256 {
257 utility<type, std::allocator<type> > u;
258 u.add();
259 }
260 ``
261
262 The optional template parameter of this class allows specifying a
263 minimum alignment to use for allocations. The default minimum alignment
264 is 1.
265
266 ``
267 #include <boost/align/aligned_allocator_adaptor.hpp>
268 #include <vector>
269
270 template<class T, class Allocator, std::size_t Alignment>
271 class utility {
272 public:
273 void add() {
274 v.v.emplace_back();
275 }
276 private:
277 std::vector<T, boost::alignment::
278 aligned_allocator_adaptor<Allocator, Alignment> > v;
279 };
280
281 int main()
282 {
283 utility<char, std::allocator<char>, 16> u;
284 u.add();
285 }
286 ``
287
288 [endsect]
289
290 [section aligned_delete]
291
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`.
298
299 ``
300 #include <boost/align/aligned_delete.hpp>
301 #include <memory>
302
303 struct alignas(16) type {
304 float data[4];
305 };
306
307 int main()
308 {
309 void* p = boost::alignment::aligned_alloc(16, sizeof(type));
310 if (p) {
311 type* q = ::new(p) type;
312 std::unique_ptr<type, boost::alignment::aligned_delete>(q);
313 }
314 }
315 ``
316
317 [endsect]
318
319 [section alignment_of]
320
321 This type trait can be used to query the alignment requirement of a
322 type at compile time.
323
324 ``
325 #include <boost/align/alignment_of.hpp>
326 #include <type_traits>
327
328 template<class T>
329 class utility {
330 public:
331 void construct() {
332 void* p = &v;
333 ::new(p) T();
334 }
335 private:
336 std::aligned_storage_t<sizeof(T),
337 boost::alignment::alignment_of<T>::value> v;
338 };
339
340 struct alignas(16) type {
341 float data[4];
342 };
343
344 int main()
345 {
346 utility<type> u;
347 u.construct();
348 }
349 ``
350
351 [endsect]
352
353 [section assume_aligned]
354
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.
358
359 ``
360 #include <boost/align/assume_aligned.hpp>
361
362 void use(double* array, std::size_t size)
363 {
364 BOOST_ALIGN_ASSUME_ALIGNED(array, 16);
365 for (std::size_t i = 0; i < size; i++) {
366 array[i]++;
367 }
368 }
369
370 int main()
371 {
372 alignas(16) double d[4] { };
373 use(d, 4);
374 }
375 ``
376
377 [endsect]
378
379 [section is_aligned]
380
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
383 given boundary.
384
385 ``
386 #include <boost/align/is_aligned.hpp>
387 #include <cassert>
388
389 void use(void* ptr)
390 {
391 assert(boost::alignment::is_aligned(ptr, 16));
392 }
393
394 int main()
395 {
396 alignas(16) char c[64];
397 use(c);
398 }
399 ``
400
401 [endsect]
402
403 [endsect]
404
405 [section Examples]
406
407 [section:aligned_ptr aligned_ptr and make_aligned]
408
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.
416
417 [heading Implementation]
418
419 ``
420 #include <boost/align/aligned_alloc.hpp>
421 #include <boost/align/aligned_delete.hpp>
422 #include <boost/align/alignment_of.hpp>
423 #include <memory>
424
425 template<class T>
426 using aligned_ptr = std::unique_ptr<T,
427 boost::alignment::aligned_delete>;
428
429 template<class T, class... Args>
430 inline aligned_ptr<T> make_aligned(Args&&... args)
431 {
432 auto p = boost::alignment::aligned_alloc(boost::
433 alignment::alignment_of<T>::value, sizeof(T));
434 if (!p) {
435 throw std::bad_alloc();
436 }
437 try {
438 auto q = ::new(p) T(std::forward<Args>(args)...);
439 return aligned_ptr<T>(q);
440 } catch (...) {
441 boost::alignment::aligned_free(p);
442 throw;
443 }
444 }
445 ``
446
447 [heading Usage]
448
449 ``
450 struct alignas(16) type {
451 float data[4];
452 };
453
454 int main()
455 {
456 auto p = make_aligned<type>();
457 p.reset();
458 }
459 ``
460
461 [endsect]
462
463 [section aligned_vector]
464
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.
469
470 [heading Implementation]
471
472 ``
473 #include <boost/align/aligned_allocator.hpp>
474 #include <vector>
475
476 template<class T, std::size_t Alignment = 1>
477 using aligned_vector = std::vector<T,
478 boost::alignment::aligned_allocator<T, Alignment> >;
479 ``
480
481 [heading Usage]
482
483 ``
484 enum
485 : std::size_t {
486 cache_line = 64
487 };
488
489 int main()
490 {
491 aligned_vector<char, cache_line> v;
492 v.emplace_back();
493 }
494 ``
495
496 [endsect]
497
498 [endsect]
499
500 [section Reference]
501
502 [section align]
503
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.
509
510 [heading:synopsis Header <boost/align/align.hpp>]
511
512 ``
513 namespace boost {
514 namespace alignment {
515 void* align(std::size_t alignment, std::size_t size, void*& ptr,
516 std::size_t& space);
517 }
518 }
519 ``
520
521 [heading:align Function align]
522
523 ``
524 void* align(std::size_t alignment, std::size_t size, void*& ptr,
525 std::size_t& space);
526 ``
527
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.
533
534 [*Requires:]
535 [itemized_list
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]
539 ]
540
541 [*Returns:] A null pointer if the requested aligned buffer would not
542 fit into the available space, otherwise the adjusted value of `ptr`.
543
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.
547
548 [endsect]
549
550 [section:align_down align_down]
551
552 The directional alignment functions can be used with pointers or
553 integral values to align down. This functionality is not yet provided by
554 the C++ standard.
555
556 [heading:synopsis Header <boost/align/align_down.hpp>]
557
558 ``
559 namespace boost {
560 namespace alignment {
561 constexpr std::size_t align_down(std::size_t value, std::size_t
562 alignment) noexcept;
563
564 void* align_down(void* ptr, std::size_t alignment) noexcept;
565 }
566 }
567 ``
568
569 [heading:align_down Function align_down]
570
571 ``
572 void* align_down(void* ptr, std::size_t alignment) noexcept;
573 ``
574
575 [*Requires:]
576 [itemized_list
577 [`ptr` shall point to contiguous storage with sufficient space
578 for alignment]
579 [`alignment` shall be a power of two]
580 ]
581
582 [*Returns:] A pointer value at or before `ptr` that is at least
583 `alignment` bytes aligned.
584
585 [endsect]
586
587 [section:align_up align_up]
588
589 The directional alignment functions can be used with pointers or
590 integral values to align up. This functionality is not yet provided by
591 the C++ standard.
592
593 [heading:synopsis Header <boost/align/align_up.hpp>]
594
595 ``
596 namespace boost {
597 namespace alignment {
598 constexpr std::size_t align_up(std::size_t value, std::size_t
599 alignment) noexcept;
600
601 void* align_up(void* ptr, std::size_t alignment) noexcept;
602 }
603 }
604 ``
605
606 [heading:align_up Function align_up]
607
608 ``
609 void* align_up(void* ptr, std::size_t alignment) noexcept;
610 ``
611
612 [*Requires:]
613 [itemized_list
614 [`ptr` shall point to contiguous storage with sufficient space
615 for alignment]
616 [`alignment` shall be a power of two]
617 ]
618
619 [*Returns:] A pointer value at or after `ptr` that is at least
620 `alignment` bytes aligned.
621
622 [endsect]
623
624 [section:aligned_alloc aligned_alloc and aligned_free]
625
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.
631
632 [heading:synopsis Header <boost/align/aligned_alloc.hpp>]
633
634 ``
635 namespace boost {
636 namespace alignment {
637 void* aligned_alloc(std::size_t alignment, std::size_t size);
638
639 void aligned_free(void* ptr);
640 }
641 }
642 ``
643
644 [heading:aligned_alloc Function aligned_alloc]
645
646 ``
647 void* aligned_alloc(std::size_t alignment, std::size_t size);
648 ``
649
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
653 power of two.
654
655 [*Requires:] `alignment` shall be a power of two.
656
657 [*Returns:] A null pointer or a pointer to the allocated space.
658
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`
662 bytes.
663
664 [heading:aligned_free Function aligned_free]
665
666 ``
667 void aligned_free(void* ptr);
668 ``
669
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
675 is undefined.
676
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
679 to `aligned_free`.
680
681 [*Returns:] The `aligned_free` function returns no value.
682
683 [endsect]
684
685 [section aligned_allocator]
686
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.
692
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
697 object.]
698
699 [heading:synopsis Header <boost/align/aligned_allocator.hpp>]
700
701 ``
702 namespace boost {
703 namespace alignment {
704 template<class T, std::size_t Alignment = 1>
705 class aligned_allocator;
706
707 template<std::size_t Alignment>
708 class aligned_allocator<void, Alignment>;
709
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;
714
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;
719 }
720 }
721 ``
722
723 [heading:aligned_allocator Class template aligned_allocator]
724
725 ``
726 template<class T, std::size_t Alignment = 1>
727 class aligned_allocator {
728 public:
729 typedef T value_type;
730 typedef T* pointer;
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;
738
739 template<class U>
740 struct rebind {
741 typedef aligned_allocator<U, Alignment> other;
742 };
743
744 aligned_allocator() = default;
745
746 template<class U>
747 aligned_allocator(const aligned_allocator<U, Alignment>&) noexcept;
748
749 pointer address(reference value) const noexcept;
750
751 const_pointer address(const_reference value) const noexcept;
752
753 pointer allocate(size_type size, const_void_pointer = 0);
754
755 void deallocate(pointer ptr, size_type);
756
757 size_type max_size() const noexcept;
758
759 template<class U, class... Args>
760 void construct(U* ptr, Args&&... args);
761
762 template<class U>
763 void destroy(U* ptr);
764 };
765
766 template<std::size_t Alignment>
767 class aligned_allocator<void, Alignment> {
768 public:
769 typedef void value_type;
770 typedef void* pointer;
771 typedef const void* const_pointer;
772
773 template<class U>
774 struct rebind {
775 typedef aligned_allocator<U, Alignment> other;
776 };
777 };
778 ``
779
780 [heading Members]
781
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.
788
789 ``
790 pointer address(reference value) const noexcept;
791 ``
792
793 [*Returns:] The actual address of the object referenced by `value`,
794 even in the presence of an overloaded `operator&`.
795
796 ``
797 const_pointer address(const_reference value) const noexcept;
798 ``
799
800 [*Returns:] The actual address of the object referenced by `value`,
801 even in the presence of an overloaded `operator&`.
802
803 ``
804 pointer allocate(size_type size, const_void_pointer = 0);
805 ``
806
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`.
810
811 [*Remark:] The storage is obtained by calling
812 `aligned_alloc(std::size_t, std::size_t)`.
813
814 [*Throws:] `std::bad_alloc` if the storage cannot be obtained.
815
816 ``
817 void deallocate(pointer ptr, size_type);
818 ``
819
820 [*Requires:] `ptr` shall be a pointer value obtained from
821 `allocate()`.
822
823 [*Effects:] Deallocates the storage referenced by `ptr`.
824
825 [*Remark:] Uses `alignment::aligned_free(void*)`.
826
827 ``
828 size_type max_size() const noexcept;
829 ``
830
831 [*Returns:] The largest value `N` for which the call `allocate(N)`
832 might succeed.
833
834 ``
835 template<class U, class... Args>
836 void construct(U* ptr, Args&&... args);
837 ``
838
839 [*Effects:] `::new((void*)ptr) U(std::forward<Args>(args)...)`
840
841 ``
842 template<class U>
843 void destroy(U* ptr);
844 ``
845
846 [*Effects:] `ptr->~U()`
847
848 [heading Globals]
849
850 ``
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;
855 ``
856
857 [*Returns:] `true`.
858
859 ``
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;
864 ``
865
866 [*Returns:] `false`.
867
868 [endsect]
869
870 [section aligned_allocator_adaptor]
871
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.
877
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
880 pointer types.]
881
882 [heading:synopsis Header <boost/align/aligned_allocator_adaptor.hpp>]
883
884 ``
885 namespace boost {
886 namespace alignment {
887 template<class Allocator, std::size_t Alignment = 1>
888 class aligned_allocator_adaptor;
889
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;
894
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;
899 }
900 }
901 ``
902
903 [heading:aligned_allocator_adaptor Class template
904 aligned_allocator_adaptor]
905
906 ``
907 template<class Allocator, std::size_t Alignment = 1>
908 class aligned_allocator_adaptor
909 : public Allocator {
910 typedef std::allocator_traits<Allocator> Traits; // exposition
911
912 public:
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;
920
921 template<class U>
922 struct rebind {
923 typedef aligned_allocator_adaptor<typename Traits::template
924 rebind_alloc<U>, Alignment> other;
925 };
926
927 aligned_allocator_adaptor() = default;
928
929 template<class A>
930 explicit aligned_allocator_adaptor(A&& alloc) noexcept;
931
932 template<class U>
933 aligned_allocator_adaptor(const aligned_allocator_adaptor<U,
934 Alignment>& other) noexcept;
935
936 Allocator& base() noexcept;
937
938 const Allocator& base() const noexcept;
939
940 pointer allocate(size_type size);
941
942 pointer allocate(size_type size, const_void_pointer hint);
943
944 void deallocate(pointer ptr, size_type size);
945 };
946 ``
947
948 [heading Constructors]
949
950 ``
951 aligned_allocator_adaptor() = default;
952 ``
953
954 [*Effects:] Value-initializes the `Allocator` base class.
955
956 ``
957 template<class A>
958 explicit aligned_allocator_adaptor(A&& alloc) noexcept;
959 ``
960
961 [*Requires:] `Allocator` shall be constructible from `A`.
962
963 [*Effects:] Initializes the `Allocator` base class with
964 `std::forward<A>(alloc)`.
965
966 ``
967 template<class U>
968 aligned_allocator_adaptor(const aligned_allocator_adaptor<U,
969 Alignment>& other) noexcept;
970 ``
971
972 [*Requires:] `Allocator` shall be constructible from `A`.
973
974 [*Effects:] Initializes the `Allocator` base class with
975 `other.base()`.
976
977 [heading Members]
978
979 ``
980 Allocator& base() noexcept;
981 ``
982
983 [*Returns:] `static_cast<Allocator&>(*this)`
984
985 ``
986 const Allocator& base() const noexcept;
987 ``
988
989 [*Returns:] `static_cast<const Allocator&>(*this)`
990
991 ``
992 pointer allocate(size_type size);
993 ``
994
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
998 `value_type`.
999
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.
1003
1004 [*Throws:] Throws an exception thrown from `A2::allocate` if the
1005 storage cannot be obtained.
1006
1007 ``
1008 pointer allocate(size_type size, const_void_pointer hint);
1009 ``
1010
1011 [*Requires:] `hint` is a value obtained by calling `allocate()` on
1012 any equivalent aligned allocator adaptor object, or else `nullptr`.
1013
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
1017 `value_type`.
1018
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.
1022
1023 [*Throws:] Throws an exception thrown from `A2::allocate` if the
1024 storage cannot be obtained.
1025
1026 ``
1027 void deallocate(pointer ptr, size_type size);
1028 ``
1029
1030 [*Requires:]
1031 [itemized_list
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`]
1035 ]
1036
1037 [*Effects:] Deallocates the storage referenced by `ptr`.
1038
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
1041 unspecified.
1042
1043 [heading Globals]
1044
1045 ``
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;
1050 ``
1051
1052 [*Returns:] `a1.base() == a2.base()`
1053
1054 ``
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;
1059 ``
1060
1061 [*Returns:] `!(a1 == a2)`
1062
1063 [endsect]
1064
1065 [section aligned_delete]
1066
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.
1071
1072 [heading:synopsis Header <boost/align/aligned_delete.hpp>]
1073
1074 ``
1075 namespace boost {
1076 namespace alignment {
1077 class aligned_delete;
1078 }
1079 }
1080 ``
1081
1082 [heading:aligned_delete Class aligned_delete]
1083
1084 ``
1085 class aligned_delete {
1086 public:
1087 template<class T>
1088 void operator()(T* ptr) const noexcept(noexcept(ptr->~T()));
1089 };
1090 ``
1091
1092 [heading Members]
1093
1094 ``
1095 template<class T>
1096 void operator()(T* ptr) const noexcept(noexcept(ptr->~T()));
1097 ``
1098
1099 [*Effects:] Calls `~T()` on `ptr` to destroy the object and then
1100 calls `alignment::aligned_free` on `ptr` to free the allocated
1101 memory.
1102
1103 [*Note:] If `T` is an incomplete type, the program is ill-formed.
1104
1105 [endsect]
1106
1107 [section alignment_of]
1108
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.
1113
1114 [heading:synopsis Header <boost/align/alignment_of.hpp>]
1115
1116 ``
1117 namespace boost {
1118 namespace alignment {
1119 template<class T>
1120 struct alignment_of;
1121
1122 template<class T>
1123 constexpr std::size_t alignment_of_v = alignment_of<T>::value;
1124 }
1125 }
1126 ``
1127
1128 [heading:alignment_of Type trait alignment_of]
1129
1130 ``
1131 template<class T>
1132 struct alignment_of;
1133 ``
1134
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.
1139
1140 [*Requires:] `T` shall be a complete object type, or an array
1141 thereof, or a reference to one of those types.
1142
1143 [endsect]
1144
1145 [section assume_aligned]
1146
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.
1150
1151 [heading:synopsis Header <boost/align/assume_aligned.hpp>]
1152
1153 ``
1154 BOOST_ALIGN_ASSUME_ALIGNED(ptr, alignment)
1155 ``
1156
1157 [heading:assume_aligned Macro BOOST_ALIGN_ASSUME_ALIGNED]
1158
1159 ``
1160 BOOST_ALIGN_ASSUME_ALIGNED(ptr, alignment)
1161 ``
1162
1163 [*Requires:]
1164 [itemized_list
1165 [`alignment` shall be a power of two]
1166 [`ptr` shall be mutable]
1167 ]
1168
1169 [*Effect:] `ptr` may be modified in an implementation specific way to
1170 inform the compiler of its alignment.
1171
1172 [endsect]
1173
1174 [section is_aligned]
1175
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.
1180
1181 [heading:synopsis Header <boost/align/is_aligned.hpp>]
1182
1183 ``
1184 namespace boost {
1185 namespace alignment {
1186 constexpr bool is_aligned(std::size_t value, std::size_t
1187 alignment) noexcept;
1188
1189 bool is_aligned(const void* ptr, std::size_t alignment) noexcept;
1190 }
1191 }
1192 ``
1193
1194 [heading:is_aligned Function is_aligned]
1195
1196 ``
1197 bool is_aligned(const void* ptr, std::size_t alignment) noexcept;
1198 ``
1199
1200 [*Requires:] `alignment` shall be a power of two.
1201
1202 [*Returns:] `true` if the value of `ptr` is aligned on the boundary
1203 specified by `alignment`, otherwise `false`.
1204
1205 [endsect]
1206
1207 [endsect]
1208
1209 [section Vocabulary]
1210
1211 [heading:basic_align \[basic.align\]]
1212
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.
1220
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.
1226
1227 [tip
1228 ``
1229 struct B { long double d; };
1230 struct D : virtual B { char c; };
1231 ``
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.]
1238
1239 The result of the `alignof` operator reflects the alignment requirement
1240 of the type in the complete-object case.
1241
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*.
1247
1248 [note Every over-aligned type is or contains a class type to which
1249 extended alignment applies (possibly through a non-static data
1250 member).]
1251
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.
1257
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.
1262
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.
1266
1267 [note This enables the character types to be used as the underlying
1268 type for an aligned memory area.]
1269
1270 Comparing alignments is meaningful and provides the obvious results:
1271
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
1275 alignment.
1276
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.]
1280
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
1285 allocation failure.
1286
1287 [endsect]
1288
1289 [section Compatibility]
1290
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
1293 compilers.
1294
1295 [table Compilers Tested
1296 [ [Compiler]
1297 [Version]
1298 [Library]
1299 [Platform]
1300 [Hardware]
1301 [Standard]
1302 ]
1303 [ [clang]
1304 [3.0 - 3.6]
1305 [libstdc++, libc++]
1306 [linux, darwin]
1307 [x86, x64]
1308 [c++11, c++03]
1309 ]
1310 [ [gcc]
1311 [4.4 - 5.1]
1312 [libstdc++]
1313 [linux, windows]
1314 [x86, x64, arm]
1315 [c++11, c++03]
1316 ]
1317 [ [intel]
1318 [11.1 - 15.0]
1319 [libstdc++]
1320 [linux]
1321 [x86, x64]
1322 [c++11, c++03]]
1323 [ [msvc]
1324 [8.0 - 14.0]
1325 [dinkumware]
1326 [windows]
1327 [x86, x64]
1328 [default]
1329 ]
1330 ]
1331
1332 [endsect]
1333
1334 [section Acknowledgments]
1335
1336 Glen Fernandes authored the library and contributed it to the Boost C++
1337 library collection.
1338
1339 [heading Library Reviews]
1340
1341 Thank you to the following developers who reviewed the design, code,
1342 examples, tests, or documentation.
1343
1344 * Peter Dimov
1345 * Andrey Semashev
1346 * Bjorn Reese
1347 * Steven Watanabe
1348 * Antony Polukhin
1349 * Lars Viklund
1350 * Michael Spencer
1351 * Paul A. Bristow
1352
1353 Thank you to Ahmed Charles who served as review manager for the formal
1354 review of the library.
1355
1356 [heading Contributions]
1357
1358 Thank you to Joel Falcou and Charly Chevalier for both your contribution
1359 and your patience.
1360
1361 [endsect]
1362
1363 [section History]
1364
1365 [heading Boost 1.61]
1366
1367 * Added functions for aligning up and aligning down of pointer and
1368 integral values.
1369
1370 [heading Boost 1.59]
1371
1372 * Joel Falcou and Charly Chevalier together contributed the alignment
1373 hint macros.
1374
1375 [heading Boost 1.56]
1376
1377 * Glen Fernandes implemented the Boost.Align library.
1378
1379 [endsect]