]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | /* |
b32b8144 FG |
2 | Copyright 2014-2015 Glen Joseph Fernandes |
3 | (glenjofe@gmail.com) | |
7c673cae | 4 | |
b32b8144 FG |
5 | Distributed under the Boost Software License, Version 1.0. |
6 | (http://www.boost.org/LICENSE_1_0.txt) | |
7c673cae FG |
7 | */ |
8 | #ifndef BOOST_ALIGN_ALIGNED_ALLOCATOR_HPP | |
9 | #define BOOST_ALIGN_ALIGNED_ALLOCATOR_HPP | |
10 | ||
92f5a8d4 | 11 | #include <boost/align/detail/add_reference.hpp> |
7c673cae FG |
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> | |
92f5a8d4 | 15 | #include <boost/align/detail/throw_exception.hpp> |
7c673cae FG |
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> | |
7c673cae FG |
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 { | |
b32b8144 | 31 | BOOST_STATIC_ASSERT(detail::is_alignment_constant<Alignment>::value); |
7c673cae FG |
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; | |
92f5a8d4 TL |
39 | typedef typename detail::add_lvalue_reference<T>::type reference; |
40 | typedef typename detail::add_lvalue_reference<const | |
41 | T>::type const_reference; | |
7c673cae FG |
42 | typedef std::size_t size_type; |
43 | typedef std::ptrdiff_t difference_type; | |
92f5a8d4 TL |
44 | typedef detail::true_type propagate_on_container_move_assignment; |
45 | typedef detail::true_type is_always_equal; | |
7c673cae | 46 | |
7c673cae FG |
47 | template<class U> |
48 | struct rebind { | |
49 | typedef aligned_allocator<U, Alignment> other; | |
50 | }; | |
51 | ||
52 | #if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) | |
53 | aligned_allocator() = default; | |
54 | #else | |
55 | aligned_allocator() BOOST_NOEXCEPT { } | |
56 | #endif | |
57 | ||
58 | template<class U> | |
59 | aligned_allocator(const aligned_allocator<U, Alignment>&) | |
60 | BOOST_NOEXCEPT { } | |
61 | ||
7c673cae | 62 | pointer allocate(size_type size, const_void_pointer = 0) { |
92f5a8d4 TL |
63 | enum { |
64 | m = detail::max_size<Alignment, | |
65 | alignment_of<value_type>::value>::value | |
66 | }; | |
b32b8144 FG |
67 | if (size == 0) { |
68 | return 0; | |
69 | } | |
92f5a8d4 | 70 | void* p = boost::alignment::aligned_alloc(m, sizeof(T) * size); |
b32b8144 | 71 | if (!p) { |
92f5a8d4 | 72 | detail::throw_exception(std::bad_alloc()); |
7c673cae FG |
73 | } |
74 | return static_cast<T*>(p); | |
75 | } | |
76 | ||
77 | void deallocate(pointer ptr, size_type) { | |
78 | boost::alignment::aligned_free(ptr); | |
79 | } | |
80 | ||
81 | BOOST_CONSTEXPR size_type max_size() const BOOST_NOEXCEPT { | |
82 | return detail::max_objects<T>::value; | |
83 | } | |
84 | ||
85 | #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) | |
86 | #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) | |
87 | template<class U, class... Args> | |
88 | void construct(U* ptr, Args&&... args) { | |
89 | ::new((void*)ptr) U(std::forward<Args>(args)...); | |
90 | } | |
91 | #else | |
92 | template<class U, class V> | |
93 | void construct(U* ptr, V&& value) { | |
94 | ::new((void*)ptr) U(std::forward<V>(value)); | |
95 | } | |
96 | #endif | |
97 | #else | |
98 | template<class U, class V> | |
99 | void construct(U* ptr, const V& value) { | |
100 | ::new((void*)ptr) U(value); | |
101 | } | |
92f5a8d4 TL |
102 | |
103 | template<class U, class V> | |
104 | void construct(U* ptr, V& value) { | |
105 | ::new((void*)ptr) U(value); | |
106 | } | |
7c673cae FG |
107 | #endif |
108 | ||
109 | template<class U> | |
110 | void construct(U* ptr) { | |
111 | ::new((void*)ptr) U(); | |
112 | } | |
113 | ||
114 | template<class U> | |
115 | void destroy(U* ptr) { | |
116 | (void)ptr; | |
117 | ptr->~U(); | |
118 | } | |
119 | }; | |
120 | ||
b32b8144 FG |
121 | template<class T, class U, std::size_t Alignment> |
122 | inline bool | |
123 | operator==(const aligned_allocator<T, Alignment>&, | |
124 | const aligned_allocator<U, Alignment>&) BOOST_NOEXCEPT | |
7c673cae FG |
125 | { |
126 | return true; | |
127 | } | |
128 | ||
b32b8144 FG |
129 | template<class T, class U, std::size_t Alignment> |
130 | inline bool | |
131 | operator!=(const aligned_allocator<T, Alignment>&, | |
132 | const aligned_allocator<U, Alignment>&) BOOST_NOEXCEPT | |
7c673cae FG |
133 | { |
134 | return false; | |
135 | } | |
136 | ||
b32b8144 FG |
137 | } /* alignment */ |
138 | } /* boost */ | |
7c673cae FG |
139 | |
140 | #endif |