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