summaryrefslogtreecommitdiff
path: root/boost/align/aligned_allocator.hpp
blob: 114e37bb1c05d18d8cca511a333633526f9096ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 (c) 2014 Glen Joseph Fernandes
 glenjofe at gmail dot com

 Distributed under the Boost Software
 License, Version 1.0.
 http://boost.org/LICENSE_1_0.txt
*/
#ifndef BOOST_ALIGN_ALIGNED_ALLOCATOR_HPP
#define BOOST_ALIGN_ALIGNED_ALLOCATOR_HPP

#include <boost/config.hpp>
#include <boost/static_assert.hpp>
#include <boost/throw_exception.hpp>
#include <boost/align/aligned_alloc.hpp>
#include <boost/align/aligned_allocator_forward.hpp>
#include <boost/align/alignment_of.hpp>
#include <boost/align/detail/addressof.hpp>
#include <boost/align/detail/is_alignment_constant.hpp>
#include <boost/align/detail/max_align.hpp>
#include <boost/align/detail/max_count_of.hpp>
#include <new>

#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
#include <utility>
#endif

namespace boost {
    namespace alignment {
        template<class T, std::size_t Alignment>
        class aligned_allocator {
            BOOST_STATIC_ASSERT(detail::
                is_alignment_constant<Alignment>::value);

        public:
            typedef T value_type;
            typedef T* pointer;
            typedef const T* const_pointer;
            typedef void* void_pointer;
            typedef const void* const_void_pointer;
            typedef std::size_t size_type;
            typedef std::ptrdiff_t difference_type;
            typedef T& reference;
            typedef const T& const_reference;

        private:
            typedef detail::max_align<Alignment,
                alignment_of<value_type>::value> MaxAlign;

        public:
            template<class U>
            struct rebind {
                typedef aligned_allocator<U, Alignment> other;
            };

#if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS)
            aligned_allocator()
                BOOST_NOEXCEPT = default;
#else
            aligned_allocator()
                BOOST_NOEXCEPT {
            }
#endif

            template<class U>
            aligned_allocator(const aligned_allocator<U,
                Alignment>&) BOOST_NOEXCEPT {
            }

            pointer address(reference value) const
                BOOST_NOEXCEPT {
                return detail::addressof(value);
            }

            const_pointer address(const_reference value) const
                BOOST_NOEXCEPT {
                return detail::addressof(value);
            }

            pointer allocate(size_type size,
                const_void_pointer = 0) {
                void* p = aligned_alloc(MaxAlign::value,
                    sizeof(T) * size);
                if (!p && size > 0) {
                    boost::throw_exception(std::bad_alloc());
                }
                return static_cast<T*>(p);
            }

            void deallocate(pointer ptr, size_type) {
                alignment::aligned_free(ptr);
            }

            BOOST_CONSTEXPR size_type max_size() const
                BOOST_NOEXCEPT {
                return detail::max_count_of<T>::value;
            }

#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
            template<class U, class... Args>
            void construct(U* ptr, Args&&... args) {
                void* p = ptr;
                ::new(p) U(std::forward<Args>(args)...);
            }
#else
            template<class U, class V>
            void construct(U* ptr, V&& value) {
                void* p = ptr;
                ::new(p) U(std::forward<V>(value));
            }
#endif
#else
            template<class U, class V>
            void construct(U* ptr, const V& value) {
                void* p = ptr;
                ::new(p) U(value);
            }
#endif

            template<class U>
            void construct(U* ptr) {
                void* p = ptr;
                ::new(p) U();
            }

            template<class U>
            void destroy(U* ptr) {
                (void)ptr;
                ptr->~U();
            }
        };

        template<std::size_t Alignment>
        class aligned_allocator<void, Alignment> {
            BOOST_STATIC_ASSERT(detail::
                is_alignment_constant<Alignment>::value);

        public:
            typedef void value_type;
            typedef void* pointer;
            typedef const void* const_pointer;

            template<class U>
            struct rebind {
                typedef aligned_allocator<U, Alignment> other;
            };
        };

        template<class T1, class T2, std::size_t Alignment>
        inline bool operator==(const aligned_allocator<T1,
            Alignment>&, const aligned_allocator<T2,
            Alignment>&) BOOST_NOEXCEPT
        {
            return true;
        }

        template<class T1, class T2, std::size_t Alignment>
        inline bool operator!=(const aligned_allocator<T1,
            Alignment>&, const aligned_allocator<T2,
            Alignment>&) BOOST_NOEXCEPT
        {
            return false;
        }
    }
}

#endif