xref: /dflybsd-src/contrib/gcc-8.0/libstdc++-v3/include/debug/safe_base.h (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj // Safe sequence/iterator base implementation  -*- C++ -*-
2*38fd1498Szrj 
3*38fd1498Szrj // Copyright (C) 2003-2018 Free Software Foundation, Inc.
4*38fd1498Szrj //
5*38fd1498Szrj // This file is part of the GNU ISO C++ Library.  This library is free
6*38fd1498Szrj // software; you can redistribute it and/or modify it under the
7*38fd1498Szrj // terms of the GNU General Public License as published by the
8*38fd1498Szrj // Free Software Foundation; either version 3, or (at your option)
9*38fd1498Szrj // any later version.
10*38fd1498Szrj 
11*38fd1498Szrj // This library is distributed in the hope that it will be useful,
12*38fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*38fd1498Szrj // GNU General Public License for more details.
15*38fd1498Szrj 
16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional
17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version
18*38fd1498Szrj // 3.1, as published by the Free Software Foundation.
19*38fd1498Szrj 
20*38fd1498Szrj // You should have received a copy of the GNU General Public License and
21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program;
22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23*38fd1498Szrj // <http://www.gnu.org/licenses/>.
24*38fd1498Szrj 
25*38fd1498Szrj /** @file debug/safe_base.h
26*38fd1498Szrj  *  This file is a GNU debug extension to the Standard C++ Library.
27*38fd1498Szrj  */
28*38fd1498Szrj 
29*38fd1498Szrj #ifndef _GLIBCXX_DEBUG_SAFE_BASE_H
30*38fd1498Szrj #define _GLIBCXX_DEBUG_SAFE_BASE_H 1
31*38fd1498Szrj 
32*38fd1498Szrj #include <ext/concurrence.h>
33*38fd1498Szrj 
34*38fd1498Szrj namespace __gnu_debug
35*38fd1498Szrj {
36*38fd1498Szrj   class _Safe_sequence_base;
37*38fd1498Szrj 
38*38fd1498Szrj   /** \brief Basic functionality for a @a safe iterator.
39*38fd1498Szrj    *
40*38fd1498Szrj    *  The %_Safe_iterator_base base class implements the functionality
41*38fd1498Szrj    *  of a safe iterator that is not specific to a particular iterator
42*38fd1498Szrj    *  type. It contains a pointer back to the sequence it references
43*38fd1498Szrj    *  along with iterator version information and pointers to form a
44*38fd1498Szrj    *  doubly-linked list of iterators referenced by the container.
45*38fd1498Szrj    *
46*38fd1498Szrj    *  This class must not perform any operations that can throw an
47*38fd1498Szrj    *  exception, or the exception guarantees of derived iterators will
48*38fd1498Szrj    *  be broken.
49*38fd1498Szrj    */
50*38fd1498Szrj   class _Safe_iterator_base
51*38fd1498Szrj   {
52*38fd1498Szrj     friend class _Safe_sequence_base;
53*38fd1498Szrj 
54*38fd1498Szrj   public:
55*38fd1498Szrj     /** The sequence this iterator references; may be NULL to indicate
56*38fd1498Szrj 	a singular iterator. */
57*38fd1498Szrj     _Safe_sequence_base*	_M_sequence;
58*38fd1498Szrj 
59*38fd1498Szrj     /** The version number of this iterator. The sentinel value 0 is
60*38fd1498Szrj      *  used to indicate an invalidated iterator (i.e., one that is
61*38fd1498Szrj      *  singular because of an operation on the container). This
62*38fd1498Szrj      *  version number must equal the version number in the sequence
63*38fd1498Szrj      *  referenced by _M_sequence for the iterator to be
64*38fd1498Szrj      *  non-singular.
65*38fd1498Szrj      */
66*38fd1498Szrj     unsigned int		_M_version;
67*38fd1498Szrj 
68*38fd1498Szrj     /** Pointer to the previous iterator in the sequence's list of
69*38fd1498Szrj 	iterators. Only valid when _M_sequence != NULL. */
70*38fd1498Szrj     _Safe_iterator_base*	_M_prior;
71*38fd1498Szrj 
72*38fd1498Szrj     /** Pointer to the next iterator in the sequence's list of
73*38fd1498Szrj 	iterators. Only valid when _M_sequence != NULL. */
74*38fd1498Szrj     _Safe_iterator_base*	_M_next;
75*38fd1498Szrj 
76*38fd1498Szrj   protected:
77*38fd1498Szrj     /** Initializes the iterator and makes it singular. */
_Safe_iterator_base()78*38fd1498Szrj     _Safe_iterator_base()
79*38fd1498Szrj     : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0)
80*38fd1498Szrj     { }
81*38fd1498Szrj 
82*38fd1498Szrj     /** Initialize the iterator to reference the sequence pointed to
83*38fd1498Szrj      *  by @p __seq. @p __constant is true when we are initializing a
84*38fd1498Szrj      *  constant iterator, and false if it is a mutable iterator. Note
85*38fd1498Szrj      *  that @p __seq may be NULL, in which case the iterator will be
86*38fd1498Szrj      *  singular. Otherwise, the iterator will reference @p __seq and
87*38fd1498Szrj      *  be nonsingular.
88*38fd1498Szrj      */
_Safe_iterator_base(const _Safe_sequence_base * __seq,bool __constant)89*38fd1498Szrj     _Safe_iterator_base(const _Safe_sequence_base* __seq, bool __constant)
90*38fd1498Szrj     : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0)
91*38fd1498Szrj     { this->_M_attach(const_cast<_Safe_sequence_base*>(__seq), __constant); }
92*38fd1498Szrj 
93*38fd1498Szrj     /** Initializes the iterator to reference the same sequence that
94*38fd1498Szrj 	@p __x does. @p __constant is true if this is a constant
95*38fd1498Szrj 	iterator, and false if it is mutable. */
_Safe_iterator_base(const _Safe_iterator_base & __x,bool __constant)96*38fd1498Szrj     _Safe_iterator_base(const _Safe_iterator_base& __x, bool __constant)
97*38fd1498Szrj     : _M_sequence(0), _M_version(0), _M_prior(0), _M_next(0)
98*38fd1498Szrj     { this->_M_attach(__x._M_sequence, __constant); }
99*38fd1498Szrj 
~_Safe_iterator_base()100*38fd1498Szrj     ~_Safe_iterator_base() { this->_M_detach(); }
101*38fd1498Szrj 
102*38fd1498Szrj     /** For use in _Safe_iterator. */
103*38fd1498Szrj     __gnu_cxx::__mutex&
104*38fd1498Szrj     _M_get_mutex() throw ();
105*38fd1498Szrj 
106*38fd1498Szrj     /** Attaches this iterator to the given sequence, detaching it
107*38fd1498Szrj      *	from whatever sequence it was attached to originally. If the
108*38fd1498Szrj      *	new sequence is the NULL pointer, the iterator is left
109*38fd1498Szrj      *	unattached.
110*38fd1498Szrj      */
111*38fd1498Szrj     void
112*38fd1498Szrj     _M_attach(_Safe_sequence_base* __seq, bool __constant);
113*38fd1498Szrj 
114*38fd1498Szrj     /** Likewise, but not thread-safe. */
115*38fd1498Szrj     void
116*38fd1498Szrj     _M_attach_single(_Safe_sequence_base* __seq, bool __constant) throw ();
117*38fd1498Szrj 
118*38fd1498Szrj     /** Detach the iterator for whatever sequence it is attached to,
119*38fd1498Szrj      *	if any.
120*38fd1498Szrj     */
121*38fd1498Szrj     void
122*38fd1498Szrj     _M_detach();
123*38fd1498Szrj 
124*38fd1498Szrj   public:
125*38fd1498Szrj     /** Likewise, but not thread-safe. */
126*38fd1498Szrj     void
127*38fd1498Szrj     _M_detach_single() throw ();
128*38fd1498Szrj 
129*38fd1498Szrj     /** Determines if we are attached to the given sequence. */
130*38fd1498Szrj     bool
_M_attached_to(const _Safe_sequence_base * __seq)131*38fd1498Szrj     _M_attached_to(const _Safe_sequence_base* __seq) const
132*38fd1498Szrj     { return _M_sequence == __seq; }
133*38fd1498Szrj 
134*38fd1498Szrj     /** Is this iterator singular? */
135*38fd1498Szrj     _GLIBCXX_PURE bool
136*38fd1498Szrj     _M_singular() const throw ();
137*38fd1498Szrj 
138*38fd1498Szrj     /** Can we compare this iterator to the given iterator @p __x?
139*38fd1498Szrj 	Returns true if both iterators are nonsingular and reference
140*38fd1498Szrj 	the same sequence. */
141*38fd1498Szrj     _GLIBCXX_PURE bool
142*38fd1498Szrj     _M_can_compare(const _Safe_iterator_base& __x) const throw ();
143*38fd1498Szrj 
144*38fd1498Szrj     /** Invalidate the iterator, making it singular. */
145*38fd1498Szrj     void
_M_invalidate()146*38fd1498Szrj     _M_invalidate()
147*38fd1498Szrj     { _M_version = 0; }
148*38fd1498Szrj 
149*38fd1498Szrj     /** Reset all member variables */
150*38fd1498Szrj     void
151*38fd1498Szrj     _M_reset() throw ();
152*38fd1498Szrj 
153*38fd1498Szrj     /** Unlink itself */
154*38fd1498Szrj     void
_M_unlink()155*38fd1498Szrj     _M_unlink() throw ()
156*38fd1498Szrj     {
157*38fd1498Szrj       if (_M_prior)
158*38fd1498Szrj 	_M_prior->_M_next = _M_next;
159*38fd1498Szrj       if (_M_next)
160*38fd1498Szrj 	_M_next->_M_prior = _M_prior;
161*38fd1498Szrj     }
162*38fd1498Szrj   };
163*38fd1498Szrj 
164*38fd1498Szrj   /** Iterators that derive from _Safe_iterator_base can be determined singular
165*38fd1498Szrj    *  or non-singular.
166*38fd1498Szrj    **/
167*38fd1498Szrj   inline bool
__check_singular_aux(const _Safe_iterator_base * __x)168*38fd1498Szrj   __check_singular_aux(const _Safe_iterator_base* __x)
169*38fd1498Szrj   { return __x->_M_singular(); }
170*38fd1498Szrj 
171*38fd1498Szrj   /**
172*38fd1498Szrj    * @brief Base class that supports tracking of iterators that
173*38fd1498Szrj    * reference a sequence.
174*38fd1498Szrj    *
175*38fd1498Szrj    * The %_Safe_sequence_base class provides basic support for
176*38fd1498Szrj    * tracking iterators into a sequence. Sequences that track
177*38fd1498Szrj    * iterators must derived from %_Safe_sequence_base publicly, so
178*38fd1498Szrj    * that safe iterators (which inherit _Safe_iterator_base) can
179*38fd1498Szrj    * attach to them. This class contains two linked lists of
180*38fd1498Szrj    * iterators, one for constant iterators and one for mutable
181*38fd1498Szrj    * iterators, and a version number that allows very fast
182*38fd1498Szrj    * invalidation of all iterators that reference the container.
183*38fd1498Szrj    *
184*38fd1498Szrj    * This class must ensure that no operation on it may throw an
185*38fd1498Szrj    * exception, otherwise @a safe sequences may fail to provide the
186*38fd1498Szrj    * exception-safety guarantees required by the C++ standard.
187*38fd1498Szrj    */
188*38fd1498Szrj   class _Safe_sequence_base
189*38fd1498Szrj   {
190*38fd1498Szrj     friend class _Safe_iterator_base;
191*38fd1498Szrj 
192*38fd1498Szrj   public:
193*38fd1498Szrj     /// The list of mutable iterators that reference this container
194*38fd1498Szrj     _Safe_iterator_base* _M_iterators;
195*38fd1498Szrj 
196*38fd1498Szrj     /// The list of constant iterators that reference this container
197*38fd1498Szrj     _Safe_iterator_base* _M_const_iterators;
198*38fd1498Szrj 
199*38fd1498Szrj     /// The container version number. This number may never be 0.
200*38fd1498Szrj     mutable unsigned int _M_version;
201*38fd1498Szrj 
202*38fd1498Szrj   protected:
203*38fd1498Szrj     // Initialize with a version number of 1 and no iterators
_Safe_sequence_base()204*38fd1498Szrj     _Safe_sequence_base() _GLIBCXX_NOEXCEPT
205*38fd1498Szrj     : _M_iterators(0), _M_const_iterators(0), _M_version(1)
206*38fd1498Szrj     { }
207*38fd1498Szrj 
208*38fd1498Szrj #if __cplusplus >= 201103L
_Safe_sequence_base(const _Safe_sequence_base &)209*38fd1498Szrj     _Safe_sequence_base(const _Safe_sequence_base&) noexcept
210*38fd1498Szrj     : _Safe_sequence_base() { }
211*38fd1498Szrj 
212*38fd1498Szrj     // Move constructor swap iterators.
_Safe_sequence_base(_Safe_sequence_base && __seq)213*38fd1498Szrj     _Safe_sequence_base(_Safe_sequence_base&& __seq) noexcept
214*38fd1498Szrj     : _Safe_sequence_base()
215*38fd1498Szrj     { _M_swap(__seq); }
216*38fd1498Szrj #endif
217*38fd1498Szrj 
218*38fd1498Szrj     /** Notify all iterators that reference this sequence that the
219*38fd1498Szrj 	sequence is being destroyed. */
~_Safe_sequence_base()220*38fd1498Szrj     ~_Safe_sequence_base()
221*38fd1498Szrj     { this->_M_detach_all(); }
222*38fd1498Szrj 
223*38fd1498Szrj     /** Detach all iterators, leaving them singular. */
224*38fd1498Szrj     void
225*38fd1498Szrj     _M_detach_all();
226*38fd1498Szrj 
227*38fd1498Szrj     /** Detach all singular iterators.
228*38fd1498Szrj      *  @post for all iterators i attached to this sequence,
229*38fd1498Szrj      *   i->_M_version == _M_version.
230*38fd1498Szrj      */
231*38fd1498Szrj     void
232*38fd1498Szrj     _M_detach_singular();
233*38fd1498Szrj 
234*38fd1498Szrj     /** Revalidates all attached singular iterators.  This method may
235*38fd1498Szrj      *  be used to validate iterators that were invalidated before
236*38fd1498Szrj      *  (but for some reason, such as an exception, need to become
237*38fd1498Szrj      *  valid again).
238*38fd1498Szrj      */
239*38fd1498Szrj     void
240*38fd1498Szrj     _M_revalidate_singular();
241*38fd1498Szrj 
242*38fd1498Szrj     /** Swap this sequence with the given sequence. This operation
243*38fd1498Szrj      *  also swaps ownership of the iterators, so that when the
244*38fd1498Szrj      *  operation is complete all iterators that originally referenced
245*38fd1498Szrj      *  one container now reference the other container.
246*38fd1498Szrj      */
247*38fd1498Szrj     void
248*38fd1498Szrj     _M_swap(_Safe_sequence_base& __x) _GLIBCXX_USE_NOEXCEPT;
249*38fd1498Szrj 
250*38fd1498Szrj     /** For use in _Safe_sequence. */
251*38fd1498Szrj     __gnu_cxx::__mutex&
252*38fd1498Szrj     _M_get_mutex() throw ();
253*38fd1498Szrj 
254*38fd1498Szrj     /** Invalidates all iterators. */
255*38fd1498Szrj     void
_M_invalidate_all()256*38fd1498Szrj     _M_invalidate_all() const
257*38fd1498Szrj     { if (++_M_version == 0) _M_version = 1; }
258*38fd1498Szrj 
259*38fd1498Szrj   private:
260*38fd1498Szrj     /** Attach an iterator to this sequence. */
261*38fd1498Szrj     void
262*38fd1498Szrj     _M_attach(_Safe_iterator_base* __it, bool __constant);
263*38fd1498Szrj 
264*38fd1498Szrj     /** Likewise but not thread safe. */
265*38fd1498Szrj     void
266*38fd1498Szrj     _M_attach_single(_Safe_iterator_base* __it, bool __constant) throw ();
267*38fd1498Szrj 
268*38fd1498Szrj     /** Detach an iterator from this sequence */
269*38fd1498Szrj     void
270*38fd1498Szrj     _M_detach(_Safe_iterator_base* __it);
271*38fd1498Szrj 
272*38fd1498Szrj     /** Likewise but not thread safe. */
273*38fd1498Szrj     void
274*38fd1498Szrj     _M_detach_single(_Safe_iterator_base* __it) throw ();
275*38fd1498Szrj   };
276*38fd1498Szrj } // namespace __gnu_debug
277*38fd1498Szrj 
278*38fd1498Szrj #endif
279