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

@ -77,9 +77,33 @@ struct block_syms_iterator_object {
} \
} while (0)
/* This is called when an objfile is about to be freed.
Invalidate the block as further actions on the block would result
in bad data. All access to obj->symbol should be gated by
BLPY_REQUIRE_VALID which will raise an exception on invalid
blocks. */
struct blpy_deleter
{
void operator() (block_object *obj)
{
while (obj)
{
block_object *next = obj->next;
obj->block = NULL;
obj->objfile = NULL;
obj->next = NULL;
obj->prev = NULL;
obj = next;
}
}
};
extern PyTypeObject block_syms_iterator_object_type
CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("block_syms_iterator_object");
static const struct objfile_data *blpy_objfile_data_key;
static const registry<objfile>::key<block_object, blpy_deleter>
blpy_objfile_data_key;
static PyObject *
blpy_iter (PyObject *self)
@ -269,10 +293,7 @@ blpy_dealloc (PyObject *obj)
if (block->prev)
block->prev->next = block->next;
else if (block->objfile)
{
set_objfile_data (block->objfile, blpy_objfile_data_key,
block->next);
}
blpy_objfile_data_key.set (block->objfile, block->next);
if (block->next)
block->next->prev = block->prev;
block->block = NULL;
@ -293,11 +314,10 @@ set_block (block_object *obj, const struct block *block,
if (objfile)
{
obj->objfile = objfile;
obj->next = ((block_object *)
objfile_data (objfile, blpy_objfile_data_key));
obj->next = blpy_objfile_data_key.get (objfile);
if (obj->next)
obj->next->prev = obj;
set_objfile_data (objfile, blpy_objfile_data_key, obj);
blpy_objfile_data_key.set (objfile, obj);
}
else
obj->next = NULL;
@ -404,40 +424,6 @@ blpy_iter_is_valid (PyObject *self, PyObject *args)
Py_RETURN_TRUE;
}
/* This function is called when an objfile is about to be freed.
Invalidate the block as further actions on the block would result
in bad data. All access to obj->symbol should be gated by
BLPY_REQUIRE_VALID which will raise an exception on invalid
blocks. */
static void
del_objfile_blocks (struct objfile *objfile, void *datum)
{
block_object *obj = (block_object *) datum;
while (obj)
{
block_object *next = obj->next;
obj->block = NULL;
obj->objfile = NULL;
obj->next = NULL;
obj->prev = NULL;
obj = next;
}
}
void _initialize_py_block ();
void
_initialize_py_block ()
{
/* Register an objfile "free" callback so we can properly
invalidate blocks when an object file is about to be
deleted. */
blpy_objfile_data_key
= register_objfile_data_with_cleanup (NULL, del_objfile_blocks);
}
int
gdbpy_initialize_blocks (void)
{