2 Copyright 2014-2015 Glen Joseph Fernandes
5 Distributed under the Boost Software License, Version 1.0.
6 (http://www.boost.org/LICENSE_1_0.txt)
8 #ifndef BOOST_ALIGN_ALIGNED_ALLOCATOR_HPP
9 #define BOOST_ALIGN_ALIGNED_ALLOCATOR_HPP
11 #include <boost/align/detail/addressof.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/aligned_alloc.hpp>
16 #include <boost/align/aligned_allocator_forward.hpp>
17 #include <boost/align/alignment_of.hpp>
18 #include <boost/static_assert.hpp>
19 #include <boost/throw_exception.hpp>
22 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
29 template<class T, std::size_t Alignment>
30 class aligned_allocator {
31 BOOST_STATIC_ASSERT(detail::is_alignment_constant<Alignment>::value);
36 typedef const T* const_pointer;
37 typedef void* void_pointer;
38 typedef const void* const_void_pointer;
39 typedef std::size_t size_type;
40 typedef std::ptrdiff_t difference_type;
42 typedef const T& const_reference;
46 min_align = detail::max_size<Alignment,
47 alignment_of<value_type>::value>::value
53 typedef aligned_allocator<U, Alignment> other;
56 #if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS)
57 aligned_allocator() = default;
59 aligned_allocator() BOOST_NOEXCEPT { }
63 aligned_allocator(const aligned_allocator<U, Alignment>&)
66 pointer address(reference value) const BOOST_NOEXCEPT {
67 return detail::addressof(value);
70 const_pointer address(const_reference value) const BOOST_NOEXCEPT {
71 return detail::addressof(value);
74 pointer allocate(size_type size, const_void_pointer = 0) {
78 void* p = aligned_alloc(min_align, sizeof(T) * size);
80 boost::throw_exception(std::bad_alloc());
82 return static_cast<T*>(p);
85 void deallocate(pointer ptr, size_type) {
86 boost::alignment::aligned_free(ptr);
89 BOOST_CONSTEXPR size_type max_size() const BOOST_NOEXCEPT {
90 return detail::max_objects<T>::value;
93 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
94 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
95 template<class U, class... Args>
96 void construct(U* ptr, Args&&... args) {
97 ::new((void*)ptr) U(std::forward<Args>(args)...);
100 template<class U, class V>
101 void construct(U* ptr, V&& value) {
102 ::new((void*)ptr) U(std::forward<V>(value));
106 template<class U, class V>
107 void construct(U* ptr, const V& value) {
108 ::new((void*)ptr) U(value);
113 void construct(U* ptr) {
114 ::new((void*)ptr) U();
118 void destroy(U* ptr) {
124 template<std::size_t Alignment>
125 class aligned_allocator<void, Alignment> {
126 BOOST_STATIC_ASSERT(detail::is_alignment_constant<Alignment>::value);
129 typedef void value_type;
130 typedef void* pointer;
131 typedef const void* const_pointer;
135 typedef aligned_allocator<U, Alignment> other;
139 template<class T, class U, std::size_t Alignment>
141 operator==(const aligned_allocator<T, Alignment>&,
142 const aligned_allocator<U, Alignment>&) BOOST_NOEXCEPT
147 template<class T, class U, std::size_t Alignment>
149 operator!=(const aligned_allocator<T, Alignment>&,
150 const aligned_allocator<U, Alignment>&) BOOST_NOEXCEPT