diff options
author | Tom Tromey <tom@tromey.com> | 2020-10-18 11:38:10 -0600 |
---|---|---|
committer | Tom Tromey <tom@tromey.com> | 2022-07-28 14:16:50 -0600 |
commit | 08b8a139c9e8adcb4ec12a8a17e5836b8b5acb63 (patch) | |
tree | e1050b58dcebee1150881f43f64c1c186955d240 /gdb/python/py-inferior.c | |
parent | 8f83e7b9262e08fa43ca6e645337511c68ddc52a (diff) | |
download | binutils-08b8a139c9e8adcb4ec12a8a17e5836b8b5acb63.zip binutils-08b8a139c9e8adcb4ec12a8a17e5836b8b5acb63.tar.gz binutils-08b8a139c9e8adcb4ec12a8a17e5836b8b5acb63.tar.bz2 |
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.
Diffstat (limited to 'gdb/python/py-inferior.c')
-rw-r--r-- | gdb/python/py-inferior.c | 68 |
1 files changed, 31 insertions, 37 deletions
diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c index 61ed342..6ea384e 100644 --- a/gdb/python/py-inferior.c +++ b/gdb/python/py-inferior.c @@ -60,7 +60,35 @@ struct inferior_object extern PyTypeObject inferior_object_type CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("inferior_object"); -static const struct inferior_data *infpy_inf_data_key; +/* Deleter to clean up when an inferior is removed. */ +struct infpy_deleter +{ + void operator() (inferior_object *obj) + { + struct threadlist_entry *th_entry, *th_tmp; + + if (!gdb_python_initialized) + return; + + gdbpy_enter enter_py; + gdbpy_ref<inferior_object> inf_obj (obj); + + inf_obj->inferior = NULL; + + /* Deallocate threads list. */ + for (th_entry = inf_obj->threads; th_entry != NULL;) + { + th_tmp = th_entry; + th_entry = th_entry->next; + delete th_tmp; + } + + inf_obj->nthreads = 0; + } +}; + +static const registry<inferior>::key<inferior_object, infpy_deleter> + infpy_inf_data_key; /* Require that INFERIOR be a valid inferior ID. */ #define INFPY_REQUIRE_VALID(Inferior) \ @@ -221,7 +249,7 @@ inferior_to_inferior_object (struct inferior *inferior) { inferior_object *inf_obj; - inf_obj = (inferior_object *) inferior_data (inferior, infpy_inf_data_key); + inf_obj = infpy_inf_data_key.get (inferior); if (!inf_obj) { inf_obj = PyObject_New (inferior_object, &inferior_object_type); @@ -234,7 +262,7 @@ inferior_to_inferior_object (struct inferior *inferior) /* PyObject_New initializes the new object with a refcount of 1. This counts for the reference we are keeping in the inferior data. */ - set_inferior_data (inferior, infpy_inf_data_key, inf_obj); + infpy_inf_data_key.set (inferior, inf_obj); } /* We are returning a new reference. */ @@ -795,32 +823,6 @@ infpy_dealloc (PyObject *obj) Py_TYPE (obj)->tp_free (obj); } -/* Clear the INFERIOR pointer in an Inferior object and clear the - thread list. */ -static void -py_free_inferior (struct inferior *inf, void *datum) -{ - struct threadlist_entry *th_entry, *th_tmp; - - if (!gdb_python_initialized) - return; - - gdbpy_enter enter_py; - gdbpy_ref<inferior_object> inf_obj ((inferior_object *) datum); - - inf_obj->inferior = NULL; - - /* Deallocate threads list. */ - for (th_entry = inf_obj->threads; th_entry != NULL;) - { - th_tmp = th_entry; - th_entry = th_entry->next; - delete th_tmp; - } - - inf_obj->nthreads = 0; -} - /* Implementation of gdb.selected_inferior() -> gdb.Inferior. Returns the current inferior object. */ @@ -831,14 +833,6 @@ gdbpy_selected_inferior (PyObject *self, PyObject *args) inferior_to_inferior_object (current_inferior ()).release ()); } -void _initialize_py_inferior (); -void -_initialize_py_inferior () -{ - infpy_inf_data_key = - register_inferior_data_with_cleanup (NULL, py_free_inferior); -} - int gdbpy_initialize_inferior (void) { |