]>
Commit | Line | Data |
---|---|---|
92f5a8d4 TL |
1 | /* |
2 | Copyright 2019 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_CORE_DEFAULT_ALLOCATOR_HPP | |
9 | #define BOOST_CORE_DEFAULT_ALLOCATOR_HPP | |
10 | ||
11 | #include <boost/config.hpp> | |
12 | #include <new> | |
92f5a8d4 TL |
13 | |
14 | namespace boost { | |
15 | ||
16 | #if defined(BOOST_NO_EXCEPTIONS) | |
17 | BOOST_NORETURN void throw_exception(const std::exception&); | |
18 | #endif | |
19 | ||
20 | namespace default_ { | |
21 | ||
1e59de90 TL |
22 | template<bool V> |
23 | struct bool_constant { | |
92f5a8d4 | 24 | typedef bool value_type; |
1e59de90 | 25 | typedef bool_constant type; |
92f5a8d4 | 26 | |
1e59de90 | 27 | static const bool value = V; |
92f5a8d4 | 28 | |
1e59de90 TL |
29 | operator bool() const BOOST_NOEXCEPT { |
30 | return V; | |
92f5a8d4 TL |
31 | } |
32 | ||
1e59de90 TL |
33 | bool operator()() const BOOST_NOEXCEPT { |
34 | return V; | |
92f5a8d4 TL |
35 | } |
36 | }; | |
37 | ||
1e59de90 TL |
38 | template<bool V> |
39 | const bool bool_constant<V>::value; | |
40 | ||
92f5a8d4 TL |
41 | template<class T> |
42 | struct add_reference { | |
43 | typedef T& type; | |
44 | }; | |
45 | ||
46 | template<> | |
47 | struct add_reference<void> { | |
48 | typedef void type; | |
49 | }; | |
50 | ||
51 | template<> | |
52 | struct add_reference<const void> { | |
53 | typedef const void type; | |
54 | }; | |
55 | ||
56 | template<class T> | |
57 | struct default_allocator { | |
58 | typedef T value_type; | |
59 | typedef T* pointer; | |
60 | typedef const T* const_pointer; | |
61 | typedef typename add_reference<T>::type reference; | |
62 | typedef typename add_reference<const T>::type const_reference; | |
63 | typedef std::size_t size_type; | |
64 | typedef std::ptrdiff_t difference_type; | |
1e59de90 TL |
65 | typedef bool_constant<true> propagate_on_container_move_assignment; |
66 | typedef bool_constant<true> is_always_equal; | |
92f5a8d4 TL |
67 | |
68 | template<class U> | |
69 | struct rebind { | |
70 | typedef default_allocator<U> other; | |
71 | }; | |
72 | ||
73 | #if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) | |
74 | default_allocator() = default; | |
75 | #else | |
76 | BOOST_CONSTEXPR default_allocator() BOOST_NOEXCEPT { } | |
77 | #endif | |
78 | ||
79 | template<class U> | |
80 | BOOST_CONSTEXPR default_allocator(const default_allocator<U>&) | |
81 | BOOST_NOEXCEPT { } | |
82 | ||
92f5a8d4 | 83 | BOOST_CONSTEXPR std::size_t max_size() const BOOST_NOEXCEPT { |
20effc67 | 84 | return static_cast<std::size_t>(-1) / (2 < sizeof(T) ? sizeof(T) : 2); |
92f5a8d4 | 85 | } |
92f5a8d4 TL |
86 | |
87 | #if !defined(BOOST_NO_EXCEPTIONS) | |
88 | T* allocate(std::size_t n) { | |
89 | if (n > max_size()) { | |
90 | throw std::bad_alloc(); | |
91 | } | |
92 | return static_cast<T*>(::operator new(sizeof(T) * n)); | |
93 | } | |
94 | ||
95 | void deallocate(T* p, std::size_t) { | |
96 | ::operator delete(p); | |
97 | } | |
98 | #else | |
99 | T* allocate(std::size_t n) { | |
100 | if (n > max_size()) { | |
101 | boost::throw_exception(std::bad_alloc()); | |
102 | } | |
103 | void* p = ::operator new(sizeof(T) * n, std::nothrow); | |
104 | if (!p) { | |
105 | boost::throw_exception(std::bad_alloc()); | |
106 | } | |
107 | return static_cast<T*>(p); | |
108 | } | |
109 | ||
110 | void deallocate(T* p, std::size_t) { | |
111 | ::operator delete(p, std::nothrow); | |
112 | } | |
113 | #endif | |
114 | ||
1e59de90 TL |
115 | #if defined(BOOST_NO_CXX11_ALLOCATOR) |
116 | T* allocate(std::size_t n, const void*) { | |
117 | return allocate(n); | |
118 | } | |
119 | #endif | |
120 | ||
20effc67 TL |
121 | #if (defined(BOOST_LIBSTDCXX_VERSION) && BOOST_LIBSTDCXX_VERSION < 60000) || \ |
122 | defined(BOOST_NO_CXX11_ALLOCATOR) | |
92f5a8d4 TL |
123 | template<class U, class V> |
124 | void construct(U* p, const V& v) { | |
125 | ::new(p) U(v); | |
126 | } | |
127 | ||
128 | template<class U> | |
129 | void destroy(U* p) { | |
130 | p->~U(); | |
20effc67 | 131 | (void)p; |
92f5a8d4 TL |
132 | } |
133 | #endif | |
134 | }; | |
135 | ||
136 | template<class T, class U> | |
137 | BOOST_CONSTEXPR inline bool | |
138 | operator==(const default_allocator<T>&, | |
139 | const default_allocator<U>&) BOOST_NOEXCEPT | |
140 | { | |
141 | return true; | |
142 | } | |
143 | ||
144 | template<class T, class U> | |
145 | BOOST_CONSTEXPR inline bool | |
146 | operator!=(const default_allocator<T>&, | |
147 | const default_allocator<U>&) BOOST_NOEXCEPT | |
148 | { | |
149 | return false; | |
150 | } | |
151 | ||
152 | } /* default_ */ | |
153 | ||
154 | using default_::default_allocator; | |
155 | ||
156 | } /* boost */ | |
157 | ||
158 | #endif |