mirror of
https://sourceware.org/git/binutils-gdb.git
synced 2024-11-23 01:53:38 +08:00
8b8f98ad2b
It occured to me that `intrusive_list<solib>`, as returned by `solib_ops::current_sos`, for instance, is not very safe. The current_sos method returns the ownership of the solib objects (heap-allocated) to its caller, but the `intrusive_list<solib>` type does not convey it. If a function is building an `intrusive_list<solib>` and something throws, the solibs won't automatically be deleted. Introduce owning_intrusive_list to fill this gap. Interface --------- The interface of owning_intrusive_list is mostly equivalent to intrusive_list, with the following differences: - When destroyed, owning_intrusive_list deletes all element objects. The clear method does so as well. - The erase method destroys the removed object. - The push_front, push_back and insert methods accept a `unique_ptr<T>` (compared to `T &` for intrusive_list), taking ownership of the object. - owning_intrusive_list has emplace_front, emplace_back and emplace methods, allowing to allocate and construct an object directly in the list. This is really just a shorthand over std::make_unique and insert (or push_back / push_front if you don't care about the return value), but I think it is nicer to read: list.emplace (pos, "hello", 2); rather than list.insert (pos, std::make_unique<Foo> ("hello", 2)); These methods are not `noexcept`, since the allocation or the constructor could throw. - owning_intrusive_list has a release method, allowing to remove an element without destroying it. The release method returns a pair-like struct with an iterator to the next element in the list (like the erase method) and a unique pointer transferring the ownership of the released element to the caller. - owning_intrusive_list does not have a clear_and_dispose method, since that is typically used to manually free items. Implementation -------------- owning_intrusive_list privately inherits from intrusive_list, in order to re-use the linked list machinery. It adds ownership semantics around it. Testing ------- Because of the subtle differences in the behavior in behavior and what we want to test for each type of intrusive list, I didn't see how to share the tests for the two implementations. I chose to copy the intrusive_list tests and adjust them for owning_intrusive_list. The verify_items function was made common though, and it tries to dereference the items in the list, to make sure they have not been deleted by mistake (which would be caught by Valgrind / ASan). Change-Id: Idbde09c1417b79992a0a9534d6907433e706f760 Co-Authored-By: Pedro Alves <pedro@palves.net> Reviewed-by: Keith Seitz <keiths@redhat.com>
169 lines
4.8 KiB
C++
169 lines
4.8 KiB
C++
/* Owning version of intrusive_list for GDB, the GNU debugger.
|
|
Copyright (C) 2024 Free Software Foundation, Inc.
|
|
|
|
This file is part of GDB.
|
|
|
|
This program 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 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program 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.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
#ifndef GDBSUPPORT_OWNING_INTRUSIVE_LIST_H
|
|
#define GDBSUPPORT_OWNING_INTRUSIVE_LIST_H
|
|
|
|
#include "intrusive_list.h"
|
|
|
|
/* An owning version of intrusive_list. */
|
|
|
|
template<typename T, typename AsNode = intrusive_base_node<T>>
|
|
class owning_intrusive_list : private intrusive_list<T, AsNode>
|
|
{
|
|
using base = intrusive_list<T, AsNode>;
|
|
|
|
public:
|
|
using value_type = typename base::value_type;
|
|
using reference = typename base::reference;
|
|
using iterator = typename base::iterator;
|
|
using reverse_iterator = typename base::reverse_iterator;
|
|
using const_iterator = typename base::const_iterator;
|
|
using unique_pointer = std::unique_ptr<T>;
|
|
|
|
using base::iterator_to;
|
|
using base::front;
|
|
using base::back;
|
|
using base::empty;
|
|
using base::begin;
|
|
using base::cbegin;
|
|
using base::end;
|
|
using base::cend;
|
|
using base::rbegin;
|
|
using base::crbegin;
|
|
using base::rend;
|
|
using base::crend;
|
|
|
|
owning_intrusive_list () noexcept = default;
|
|
|
|
owning_intrusive_list (owning_intrusive_list &&other) noexcept
|
|
: base (std::move (other))
|
|
{
|
|
}
|
|
|
|
~owning_intrusive_list ()
|
|
{ this->clear (); }
|
|
|
|
owning_intrusive_list &operator= (owning_intrusive_list &&other) noexcept
|
|
{
|
|
this->clear ();
|
|
this->base::operator= (std::move (other));
|
|
return *this;
|
|
}
|
|
|
|
void swap (owning_intrusive_list &other) noexcept
|
|
{ this->base::swap (other); }
|
|
|
|
/* Insert ELEM at the front of the list.
|
|
|
|
The list takes ownership of ELEM. */
|
|
void push_front (unique_pointer elem) noexcept
|
|
{ this->base::push_front (*elem.release ()); }
|
|
|
|
/* Insert ELEM at the back of the list.
|
|
|
|
The list takes ownership of ELEM. */
|
|
void push_back (unique_pointer elem) noexcept
|
|
{ this->base::push_back (*elem.release ()); }
|
|
|
|
/* Insert ELEM before POS in the list.
|
|
|
|
The list takes ownership of ELEM. */
|
|
iterator insert (const_iterator pos, unique_pointer elem) noexcept
|
|
{ return this->base::insert (pos, *elem.release ()); }
|
|
|
|
void splice (owning_intrusive_list &&other) noexcept
|
|
{ this->base::splice (std::move (other)); }
|
|
|
|
/* Remove the element at the front of the list. The element is destroyed. */
|
|
void pop_front () noexcept
|
|
{
|
|
unique_pointer holder (&this->front ());
|
|
this->base::pop_front ();
|
|
}
|
|
|
|
/* Remove the element at the back of the list. The element is destroyed. */
|
|
void pop_back () noexcept
|
|
{
|
|
unique_pointer holder (&this->back ());
|
|
this->base::pop_back ();
|
|
}
|
|
|
|
/* Remove the element pointed by I from the list. The element
|
|
pointed by I is destroyed. */
|
|
iterator erase (const_iterator i) noexcept
|
|
{
|
|
unique_pointer holder (&*i);
|
|
return this->base::erase (i);
|
|
}
|
|
|
|
/* Remove all elements from the list. All elements are destroyed. */
|
|
void clear () noexcept
|
|
{
|
|
while (!this->empty ())
|
|
this->pop_front ();
|
|
}
|
|
|
|
/* Construct an element in-place at the front of the list.
|
|
|
|
Return a reference to the new element. */
|
|
template<typename... Args>
|
|
reference emplace_front (Args &&...args)
|
|
{ return this->emplace (this->begin (), std::forward<Args> (args)...); }
|
|
|
|
/* Construct an element in-place at the back of the list.
|
|
|
|
Return a reference to the new element. */
|
|
template<typename... Args>
|
|
reference emplace_back (Args &&...args)
|
|
{ return this->emplace (this->end (), std::forward<Args> (args)...); }
|
|
|
|
/* Construct an element in-place in the list, before POS.
|
|
|
|
Return a reference to the new element. */
|
|
template<typename... Args>
|
|
reference emplace (const_iterator pos, Args &&...args)
|
|
{
|
|
return *this->insert (pos,
|
|
std::make_unique<T> (std::forward<Args> (args)...));
|
|
}
|
|
|
|
/* Return type for the release method. */
|
|
struct release_ret
|
|
{
|
|
/* Iterator to the following element in the list. */
|
|
iterator next;
|
|
|
|
/* The released element. */
|
|
unique_pointer released;
|
|
};
|
|
|
|
release_ret release (const_iterator i) noexcept
|
|
{
|
|
iterator next = i;
|
|
++next;
|
|
unique_pointer released (&*i);
|
|
|
|
this->unlink_element (*i);
|
|
|
|
return { next, std::move (released) };
|
|
}
|
|
};
|
|
|
|
#endif /* GDBSUPPORT_OWNING_INTRUSIVE_LIST_H */
|