Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Jan Vrany <jan.vrany@labware.com>
To: gdb-patches@sourceware.org
Cc: Jan Vrany <jan.vrany@labware.com>
Subject: [RFC 6/9] gdb/python: convert gdb.Symbol to use gdbpy_registry
Date: Mon, 27 Jan 2025 10:44:32 +0000	[thread overview]
Message-ID: <20250127104435.823519-7-jan.vrany@labware.com> (raw)
In-Reply-To: <20250127104435.823519-1-jan.vrany@labware.com>

This commit converts gdb.Symbol to use gdbpy_registry for lifecycle
management. Since gdb.Symbol only holds on the struct symbol * (and
prev/next links) the default invalidator can be used.
---
 gdb/python/py-symbol.c | 91 +++++-------------------------------------
 1 file changed, 9 insertions(+), 82 deletions(-)

diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c
index c157bd7f5d7..f891c5619d6 100644
--- a/gdb/python/py-symbol.c
+++ b/gdb/python/py-symbol.c
@@ -29,12 +29,6 @@ struct symbol_object {
   PyObject_HEAD
   /* The GDB symbol structure this object is wrapping.  */
   struct symbol *symbol;
-  /* A symbol object is associated with an objfile, so keep track with
-     doubly-linked list, rooted in the objfile.  This lets us
-     invalidate the underlying struct symbol when the objfile is
-     deleted.  */
-  symbol_object *prev;
-  symbol_object *next;
 };
 
 /* Require a valid symbol.  All access to symbol_object->symbol should be
@@ -50,28 +44,8 @@ struct symbol_object {
       }							\
   } while (0)
 
-/* A deleter that is used when an objfile is about to be freed.  */
-struct symbol_object_deleter
-{
-  void operator() (symbol_object *obj)
-  {
-    while (obj)
-      {
-	symbol_object *next = obj->next;
-
-	obj->symbol = NULL;
-	obj->next = NULL;
-	obj->prev = NULL;
-
-	obj = next;
-      }
-  }
-};
-
-static const registry<objfile>::key<symbol_object, symbol_object_deleter>
-     sympy_objfile_data_key;
-static const registry<gdbarch>::key<symbol_object, symbol_object_deleter>
-     sympy_gdbarch_data_key;
+static const gdbpy_registry<gdbpy_memoizing_registry_storage<symbol_object, symbol, &symbol_object::symbol>>
+     sympy_registry;
 
 static PyObject *
 sympy_str (PyObject *self)
@@ -336,28 +310,17 @@ static void
 set_symbol (symbol_object *obj, struct symbol *symbol)
 {
   obj->symbol = symbol;
-  obj->prev = nullptr;
   if (symbol->is_objfile_owned ())
     {
       /* Can it really happen that symbol->symtab () is NULL?  */
       if (symbol->symtab () != nullptr)
 	{
-	  struct objfile *objfile = symbol->objfile ();
-
-	  obj->next = sympy_objfile_data_key.get (objfile);
-	  if (obj->next)
-	    obj->next->prev = obj;
-	  sympy_objfile_data_key.set (objfile, obj);
+	  sympy_registry.add (symbol->objfile (), obj);
 	}
     }
   else
     {
-      struct gdbarch *arch = symbol->arch ();
-
-      obj->next = sympy_gdbarch_data_key.get (arch);
-      if (obj->next)
-	obj->next->prev = obj;
-      sympy_gdbarch_data_key.set (arch, obj);
+      sympy_registry.add (symbol->arch (), obj);
     }
 }
 
@@ -371,19 +334,11 @@ symbol_to_symbol_object (struct symbol *sym)
   /* Look if there's already a gdb.Symbol object for given SYMBOL
      and if so, return it.  */
   if (sym->is_objfile_owned ())
-    sym_obj = sympy_objfile_data_key.get (sym->objfile ());
+    sym_obj = sympy_registry.lookup (sym->objfile (), sym);
   else
