libstdc++
memory
Go to the documentation of this file.
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 #endif
95 namespace std _GLIBCXX_VISIBILITY(default)
96 {
97 _GLIBCXX_BEGIN_NAMESPACE_VERSION
98 
99 /**
100  * @brief Fit aligned storage in buffer.
101  *
102  * [ptr.align]
103  *
104  * This function tries to fit @a __size bytes of storage with alignment
105  * @a __align into the buffer @a __ptr of size @a __space bytes. If such
106  * a buffer fits then @a __ptr is changed to point to the first byte of the
107  * aligned storage and @a __space is reduced by the bytes used for alignment.
108  *
109  * @param __align A fundamental or extended alignment value.
110  * @param __size Size of the aligned storage required.
111  * @param __ptr Pointer to a buffer of @a __space bytes.
112  * @param __space Size of the buffer pointed to by @a __ptr.
113  * @return the updated pointer if the aligned storage fits, otherwise nullptr.
114  */
115 inline void*
116 align(size_t __align, size_t __size, void*& __ptr, size_t& __space) noexcept
117 {
118 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
119  const auto __intptr = reinterpret_cast<uintptr_t>(__ptr);
120 #else
121  // Cannot use std::uintptr_t so assume that std::size_t can be used instead.
122  static_assert(sizeof(size_t) >= sizeof(void*),
123  "std::size_t must be a suitable substitute for std::uintptr_t");
124  const auto __intptr = reinterpret_cast<unsigned long long>(__ptr);
125 #endif
126  const auto __aligned = (__intptr - 1u + __align) & -__align;
127  const auto __diff = __aligned - __intptr;
128  if ((__size + __diff) > __space)
129  return nullptr;
130  else
131  {
132  __space -= __diff;
133  return __ptr = reinterpret_cast<void*>(__aligned);
134  }
135 }
136 
137 // 20.7.4 [util.dynamic.safety], pointer safety
138 
139 enum class pointer_safety { relaxed, preferred, strict };
140 
141 inline void
142 declare_reachable(void*) { }
143 
144 template <typename _Tp>
145  inline _Tp*
146  undeclare_reachable(_Tp* __p) { return __p; }
147 
148 inline void
149 declare_no_pointers(char*, size_t) { }
150 
151 inline void
152 undeclare_no_pointers(char*, size_t) { }
153 
154 inline pointer_safety
155 get_pointer_safety() noexcept { return pointer_safety::relaxed; }
156 
157 #if __cplusplus > 201703L
158  /// Inform the compiler that a pointer is aligned.
159  template<size_t _Align, class _Tp>
160  [[nodiscard,__gnu__::__always_inline__]]
161  constexpr _Tp* assume_aligned(_Tp* __ptr)
162  {
163  static_assert(std::ispow2(_Align));
164  _GLIBCXX_DEBUG_ASSERT((std::uintptr_t)__ptr % _Align == 0);
165  return static_cast<_Tp*>(__builtin_assume_aligned(__ptr, _Align));
166  }
167 #endif // C++2a
168 
169 _GLIBCXX_END_NAMESPACE_VERSION
170 } // namespace
171 #endif // C++11
172 
173 #endif /* _GLIBCXX_MEMORY */