libstdc++
memory
Go to the documentation of this file.
1 // <memory> -*- C++ -*-
2 
3 // Copyright (C) 2001-2020 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  * @ingroup memory
42  */
43 
44 #ifndef _GLIBCXX_MEMORY
45 #define _GLIBCXX_MEMORY 1
46 
47 #pragma GCC system_header
48 
49 /**
50  * @defgroup memory Memory
51  * @ingroup utilities
52  *
53  * Components for memory allocation, deallocation, and management.
54  */
55 
56 /**
57  * @defgroup pointer_abstractions Pointer Abstractions
58  * @ingroup memory
59  *
60  * Smart pointers, etc.
61  */
62 
63 #include <bits/stl_algobase.h>
64 #include <bits/allocator.h>
65 #include <bits/stl_construct.h>
66 #include <bits/stl_uninitialized.h>
67 #include <bits/stl_tempbuf.h>
70 
71 #if __cplusplus >= 201103L
72 # include <exception> // std::exception
73 # include <typeinfo> // std::type_info in get_deleter
74 # include <iosfwd> // std::basic_ostream
75 # include <ext/atomicity.h>
76 # include <ext/concurrence.h>
77 # include <bits/functexcept.h>
78 # include <bits/stl_function.h> // std::less
79 # include <bits/uses_allocator.h>
80 # include <bits/alloc_traits.h>
81 # include <type_traits>
82 # include <debug/debug.h>
83 # include <bits/unique_ptr.h>
84 # include <bits/shared_ptr.h>
85 # include <bits/shared_ptr_atomic.h>
86 # if _GLIBCXX_USE_DEPRECATED
87 # include <backward/auto_ptr.h>
88 # endif
89 #else
90 # include <backward/auto_ptr.h>
91 #endif
92 
93 #if __cplusplus >= 201103L
94 #include <cstdint>
95 #if __cplusplus > 201703L
96 # include <bit> // for has_single_bit
97 # include <new> // for placement operator new
98 # include <tuple> // for tuple, make_tuple, make_from_tuple
99 #endif
100 namespace std _GLIBCXX_VISIBILITY(default)
101 {
102 _GLIBCXX_BEGIN_NAMESPACE_VERSION
103 
104 /**
105  * @brief Fit aligned storage in buffer.
106  * @ingroup memory
107  *
108  * This function tries to fit @a __size bytes of storage with alignment
109  * @a __align into the buffer @a __ptr of size @a __space bytes. If such
110  * a buffer fits then @a __ptr is changed to point to the first byte of the
111  * aligned storage and @a __space is reduced by the bytes used for alignment.
112  *
113  * C++11 20.6.5 [ptr.align]
114  *
115  * @param __align A fundamental or extended alignment value.
116  * @param __size Size of the aligned storage required.
117  * @param __ptr Pointer to a buffer of @a __space bytes.
118  * @param __space Size of the buffer pointed to by @a __ptr.
119  * @return the updated pointer if the aligned storage fits, otherwise nullptr.
120  *
121  */
122 inline void*
123 align(size_t __align, size_t __size, void*& __ptr, size_t& __space) noexcept
124 {
125 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
126  const auto __intptr = reinterpret_cast<uintptr_t>(__ptr);
127 #else
128  // Cannot use std::uintptr_t so assume that std::size_t can be used instead.
129  static_assert(sizeof(size_t) >= sizeof(void*),
130  "std::size_t must be a suitable substitute for std::uintptr_t");
131  const auto __intptr = reinterpret_cast<unsigned long long>(__ptr);
132 #endif
133  const auto __aligned = (__intptr - 1u + __align) & -__align;
134  const auto __diff = __aligned - __intptr;
135  if ((__size + __diff) > __space)
136  return nullptr;
137  else
138  {
139  __space -= __diff;
140  return __ptr = reinterpret_cast<void*>(__aligned);
141  }
142 }
143 
144 /** @defgroup ptr_safety Pointer Safety and Garbage Collection
145  * @ingroup memory
146  *
147  * Utilities to assist with garbage collection in an implementation
148  * that supports <em>strict pointer safety</em>.
149  * This implementation only supports <em>relaxed pointer safety</em>
150  * and so these functions have no effect.
151  *
152  * C++11 20.6.4 [util.dynamic.safety], Pointer safety
153  *
154  * @{
155  */
156 
157 /// Constants representing the different types of pointer safety.
158 enum class pointer_safety { relaxed, preferred, strict };
159 
160 /// Inform a garbage collector that an object is still in use.
161 inline void
163 
164 /// Unregister an object previously registered with declare_reachable.
165 template <typename _Tp>
166  inline _Tp*
167  undeclare_reachable(_Tp* __p) { return __p; }
168 
169 /// Inform a garbage collector that a region of memory need not be traced.
170 inline void
171 declare_no_pointers(char*, size_t) { }
172 
173 /// Unregister a range previously registered with declare_no_pointers.
174 inline void
175 undeclare_no_pointers(char*, size_t) { }
176 
177 /// The type of pointer safety supported by the implementation.
178 inline pointer_safety
179 get_pointer_safety() noexcept { return pointer_safety::relaxed; }
180 // @}
181 
182 #if __cplusplus > 201703L
183 #define __cpp_lib_assume_aligned 201811L
184  /** @brief Inform the compiler that a pointer is aligned.
185  *
186  * @tparam _Align An alignment value (i.e. a power of two)
187  * @tparam _Tp An object type
188  * @param __ptr A pointer that is aligned to _Align
189  * @ingroup memory
190  */
191  template<size_t _Align, class _Tp>
192  [[nodiscard,__gnu__::__always_inline__]]
193  constexpr _Tp* assume_aligned(_Tp* __ptr)
194  {
195  static_assert(std::has_single_bit(_Align));
196  _GLIBCXX_DEBUG_ASSERT((std::uintptr_t)__ptr % _Align == 0);
197  return static_cast<_Tp*>(__builtin_assume_aligned(__ptr, _Align));
198  }
199 #endif // C++2a
200 
201 #if __cplusplus > 201703L
202  template<typename _Tp>
203  struct __is_pair : false_type { };
204  template<typename _Tp, typename _Up>
205  struct __is_pair<pair<_Tp, _Up>> : true_type { };
206  template<typename _Tp, typename _Up>
207  struct __is_pair<const pair<_Tp, _Up>> : true_type { };
208 
209 /** @addtogroup allocators
210  * @{
211  */
212  template<typename _Tp, typename __ = _Require<__not_<__is_pair<_Tp>>>,
213  typename _Alloc, typename... _Args>
214  constexpr auto
215  __uses_alloc_args(const _Alloc& __a, _Args&&... __args) noexcept
216  {
217  if constexpr (uses_allocator_v<remove_cv_t<_Tp>, _Alloc>)
218  {
219  if constexpr (is_constructible_v<_Tp, allocator_arg_t,
220  const _Alloc&, _Args...>)
221  {
222  return tuple<allocator_arg_t, const _Alloc&, _Args&&...>(
223  allocator_arg, __a, std::forward<_Args>(__args)...);
224  }
225  else
226  {
227  static_assert(is_constructible_v<_Tp, _Args..., const _Alloc&>,
228  "construction with an allocator must be possible"
229  " if uses_allocator is true");
230 
231  return tuple<_Args&&..., const _Alloc&>(
232  std::forward<_Args>(__args)..., __a);
233  }
234  }
235  else
236  {
237  static_assert(is_constructible_v<_Tp, _Args...>);
238 
239  return tuple<_Args&&...>(std::forward<_Args>(__args)...);
240  }
241  }
242 
243 #if __cpp_concepts
244  template<typename _Tp>
245  concept _Std_pair = __is_pair<_Tp>::value;
246 #endif
247 
248 // This is a temporary workaround until -fconcepts is implied by -std=gnu++2a
249 #if __cpp_concepts
250 # define _GLIBCXX_STD_PAIR_CONSTRAINT(T) _Std_pair T
251 # define _GLIBCXX_STD_PAIR_CONSTRAINT_(T) _Std_pair T
252 #else
253 # define _GLIBCXX_STD_PAIR_CONSTRAINT(T) \
254  typename T, typename __ = _Require<__is_pair<T>>
255 # define _GLIBCXX_STD_PAIR_CONSTRAINT_(T) typename T, typename
256 #endif
257 
258  template<typename _Tp,
259 #if ! __cpp_concepts
260  typename __ = _Require<__not_<__is_pair<_Tp>>>,
261 #endif
262  typename _Alloc, typename... _Args>
263  constexpr auto
264  uses_allocator_construction_args(const _Alloc& __a,
265  _Args&&... __args) noexcept
266 #if __cpp_concepts
267  requires (! _Std_pair<_Tp>)
268 #endif
269  {
270  return std::__uses_alloc_args<_Tp>(__a, std::forward<_Args>(__args)...);
271  }
272 
273  template<_GLIBCXX_STD_PAIR_CONSTRAINT(_Tp), typename _Alloc,
274  typename _Tuple1, typename _Tuple2>
275  constexpr auto
276  uses_allocator_construction_args(const _Alloc& __a, piecewise_construct_t,
277  _Tuple1&& __x, _Tuple2&& __y) noexcept;
278 
279  template<_GLIBCXX_STD_PAIR_CONSTRAINT(_Tp), typename _Alloc>
280  constexpr auto
281  uses_allocator_construction_args(const _Alloc&) noexcept;
282 
283  template<_GLIBCXX_STD_PAIR_CONSTRAINT(_Tp), typename _Alloc,
284  typename _Up, typename _Vp>
285  constexpr auto
286  uses_allocator_construction_args(const _Alloc&, _Up&&, _Vp&&) noexcept;
287 
288  template<_GLIBCXX_STD_PAIR_CONSTRAINT(_Tp), typename _Alloc,
289  typename _Up, typename _Vp>
290  constexpr auto
291  uses_allocator_construction_args(const _Alloc&,
292  const pair<_Up, _Vp>&) noexcept;
293 
294  template<_GLIBCXX_STD_PAIR_CONSTRAINT(_Tp), typename _Alloc,
295  typename _Up, typename _Vp>
296  constexpr auto
297  uses_allocator_construction_args(const _Alloc&, pair<_Up, _Vp>&&) noexcept;
298 
299  template<_GLIBCXX_STD_PAIR_CONSTRAINT_(_Tp), typename _Alloc,
300  typename _Tuple1, typename _Tuple2>
301  constexpr auto
302  uses_allocator_construction_args(const _Alloc& __a, piecewise_construct_t,
303  _Tuple1&& __x, _Tuple2&& __y) 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::apply([&__a](auto&&... __args1) {
310  return std::uses_allocator_construction_args<_Tp1>(
311  __a, std::forward<decltype(__args1)>(__args1)...);
312  }, std::forward<_Tuple1>(__x)),
313  std::apply([&__a](auto&&... __args2) {
314  return std::uses_allocator_construction_args<_Tp2>(
315  __a, std::forward<decltype(__args2)>(__args2)...);
316  }, std::forward<_Tuple2>(__y)));
317  }
318 
319  template<_GLIBCXX_STD_PAIR_CONSTRAINT_(_Tp), typename _Alloc>
320  constexpr auto
321  uses_allocator_construction_args(const _Alloc& __a) noexcept
322  {
323  using _Tp1 = typename _Tp::first_type;
324  using _Tp2 = typename _Tp::second_type;
325 
326  return std::make_tuple(piecewise_construct,
327  std::uses_allocator_construction_args<_Tp1>(__a),
328  std::uses_allocator_construction_args<_Tp2>(__a));
329  }
330 
331  template<_GLIBCXX_STD_PAIR_CONSTRAINT_(_Tp), typename _Alloc,
332  typename _Up, typename _Vp>
333  constexpr auto
334  uses_allocator_construction_args(const _Alloc& __a, _Up&& __u, _Vp&& __v)
335  noexcept
336  {
337  using _Tp1 = typename _Tp::first_type;
338  using _Tp2 = typename _Tp::second_type;
339 
340  return std::make_tuple(piecewise_construct,
341  std::uses_allocator_construction_args<_Tp1>(__a,
342  std::forward<_Up>(__u)),
343  std::uses_allocator_construction_args<_Tp2>(__a,
344  std::forward<_Vp>(__v)));
345  }
346 
347  template<_GLIBCXX_STD_PAIR_CONSTRAINT_(_Tp), typename _Alloc,
348  typename _Up, typename _Vp>
349  constexpr auto
350  uses_allocator_construction_args(const _Alloc& __a,
351  const pair<_Up, _Vp>& __pr) noexcept
352  {
353  using _Tp1 = typename _Tp::first_type;
354  using _Tp2 = typename _Tp::second_type;
355 
356  return std::make_tuple(piecewise_construct,
357  std::uses_allocator_construction_args<_Tp1>(__a, __pr.first),
358  std::uses_allocator_construction_args<_Tp2>(__a, __pr.second));
359  }
360 
361  template<_GLIBCXX_STD_PAIR_CONSTRAINT_(_Tp), typename _Alloc,
362  typename _Up, typename _Vp>
363  constexpr auto
364  uses_allocator_construction_args(const _Alloc& __a,
365  pair<_Up, _Vp>&& __pr) noexcept
366  {
367  using _Tp1 = typename _Tp::first_type;
368  using _Tp2 = typename _Tp::second_type;
369 
370  return std::make_tuple(piecewise_construct,
371  std::uses_allocator_construction_args<_Tp1>(__a,
372  std::move(__pr).first),
373  std::uses_allocator_construction_args<_Tp2>(__a,
374  std::move(__pr).second));
375  }
376 
377  template<typename _Tp, typename _Alloc, typename... _Args>
378  inline _Tp
379  make_obj_using_allocator(const _Alloc& __a, _Args&&... __args)
380  {
381  return std::make_from_tuple<_Tp>(
382  std::uses_allocator_construction_args<_Tp>(__a,
383  std::forward<_Args>(__args)...));
384  }
385 
386  template<typename _Tp, typename _Alloc, typename... _Args>
387  inline _Tp*
388  uninitialized_construct_using_allocator(_Tp* __p, const _Alloc& __a,
389  _Args&&... __args)
390  {
391  return std::apply([&](auto&&... __xs) {
392  return std::construct_at(__p, std::forward<decltype(__xs)>(__xs)...);
393  }, std::uses_allocator_construction_args<_Tp>(__a,
394  std::forward<_Args>(__args)...));
395  }
396 // @}
397 
398 #endif // C++2a
399 
400 _GLIBCXX_END_NAMESPACE_VERSION
401 } // namespace
402 #endif // C++11
403 
404 #if __cplusplus > 201402L
405 // Parallel STL algorithms
406 # if _PSTL_EXECUTION_POLICIES_DEFINED
407 // If <execution> has already been included, pull in implementations
408 # include <pstl/glue_memory_impl.h>
409 # else
410 // Otherwise just pull in forward declarations
411 # include <pstl/glue_memory_defs.h>
412 # endif
413 
414 // Feature test macro for parallel algorithms
415 # define __cpp_lib_parallel_algorithm 201603L
416 #endif // C++17
417 
418 #endif /* _GLIBCXX_MEMORY */
iosfwd
stl_raw_storage_iter.h
alloc_traits.h
stl_function.h
atomicity.h
ranges_uninitialized.h
exception
std::undeclare_reachable
_Tp * undeclare_reachable(_Tp *__p)
Unregister an object previously registered with declare_reachable.
Definition: memory:167
functexcept.h
std::pointer_safety
pointer_safety
Constants representing the different types of pointer safety.
Definition: memory:158
std::declare_no_pointers
void declare_no_pointers(char *, size_t)
Inform a garbage collector that a region of memory need not be traced.
Definition: memory:171
std
ISO C++ entities toplevel namespace is std.
stl_construct.h
std::true_type
integral_constant< bool, true > true_type
The type used as a compile-time boolean with true value.
Definition: type_traits:75
typeinfo
stl_algobase.h
std::get_pointer_safety
pointer_safety get_pointer_safety() noexcept
The type of pointer safety supported by the implementation.
Definition: memory:179
std::forward
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition: move.h:76
std::align
void * align(size_t __align, size_t __size, void *&__ptr, size_t &__space) noexcept
Fit aligned storage in buffer.
Definition: memory:123
allocator.h
stl_tempbuf.h
std::declare_reachable
void declare_reachable(void *)
Inform a garbage collector that an object is still in use.
Definition: memory:162
type_traits
new
unique_ptr.h
stl_uninitialized.h
concurrence.h
std::undeclare_no_pointers
void undeclare_no_pointers(char *, size_t)
Unregister a range previously registered with declare_no_pointers.
Definition: memory:175
auto_ptr.h
shared_ptr_atomic.h
debug.h
cstdint
std::piecewise_construct
constexpr piecewise_construct_t piecewise_construct
Tag for piecewise construction of std::pair objects.
Definition: stl_pair.h:82
bit
std::move
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:101
std::false_type
integral_constant< bool, false > false_type
The type used as a compile-time boolean with false value.
Definition: type_traits:78