From 08b8a139c9e8adcb4ec12a8a17e5836b8b5acb63 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Sun, 18 Oct 2020 11:38:10 -0600 Subject: Rewrite registry.h This rewrites registry.h, removing all the macros and replacing it with relatively ordinary template classes. The result is less code than the previous setup. It replaces large macros with a relatively straightforward C++ class, and now manages its own cleanup. The existing type-safe "key" class is replaced with the equivalent template class. This approach ended up requiring relatively few changes to the users of the registry code in gdb -- code using the key system just required a small change to the key's declaration. All existing users of the old C-like API are now converted to use the type-safe API. This mostly involved changing explicit deletion functions to be an operator() in a deleter class. The old "save/free" two-phase process is removed, and replaced with a single "free" phase. No existing code used both phases. The old "free" callbacks took a parameter for the enclosing container object. However, this wasn't truly needed and is removed here as well. --- gdb/python/py-objfile.c | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) (limited to 'gdb/python/py-objfile.c') diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c index 298f3f2..4cc5702 100644 --- a/gdb/python/py-objfile.c +++ b/gdb/python/py-objfile.c @@ -55,7 +55,20 @@ struct objfile_object extern PyTypeObject objfile_object_type CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("objfile_object"); -static const struct objfile_data *objfpy_objfile_data_key; +/* Clear the OBJFILE pointer in an Objfile object and remove the + reference. */ +struct objfpy_deleter +{ + void operator() (objfile_object *obj) + { + gdbpy_enter enter_py; + gdbpy_ref object (obj); + object->objfile = nullptr; + } +}; + +static const registry::key + objfpy_objfile_data_key; /* Require that OBJF be a valid objfile. */ #define OBJFPY_REQUIRE_VALID(obj) \ @@ -668,16 +681,6 @@ gdbpy_lookup_objfile (PyObject *self, PyObject *args, PyObject *kw) -/* Clear the OBJFILE pointer in an Objfile object and remove the - reference. */ -static void -py_free_objfile (struct objfile *objfile, void *datum) -{ - gdbpy_enter enter_py (objfile->arch ()); - gdbpy_ref object ((objfile_object *) datum); - object->objfile = NULL; -} - /* Return a new reference to the Python object of type Objfile representing OBJFILE. If the object has already been created, return it. Otherwise, create it. Return NULL and set the Python @@ -687,7 +690,7 @@ gdbpy_ref<> objfile_to_objfile_object (struct objfile *objfile) { PyObject *result - = ((PyObject *) objfile_data (objfile, objfpy_objfile_data_key)); + = (PyObject *) objfpy_objfile_data_key.get (objfile); if (result == NULL) { gdbpy_ref object @@ -698,21 +701,13 @@ objfile_to_objfile_object (struct objfile *objfile) return NULL; object->objfile = objfile; - set_objfile_data (objfile, objfpy_objfile_data_key, object.get ()); + objfpy_objfile_data_key.set (objfile, object.get ()); result = (PyObject *) object.release (); } return gdbpy_ref<>::new_reference (result); } -void _initialize_py_objfile (); -void -_initialize_py_objfile () -{ - objfpy_objfile_data_key - = register_objfile_data_with_cleanup (NULL, py_free_objfile); -} - int gdbpy_initialize_objfile (void) { -- cgit v1.1