mirror of
https://github.com/gcc-mirror/gcc.git
synced 2025-08-01 16:38:48 +00:00

In revision a35dd276cb
the debug sequence
have been made mutable to allow attach iterators to const containers.
This change completes this fix by also declaring debug unordered container
members mutable.
Additionally the debug iterator sequence is now a pointer-to-const and so
_Safe_sequence_base _M_attach and all other methods are const qualified.
Not-const methods exported are preserved for abi backward compatibility.
libstdc++-v3/ChangeLog:
PR c++/116369
* config/abi/pre/gnu-versioned-namespace.ver: Use new const qualified symbols.
* config/abi/pre/gnu.ver: Add new const qualified symbols.
* include/debug/safe_base.h
(_Safe_iterator_base::_M_sequence): Declare as pointer-to-const.
(_Safe_iterator_base::_M_attach, _M_attach_single): New, take pointer-to-const
_Safe_sequence_base.
(_Safe_sequence_base::_M_detach_all, _M_detach_singular, _M_revalidate_singular)
(_M_swap, _M_get_mutex): New, const qualified.
(_Safe_sequence_base::_M_attach, _M_attach_single, _M_detach, _M_detach_single):
const qualify.
* include/debug/safe_container.h (_Safe_container<>::_M_cont): Add const qualifier.
(_Safe_container<>::_M_swap_base): New.
(_Safe_container(_Safe_container&&, const _Alloc&, std::false_type)):
Adapt to use latter.
(_Safe_container<>::operator=(_Safe_container&&)): Likewise.
(_Safe_container<>::_M_swap): Likewise and take parameter as const reference.
* include/debug/safe_unordered_base.h
(_Safe_local_iterator_base::_M_safe_container): New.
(_Safe_local_iterator_base::_Safe_local_iterator_base): Take
_Safe_unordered_container_base as pointer-to-const.
(_Safe_unordered_container_base::_M_attach, _M_attach_single): New, take
container as _Safe_unordered_container_base pointer-to-const.
(_Safe_unordered_container_base::_M_local_iterators, _M_const_local_iterators):
Add mutable.
(_Safe_unordered_container_base::_M_detach_all, _M_swap): New, const qualify.
(_Safe_unordered_container_base::_M_attach_local, _M_attach_local_single)
(_M_detach_local, _M_detach_local_single): Add const qualifier.
* include/debug/safe_unordered_container.h (_Safe_unordered_container::_M_self()): New.
* include/debug/safe_unordered_container.tcc
(_Safe_unordered_container::_M_invalidate_if, _M_invalidated_local_if): Use latter.
* include/debug/safe_iterator.h (_Safe_iterator<>::_M_attach, _M_attach_single):
Take _Safe_sequence_base as pointer-to-const.
(_Safe_iterator<>::_M_get_sequence): Add const_cast and comment about it.
* include/debug/safe_local_iterator.h (_Safe_local_iterator<>): Replace usages
of _M_sequence member by _M_safe_container().
(_Safe_local_iterator<>::_M_attach, _M_attach_single): Take
_Safe_unordered_container_base as pointer-to-const.
(_Safe_local_iterator<>::_M_get_sequence): Rename into...
(_Safe_local_iterator<>::_M_get_ucontainer): ...this. Add necessary const_cast and
comment to explain it.
(_Safe_local_iterator<>::_M_is_begin, _M_is_end): Adapt.
* include/debug/safe_local_iterator.tcc: Adapt.
* include/debug/safe_sequence.h
(_Safe_sequence<>::_M_invalidate_if, _M_transfer_from_if): Add const qualifier.
* include/debug/safe_sequence.tcc: Adapt.
* include/debug/deque (std::__debug::deque::erase): Adapt to use new const
qualified methods.
* include/debug/formatter.h: Adapt.
* include/debug/forward_list (_Safe_forward_list::_M_this): Add const
qualification and return pointer for consistency with 'this' keyword.
(_Safe_forward_list::_M_swap_aux): Rename into...
(_Safe_forward_list::_S_swap_aux): ...this and take sequence as const reference.
(forward_list<>::resize): Adapt to use const methods.
* include/debug/list (list<>::resize): Likewise.
* src/c++11/debug.cc: Adapt to const qualification.
* testsuite/util/testsuite_containers.h
(forward_members_unordered::forward_members_unordered): Add check on local_iterator
conversion to const_local_iterator.
(forward_members::forward_members): Add check on iterator conversion to
const_iterator.
* testsuite/23_containers/unordered_map/const_container.cc: New test case.
* testsuite/23_containers/unordered_multimap/const_container.cc: New test case.
* testsuite/23_containers/unordered_multiset/const_container.cc: New test case.
* testsuite/23_containers/unordered_set/const_container.cc: New test case.
* testsuite/23_containers/vector/debug/mutex_association.cc: Adapt.
219 lines
7.8 KiB
C++
219 lines
7.8 KiB
C++
// Safe container/iterator base implementation -*- C++ -*-
|
|
|
|
// Copyright (C) 2011-2025 Free Software Foundation, Inc.
|
|
//
|
|
// This file is part of the GNU ISO C++ Library. This library is free
|
|
// software; you can redistribute it and/or modify it under the
|
|
// terms of the GNU General Public License as published by the
|
|
// Free Software Foundation; either version 3, or (at your option)
|
|
// any later version.
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
|
|
// Under Section 7 of GPL version 3, you are granted additional
|
|
// permissions described in the GCC Runtime Library Exception, version
|
|
// 3.1, as published by the Free Software Foundation.
|
|
|
|
// You should have received a copy of the GNU General Public License and
|
|
// a copy of the GCC Runtime Library Exception along with this program;
|
|
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
|
// <http://www.gnu.org/licenses/>.
|
|
|
|
/** @file debug/safe_unordered_base.h
|
|
* This file is a GNU debug extension to the Standard C++ Library.
|
|
*/
|
|
|
|
#ifndef _GLIBCXX_DEBUG_SAFE_UNORDERED_BASE_H
|
|
#define _GLIBCXX_DEBUG_SAFE_UNORDERED_BASE_H 1
|
|
|
|
#include <debug/safe_base.h>
|
|
|
|
namespace __gnu_debug
|
|
{
|
|
class _Safe_unordered_container_base;
|
|
|
|
/** \brief Basic functionality for a @a safe iterator.
|
|
*
|
|
* The %_Safe_local_iterator_base base class implements the functionality
|
|
* of a safe local iterator that is not specific to a particular iterator
|
|
* type. It contains a pointer back to the container it references
|
|
* along with iterator version information and pointers to form a
|
|
* doubly-linked list of local iterators referenced by the container.
|
|
*
|
|
* This class must not perform any operations that can throw an
|
|
* exception, or the exception guarantees of derived iterators will
|
|
* be broken.
|
|
*/
|
|
class _Safe_local_iterator_base : public _Safe_iterator_base
|
|
{
|
|
public:
|
|
const _Safe_unordered_container_base*
|
|
_M_safe_container() const noexcept;
|
|
|
|
protected:
|
|
/** Initializes the iterator and makes it singular. */
|
|
_Safe_local_iterator_base()
|
|
{ }
|
|
|
|
/** Initialize the iterator to reference the container pointed to
|
|
* by @p __seq. @p __constant is true when we are initializing a
|
|
* constant local iterator, and false if it is a mutable local iterator.
|
|
* Note that @p __seq may be NULL, in which case the iterator will be
|
|
* singular. Otherwise, the iterator will reference @p __seq and
|
|
* be nonsingular.
|
|
*/
|
|
_Safe_local_iterator_base(const _Safe_unordered_container_base* __seq,
|
|
bool __constant)
|
|
{ _M_attach(__seq, __constant); }
|
|
|
|
/** Initializes the iterator to reference the same container that
|
|
@p __x does. @p __constant is true if this is a constant
|
|
iterator, and false if it is mutable. */
|
|
_Safe_local_iterator_base(const _Safe_local_iterator_base& __x,
|
|
bool __constant)
|
|
{ this->_M_attach(__x._M_safe_container(), __constant); }
|
|
|
|
~_Safe_local_iterator_base() { this->_M_detach(); }
|
|
|
|
/** Attaches this iterator to the given container, detaching it
|
|
* from whatever container it was attached to originally. If the
|
|
* new container is the NULL pointer, the iterator is left
|
|
* unattached.
|
|
*/
|
|
void
|
|
_M_attach(const _Safe_unordered_container_base* __cont,
|
|
bool __constant);
|
|
|
|
/** Likewise, but not thread-safe. */
|
|
void
|
|
_M_attach_single(const _Safe_unordered_container_base* __cont,
|
|
bool __constant) noexcept;
|
|
|
|
/** Detach the iterator for whatever container it is attached to,
|
|
* if any.
|
|
*/
|
|
void
|
|
_M_detach();
|
|
|
|
/** Likewise, but not thread-safe. */
|
|
void
|
|
_M_detach_single() noexcept;
|
|
|
|
#if !_GLIBCXX_INLINE_VERSION
|
|
private:
|
|
/***************************************************************/
|
|
/** Not-const method preserved for abi backward compatibility. */
|
|
void
|
|
_M_attach(_Safe_sequence_base* __seq, bool __constant);
|
|
|
|
void
|
|
_M_attach_single(_Safe_sequence_base* __seq, bool __constant) noexcept;
|
|
/***************************************************************/
|
|
#endif
|
|
};
|
|
|
|
/**
|
|
* @brief Base class that supports tracking of local iterators that
|
|
* reference an unordered container.
|
|
*
|
|
* The %_Safe_unordered_container_base class provides basic support for
|
|
* tracking iterators into an unordered container. Containers that track
|
|
* iterators must derived from %_Safe_unordered_container_base publicly, so
|
|
* that safe iterators (which inherit _Safe_iterator_base) can
|
|
* attach to them. This class contains four linked lists of
|
|
* iterators, one for constant iterators, one for mutable
|
|
* iterators, one for constant local iterators, one for mutable local
|
|
* iterators and a version number that allows very fast
|
|
* invalidation of all iterators that reference the container.
|
|
*
|
|
* This class must ensure that no operation on it may throw an
|
|
* exception, otherwise @a safe containers may fail to provide the
|
|
* exception-safety guarantees required by the C++ standard.
|
|
*/
|
|
class _Safe_unordered_container_base : public _Safe_sequence_base
|
|
{
|
|
friend class _Safe_local_iterator_base;
|
|
typedef _Safe_sequence_base _Base;
|
|
|
|
public:
|
|
/// The list of mutable local iterators that reference this container
|
|
mutable _Safe_iterator_base* _M_local_iterators;
|
|
|
|
/// The list of constant local iterators that reference this container
|
|
mutable _Safe_iterator_base* _M_const_local_iterators;
|
|
|
|
protected:
|
|
// Initialize with a version number of 1 and no iterators
|
|
_Safe_unordered_container_base() noexcept
|
|
: _M_local_iterators(nullptr), _M_const_local_iterators(nullptr)
|
|
{ }
|
|
|
|
// Copy constructor does not copy iterators.
|
|
_Safe_unordered_container_base(const _Safe_unordered_container_base&)
|
|
noexcept
|
|
: _Safe_unordered_container_base() { }
|
|
|
|
// When moved unordered containers iterators are swapped.
|
|
_Safe_unordered_container_base(_Safe_unordered_container_base&& __x)
|
|
noexcept
|
|
: _Safe_unordered_container_base()
|
|
{ this->_M_swap(__x); }
|
|
|
|
/** Notify all iterators that reference this container that the
|
|
container is being destroyed. */
|
|
~_Safe_unordered_container_base() noexcept
|
|
{ this->_M_detach_all(); }
|
|
|
|
/** Detach all iterators, leaving them singular. */
|
|
void
|
|
_M_detach_all() const;
|
|
|
|
/** Swap this container with the given container. This operation
|
|
* also swaps ownership of the iterators, so that when the
|
|
* operation is complete all iterators that originally referenced
|
|
* one container now reference the other container.
|
|
*/
|
|
void
|
|
_M_swap(const _Safe_unordered_container_base& __x) const noexcept;
|
|
|
|
private:
|
|
#if !_GLIBCXX_INLINE_VERSION
|
|
/***************************************************************/
|
|
/** Not-const method preserved for abi backward compatibility. */
|
|
void
|
|
_M_detach_all();
|
|
|
|
void
|
|
_M_swap(_Safe_unordered_container_base& __x) noexcept;
|
|
/***************************************************************/
|
|
#endif
|
|
|
|
/** Attach an iterator to this container. */
|
|
void
|
|
_M_attach_local(_Safe_iterator_base* __it, bool __constant) const;
|
|
|
|
/** Likewise but not thread safe. */
|
|
void
|
|
_M_attach_local_single(_Safe_iterator_base* __it,
|
|
bool __constant) const noexcept;
|
|
|
|
/** Detach an iterator from this container */
|
|
void
|
|
_M_detach_local(_Safe_iterator_base* __it) const;
|
|
|
|
/** Likewise but not thread safe. */
|
|
void
|
|
_M_detach_local_single(_Safe_iterator_base* __it) const noexcept;
|
|
};
|
|
|
|
inline const _Safe_unordered_container_base*
|
|
_Safe_local_iterator_base::
|
|
_M_safe_container() const noexcept
|
|
{ return static_cast<const _Safe_unordered_container_base*>(_M_sequence); }
|
|
} // namespace __gnu_debug
|
|
|
|
#endif
|