safe_base.h 9.1 KB

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