| 1 | // Safe sequence/iterator base implementation -*- C++ -*- |
| 2 | |
| 3 | // Copyright (C) 2003-2026 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 | /** @file debug/safe_base.h |
| 26 | * This file is a GNU debug extension to the Standard C++ Library. |
| 27 | */ |
| 28 | |
| 29 | #ifndef _GLIBCXX_DEBUG_SAFE_BASE_H |
| 30 | #define _GLIBCXX_DEBUG_SAFE_BASE_H 1 |
| 31 | |
| 32 | #include <ext/concurrence.h> |
| 33 | |
| 34 | namespace __gnu_debug |
| 35 | { |
| 36 | class _Safe_sequence_base; |
| 37 | |
| 38 | /** \brief Basic functionality for a @a safe iterator. |
| 39 | * |
| 40 | * The %_Safe_iterator_base base class implements the functionality |
| 41 | * of a safe iterator that is not specific to a particular iterator |
| 42 | * type. It contains a pointer back to the sequence it references |
| 43 | * along with iterator version information and pointers to form a |
| 44 | * doubly-linked list of iterators referenced by the container. |
| 45 | * |
| 46 | * This class must not perform any operations that can throw an |
| 47 | * exception, or the exception guarantees of derived iterators will |
| 48 | * be broken. |
| 49 | */ |
| 50 | class _Safe_iterator_base |
| 51 | { |
| 52 | friend class _Safe_sequence_base; |
| 53 | |
| 54 | public: |
| 55 | /** The sequence this iterator references; may be NULL to indicate |
| 56 | * a singular iterator. Stored as pointer-to-const because sequence |
| 57 | * could be declared as const. |
| 58 | */ |
| 59 | const _Safe_sequence_base* _M_sequence; |
| 60 | |
| 61 | /** The version number of this iterator. The sentinel value 0 is |
| 62 | * used to indicate an invalidated iterator (i.e., one that is |
| 63 | * singular because of an operation on the container). This |
| 64 | * version number must equal the version number in the sequence |
| 65 | * referenced by _M_sequence for the iterator to be |
| 66 | * non-singular. |
| 67 | */ |
| 68 | unsigned int _M_version; |
| 69 | |
| 70 | /** Pointer to the previous iterator in the sequence's list of |
| 71 | iterators. Only valid when _M_sequence != NULL. */ |
| 72 | _Safe_iterator_base* _M_prior; |
| 73 | |
| 74 | /** Pointer to the next iterator in the sequence's list of |
| 75 | iterators. Only valid when _M_sequence != NULL. */ |
| 76 | _Safe_iterator_base* _M_next; |
| 77 | |
| 78 | protected: |
| 79 | /** Initializes the iterator and makes it singular. */ |
| 80 | _GLIBCXX20_CONSTEXPR |
| 81 | _Safe_iterator_base() |
| 82 | : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0) |
| 83 | { } |
| 84 | |
| 85 | /** Initialize the iterator to reference the sequence pointed to |
| 86 | * by @p __seq. @p __constant is true when we are initializing a |
| 87 | * constant iterator, and false if it is a mutable iterator. Note |
| 88 | * that @p __seq may be NULL, in which case the iterator will be |
| 89 | * singular. Otherwise, the iterator will reference @p __seq and |
| 90 | * be nonsingular. |
| 91 | */ |
| 92 | _GLIBCXX20_CONSTEXPR |
| 93 | _Safe_iterator_base(const _Safe_sequence_base* __seq, bool __constant) |
| 94 | : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0) |
| 95 | { |
| 96 | if (!std::__is_constant_evaluated()) |
| 97 | this->_M_attach(__seq, __constant); |
| 98 | } |
| 99 | |
| 100 | /** Initializes the iterator to reference the same sequence that |
| 101 | @p __x does. @p __constant is true if this is a constant |
| 102 | iterator, and false if it is mutable. */ |
| 103 | _GLIBCXX20_CONSTEXPR |
| 104 | _Safe_iterator_base(const _Safe_iterator_base& __x, bool __constant) |
| 105 | : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0) |
| 106 | { |
| 107 | if (!std::__is_constant_evaluated()) |
| 108 | this->_M_attach(seq: __x._M_sequence, __constant); |
| 109 | } |
| 110 | |
| 111 | _GLIBCXX20_CONSTEXPR |
| 112 | ~_Safe_iterator_base() |
| 113 | { |
| 114 | if (!std::__is_constant_evaluated()) |
| 115 | this->_M_detach(); |
| 116 | } |
| 117 | |
| 118 | /** For use in _Safe_iterator. */ |
| 119 | __gnu_cxx::__mutex& |
| 120 | _M_get_mutex() _GLIBCXX_USE_NOEXCEPT; |
| 121 | |
| 122 | /** Attaches this iterator to the given sequence, detaching it |
| 123 | * from whatever sequence it was attached to originally. If the |
| 124 | * new sequence is the NULL pointer, the iterator is left |
| 125 | * unattached. |
| 126 | */ |
| 127 | void |
| 128 | _M_attach(const _Safe_sequence_base* __seq, bool __constant); |
| 129 | |
| 130 | /** Likewise, but not thread-safe. */ |
| 131 | void |
| 132 | _M_attach_single(const _Safe_sequence_base* __seq, |
| 133 | bool __constant) _GLIBCXX_USE_NOEXCEPT; |
| 134 | |
| 135 | /** Detach the iterator for whatever sequence it is attached to, |
| 136 | * if any. |
| 137 | */ |
| 138 | void |
| 139 | _M_detach(); |
| 140 | |
| 141 | #if !_GLIBCXX_INLINE_VERSION |
| 142 | private: |
| 143 | /***************************************************************/ |
| 144 | /** Not-const method preserved for abi backward compatibility. */ |
| 145 | void |
| 146 | _M_attach(_Safe_sequence_base* __seq, bool __constant); |
| 147 | |
| 148 | void |
| 149 | _M_attach_single(_Safe_sequence_base* __seq, |
| 150 | bool __constant) _GLIBCXX_USE_NOEXCEPT; |
| 151 | /***************************************************************/ |
| 152 | #endif |
| 153 | |
| 154 | public: |
| 155 | /** Likewise, but not thread-safe. */ |
| 156 | void |
| 157 | _M_detach_single() _GLIBCXX_USE_NOEXCEPT; |
| 158 | |
| 159 | /** Determines if we are attached to the given sequence. */ |
| 160 | bool |
| 161 | _M_attached_to(const _Safe_sequence_base* __seq) const |
| 162 | { return _M_sequence == __seq; } |
| 163 | |
| 164 | /** Is this iterator singular? */ |
| 165 | _GLIBCXX_PURE bool |
| 166 | _M_singular() const _GLIBCXX_USE_NOEXCEPT; |
| 167 | |
| 168 | /** Can we compare this iterator to the given iterator @p __x? |
| 169 | Returns true if both iterators are nonsingular and reference |
| 170 | the same sequence. */ |
| 171 | _GLIBCXX_PURE bool |
| 172 | _M_can_compare(const _Safe_iterator_base& __x) const _GLIBCXX_USE_NOEXCEPT; |
| 173 | |
| 174 | /** Invalidate the iterator, making it singular. */ |
| 175 | void |
| 176 | _M_invalidate() |
| 177 | { _M_version = 0; } |
| 178 | |
| 179 | /** Reset all member variables */ |
| 180 | void |
| 181 | _M_reset() _GLIBCXX_USE_NOEXCEPT; |
| 182 | |
| 183 | /** Unlink itself */ |
| 184 | void |
| 185 | _M_unlink() _GLIBCXX_USE_NOEXCEPT |
| 186 | { |
| 187 | if (_M_prior) |
| 188 | _M_prior->_M_next = _M_next; |
| 189 | if (_M_next) |
| 190 | _M_next->_M_prior = _M_prior; |
| 191 | } |
| 192 | }; |
| 193 | |
| 194 | /** Iterators that derive from _Safe_iterator_base can be determined singular |
| 195 | * or non-singular. |
| 196 | **/ |
| 197 | inline bool |
| 198 | __check_singular_aux(const _Safe_iterator_base* __x) |
| 199 | { return __x->_M_singular(); } |
| 200 | |
| 201 | /** |
| 202 | * @brief Base class that supports tracking of iterators that |
| 203 | * reference a sequence. |
| 204 | * |
| 205 | * The %_Safe_sequence_base class provides basic support for |
| 206 | * tracking iterators into a sequence. Sequences that track |
| 207 | * iterators must derived from %_Safe_sequence_base publicly, so |
| 208 | * that safe iterators (which inherit _Safe_iterator_base) can |
| 209 | * attach to them. This class contains two linked lists of |
| 210 | * iterators, one for constant iterators and one for mutable |
| 211 | * iterators, and a version number that allows very fast |
| 212 | * invalidation of all iterators that reference the container. |
| 213 | * |
| 214 | * This class must ensure that no operation on it may throw an |
| 215 | * exception, otherwise @a safe sequences may fail to provide the |
| 216 | * exception-safety guarantees required by the C++ standard. |
| 217 | */ |
| 218 | class _Safe_sequence_base |
| 219 | { |
| 220 | friend class _Safe_iterator_base; |
| 221 | |
| 222 | public: |
| 223 | /// The list of mutable iterators that reference this container |
| 224 | mutable _Safe_iterator_base* _M_iterators; |
| 225 | |
| 226 | /// The list of constant iterators that reference this container |
| 227 | mutable _Safe_iterator_base* _M_const_iterators; |
| 228 | |
| 229 | /// The container version number. This number may never be 0. |
| 230 | mutable unsigned int _M_version; |
| 231 | |
| 232 | protected: |
| 233 | // Initialize with a version number of 1 and no iterators |
| 234 | _GLIBCXX20_CONSTEXPR |
| 235 | _Safe_sequence_base() _GLIBCXX_NOEXCEPT |
| 236 | : _M_iterators(0), _M_const_iterators(0), _M_version(1) |
| 237 | { } |
| 238 | |
| 239 | #if __cplusplus >= 201103L |
| 240 | _GLIBCXX20_CONSTEXPR |
| 241 | _Safe_sequence_base(const _Safe_sequence_base&) noexcept |
| 242 | : _Safe_sequence_base() { } |
| 243 | |
| 244 | // Move constructor swap iterators. |
| 245 | _GLIBCXX20_CONSTEXPR |
| 246 | _Safe_sequence_base(_Safe_sequence_base&& __seq) noexcept |
| 247 | : _Safe_sequence_base() |
| 248 | { |
| 249 | if (!std::__is_constant_evaluated()) |
| 250 | _M_swap(x&: __seq); |
| 251 | } |
| 252 | #endif |
| 253 | |
| 254 | /** Notify all iterators that reference this sequence that the |
| 255 | sequence is being destroyed. */ |
| 256 | _GLIBCXX20_CONSTEXPR |
| 257 | ~_Safe_sequence_base() _GLIBCXX_NOEXCEPT |
| 258 | { |
| 259 | if (!std::__is_constant_evaluated()) |
| 260 | this->_M_detach_all(); |
| 261 | } |
| 262 | |
| 263 | // Copy assignment invalidate all iterators. |
| 264 | _GLIBCXX20_CONSTEXPR _Safe_sequence_base& |
| 265 | operator=(const _Safe_sequence_base&) _GLIBCXX_NOEXCEPT |
| 266 | { |
| 267 | _M_invalidate_all(); |
| 268 | return *this; |
| 269 | } |
| 270 | |
| 271 | #if __cplusplus >= 201103L |
| 272 | _GLIBCXX20_CONSTEXPR _Safe_sequence_base& |
| 273 | operator=(_Safe_sequence_base&& __x) noexcept |
| 274 | { |
| 275 | _M_invalidate_all(); |
| 276 | __x._M_invalidate_all(); |
| 277 | return *this; |
| 278 | } |
| 279 | #endif |
| 280 | |
| 281 | /** Detach all iterators, leaving them singular. */ |
| 282 | void |
| 283 | _M_detach_all() const; |
| 284 | |
| 285 | /** Detach all singular iterators. |
| 286 | * @post for all iterators i attached to this sequence, |
| 287 | * i->_M_version == _M_version. |
| 288 | */ |
| 289 | void |
| 290 | _M_detach_singular() const; |
| 291 | |
| 292 | /** Revalidates all attached singular iterators. This method may |
| 293 | * be used to validate iterators that were invalidated before |
| 294 | * (but for some reason, such as an exception, need to become |
| 295 | * valid again). |
| 296 | */ |
| 297 | void |
| 298 | _M_revalidate_singular() const; |
| 299 | |
| 300 | /** Swap this sequence with the given sequence. This operation |
| 301 | * also swaps ownership of the iterators, so that when the |
| 302 | * operation is complete all iterators that originally referenced |
| 303 | * one container now reference the other container. |
| 304 | */ |
| 305 | void |
| 306 | _M_swap(const _Safe_sequence_base& __x) const _GLIBCXX_USE_NOEXCEPT; |
| 307 | |
| 308 | /** For use in _Safe_sequence. */ |
| 309 | __gnu_cxx::__mutex& |
| 310 | _M_get_mutex() const _GLIBCXX_USE_NOEXCEPT; |
| 311 | |
| 312 | /** Invalidates all iterators. */ |
| 313 | _GLIBCXX20_CONSTEXPR void |
| 314 | _M_invalidate_all() const |
| 315 | { if (++_M_version == 0) _M_version = 1; } |
| 316 | |
| 317 | private: |
| 318 | #if !_GLIBCXX_INLINE_VERSION |
| 319 | /***************************************************************/ |
| 320 | /** Not-const method preserved for abi backward compatibility. */ |
| 321 | void |
| 322 | _M_detach_all(); |
| 323 | |
| 324 | void |
| 325 | _M_detach_singular(); |
| 326 | |
| 327 | void |
| 328 | _M_revalidate_singular(); |
| 329 | |
| 330 | void |
| 331 | _M_swap(_Safe_sequence_base& __x) _GLIBCXX_USE_NOEXCEPT; |
| 332 | |
| 333 | __gnu_cxx::__mutex& |
| 334 | _M_get_mutex() _GLIBCXX_USE_NOEXCEPT; |
| 335 | /***************************************************************/ |
| 336 | #endif |
| 337 | |
| 338 | /** Attach an iterator to this sequence. */ |
| 339 | void |
| 340 | _M_attach(_Safe_iterator_base* __it, bool __constant) const; |
| 341 | |
| 342 | /** Likewise but not thread safe. */ |
| 343 | void |
| 344 | _M_attach_single(_Safe_iterator_base* __it, |
| 345 | bool __constant) const _GLIBCXX_USE_NOEXCEPT; |
| 346 | |
| 347 | /** Detach an iterator from this sequence */ |
| 348 | void |
| 349 | _M_detach(_Safe_iterator_base* __it) const; |
| 350 | |
| 351 | /** Likewise but not thread safe. */ |
| 352 | void |
| 353 | _M_detach_single(_Safe_iterator_base* __it) const _GLIBCXX_USE_NOEXCEPT; |
| 354 | }; |
| 355 | } // namespace __gnu_debug |
| 356 | |
| 357 | #endif |
| 358 | |