]>
Commit | Line | Data |
---|---|---|
d38d4e5d | 1 | // Allocator that wraps operator new -*- C++ -*- |
42526146 | 2 | |
8d9254fc | 3 | // Copyright (C) 2001-2020 Free Software Foundation, Inc. |
42526146 PE |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
42526146 PE |
9 | // any later version. |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
748086b7 JJ |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
19 | ||
20 | // You should have received a copy of the GNU General Public License and | |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
42526146 | 24 | |
0aa06b18 BK |
25 | /** @file ext/new_allocator.h |
26 | * This file is a GNU extension to the Standard C++ Library. | |
27 | */ | |
28 | ||
1ff9402d BK |
29 | #ifndef _NEW_ALLOCATOR_H |
30 | #define _NEW_ALLOCATOR_H 1 | |
31 | ||
8fc81078 | 32 | #include <bits/c++config.h> |
1ff9402d | 33 | #include <new> |
a063e891 | 34 | #include <bits/functexcept.h> |
ca0f8fd1 | 35 | #include <bits/move.h> |
1b5dc776 JW |
36 | #if __cplusplus >= 201103L |
37 | #include <type_traits> | |
38 | #endif | |
1ff9402d | 39 | |
12ffa228 BK |
40 | namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) |
41 | { | |
42 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
3cbc7af0 | 43 | |
1ff9402d | 44 | /** |
d38d4e5d | 45 | * @brief An allocator that uses global new, as per [20.4]. |
5b9daa7e | 46 | * @ingroup allocators |
d38d4e5d | 47 | * |
88b3e631 | 48 | * This is precisely the allocator defined in the C++ Standard. |
d38d4e5d BK |
49 | * - all allocation calls operator new |
50 | * - all deallocation calls operator delete | |
fa754848 BK |
51 | * |
52 | * @tparam _Tp Type of allocated object. | |
1ff9402d | 53 | */ |
d38d4e5d BK |
54 | template<typename _Tp> |
55 | class new_allocator | |
56 | { | |
57 | public: | |
2cae56bd | 58 | typedef _Tp value_type; |
3263fb9c JW |
59 | typedef std::size_t size_type; |
60 | typedef std::ptrdiff_t difference_type; | |
2cae56bd | 61 | #if __cplusplus <= 201703L |
d38d4e5d BK |
62 | typedef _Tp* pointer; |
63 | typedef const _Tp* const_pointer; | |
64 | typedef _Tp& reference; | |
65 | typedef const _Tp& const_reference; | |
d38d4e5d BK |
66 | |
67 | template<typename _Tp1> | |
88b3e631 JW |
68 | struct rebind |
69 | { typedef new_allocator<_Tp1> other; }; | |
2cae56bd | 70 | #endif |
d38d4e5d | 71 | |
1b5dc776 JW |
72 | #if __cplusplus >= 201103L |
73 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
74 | // 2103. propagate_on_container_move_assignment | |
75 | typedef std::true_type propagate_on_container_move_assignment; | |
76 | #endif | |
77 | ||
3be9ded2 | 78 | _GLIBCXX20_CONSTEXPR |
7d9cb054 | 79 | new_allocator() _GLIBCXX_USE_NOEXCEPT { } |
d38d4e5d | 80 | |
3be9ded2 | 81 | _GLIBCXX20_CONSTEXPR |
7d9cb054 | 82 | new_allocator(const new_allocator&) _GLIBCXX_USE_NOEXCEPT { } |
d38d4e5d BK |
83 | |
84 | template<typename _Tp1> | |
3be9ded2 | 85 | _GLIBCXX20_CONSTEXPR |
88b3e631 | 86 | new_allocator(const new_allocator<_Tp1>&) _GLIBCXX_USE_NOEXCEPT { } |
d38d4e5d | 87 | |
2cae56bd | 88 | #if __cplusplus <= 201703L |
7d9cb054 | 89 | ~new_allocator() _GLIBCXX_USE_NOEXCEPT { } |
d38d4e5d BK |
90 | |
91 | pointer | |
7d9cb054 PC |
92 | address(reference __x) const _GLIBCXX_NOEXCEPT |
93 | { return std::__addressof(__x); } | |
d38d4e5d BK |
94 | |
95 | const_pointer | |
7d9cb054 PC |
96 | address(const_reference __x) const _GLIBCXX_NOEXCEPT |
97 | { return std::__addressof(__x); } | |
2cae56bd | 98 | #endif |
d38d4e5d BK |
99 | |
100 | // NB: __n is permitted to be 0. The C++ standard says nothing | |
101 | // about what the return value is when __n == 0. | |
2cae56bd | 102 | _GLIBCXX_NODISCARD _Tp* |
89c6ecfa | 103 | allocate(size_type __n, const void* = static_cast<const void*>(0)) |
88b3e631 | 104 | { |
2cae56bd | 105 | if (__n > this->_M_max_size()) |
a063e891 PC |
106 | std::__throw_bad_alloc(); |
107 | ||
ace4c2f0 JW |
108 | #if __cpp_aligned_new |
109 | if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__) | |
110 | { | |
111 | std::align_val_t __al = std::align_val_t(alignof(_Tp)); | |
112 | return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), __al)); | |
113 | } | |
114 | #endif | |
a063e891 PC |
115 | return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp))); |
116 | } | |
d38d4e5d BK |
117 | |
118 | // __p is not permitted to be a null pointer. | |
119 | void | |
2cae56bd | 120 | deallocate(_Tp* __p, size_type __t) |
ace4c2f0 JW |
121 | { |
122 | #if __cpp_aligned_new | |
123 | if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__) | |
124 | { | |
e5178b98 PB |
125 | ::operator delete(__p, |
126 | # if __cpp_sized_deallocation | |
127 | __t * sizeof(_Tp), | |
128 | # endif | |
129 | std::align_val_t(alignof(_Tp))); | |
ace4c2f0 JW |
130 | return; |
131 | } | |
132 | #endif | |
e5178b98 PB |
133 | ::operator delete(__p |
134 | #if __cpp_sized_deallocation | |
135 | , __t * sizeof(_Tp) | |
136 | #endif | |
137 | ); | |
ace4c2f0 | 138 | } |
d38d4e5d | 139 | |
2cae56bd | 140 | #if __cplusplus <= 201703L |
d38d4e5d | 141 | size_type |
7d9cb054 | 142 | max_size() const _GLIBCXX_USE_NOEXCEPT |
2cae56bd | 143 | { return _M_max_size(); } |
d38d4e5d | 144 | |
734f5023 | 145 | #if __cplusplus >= 201103L |
45ba8f9f | 146 | template<typename _Up, typename... _Args> |
88b3e631 JW |
147 | void |
148 | construct(_Up* __p, _Args&&... __args) | |
0f317ef7 MG |
149 | noexcept(noexcept(::new((void *)__p) |
150 | _Up(std::forward<_Args>(__args)...))) | |
45ba8f9f JW |
151 | { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); } |
152 | ||
153 | template<typename _Up> | |
88b3e631 | 154 | void |
0f317ef7 MG |
155 | destroy(_Up* __p) |
156 | noexcept(noexcept( __p->~_Up())) | |
157 | { __p->~_Up(); } | |
45ba8f9f | 158 | #else |
d38d4e5d BK |
159 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
160 | // 402. wrong new expression in [some_] allocator::construct | |
88b3e631 JW |
161 | void |
162 | construct(pointer __p, const _Tp& __val) | |
61fcb9fb PC |
163 | { ::new((void *)__p) _Tp(__val); } |
164 | ||
88b3e631 | 165 | void |
d38d4e5d | 166 | destroy(pointer __p) { __p->~_Tp(); } |
45ba8f9f | 167 | #endif |
2cae56bd | 168 | #endif // ! C++20 |
f263b26e | 169 | |
c7790bdb | 170 | template<typename _Up> |
85f24114 | 171 | friend _GLIBCXX20_CONSTEXPR bool |
c7790bdb JW |
172 | operator==(const new_allocator&, const new_allocator<_Up>&) |
173 | _GLIBCXX_NOTHROW | |
174 | { return true; } | |
88b3e631 | 175 | |
c7790bdb | 176 | template<typename _Up> |
85f24114 | 177 | friend _GLIBCXX20_CONSTEXPR bool |
c7790bdb JW |
178 | operator!=(const new_allocator&, const new_allocator<_Up>&) |
179 | _GLIBCXX_NOTHROW | |
180 | { return false; } | |
2cae56bd JW |
181 | |
182 | private: | |
183 | _GLIBCXX_CONSTEXPR size_type | |
184 | _M_max_size() const _GLIBCXX_USE_NOEXCEPT | |
185 | { | |
186 | #if __PTRDIFF_MAX__ < __SIZE_MAX__ | |
187 | return std::size_t(__PTRDIFF_MAX__) / sizeof(_Tp); | |
188 | #else | |
189 | return std::size_t(-1) / sizeof(_Tp); | |
190 | #endif | |
191 | } | |
c7790bdb | 192 | }; |
3cbc7af0 | 193 | |
12ffa228 BK |
194 | _GLIBCXX_END_NAMESPACE_VERSION |
195 | } // namespace | |
1ff9402d BK |
196 | |
197 | #endif |