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
9 #ifndef BOOST_ALIGN_ALIGNED_ALLOCATOR_HPP
10 #define BOOST_ALIGN_ALIGNED_ALLOCATOR_HPP
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>
23 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
30 template<class T, std::size_t Alignment>
31 class aligned_allocator {
32 BOOST_STATIC_ASSERT(detail::
33 is_alignment_constant<Alignment>::value);
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;
44 typedef const T& const_reference;
48 min_align = detail::max_size<Alignment,
49 alignment_of<value_type>::value>::value
55 typedef aligned_allocator<U, Alignment> other;
58 #if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS)
59 aligned_allocator() = default;
61 aligned_allocator() BOOST_NOEXCEPT { }
65 aligned_allocator(const aligned_allocator<U, Alignment>&)
68 pointer address(reference value) const BOOST_NOEXCEPT {
69 return detail::addressof(value);
72 const_pointer address(const_reference value) const BOOST_NOEXCEPT {
73 return detail::addressof(value);
76 pointer allocate(size_type size, const_void_pointer = 0) {
79 p = aligned_alloc(min_align, sizeof(T) * size);
81 boost::throw_exception(std::bad_alloc());
84 return static_cast<T*>(p);
87 void deallocate(pointer ptr, size_type) {
88 boost::alignment::aligned_free(ptr);
91 BOOST_CONSTEXPR size_type max_size() const BOOST_NOEXCEPT {
92 return detail::max_objects<T>::value;
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)...);
102 template<class U, class V>
103 void construct(U* ptr, V&& value) {
104 ::new((void*)ptr) U(std::forward<V>(value));
108 template<class U, class V>
109 void construct(U* ptr, const V& value) {
110 ::new((void*)ptr) U(value);
115 void construct(U* ptr) {
116 ::new((void*)ptr) U();
120 void destroy(U* ptr) {
126 template<std::size_t Alignment>
127 class aligned_allocator<void, Alignment> {
128 BOOST_STATIC_ASSERT(detail::
129 is_alignment_constant<Alignment>::value);
132 typedef void value_type;
133 typedef void* pointer;
134 typedef const void* const_pointer;
138 typedef aligned_allocator<U, Alignment> other;
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
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