-    sym_obj = sympy_gdbarch_data_key.get (sym->arch ());
-
-  while (sym_obj != nullptr)
-    {
-      if (sym_obj->symbol == sym)
-	{
-	  Py_INCREF (sym_obj);
-	  return (PyObject*)sym_obj;
-	}
-      sym_obj = sym_obj->next;
-    }
+    sym_obj = sympy_registry.lookup (sym->arch (), sym);
+  if (sym_obj != nullptr)
+    return (PyObject*)sym_obj;
 
   sym_obj = PyObject_New (symbol_object, &symbol_object_type);
   if (sym_obj)
@@ -401,34 +356,6 @@ symbol_object_to_symbol (PyObject *obj)
   return ((symbol_object *) obj)->symbol;
 }
 
-static void
-sympy_dealloc (PyObject *obj)
-{
-  symbol_object *sym_obj = (symbol_object *) obj;
-
-  if (sym_obj->prev)
-    sym_obj->prev->next = sym_obj->next;
-  else if (sym_obj->symbol != nullptr)
-    {
-      if (sym_obj->symbol->is_objfile_owned ())
-	{
-	  /* Can it really happen that symbol->symtab () is NULL?  */
-	  if (sym_obj->symbol->symtab () != nullptr)
-	    sympy_objfile_data_key.set (sym_obj->symbol->objfile (),
-					sym_obj->next);
-	}
-      else
-	{
-	  sympy_gdbarch_data_key.set (sym_obj->symbol->arch (),
-				      sym_obj->next);
-	}
-    }
-  if (sym_obj->next)
-    sym_obj->next->prev = sym_obj->prev;
-  sym_obj->symbol = NULL;
-  Py_TYPE (obj)->tp_free (obj);
-}
-
 /* __repr__ implementation for gdb.Symbol.  */
 
 static PyObject *
@@ -791,7 +718,7 @@ PyTypeObject symbol_object_type = {
   "gdb.Symbol",			  /*tp_name*/
   sizeof (symbol_object),	  /*tp_basicsize*/
   0,				  /*tp_itemsize*/
-  sympy_dealloc,		  /*tp_dealloc*/
+  0,				  /*tp_dealloc*/
   0,				  /*tp_print*/
   0,				  /*tp_getattr*/
   0,				  /*tp_setattr*/
-- 
2.45.2


  parent reply	other threads:[~2025-01-27 10:49 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-27 10:44 [RFC 0/9] Attempt to unify Python object's lifecycle Jan Vrany
2025-01-27 10:44 ` [RFC 1/9] gdb/python: preserve identity for gdb.Symtab objects Jan Vrany
2025-01-27 10:44 ` [RFC 2/9] gdb/python: preserve identity for gdb.Symbol objects Jan Vrany
2025-02-20 19:22   ` Tom Tromey
2025-01-27 10:44 ` [RFC 3/9] gdb/python: do not hold on gdb.Symtab object from gdb.Symtab_and_line Jan Vrany
2025-01-27 10:44 ` [RFC 4/9] gdb/python: preserve identity for gdb.Type objects Jan Vrany
2025-01-27 10:44 ` [RFC 5/9] gdb/python: introduce gdbpy_registry Jan Vrany
2025-02-20 19:28   ` Tom Tromey
2025-01-27 10:44 ` Jan Vrany [this message]
2025-01-27 10:44 ` [RFC 7/9] gdb/python: convert gdb.Type to use gdbpy_registry Jan Vrany
2025-01-27 10:44 ` [RFC 8/9] gdb/python: convert gdb.Symtab " Jan Vrany
2025-01-27 10:44 ` [RFC 9/9] gdb/python: convert gdb.Symtab_and_line " Jan Vrany
2025-02-18 11:15 ` [PING] Re: [RFC 0/9] Attempt to unify Python object's lifecycle Jan Vraný
2025-02-19 21:00 ` Simon Marchi
2025-02-20 17:50   ` Jan Vraný
2025-02-20 19:18 ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250127104435.823519-7-jan.vrany@labware.com \
    --to=jan.vrany@labware.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox