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