| 1 |
// Allocators -*- 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) 1996-1997 |
| 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 |
/** @file bits/allocator.h |
| 39 |
* This is an internal header file, included by other library headers. |
| 40 |
* Do not attempt to use it directly. @headername{memory} |
| 41 |
*/ |
| 42 |
|
| 43 |
#ifndef _ALLOCATOR_H |
| 44 |
#define _ALLOCATOR_H 1 |
| 45 |
|
| 46 |
#include <bits/c++allocator.h> // Define the base class to std::allocator. |
| 47 |
#include <bits/memoryfwd.h> |
| 48 |
#if __cplusplus >= 201103L |
| 49 |
#include <type_traits> |
| 50 |
#endif |
| 51 |
|
| 52 |
#pragma GCC diagnostic push |
| 53 |
#pragma GCC diagnostic ignored "-Wc++11-extensions" |
| 54 |
|
| 55 |
namespace std _GLIBCXX_VISIBILITY(default) |
| 56 |
{ |
| 57 |
_GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 58 |
|
| 59 |
/** |
| 60 |
* @addtogroup allocators |
| 61 |
* @{ |
| 62 |
*/ |
| 63 |
|
| 64 |
// Since C++20 the primary template should be used for allocator<void>, |
| 65 |
// but then it would have a non-trivial default ctor and dtor for C++20, |
| 66 |
// but trivial for C++98-17, which would be an ABI incompatibility between |
| 67 |
// different standard dialects. So C++20 still uses the allocator<void> |
| 68 |
// explicit specialization, with the historical ABI properties, but with |
| 69 |
// the same members that are present in the primary template. |
| 70 |
|
| 71 |
/** std::allocator<void> specialization. |
| 72 |
* |
| 73 |
* @headerfile memory |
| 74 |
*/ |
| 75 |
template<> |
| 76 |
class allocator<void> |
| 77 |
{ |
| 78 |
public: |
| 79 |
typedef void value_type; |
| 80 |
typedef size_t size_type; |
| 81 |
typedef ptrdiff_t difference_type; |
| 82 |
|
| 83 |
#if __cplusplus <= 201703L |
| 84 |
// These were removed for C++20, allocator_traits does the right thing. |
| 85 |
typedef void* pointer; |
| 86 |
typedef const void* const_pointer; |
| 87 |
|
| 88 |
template<typename _Tp1> |
| 89 |
struct rebind |
| 90 |
{ typedef allocator<_Tp1> other; }; |
| 91 |
#endif |
| 92 |
|
| 93 |
#if __cplusplus >= 201103L |
| 94 |
// _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 95 |
// 2103. std::allocator propagate_on_container_move_assignment |
| 96 |
using propagate_on_container_move_assignment = true_type; |
| 97 |
|
| 98 |
#if __cplusplus <= 202302L |
| 99 |
using is_always_equal |
| 100 |
_GLIBCXX20_DEPRECATED_SUGGEST("std::allocator_traits::is_always_equal") |
| 101 |
= true_type; |
| 102 |
#endif |
| 103 |
|
| 104 |
#if __cplusplus >= 202002L |
| 105 |
// As noted above, these members are present for C++20 to provide the |
| 106 |
// same API as the primary template, but still trivial as in pre-C++20. |
| 107 |
allocator() = default; |
| 108 |
~allocator() = default; |
| 109 |
|
| 110 |
template<typename _Up> |
| 111 |
__attribute__((__always_inline__)) |
| 112 |
constexpr |
| 113 |
allocator(const allocator<_Up>&) noexcept { } |
| 114 |
|
| 115 |
// No allocate member because it's ill-formed by LWG 3307. |
| 116 |
// No deallocate member because it would be undefined to call it |
| 117 |
// with any pointer which wasn't obtained from allocate. |
| 118 |
#endif // C++20 |
| 119 |
#endif // C++11 |
| 120 |
}; |
| 121 |
|
| 122 |
/** |
| 123 |
* @brief The @a standard allocator, as per C++03 [20.4.1]. |
| 124 |
* |
| 125 |
* See https://gcc.gnu.org/onlinedocs/libstdc++/manual/memory.html#std.util.memory.allocator |
| 126 |
* for further details. |
| 127 |
* |
| 128 |
* @tparam _Tp Type of allocated object. |
| 129 |
* |
| 130 |
* @headerfile memory |
| 131 |
*/ |
| 132 |
template<typename _Tp> |
| 133 |
class allocator : public __allocator_base<_Tp> |
| 134 |
{ |
| 135 |
public: |
| 136 |
typedef _Tp value_type; |
| 137 |
typedef size_t size_type; |
| 138 |
typedef ptrdiff_t difference_type; |
| 139 |
|
| 140 |
#if __cplusplus <= 201703L |
| 141 |
// These were removed for C++20. |
| 142 |
typedef _Tp* pointer; |
| 143 |
typedef const _Tp* const_pointer; |
| 144 |
typedef _Tp& reference; |
| 145 |
typedef const _Tp& const_reference; |
| 146 |
|
| 147 |
template<typename _Tp1> |
| 148 |
struct rebind |
| 149 |
{ typedef allocator<_Tp1> other; }; |
| 150 |
#endif |
| 151 |
|
| 152 |
#if __cplusplus >= 201103L |
| 153 |
// _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 154 |
// 2103. std::allocator propagate_on_container_move_assignment |
| 155 |
using propagate_on_container_move_assignment = true_type; |
| 156 |
|
| 157 |
#if __cplusplus <= 202302L |
| 158 |
using is_always_equal |
| 159 |
_GLIBCXX20_DEPRECATED_SUGGEST("std::allocator_traits::is_always_equal") |
| 160 |
= true_type; |
| 161 |
#endif |
| 162 |
#endif |
| 163 |
|
| 164 |
// _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 165 |
// 3035. std::allocator's constructors should be constexpr |
| 166 |
__attribute__((__always_inline__)) |
| 167 |
_GLIBCXX20_CONSTEXPR |
| 168 |
allocator() _GLIBCXX_NOTHROW { } |
| 169 |
|
| 170 |
__attribute__((__always_inline__)) |
| 171 |
_GLIBCXX20_CONSTEXPR |
| 172 |
allocator(const allocator& __a) _GLIBCXX_NOTHROW |
| 173 |
: __allocator_base<_Tp>(__a) { } |
| 174 |
|
| 175 |
#if __cplusplus >= 201103L |
| 176 |
// Avoid implicit deprecation. |
| 177 |
allocator& operator=(const allocator&) = default; |
| 178 |
#endif |
| 179 |
|
| 180 |
template<typename _Tp1> |
| 181 |
__attribute__((__always_inline__)) |
| 182 |
_GLIBCXX20_CONSTEXPR |
| 183 |
allocator(const allocator<_Tp1>&) _GLIBCXX_NOTHROW { } |
| 184 |
|
| 185 |
__attribute__((__always_inline__)) |
| 186 |
#if __cpp_constexpr_dynamic_alloc |
| 187 |
constexpr |
| 188 |
#endif |
| 189 |
~allocator() _GLIBCXX_NOTHROW { } |
| 190 |
|
| 191 |
#if __cplusplus > 201703L |
| 192 |
[[nodiscard,__gnu__::__always_inline__]] |
| 193 |
constexpr _Tp* |
| 194 |
allocate(size_t __n) |
| 195 |
{ |
| 196 |
if (std::__is_constant_evaluated()) |
| 197 |
{ |
| 198 |
if (__builtin_mul_overflow(__n, sizeof(_Tp), &__n)) |
| 199 |
std::__throw_bad_array_new_length(); |
| 200 |
return static_cast<_Tp*>(::operator new(__n)); |
| 201 |
} |
| 202 |
|
| 203 |
return __allocator_base<_Tp>::allocate(__n, 0); |
| 204 |
} |
| 205 |
|
| 206 |
[[__gnu__::__always_inline__]] |
| 207 |
constexpr void |
| 208 |
deallocate(_Tp* __p, size_t __n) |
| 209 |
{ |
| 210 |
if (std::__is_constant_evaluated()) |
| 211 |
{ |
| 212 |
::operator delete(__p); |
| 213 |
return; |
| 214 |
} |
| 215 |
__allocator_base<_Tp>::deallocate(__p, __n); |
| 216 |
} |
| 217 |
#endif // C++20 |
| 218 |
|
| 219 |
friend __attribute__((__always_inline__)) _GLIBCXX20_CONSTEXPR |
| 220 |
bool |
| 221 |
operator==(const allocator&, const allocator&) _GLIBCXX_NOTHROW |
| 222 |
{ return true; } |
| 223 |
|
| 224 |
#if __cpp_impl_three_way_comparison < 201907L |
| 225 |
friend __attribute__((__always_inline__)) _GLIBCXX20_CONSTEXPR |
| 226 |
bool |
| 227 |
operator!=(const allocator&, const allocator&) _GLIBCXX_NOTHROW |
| 228 |
{ return false; } |
| 229 |
#endif |
| 230 |
|
| 231 |
// Inherit everything else. |
| 232 |
}; |
| 233 |
|
| 234 |
/** Equality comparison for std::allocator objects |
| 235 |
* |
| 236 |
* @return true, for all std::allocator objects. |
| 237 |
* @relates std::allocator |
| 238 |
*/ |
| 239 |
template<typename _T1, typename _T2> |
| 240 |
__attribute__((__always_inline__)) |
| 241 |
inline _GLIBCXX20_CONSTEXPR bool |
| 242 |
operator==(const allocator<_T1>&, const allocator<_T2>&) |
| 243 |
_GLIBCXX_NOTHROW |
| 244 |
{ return true; } |
| 245 |
|
| 246 |
#if __cpp_impl_three_way_comparison < 201907L |
| 247 |
template<typename _T1, typename _T2> |
| 248 |
__attribute__((__always_inline__)) |
| 249 |
inline _GLIBCXX20_CONSTEXPR bool |
| 250 |
operator!=(const allocator<_T1>&, const allocator<_T2>&) |
| 251 |
_GLIBCXX_NOTHROW |
| 252 |
{ return false; } |
| 253 |
#endif |
| 254 |
|
| 255 |
/// @cond undocumented |
| 256 |
|
| 257 |
// Invalid allocator<cv T> partial specializations. |
| 258 |
// allocator_traits::rebind_alloc can be used to form a valid allocator type. |
| 259 |
template<typename _Tp> |
| 260 |
class allocator<const _Tp> |
| 261 |
{ |
| 262 |
public: |
| 263 |
typedef _Tp value_type; |
| 264 |
allocator() { } |
| 265 |
template<typename _Up> allocator(const allocator<_Up>&) { } |
| 266 |
}; |
| 267 |
|
| 268 |
template<typename _Tp> |
| 269 |
class allocator<volatile _Tp> |
| 270 |
{ |
| 271 |
public: |
| 272 |
typedef _Tp value_type; |
| 273 |
allocator() { } |
| 274 |
template<typename _Up> allocator(const allocator<_Up>&) { } |
| 275 |
}; |
| 276 |
|
| 277 |
template<typename _Tp> |
| 278 |
class allocator<const volatile _Tp> |
| 279 |
{ |
| 280 |
public: |
| 281 |
typedef _Tp value_type; |
| 282 |
allocator() { } |
| 283 |
template<typename _Up> allocator(const allocator<_Up>&) { } |
| 284 |
}; |
| 285 |
/// @endcond |
| 286 |
|
| 287 |
/// @} group allocator |
| 288 |
|
| 289 |
// Inhibit implicit instantiations for required instantiations, |
| 290 |
// which are defined via explicit instantiations elsewhere. |
| 291 |
#if _GLIBCXX_EXTERN_TEMPLATE |
| 292 |
extern template class allocator<char>; |
| 293 |
extern template class allocator<wchar_t>; |
| 294 |
#endif |
| 295 |
|
| 296 |
// Undefine. |
| 297 |
#undef __allocator_base |
| 298 |
|
| 299 |
_GLIBCXX_END_NAMESPACE_VERSION |
| 300 |
} // namespace std |
| 301 |
|
| 302 |
#pragma GCC diagnostic pop |
| 303 |
#endif |