libstdc++
memory
Go to the documentation of this file.
1// <memory> -*- C++ -*-
2
3// Copyright (C) 2001-2025 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#ifdef _GLIBCXX_SYSHDR
48#pragma GCC system_header
49#endif
50
51/**
52 * @defgroup memory Memory
53 * @ingroup utilities
54 *
55 * Components for memory allocation, deallocation, and management.
56 */
57
58/**
59 * @defgroup pointer_abstractions Pointer Abstractions
60 * @ingroup memory
61 *
62 * Smart pointers, etc.
63 */
64
65#include <bits/memoryfwd.h>
66#if _GLIBCXX_HOSTED
67# include <bits/allocator.h>
68# include <bits/stl_tempbuf.h>
69#endif
70#include <bits/stl_construct.h>
71#include <bits/stl_uninitialized.h>
72#include <bits/stl_raw_storage_iter.h>
73
74#if __cplusplus >= 201103L
75# include <type_traits>
76# include <bits/align.h>
77# include <bits/uses_allocator.h>
78# include <bits/alloc_traits.h>
79# include <debug/debug.h>
80# include <bits/unique_ptr.h>
81# if _GLIBCXX_HOSTED
82# include <bits/shared_ptr.h>
83# include <bits/shared_ptr_atomic.h>
84# endif
85#endif
86
87#if __cplusplus < 201103L || _GLIBCXX_USE_DEPRECATED
88# include <backward/auto_ptr.h>
89#endif
90
91#if __cplusplus > 201703L
92# include <bits/ranges_uninitialized.h>
93# include <bits/uses_allocator_args.h>
94#endif
95
96#if __cplusplus > 202002L
97# include <bits/out_ptr.h>
98#endif
99
100#if __cplusplus > 202302L
101# include <bits/indirect.h>
102#endif
103
104#define __glibcxx_want_addressof_constexpr
105#define __glibcxx_want_allocator_traits_is_always_equal
106#define __glibcxx_want_assume_aligned
107#define __glibcxx_want_atomic_shared_ptr
108#define __glibcxx_want_atomic_value_initialization
109#define __glibcxx_want_constexpr_dynamic_alloc
110#define __glibcxx_want_constexpr_memory
111#define __glibcxx_want_enable_shared_from_this
112#define __glibcxx_want_indirect
113#define __glibcxx_want_is_sufficiently_aligned
114#define __glibcxx_want_make_unique
115#define __glibcxx_want_out_ptr
116#define __glibcxx_want_parallel_algorithm
117#define __glibcxx_want_polymorphic
118#define __glibcxx_want_ranges
119#define __glibcxx_want_raw_memory_algorithms
120#define __glibcxx_want_shared_ptr_arrays
121#define __glibcxx_want_shared_ptr_weak_type
122#define __glibcxx_want_smart_ptr_for_overwrite
123#define __glibcxx_want_start_lifetime_as
124#define __glibcxx_want_to_address
125#define __glibcxx_want_transparent_operators
126#define __glibcxx_want_smart_ptr_owner_equality
127#include <bits/version.h>
128
129#if __cplusplus >= 201103L && __cplusplus <= 202002L && _GLIBCXX_HOSTED
130namespace std _GLIBCXX_VISIBILITY(default)
131{
132_GLIBCXX_BEGIN_NAMESPACE_VERSION
133
134/** @defgroup ptr_safety Pointer Safety and Garbage Collection
135 * @ingroup memory
136 *
137 * Utilities to assist with garbage collection in an implementation
138 * that supports <em>strict pointer safety</em>.
139 * This implementation only supports <em>relaxed pointer safety</em>
140 * and so these functions have no effect.
141 *
142 * C++11 20.6.4 [util.dynamic.safety], Pointer safety
143 *
144 * @{
145 */
146
147/// Constants representing the different types of pointer safety.
148enum class pointer_safety { relaxed, preferred, strict };
149
150/// Inform a garbage collector that an object is still in use.
151inline void
152declare_reachable(void*) { }
153
154/// Unregister an object previously registered with declare_reachable.
155template <typename _Tp>
156 inline _Tp*
157 undeclare_reachable(_Tp* __p) { return __p; }
158
159/// Inform a garbage collector that a region of memory need not be traced.
160inline void
161declare_no_pointers(char*, size_t) { }
162
163/// Unregister a range previously registered with declare_no_pointers.
164inline void
165undeclare_no_pointers(char*, size_t) { }
166
167/// The type of pointer safety supported by the implementation.
168inline pointer_safety
169get_pointer_safety() noexcept { return pointer_safety::relaxed; }
170/// @}
171
172_GLIBCXX_END_NAMESPACE_VERSION
173} // namespace
174#endif // C++11 to C++20
175
176#if __cpp_lib_start_lifetime_as >= 202207L // C++ >= 23
177namespace std _GLIBCXX_VISIBILITY(default)
178{
179_GLIBCXX_BEGIN_NAMESPACE_VERSION
180 template<typename _Tp>
181 [[__gnu__::__always_inline__]]
182 inline _Tp*
183 start_lifetime_as(void* __p) noexcept
184 {
185#if __cpp_lib_is_implicit_lifetime >= 202302L
186 static_assert(is_implicit_lifetime_v<_Tp>);
187#endif
188 auto __q = reinterpret_cast<_Tp*>(__p);
189 __asm__ __volatile__("" : "=g" (__q), "=m" (*__q)
190 : "0" (__q), "m" (*__q));
191 return __q;
192 }
193
194 template<typename _Tp>
195 [[__gnu__::__always_inline__]]
196 inline const _Tp*
197 start_lifetime_as(const void* __p) noexcept
198 {
199#if __cpp_lib_is_implicit_lifetime >= 202302L
200 static_assert(is_implicit_lifetime_v<_Tp>);
201#endif
202 auto __q = reinterpret_cast<const _Tp*>(__p);
203 auto __r = reinterpret_cast<_Tp*>(const_cast<void*>(__p));
204 __asm__ __volatile__("" : "=g" (__q), "=m" (*__r)
205 : "0" (__q), "m" (*__q));
206 return __q;
207 }
208
209 template<typename _Tp>
210 [[__gnu__::__always_inline__]]
211 inline volatile _Tp*
212 start_lifetime_as(volatile void* __p) noexcept
213 {
214#if __cpp_lib_is_implicit_lifetime >= 202302L
215 static_assert(is_implicit_lifetime_v<_Tp>);
216#endif
217 auto __q = reinterpret_cast<volatile _Tp*>(__p);
218 auto __r = reinterpret_cast<_Tp*>(const_cast<void*>(__p));
219 __asm__ __volatile__("" : "=g" (__q), "=m" (*__r)
220 : "0" (__q), "m" (*__q));
221 return __q;
222 }
223
224 template<typename _Tp>
225 [[__gnu__::__always_inline__]]
226 inline const volatile _Tp*
227 start_lifetime_as(const volatile void* __p) noexcept
228 {
229#if __cpp_lib_is_implicit_lifetime >= 202302L
230 static_assert(is_implicit_lifetime_v<_Tp>);
231#endif
232 auto __q = reinterpret_cast<const volatile _Tp*>(__p);
233 auto __r = reinterpret_cast<_Tp*>(const_cast<void*>(__p));
234 __asm__ __volatile__("" : "=g" (__q), "=m" (*__r)
235 : "0" (__q), "m" (*__q));
236 return __q;
237 }
238
239 template<typename _Tp>
240 [[__gnu__::__always_inline__]]
241 inline _Tp*
242 start_lifetime_as_array(void* __p, size_t __n) noexcept
243 {
244 auto __q = reinterpret_cast<_Tp*>(__p);
245 if (!__n)
246 return __q;
247 auto __r = (__extension__ reinterpret_cast<_Tp(*)[__n]>(__p));
248 __asm__ __volatile__("" : "=g" (__q), "=m" (*__r)
249 : "0" (__q), "m" (*__r));
250 return __q;
251 }
252
253 template<typename _Tp>
254 [[__gnu__::__always_inline__]]
255 inline const _Tp*
256 start_lifetime_as_array(const void* __p, size_t __n) noexcept
257 {
258 auto __q = reinterpret_cast<const _Tp*>(__p);
259 if (!__n)
260 return __q;
261 auto __r = (__extension__ reinterpret_cast<const _Tp(*)[__n]>(__p));
262 auto __s = (__extension__
263 reinterpret_cast<_Tp(*)[__n]>(const_cast<void*>(__p)));
264 __asm__ __volatile__("" : "=g" (__q), "=m" (*__s)
265 : "0" (__q), "m" (*__r));
266 return __q;
267 }
268
269 template<typename _Tp>
270 [[__gnu__::__always_inline__]]
271 inline volatile _Tp*
272 start_lifetime_as_array(volatile void* __p, size_t __n) noexcept
273 {
274 auto __q = reinterpret_cast<volatile _Tp*>(__p);
275 if (!__n)
276 return __q;
277 auto __r = (__extension__ reinterpret_cast<volatile _Tp(*)[__n]>(__p));
278 auto __s = (__extension__
279 reinterpret_cast<_Tp(*)[__n]>(const_cast<void*>(__p)));
280 __asm__ __volatile__("" : "=g" (__q), "=m" (*__s)
281 : "0" (__q), "m" (*__r));
282 return __q;
283 }
284
285 template<typename _Tp>
286 [[__gnu__::__always_inline__]]
287 inline const volatile _Tp*
288 start_lifetime_as_array(const volatile void* __p, size_t __n) noexcept
289 {
290 auto __q = reinterpret_cast<const volatile _Tp*>(__p);
291 if (!__n)
292 return __q;
293 auto __r = (__extension__ reinterpret_cast<const volatile _Tp(*)[__n]>(__p));
294 auto __s = (__extension__
295 reinterpret_cast<_Tp(*)[__n]>(const_cast<void*>(__p)));
296 __asm__ __volatile__("" : "=g" (__q), "=m" (*__s)
297 : "0" (__q), "m" (*__r));
298 return __q;
299 }
300_GLIBCXX_END_NAMESPACE_VERSION
301} // namespace
302#endif
303
304#ifdef __cpp_lib_parallel_algorithm // C++ >= 17 && HOSTED
305// Parallel STL algorithms
306# if _PSTL_EXECUTION_POLICIES_DEFINED
307// If <execution> has already been included, pull in implementations
308# include <pstl/glue_memory_impl.h>
309# else
310// Otherwise just pull in forward declarations
311# include <pstl/glue_memory_defs.h>
312# endif
313#endif // __cpp_lib_parallel_algorithm
314
315#endif /* _GLIBCXX_MEMORY */