1 | // <memory> -*- C++ -*- |
2 | |
3 | // Copyright (C) 2001-2019 Free Software Foundation, Inc. |
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 |
8 | // Free Software Foundation; either version 3, or (at your option) |
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 | |
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/>. |
24 | |
25 | /* |
26 | * Copyright (c) 1997-1999 |
27 | * Silicon Graphics Computer Systems, Inc. |
28 | * |
29 | * Permission to use, copy, modify, distribute and sell this software |
30 | * and its documentation for any purpose is hereby granted without fee, |
31 | * provided that the above copyright notice appear in all copies and |
32 | * that both that copyright notice and this permission notice appear |
33 | * in supporting documentation. Silicon Graphics makes no |
34 | * representations about the suitability of this software for any |
35 | * purpose. It is provided "as is" without express or implied warranty. |
36 | * |
37 | */ |
38 | |
39 | /** @file include/memory |
40 | * This is a Standard C++ Library header. |
41 | */ |
42 | |
43 | #ifndef _GLIBCXX_MEMORY |
44 | #define _GLIBCXX_MEMORY 1 |
45 | |
46 | #pragma GCC system_header |
47 | |
48 | /** |
49 | * @defgroup memory Memory |
50 | * @ingroup utilities |
51 | * |
52 | * Components for memory allocation, deallocation, and management. |
53 | */ |
54 | |
55 | /** |
56 | * @defgroup pointer_abstractions Pointer Abstractions |
57 | * @ingroup memory |
58 | * |
59 | * Smart pointers, etc. |
60 | */ |
61 | |
62 | #include <bits/stl_algobase.h> |
63 | #include <bits/allocator.h> |
64 | #include <bits/stl_construct.h> |
65 | #include <bits/stl_uninitialized.h> |
66 | #include <bits/stl_tempbuf.h> |
67 | #include <bits/stl_raw_storage_iter.h> |
68 | |
69 | #if __cplusplus >= 201103L |
70 | # include <exception> // std::exception |
71 | # include <typeinfo> // std::type_info in get_deleter |
72 | # include <iosfwd> // std::basic_ostream |
73 | # include <ext/atomicity.h> |
74 | # include <ext/concurrence.h> |
75 | # include <bits/functexcept.h> |
76 | # include <bits/stl_function.h> // std::less |
77 | # include <bits/uses_allocator.h> |
78 | # include <type_traits> |
79 | # include <debug/debug.h> |
80 | # include <bits/unique_ptr.h> |
81 | # include <bits/shared_ptr.h> |
82 | # include <bits/shared_ptr_atomic.h> |
83 | # if _GLIBCXX_USE_DEPRECATED |
84 | # include <backward/auto_ptr.h> |
85 | # endif |
86 | #else |
87 | # include <backward/auto_ptr.h> |
88 | #endif |
89 | |
90 | #if __cplusplus >= 201103L |
91 | #include <cstdint> |
92 | #if __cplusplus > 201703L |
93 | # include <bit> // for ispow2 |
94 | # include <new> // for placement operator new |
95 | # include <tuple> // for tuple, make_tuple, make_from_tuple |
96 | #endif |
97 | namespace std _GLIBCXX_VISIBILITY(default) |
98 | { |
99 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
100 | |
101 | /** |
102 | * @brief Fit aligned storage in buffer. |
103 | * |
104 | * [ptr.align] |
105 | * |
106 | * This function tries to fit @a __size bytes of storage with alignment |
107 | * @a __align into the buffer @a __ptr of size @a __space bytes. If such |
108 | * a buffer fits then @a __ptr is changed to point to the first byte of the |
109 | * aligned storage and @a __space is reduced by the bytes used for alignment. |
110 | * |
111 | * @param __align A fundamental or extended alignment value. |
112 | * @param __size Size of the aligned storage required. |
113 | * @param __ptr Pointer to a buffer of @a __space bytes. |
114 | * @param __space Size of the buffer pointed to by @a __ptr. |
115 | * @return the updated pointer if the aligned storage fits, otherwise nullptr. |
116 | */ |
117 | inline void* |
118 | align(size_t __align, size_t __size, void*& __ptr, size_t& __space) noexcept |
119 | { |
120 | #ifdef _GLIBCXX_USE_C99_STDINT_TR1 |
121 | const auto __intptr = reinterpret_cast<uintptr_t>(__ptr); |
122 | #else |
123 | // Cannot use std::uintptr_t so assume that std::size_t can be used instead. |
124 | static_assert(sizeof(size_t) >= sizeof(void*), |
125 | "std::size_t must be a suitable substitute for std::uintptr_t" ); |
126 | const auto __intptr = reinterpret_cast<unsigned long long>(__ptr); |
127 | #endif |
128 | const auto __aligned = (__intptr - 1u + __align) & -__align; |
129 | const auto __diff = __aligned - __intptr; |
130 | if ((__size + __diff) > __space) |
131 | return nullptr; |
132 | else |
133 | { |
134 | __space -= __diff; |
135 | return __ptr = reinterpret_cast<void*>(__aligned); |
136 | } |
137 | } |
138 | |
139 | // 20.7.4 [util.dynamic.safety], pointer safety |
140 | |
141 | enum class pointer_safety { relaxed, preferred, strict }; |
142 | |
143 | inline void |
144 | declare_reachable(void*) { } |
145 | |
146 | template <typename _Tp> |
147 | inline _Tp* |
148 | undeclare_reachable(_Tp* __p) { return __p; } |
149 | |
150 | inline void |
151 | declare_no_pointers(char*, size_t) { } |
152 | |
153 | inline void |
154 | undeclare_no_pointers(char*, size_t) { } |
155 | |
156 | inline pointer_safety |
157 | get_pointer_safety() noexcept { return pointer_safety::relaxed; } |
158 | |
159 | #if __cplusplus > 201703L |
160 | #define __cpp_lib_assume_aligned 201811L |
161 | /// Inform the compiler that a pointer is aligned. |
162 | template<size_t _Align, class _Tp> |
163 | [[nodiscard,__gnu__::__always_inline__]] |
164 | constexpr _Tp* assume_aligned(_Tp* __ptr) |
165 | { |
166 | static_assert(std::ispow2(_Align)); |
167 | _GLIBCXX_DEBUG_ASSERT((std::uintptr_t)__ptr % _Align == 0); |
168 | return static_cast<_Tp*>(__builtin_assume_aligned(__ptr, _Align)); |
169 | } |
170 | #endif // C++2a |
171 | |
172 | #if __cplusplus > 201703L |
173 | template<typename _Tp> |
174 | struct __is_pair : false_type { }; |
175 | template<typename _Tp, typename _Up> |
176 | struct __is_pair<pair<_Tp, _Up>> : true_type { }; |
177 | template<typename _Tp, typename _Up> |
178 | struct __is_pair<const pair<_Tp, _Up>> : true_type { }; |
179 | |
180 | template<typename _Tp, typename __ = _Require<__not_<__is_pair<_Tp>>>, |
181 | typename _Alloc, typename... _Args> |
182 | constexpr auto |
183 | __uses_alloc_args(const _Alloc& __a, _Args&&... __args) noexcept |
184 | { |
185 | if constexpr (uses_allocator_v<remove_cv_t<_Tp>, _Alloc>) |
186 | { |
187 | if constexpr (is_constructible_v<_Tp, allocator_arg_t, |
188 | const _Alloc&, _Args...>) |
189 | { |
190 | return tuple<allocator_arg_t, const _Alloc&, _Args&&...>( |
191 | allocator_arg, __a, std::forward<_Args>(__args)...); |
192 | } |
193 | else |
194 | { |
195 | static_assert(is_constructible_v<_Tp, _Args..., const _Alloc&>, |
196 | "construction with an allocator must be possible" |
197 | " if uses_allocator is true" ); |
198 | |
199 | return tuple<_Args&&..., const _Alloc&>( |
200 | std::forward<_Args>(__args)..., __a); |
201 | } |
202 | } |
203 | else |
204 | { |
205 | static_assert(is_constructible_v<_Tp, _Args...>); |
206 | |
207 | return tuple<_Args&&...>(std::forward<_Args>(__args)...); |
208 | } |
209 | } |
210 | |
211 | #if __cpp_concepts |
212 | template<typename _Tp> |
213 | concept _Std_pair = __is_pair<_Tp>::value; |
214 | #endif |
215 | |
216 | // This is a temporary workaround until -fconcepts is implied by -std=gnu++2a |
217 | #if __cpp_concepts |
218 | # define _GLIBCXX_STD_PAIR_CONSTRAINT(T) _Std_pair T |
219 | # define _GLIBCXX_STD_PAIR_CONSTRAINT_(T) _Std_pair T |
220 | #else |
221 | # define _GLIBCXX_STD_PAIR_CONSTRAINT(T) \ |
222 | typename T, typename __ = _Require<__is_pair<T>> |
223 | # define _GLIBCXX_STD_PAIR_CONSTRAINT_(T) typename T, typename |
224 | #endif |
225 | |
226 | template<typename _Tp, |
227 | #if ! __cpp_concepts |
228 | typename __ = _Require<__not_<__is_pair<_Tp>>>, |
229 | #endif |
230 | typename _Alloc, typename... _Args> |
231 | constexpr auto |
232 | uses_allocator_construction_args(const _Alloc& __a, |
233 | _Args&&... __args) noexcept |
234 | #if __cpp_concepts |
235 | requires (! _Std_pair<_Tp>) |
236 | #endif |
237 | { |
238 | return std::__uses_alloc_args<_Tp>(__a, std::forward<_Args>(__args)...); |
239 | } |
240 | |
241 | template<_GLIBCXX_STD_PAIR_CONSTRAINT(_Tp), typename _Alloc, |
242 | typename _Tuple1, typename _Tuple2> |
243 | constexpr auto |
244 | uses_allocator_construction_args(const _Alloc& __a, piecewise_construct_t, |
245 | _Tuple1&& __x, _Tuple2&& __y) noexcept; |
246 | |
247 | template<_GLIBCXX_STD_PAIR_CONSTRAINT(_Tp), typename _Alloc> |
248 | constexpr auto |
249 | uses_allocator_construction_args(const _Alloc&) noexcept; |
250 | |
251 | template<_GLIBCXX_STD_PAIR_CONSTRAINT(_Tp), typename _Alloc, |
252 | typename _Up, typename _Vp> |
253 | constexpr auto |
254 | uses_allocator_construction_args(const _Alloc&, _Up&&, _Vp&&) noexcept; |
255 | |
256 | template<_GLIBCXX_STD_PAIR_CONSTRAINT(_Tp), typename _Alloc, |
257 | typename _Up, typename _Vp> |
258 | constexpr auto |
259 | uses_allocator_construction_args(const _Alloc&, |
260 | const pair<_Up, _Vp>&) noexcept; |
261 | |
262 | template<_GLIBCXX_STD_PAIR_CONSTRAINT(_Tp), typename _Alloc, |
263 | typename _Up, typename _Vp> |
264 | constexpr auto |
265 | uses_allocator_construction_args(const _Alloc&, pair<_Up, _Vp>&&) noexcept; |
266 | |
267 | template<_GLIBCXX_STD_PAIR_CONSTRAINT_(_Tp), typename _Alloc, |
268 | typename _Tuple1, typename _Tuple2> |
269 | constexpr auto |
270 | uses_allocator_construction_args(const _Alloc& __a, piecewise_construct_t, |
271 | _Tuple1&& __x, _Tuple2&& __y) noexcept |
272 | { |
273 | using _Tp1 = typename _Tp::first_type; |
274 | using _Tp2 = typename _Tp::second_type; |
275 | |
276 | return std::make_tuple(piecewise_construct, |
277 | std::apply([&__a](auto&&... __args1) { |
278 | return std::uses_allocator_construction_args<_Tp1>( |
279 | __a, std::forward<decltype(__args1)>(__args1)...); |
280 | }, std::forward<_Tuple1>(__x)), |
281 | std::apply([&__a](auto&&... __args2) { |
282 | return std::uses_allocator_construction_args<_Tp2>( |
283 | __a, std::forward<decltype(__args2)>(__args2)...); |
284 | }, std::forward<_Tuple2>(__y))); |
285 | } |
286 | |
287 | template<_GLIBCXX_STD_PAIR_CONSTRAINT_(_Tp), typename _Alloc> |
288 | constexpr auto |
289 | uses_allocator_construction_args(const _Alloc& __a) noexcept |
290 | { |
291 | using _Tp1 = typename _Tp::first_type; |
292 | using _Tp2 = typename _Tp::second_type; |
293 | |
294 | return std::make_tuple(piecewise_construct, |
295 | std::uses_allocator_construction_args<_Tp1>(__a), |
296 | std::uses_allocator_construction_args<_Tp2>(__a)); |
297 | } |
298 | |
299 | template<_GLIBCXX_STD_PAIR_CONSTRAINT_(_Tp), typename _Alloc, |
300 | typename _Up, typename _Vp> |
301 | constexpr auto |
302 | uses_allocator_construction_args(const _Alloc& __a, _Up&& __u, _Vp&& __v) |
303 | noexcept |
304 | { |
305 | using _Tp1 = typename _Tp::first_type; |
306 | using _Tp2 = typename _Tp::second_type; |
307 | |
308 | return std::make_tuple(piecewise_construct, |
309 | std::uses_allocator_construction_args<_Tp1>(__a, |
310 | std::forward<_Up>(__u)), |
311 | std::uses_allocator_construction_args<_Tp2>(__a, |
312 | std::forward<_Vp>(__v))); |
313 | } |
314 | |
315 | template<_GLIBCXX_STD_PAIR_CONSTRAINT_(_Tp), typename _Alloc, |
316 | typename _Up, typename _Vp> |
317 | constexpr auto |
318 | uses_allocator_construction_args(const _Alloc& __a, |
319 | const pair<_Up, _Vp>& __pr) noexcept |
320 | { |
321 | using _Tp1 = typename _Tp::first_type; |
322 | using _Tp2 = typename _Tp::second_type; |
323 | |
324 | return std::make_tuple(piecewise_construct, |
325 | std::uses_allocator_construction_args<_Tp1>(__a, __pr.first), |
326 | std::uses_allocator_construction_args<_Tp2>(__a, __pr.second)); |
327 | } |
328 | |
329 | template<_GLIBCXX_STD_PAIR_CONSTRAINT_(_Tp), typename _Alloc, |
330 | typename _Up, typename _Vp> |
331 | constexpr auto |
332 | uses_allocator_construction_args(const _Alloc& __a, |
333 | pair<_Up, _Vp>&& __pr) noexcept |
334 | { |
335 | using _Tp1 = typename _Tp::first_type; |
336 | using _Tp2 = typename _Tp::second_type; |
337 | |
338 | return std::make_tuple(piecewise_construct, |
339 | std::uses_allocator_construction_args<_Tp1>(__a, |
340 | std::move(__pr).first), |
341 | std::uses_allocator_construction_args<_Tp2>(__a, |
342 | std::move(__pr).second)); |
343 | } |
344 | |
345 | template<typename _Tp, typename _Alloc, typename... _Args> |
346 | inline _Tp |
347 | make_obj_using_allocator(const _Alloc& __a, _Args&&... __args) |
348 | { |
349 | return std::make_from_tuple<_Tp>( |
350 | std::uses_allocator_construction_args<_Tp>(__a, |
351 | std::forward<_Args>(__args)...)); |
352 | } |
353 | |
354 | template<typename _Tp, typename _Alloc, typename... _Args> |
355 | inline _Tp* |
356 | uninitialized_construct_using_allocator(_Tp* __p, const _Alloc& __a, |
357 | _Args&&... __args) |
358 | { |
359 | void* __vp = const_cast<void*>(static_cast<const volatile void*>(__p)); |
360 | return ::new(__vp) _Tp(std::make_obj_using_allocator<_Tp>(__a, |
361 | std::forward<_Args>(__args)...)); |
362 | } |
363 | |
364 | #endif // C++2a |
365 | |
366 | _GLIBCXX_END_NAMESPACE_VERSION |
367 | } // namespace |
368 | #endif // C++11 |
369 | |
370 | #if __cplusplus > 201402L |
371 | // Parallel STL algorithms |
372 | # if __PSTL_EXECUTION_POLICIES_DEFINED |
373 | // If <execution> has already been included, pull in implementations |
374 | # include <pstl/glue_memory_impl.h> |
375 | # else |
376 | // Otherwise just pull in forward declarations |
377 | # include <pstl/glue_memory_defs.h> |
378 | # endif |
379 | |
380 | // Feature test macro for parallel algorithms |
381 | # define __cpp_lib_parallel_algorithm 201603L |
382 | #endif // C++17 |
383 | |
384 | #endif /* _GLIBCXX_MEMORY */ |
385 | |