]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/align/aligned_allocator.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / align / aligned_allocator.hpp
1 /*
2 Copyright 2014-2015 Glen Joseph Fernandes
3 (glenjofe@gmail.com)
4
5 Distributed under the Boost Software License, Version 1.0.
6 (http://www.boost.org/LICENSE_1_0.txt)
7 */
8 #ifndef BOOST_ALIGN_ALIGNED_ALLOCATOR_HPP
9 #define BOOST_ALIGN_ALIGNED_ALLOCATOR_HPP
10
11 #include <boost/align/detail/addressof.hpp>
12 #include <boost/align/detail/is_alignment_constant.hpp>
13 #include <boost/align/detail/max_objects.hpp>
14 #include <boost/align/detail/max_size.hpp>
15 #include <boost/align/aligned_alloc.hpp>
16 #include <boost/align/aligned_allocator_forward.hpp>
17 #include <boost/align/alignment_of.hpp>
18 #include <boost/static_assert.hpp>
19 #include <boost/throw_exception.hpp>
20 #include <new>
21
22 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
23 #include <utility>
24 #endif
25
26 namespace boost {
27 namespace alignment {
28
29 template<class T, std::size_t Alignment>
30 class aligned_allocator {
31 BOOST_STATIC_ASSERT(detail::is_alignment_constant<Alignment>::value);
32
33 public:
34 typedef T value_type;
35 typedef T* pointer;
36 typedef const T* const_pointer;
37 typedef void* void_pointer;
38 typedef const void* const_void_pointer;
39 typedef std::size_t size_type;
40 typedef std::ptrdiff_t difference_type;
41 typedef T& reference;
42 typedef const T& const_reference;
43
44 private:
45 enum {
46 min_align = detail::max_size<Alignment,
47 alignment_of<value_type>::value>::value
48 };
49
50 public:
51 template<class U>
52 struct rebind {
53 typedef aligned_allocator<U, Alignment> other;
54 };
55
56 #if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS)
57 aligned_allocator() = default;
58 #else
59 aligned_allocator() BOOST_NOEXCEPT { }
60 #endif
61
62 template<class U>
63 aligned_allocator(const aligned_allocator<U, Alignment>&)
64 BOOST_NOEXCEPT { }
65
66 pointer address(reference value) const BOOST_NOEXCEPT {
67 return detail::addressof(value);
68 }
69
70 const_pointer address(const_reference value) const BOOST_NOEXCEPT {
71 return detail::addressof(value);
72 }
73
74 pointer allocate(size_type size, const_void_pointer = 0) {
75 if (size == 0) {
76 return 0;
77 }
78 void* p = aligned_alloc(min_align, sizeof(T) * size);
79 if (!p) {
80 boost::throw_exception(std::bad_alloc());
81 }
82 return static_cast<T*>(p);
83 }
84
85 void deallocate(pointer ptr, size_type) {
86 boost::alignment::aligned_free(ptr);
87 }
88
89 BOOST_CONSTEXPR size_type max_size() const BOOST_NOEXCEPT {
90 return detail::max_objects<T>::value;
91 }
92
93 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
94 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
95 template<class U, class... Args>
96 void construct(U* ptr, Args&&... args) {
97 ::new((void*)ptr) U(std::forward<Args>(args)...);
98 }
99 #else
100 template<class U, class V>
101 void construct(U* ptr, V&& value) {
102 ::new((void*)ptr) U(std::forward<V>(value));
103 }
104 #endif
105 #else
106 template<class U, class V>
107 void construct(U* ptr, const V& value) {
108 ::new((void*)ptr) U(value);
109 }
110 #endif
111
112 template<class U>
113 void construct(U* ptr) {
114 ::new((void*)ptr) U();
115 }
116
117 template<class U>
118 void destroy(U* ptr) {
119 (void)ptr;
120 ptr->~U();
121 }
122 };
123
124 template<std::size_t Alignment>
125 class aligned_allocator<void, Alignment> {
126 BOOST_STATIC_ASSERT(detail::is_alignment_constant<Alignment>::value);
127
128 public:
129 typedef void value_type;
130 typedef void* pointer;
131 typedef const void* const_pointer;
132
133 template<class U>
134 struct rebind {
135 typedef aligned_allocator<U, Alignment> other;
136 };
137 };
138
139 template<class T, class U, std::size_t Alignment>
140 inline bool
141 operator==(const aligned_allocator<T, Alignment>&,
142 const aligned_allocator<U, Alignment>&) BOOST_NOEXCEPT
143 {
144 return true;
145 }
146
147 template<class T, class U, std::size_t Alignment>
148 inline bool
149 operator!=(const aligned_allocator<T, Alignment>&,
150 const aligned_allocator<U, Alignment>&) BOOST_NOEXCEPT
151 {
152 return false;
153 }
154
155 } /* alignment */
156 } /* boost */
157
158 #endif