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