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 8/9] gdb/python: convert gdb.Symtab to use gdbpy_registry
Date: Mon, 27 Jan 2025 10:44:34 +0000	[thread overview]
Message-ID: <20250127104435.823519-9-jan.vrany@labware.com> (raw)
In-Reply-To: <20250127104435.823519-1-jan.vrany@labware.com>

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

diff --git a/gdb/python/py-symtab.c b/gdb/python/py-symtab.c
index 177028ecf01..417ad68a155 100644
--- a/gdb/python/py-symtab.c
+++ b/gdb/python/py-symtab.c
@@ -28,39 +28,12 @@ struct symtab_object {
   PyObject_HEAD
   /* The GDB Symbol table structure.  */
   struct symtab *symtab;
-  /* A symtab object is associated with an objfile, so keep track with
-     a doubly-linked list, rooted in the objfile.  This allows
-     invalidation of the underlying struct symtab when the objfile is
-     deleted.  */
-  symtab_object *prev;
-  symtab_object *next;
-};
-
-/* This function is called when an objfile is about to be freed.
-   Invalidate the symbol table as further actions on the symbol table
-   would result in bad data.  All access to obj->symtab should be
-   gated by STPY_REQUIRE_VALID which will raise an exception on
-   invalid symbol tables.  */
-struct stpy_deleter
-{
-  void operator() (symtab_object *obj)
-  {
-    while (obj)
-      {
-	symtab_object *next = obj->next;
-
-	obj->symtab = NULL;
-	obj->next = NULL;
-	obj->prev = NULL;
-	obj = next;
-      }
-  }
 };
 
 extern PyTypeObject symtab_object_type
     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("symtab_object");
-static const registry<objfile>::key<symtab_object, stpy_deleter>
-     stpy_objfile_data_key;
+static const gdbpy_registry<gdbpy_memoizing_registry_storage<symtab_object, symtab, &symtab_object::symtab>>
+     stpy_registry;
 
 /* Require a valid symbol table.  All access to symtab_object->symtab
    should be gated by this call.  */
@@ -280,23 +253,6 @@ salpy_str (PyObject *self)
 			       sal->line);
 }
 
-static void
-stpy_dealloc (PyObject *obj)
-{
-  symtab_object *symtab = (symtab_object *) obj;
-
-  if (symtab->prev)
-    symtab->prev->next = symtab->next;
-  else if (symtab->symtab)
-    stpy_objfile_data_key.set (symtab->symtab->compunit ()->objfile (),
-			       symtab->next);
-  if (symtab->next)
-    symtab->next->prev = symtab->prev;
-  symtab->symtab = NULL;
-  Py_TYPE (obj)->tp_free (obj);
-}
-
-
 static PyObject *
 salpy_get_pc (PyObject *self, void *closure)
 {
@@ -418,16 +374,8 @@ static void
 set_symtab (symtab_object *obj, struct symtab *symtab)
 {
   obj->symtab = symtab;
-  obj->prev = NULL;
   if (symtab != nullptr)
-    {
-      obj->next = stpy_objfile_data_key.get (symtab->compunit ()->objfile ());
-      if (obj->next)
-	obj->next->prev = obj;
-      stpy_objfile_data_key.set (symtab->compunit ()->objfile (), obj);
-    }
-  else
-    obj->next = NULL;
+    stpy_registry.add (symtab->compunit ()->objfile (), obj);
 }
 
 /* Create a new symbol table (gdb.Symtab) object that encapsulates the
@@ -441,16 +389,10 @@ symtab_to_symtab_object (struct symtab *symtab)
      and if so, return it.  */
   if (symtab != nullptr)
     {
-      symtab_obj = stpy_objfile_data_key.get (symtab->compunit ()->objfile ());
-      while (symtab_obj != nullptr)
-	{
-	  if (symtab_obj->symtab == symtab)
-	    {
-	      Py_INCREF (symtab_obj);
-	      return (PyObject*)symtab_obj;
-	    }
-	  symtab_obj = symtab_obj->next;
-	}
+      symtab_obj = stpy_registry.lookup (symtab->compunit ()->objfile (),
+					 symtab);
+      if (symtab_obj != nullptr)
+        return (PyObject*)symtab_obj;
     }
 
   symtab_obj = PyObject_New (symtab_object, &symtab_object_type);
@@ -545,7 +487,7 @@ PyTypeObject symtab_object_type = {
   "gdb.Symtab",			  /*tp_name*/
   sizeof (symtab_object),	  /*tp_basicsize*/
   0,				  /*tp_itemsize*/
-  stpy_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:51 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 ` [RFC 6/9] gdb/python: convert gdb.Symbol to use gdbpy_registry Jan Vrany
2025-01-27 10:44 ` [RFC 7/9] gdb/python: convert gdb.Type " Jan Vrany
2025-01-27 10:44 ` Jan Vrany [this message]
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-9-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