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