libstdc++
safe_base.h
Go to the documentation of this file.
1 // Safe sequence/iterator base implementation -*- C++ -*-
2 
3 // Copyright (C) 2003-2024 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  */
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. */
58 
59  /** The version number of this iterator. The sentinel value 0 is
60  * used to indicate an invalidated iterator (i.e., one that is
61  * singular because of an operation on the container). This
62  * version number must equal the version number in the sequence
63  * referenced by _M_sequence for the iterator to be
64  * non-singular.
65  */
66  unsigned int _M_version;
67 
68  /** Pointer to the previous iterator in the sequence's list of
69  iterators. Only valid when _M_sequence != NULL. */
71 
72  /** Pointer to the next iterator in the sequence's list of
73  iterators. Only valid when _M_sequence != NULL. */
75 
76  protected:
77  /** Initializes the iterator and makes it singular. */
78  _GLIBCXX20_CONSTEXPR
80  : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0)
81  { }
82 
83  /** Initialize the iterator to reference the sequence pointed to
84  * by @p __seq. @p __constant is true when we are initializing a
85  * constant iterator, and false if it is a mutable iterator. Note
86  * that @p __seq may be NULL, in which case the iterator will be
87  * singular. Otherwise, the iterator will reference @p __seq and
88  * be nonsingular.
89  */
90  _GLIBCXX20_CONSTEXPR
91  _Safe_iterator_base(const _Safe_sequence_base* __seq, bool __constant)
92  : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0)
93  {
94  if (!std::__is_constant_evaluated())
95  this->_M_attach(const_cast<_Safe_sequence_base*>(__seq), __constant);
96  }
97 
98  /** Initializes the iterator to reference the same sequence that
99  @p __x does. @p __constant is true if this is a constant
100  iterator, and false if it is mutable. */
101  _GLIBCXX20_CONSTEXPR
102  _Safe_iterator_base(const _Safe_iterator_base& __x, bool __constant)
103  : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0)
104  {
105  if (!std::__is_constant_evaluated())
106  this->_M_attach(__x._M_sequence, __constant);
107  }
108 
109  _GLIBCXX20_CONSTEXPR
111  {
112  if (!std::__is_constant_evaluated())
113  this->_M_detach();
114  }
115 
116  /** For use in _Safe_iterator. */
117  __gnu_cxx::__mutex&
118  _M_get_mutex() throw ();
119 
120  /** Attaches this iterator to the given sequence, detaching it
121  * from whatever sequence it was attached to originally. If the
122  * new sequence is the NULL pointer, the iterator is left
123  * unattached.
124  */
125  void
126  _M_attach(_Safe_sequence_base* __seq, bool __constant);
127 
128  /** Likewise, but not thread-safe. */
129  void
130  _M_attach_single(_Safe_sequence_base* __seq, bool __constant) throw ();
131 
132  /** Detach the iterator for whatever sequence it is attached to,
133  * if any.
134  */
135  void
137 
138  public:
139  /** Likewise, but not thread-safe. */
140  void
141  _M_detach_single() throw ();
142 
143  /** Determines if we are attached to the given sequence. */
144  bool
146  { return _M_sequence == __seq; }
147 
148  /** Is this iterator singular? */
149  _GLIBCXX_PURE bool
150  _M_singular() const throw ();
151 
152  /** Can we compare this iterator to the given iterator @p __x?
153  Returns true if both iterators are nonsingular and reference
154  the same sequence. */
155  _GLIBCXX_PURE bool
156  _M_can_compare(const _Safe_iterator_base& __x) const throw ();
157 
158  /** Invalidate the iterator, making it singular. */
159  void
161  { _M_version = 0; }
162 
163  /** Reset all member variables */
164  void
165  _M_reset() throw ();
166 
167  /** Unlink itself */
168  void
169  _M_unlink() throw ()
170  {
171  if (_M_prior)
173  if (_M_next)
175  }
176  };
177 
178  /** Iterators that derive from _Safe_iterator_base can be determined singular
179  * or non-singular.
180  **/
181  inline bool
183  { return __x->_M_singular(); }
184 
185  /**
186  * @brief Base class that supports tracking of iterators that
187  * reference a sequence.
188  *
189  * The %_Safe_sequence_base class provides basic support for
190  * tracking iterators into a sequence. Sequences that track
191  * iterators must derived from %_Safe_sequence_base publicly, so
192  * that safe iterators (which inherit _Safe_iterator_base) can
193  * attach to them. This class contains two linked lists of
194  * iterators, one for constant iterators and one for mutable
195  * iterators, and a version number that allows very fast
196  * invalidation of all iterators that reference the container.
197  *
198  * This class must ensure that no operation on it may throw an
199  * exception, otherwise @a safe sequences may fail to provide the
200  * exception-safety guarantees required by the C++ standard.
201  */
203  {
204  friend class _Safe_iterator_base;
205 
206  public:
207  /// The list of mutable iterators that reference this container
209 
210  /// The list of constant iterators that reference this container
212 
213  /// The container version number. This number may never be 0.
214  mutable unsigned int _M_version;
215 
216  protected:
217  // Initialize with a version number of 1 and no iterators
218  _GLIBCXX20_CONSTEXPR
219  _Safe_sequence_base() _GLIBCXX_NOEXCEPT
221  { }
222 
223 #if __cplusplus >= 201103L
224  _GLIBCXX20_CONSTEXPR
226  : _Safe_sequence_base() { }
227 
228  // Move constructor swap iterators.
229  _GLIBCXX20_CONSTEXPR
230  _Safe_sequence_base(_Safe_sequence_base&& __seq) noexcept
231  : _Safe_sequence_base()
232  {
233  if (!std::__is_constant_evaluated())
234  _M_swap(__seq);
235  }
236 #endif
237 
238  /** Notify all iterators that reference this sequence that the
239  sequence is being destroyed. */
240  _GLIBCXX20_CONSTEXPR
242  {
243  if (!std::__is_constant_evaluated())
244  this->_M_detach_all();
245  }
246 
247  /** Detach all iterators, leaving them singular. */
248  void
250 
251  /** Detach all singular iterators.
252  * @post for all iterators i attached to this sequence,
253  * i->_M_version == _M_version.
254  */
255  void
257 
258  /** Revalidates all attached singular iterators. This method may
259  * be used to validate iterators that were invalidated before
260  * (but for some reason, such as an exception, need to become
261  * valid again).
262  */
263  void
265 
266  /** Swap this sequence with the given sequence. This operation
267  * also swaps ownership of the iterators, so that when the
268  * operation is complete all iterators that originally referenced
269  * one container now reference the other container.
270  */
271  void
272  _M_swap(_Safe_sequence_base& __x) _GLIBCXX_USE_NOEXCEPT;
273 
274  /** For use in _Safe_sequence. */
275  __gnu_cxx::__mutex&
276  _M_get_mutex() throw ();
277 
278  /** Invalidates all iterators. */
279  void
281  { if (++_M_version == 0) _M_version = 1; }
282 
283  private:
284  /** Attach an iterator to this sequence. */
285  void
286  _M_attach(_Safe_iterator_base* __it, bool __constant);
287 
288  /** Likewise but not thread safe. */
289  void
290  _M_attach_single(_Safe_iterator_base* __it, bool __constant) throw ();
291 
292  /** Detach an iterator from this sequence */
293  void
294  _M_detach(_Safe_iterator_base* __it);
295 
296  /** Likewise but not thread safe. */
297  void
298  _M_detach_single(_Safe_iterator_base* __it) throw ();
299  };
300 } // namespace __gnu_debug
301 
302 #endif
GNU debug classes for public use.
bool __check_singular_aux(const _Safe_iterator_base *__x)
Definition: safe_base.h:182
Basic functionality for a safe iterator.
Definition: safe_base.h:51
_Safe_sequence_base * _M_sequence
Definition: safe_base.h:57
constexpr _Safe_iterator_base(const _Safe_sequence_base *__seq, bool __constant)
Definition: safe_base.h:91
void _M_attach_single(_Safe_sequence_base *__seq, bool __constant)
void _M_attach(_Safe_sequence_base *__seq, bool __constant)
bool _M_attached_to(const _Safe_sequence_base *__seq) const
Definition: safe_base.h:145
__gnu_cxx::__mutex & _M_get_mutex()
_Safe_iterator_base * _M_prior
Definition: safe_base.h:70
bool _M_can_compare(const _Safe_iterator_base &__x) const
_Safe_iterator_base * _M_next
Definition: safe_base.h:74
constexpr _Safe_iterator_base(const _Safe_iterator_base &__x, bool __constant)
Definition: safe_base.h:102
Base class that supports tracking of iterators that reference a sequence.
Definition: safe_base.h:203
void _M_swap(_Safe_sequence_base &__x) noexcept
_Safe_iterator_base * _M_const_iterators
The list of constant iterators that reference this container.
Definition: safe_base.h:211
_Safe_iterator_base * _M_iterators
The list of mutable iterators that reference this container.
Definition: safe_base.h:208
__gnu_cxx::__mutex & _M_get_mutex()
unsigned int _M_version
The container version number. This number may never be 0.
Definition: safe_base.h:214