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/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>
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 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;
49 typedef aligned_allocator<U, Alignment> other;
52 #if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS)
53 aligned_allocator() = default;
55 aligned_allocator() BOOST_NOEXCEPT { }
59 aligned_allocator(const aligned_allocator<U, Alignment>&)
62 pointer allocate(size_type size, const_void_pointer = 0) {
64 m = detail::max_size<Alignment,
65 alignment_of<value_type>::value>::value
70 void* p = boost::alignment::aligned_alloc(m, sizeof(T) * size);
72 detail::throw_exception(std::bad_alloc());
74 return static_cast<T*>(p);
77 void deallocate(pointer ptr, size_type) {
78 boost::alignment::aligned_free(ptr);
81 BOOST_CONSTEXPR size_type max_size() const BOOST_NOEXCEPT {
82 return detail::max_objects<T>::value;
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)...);
92 template<class U, class V>
93 void construct(U* ptr, V&& value) {
94 ::new((void*)ptr) U(std::forward<V>(value));
98 template<class U, class V>
99 void construct(U* ptr, const V& value) {
100 ::new((void*)ptr) U(value);
103 template<class U, class V>
104 void construct(U* ptr, V& value) {
105 ::new((void*)ptr) U(value);
110 void construct(U* ptr) {
111 ::new((void*)ptr) U();
115 void destroy(U* ptr) {
121 template<class T, class U, std::size_t Alignment>
123 operator==(const aligned_allocator<T, Alignment>&,
124 const aligned_allocator<U, Alignment>&) BOOST_NOEXCEPT
129 template<class T, class U, std::size_t Alignment>
131 operator!=(const aligned_allocator<T, Alignment>&,
132 const aligned_allocator<U, Alignment>&) BOOST_NOEXCEPT