44 #ifndef _GLIBCXX_MEMORY
45 #define _GLIBCXX_MEMORY 1
47 #pragma GCC system_header
70 #if __cplusplus >= 201103L
78 # include <bits/uses_allocator.h>
83 # include <bits/shared_ptr.h>
85 # if _GLIBCXX_USE_DEPRECATED
92 #if __cplusplus >= 201103L
94 #if __cplusplus > 201703L
99 namespace std _GLIBCXX_VISIBILITY(default)
101 _GLIBCXX_BEGIN_NAMESPACE_VERSION
122 align(
size_t __align,
size_t __size,
void*& __ptr,
size_t& __space) noexcept
124 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
125 const auto __intptr = reinterpret_cast<uintptr_t>(__ptr);
128 static_assert(
sizeof(
size_t) >=
sizeof(
void*),
129 "std::size_t must be a suitable substitute for std::uintptr_t");
130 const auto __intptr = reinterpret_cast<unsigned long long>(__ptr);
132 const auto __aligned = (__intptr - 1u + __align) & -__align;
133 const auto __diff = __aligned - __intptr;
134 if ((__size + __diff) > __space)
139 return __ptr = reinterpret_cast<void*>(__aligned);
164 template <
typename _Tp>
181 #if __cplusplus > 201703L
189 template<
size_t _Align,
class _Tp>
190 [[nodiscard,__gnu__::__always_inline__]]
191 constexpr _Tp* assume_aligned(_Tp* __ptr)
193 static_assert(std::ispow2(_Align));
194 _GLIBCXX_DEBUG_ASSERT((std::uintptr_t)__ptr % _Align == 0);
195 return static_cast<_Tp*>(__builtin_assume_aligned(__ptr, _Align));
199 #if __cplusplus > 201703L
200 template<
typename _Tp>
202 template<
typename _Tp,
typename _Up>
203 struct __is_pair<pair<_Tp, _Up>> :
true_type { };
204 template<
typename _Tp,
typename _Up>
205 struct __is_pair<const pair<_Tp, _Up>> :
true_type { };
210 template<
typename _Tp,
typename __ = _Require<__not_<__is_pair<_Tp>>>,
211 typename _Alloc,
typename... _Args>
213 __uses_alloc_args(
const _Alloc& __a, _Args&&... __args) noexcept
215 if constexpr (uses_allocator_v<remove_cv_t<_Tp>, _Alloc>)
217 if constexpr (is_constructible_v<_Tp, allocator_arg_t,
218 const _Alloc&, _Args...>)
220 return tuple<allocator_arg_t,
const _Alloc&, _Args&&...>(
221 allocator_arg, __a, std::forward<_Args>(__args)...);
225 static_assert(is_constructible_v<_Tp, _Args..., const _Alloc&>,
226 "construction with an allocator must be possible"
227 " if uses_allocator is true");
229 return tuple<_Args&&...,
const _Alloc&>(
230 std::forward<_Args>(__args)..., __a);
235 static_assert(is_constructible_v<_Tp, _Args...>);
237 return tuple<_Args&&...>(std::forward<_Args>(__args)...);
242 template<
typename _Tp>
243 concept _Std_pair = __is_pair<_Tp>::value;
248 # define _GLIBCXX_STD_PAIR_CONSTRAINT(T) _Std_pair T
249 # define _GLIBCXX_STD_PAIR_CONSTRAINT_(T) _Std_pair T
251 # define _GLIBCXX_STD_PAIR_CONSTRAINT(T) \
252 typename T, typename __ = _Require<__is_pair<T>>
253 # define _GLIBCXX_STD_PAIR_CONSTRAINT_(T) typename T, typename
256 template<
typename _Tp,
258 typename __ = _Require<__not_<__is_pair<_Tp>>>,
260 typename _Alloc,
typename... _Args>
262 uses_allocator_construction_args(
const _Alloc& __a,
263 _Args&&... __args) noexcept
265 requires (! _Std_pair<_Tp>)
268 return std::__uses_alloc_args<_Tp>(__a, std::forward<_Args>(__args)...);
271 template<_GLIBCXX_STD_PAIR_CONSTRAINT(_Tp),
typename _Alloc,
272 typename _Tuple1,
typename _Tuple2>
274 uses_allocator_construction_args(
const _Alloc& __a, piecewise_construct_t,
275 _Tuple1&& __x, _Tuple2&& __y) noexcept;
277 template<_GLIBCXX_STD_PAIR_CONSTRAINT(_Tp),
typename _Alloc>
279 uses_allocator_construction_args(
const _Alloc&) noexcept;
281 template<_GLIBCXX_STD_PAIR_CONSTRAINT(_Tp),
typename _Alloc,
282 typename _Up,
typename _Vp>
284 uses_allocator_construction_args(
const _Alloc&, _Up&&, _Vp&&) noexcept;
286 template<_GLIBCXX_STD_PAIR_CONSTRAINT(_Tp), typename _Alloc,
287 typename _Up, typename _Vp>
289 uses_allocator_construction_args(const _Alloc&,
290 const pair<_Up, _Vp>&) noexcept;
292 template<_GLIBCXX_STD_PAIR_CONSTRAINT(_Tp), typename _Alloc,
293 typename _Up, typename _Vp>
295 uses_allocator_construction_args(const _Alloc&, pair<_Up, _Vp>&&) noexcept;
297 template<_GLIBCXX_STD_PAIR_CONSTRAINT_(_Tp), typename _Alloc,
298 typename _Tuple1, typename _Tuple2>
300 uses_allocator_construction_args(const _Alloc& __a, piecewise_construct_t,
301 _Tuple1&& __x, _Tuple2&& __y) noexcept
303 using _Tp1 =
typename _Tp::first_type;
304 using _Tp2 =
typename _Tp::second_type;
307 std::apply([&__a](
auto&&... __args1) {
308 return std::uses_allocator_construction_args<_Tp1>(
310 }, std::forward<_Tuple1>(__x)),
311 std::apply([&__a](
auto&&... __args2) {
312 return std::uses_allocator_construction_args<_Tp2>(
314 }, std::forward<_Tuple2>(__y)));
317 template<_GLIBCXX_STD_PAIR_CONSTRAINT_(_Tp),
typename _Alloc>
319 uses_allocator_construction_args(
const _Alloc& __a) noexcept
321 using _Tp1 =
typename _Tp::first_type;
322 using _Tp2 =
typename _Tp::second_type;
325 std::uses_allocator_construction_args<_Tp1>(__a),
326 std::uses_allocator_construction_args<_Tp2>(__a));
329 template<_GLIBCXX_STD_PAIR_CONSTRAINT_(_Tp),
typename _Alloc,
330 typename _Up,
typename _Vp>
332 uses_allocator_construction_args(
const _Alloc& __a, _Up&& __u, _Vp&& __v)
335 using _Tp1 =
typename _Tp::first_type;
336 using _Tp2 =
typename _Tp::second_type;
339 std::uses_allocator_construction_args<_Tp1>(__a,
340 std::forward<_Up>(__u)),
341 std::uses_allocator_construction_args<_Tp2>(__a,
342 std::forward<_Vp>(__v)));
345 template<_GLIBCXX_STD_PAIR_CONSTRAINT_(_Tp),
typename _Alloc,
346 typename _Up,
typename _Vp>
348 uses_allocator_construction_args(
const _Alloc& __a,
349 const pair<_Up, _Vp>& __pr) noexcept
351 using _Tp1 =
typename _Tp::first_type;
352 using _Tp2 =
typename _Tp::second_type;
355 std::uses_allocator_construction_args<_Tp1>(__a, __pr.first),
356 std::uses_allocator_construction_args<_Tp2>(__a, __pr.second));
359 template<_GLIBCXX_STD_PAIR_CONSTRAINT_(_Tp),
typename _Alloc,
360 typename _Up,
typename _Vp>
362 uses_allocator_construction_args(
const _Alloc& __a,
363 pair<_Up, _Vp>&& __pr) noexcept
365 using _Tp1 =
typename _Tp::first_type;
366 using _Tp2 =
typename _Tp::second_type;
369 std::uses_allocator_construction_args<_Tp1>(__a,
371 std::uses_allocator_construction_args<_Tp2>(__a,
375 template<
typename _Tp,
typename _Alloc,
typename... _Args>
377 make_obj_using_allocator(
const _Alloc& __a, _Args&&... __args)
379 return std::make_from_tuple<_Tp>(
380 std::uses_allocator_construction_args<_Tp>(__a,
381 std::forward<_Args>(__args)...));
384 template<
typename _Tp,
typename _Alloc,
typename... _Args>
386 uninitialized_construct_using_allocator(_Tp* __p,
const _Alloc& __a,
389 void* __vp = const_cast<void*>(static_cast<const volatile void*>(__p));
390 return ::new(__vp) _Tp(std::make_obj_using_allocator<_Tp>(__a,
391 std::forward<_Args>(__args)...));
397 _GLIBCXX_END_NAMESPACE_VERSION
401 #if __cplusplus > 201402L
403 # if _PSTL_EXECUTION_POLICIES_DEFINED
405 # include <pstl/glue_memory_impl.h>
408 # include <pstl/glue_memory_defs.h>
412 # define __cpp_lib_parallel_algorithm 201603L