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.
This commit is contained in:
Tom Tromey
2020-10-18 11:38:10 -06:00
parent 8f83e7b926
commit 08b8a139c9
64 changed files with 664 additions and 1052 deletions

View File

@ -57,7 +57,30 @@ struct pspace_object
extern PyTypeObject pspace_object_type
CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("pspace_object");
static const struct program_space_data *pspy_pspace_data_key;
/* Clear the PSPACE pointer in a Pspace object and remove the reference. */
struct pspace_deleter
{
void operator() (pspace_object *obj)
{
/* This is a fiction, but we're in a nasty spot: The pspace is in the
process of being deleted, we can't rely on anything in it. Plus
this is one time when the current program space and current inferior
are not in sync: All inferiors that use PSPACE may no longer exist.
We don't need to do much here, and since "there is always an inferior"
using target_gdbarch suffices.
Note: We cannot call get_current_arch because it may try to access
the target, which may involve accessing data in the pspace currently
being deleted. */
struct gdbarch *arch = target_gdbarch ();
gdbpy_enter enter_py (arch);
gdbpy_ref<pspace_object> object (obj);
object->pspace = NULL;
}
};
static const registry<program_space>::key<pspace_object, pspace_deleter>
pspy_pspace_data_key;
/* Require that PSPACE_OBJ be a valid program space ID. */
#define PSPY_REQUIRE_VALID(pspace_obj) \
@ -463,27 +486,6 @@ pspy_is_valid (PyObject *o, PyObject *args)
/* Clear the PSPACE pointer in a Pspace object and remove the reference. */
static void
py_free_pspace (struct program_space *pspace, void *datum)
{
/* This is a fiction, but we're in a nasty spot: The pspace is in the
process of being deleted, we can't rely on anything in it. Plus
this is one time when the current program space and current inferior
are not in sync: All inferiors that use PSPACE may no longer exist.
We don't need to do much here, and since "there is always an inferior"
using target_gdbarch suffices.
Note: We cannot call get_current_arch because it may try to access
the target, which may involve accessing data in the pspace currently
being deleted. */
struct gdbarch *arch = target_gdbarch ();
gdbpy_enter enter_py (arch);
gdbpy_ref<pspace_object> object ((pspace_object *) datum);
object->pspace = NULL;
}
/* Return a new reference to the Python object of type Pspace
representing PSPACE. If the object has already been created,
return it. Otherwise, create it. Return NULL and set the Python
@ -492,8 +494,7 @@ py_free_pspace (struct program_space *pspace, void *datum)
gdbpy_ref<>
pspace_to_pspace_object (struct program_space *pspace)
{
PyObject *result
((PyObject *) program_space_data (pspace, pspy_pspace_data_key));
PyObject *result = (PyObject *) pspy_pspace_data_key.get (pspace);
if (result == NULL)
{
gdbpy_ref<pspace_object> object
@ -504,7 +505,7 @@ pspace_to_pspace_object (struct program_space *pspace)
return NULL;
object->pspace = pspace;
set_program_space_data (pspace, pspy_pspace_data_key, object.get ());
pspy_pspace_data_key.set (pspace, object.get ());
result = (PyObject *) object.release ();
}
@ -528,14 +529,6 @@ gdbpy_is_progspace (PyObject *obj)
return PyObject_TypeCheck (obj, &pspace_object_type);
}
void _initialize_py_progspace ();
void
_initialize_py_progspace ()
{
pspy_pspace_data_key
= register_program_space_data_with_cleanup (NULL, py_free_pspace);
}
int
gdbpy_initialize_pspace (void)
{