• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/add_reference.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/detail/throw_exception.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 <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 typename detail::add_lvalue_reference<T>::type reference;
40     typedef typename detail::add_lvalue_reference<const
41         T>::type const_reference;
42     typedef std::size_t size_type;
43     typedef std::ptrdiff_t difference_type;
44     typedef detail::true_type propagate_on_container_move_assignment;
45     typedef detail::true_type is_always_equal;
46 
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
aligned_allocator()55     aligned_allocator() BOOST_NOEXCEPT { }
56 #endif
57 
58     template<class U>
aligned_allocator(const aligned_allocator<U,Alignment> &)59     aligned_allocator(const aligned_allocator<U, Alignment>&)
60         BOOST_NOEXCEPT { }
61 
allocate(size_type size,const_void_pointer=0)62     pointer allocate(size_type size, const_void_pointer = 0) {
63         enum {
64             m = detail::max_size<Alignment,
65                 alignment_of<value_type>::value>::value
66         };
67         if (size == 0) {
68             return 0;
69         }
70         void* p = boost::alignment::aligned_alloc(m, sizeof(T) * size);
71         if (!p) {
72             detail::throw_exception(std::bad_alloc());
73         }
74         return static_cast<T*>(p);
75     }
76 
deallocate(pointer ptr,size_type)77     void deallocate(pointer ptr, size_type) {
78         boost::alignment::aligned_free(ptr);
79     }
80 
max_size() const81     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>
construct(U * ptr,Args &&...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>
construct(U * ptr,V && value)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>
construct(U * ptr,const V & value)99     void construct(U* ptr, const V& value) {
100         ::new((void*)ptr) U(value);
101     }
102 
103     template<class U, class V>
construct(U * ptr,V & value)104     void construct(U* ptr, V& value) {
105         ::new((void*)ptr) U(value);
106     }
107 #endif
108 
109     template<class U>
construct(U * ptr)110     void construct(U* ptr) {
111         ::new((void*)ptr) U();
112     }
113 
114     template<class U>
destroy(U * ptr)115     void destroy(U* ptr) {
116         (void)ptr;
117         ptr->~U();
118     }
119 };
120 
121 template<class T, class U, std::size_t Alignment>
122 inline bool
operator ==(const aligned_allocator<T,Alignment> &,const aligned_allocator<U,Alignment> &)123 operator==(const aligned_allocator<T, Alignment>&,
124     const aligned_allocator<U, Alignment>&) BOOST_NOEXCEPT
125 {
126     return true;
127 }
128 
129 template<class T, class U, std::size_t Alignment>
130 inline bool
operator !=(const aligned_allocator<T,Alignment> &,const aligned_allocator<U,Alignment> &)131 operator!=(const aligned_allocator<T, Alignment>&,
132     const aligned_allocator<U, Alignment>&) BOOST_NOEXCEPT
133 {
134     return false;
135 }
136 
137 } /* alignment */
138 } /* boost */
139 
140 #endif
141