[PATCH 2/7] jit: make gdb_object::symtabs a vector

Simon Marchi
2019-12-13T06:03:32Z

Replace the manual linked list with a vector, simplifying the memory
management. This requires allocating gdb_object with new and freeing it
with delete.

gdb/ChangeLog:

* jit.c (struct gdb_symtab) <next>: Remove field.
(struct gdb_object) <symtabs>: Change type to
std::vector<gdb_symtab *>.
(jit_object_open_impl): Allocate gdb_object with new.
(jit_symtab_open_impl): Adjust to std::vector.
(jit_object_close_impl): Adjust to std::vector.
---
gdb/jit.c | 21 ++++++++-------------
1 file changed, 8 insertions(+), 13 deletions(-)

diff --git a/gdb/jit.c b/gdb/jit.c
index f8b12443ba8d..672a74044af3 100644
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -462,14 +462,13 @@ struct gdb_symtab

/* The source file for this symtab. */
const char *file_name;
- struct gdb_symtab *next;
};

/* Proxy object for building an object. */

struct gdb_object
{
- struct gdb_symtab *symtabs;
+ std::vector<gdb_symtab *> symtabs;
};

/* The type of the `private' data passed around by the callback
@@ -501,7 +500,7 @@ jit_object_open_impl (struct gdb_symbol_callbacks *cb)
/* CB is not required right now, but sometime in the future we might
need a handle to it, and we'd like to do that without breaking
the ABI. */
- return XCNEW (struct gdb_object);
+ return new gdb_object;
}

/* Readers call into this function to open a new gdb_symtab, which,
@@ -518,8 +517,7 @@ jit_symtab_open_impl (struct gdb_symbol_callbacks *cb,

ret = XCNEW (struct gdb_symtab);
ret->file_name = file_name ? xstrdup (file_name) : xstrdup ("");
- ret->next = object->symtabs;
- object->symtabs = ret;
+ object->symtabs.push_back (ret);
return ret;
}

@@ -781,7 +779,6 @@ static void
jit_object_close_impl (struct gdb_symbol_callbacks *cb,
struct gdb_object *obj)
{
- struct gdb_symtab *i, *j;
struct objfile *objfile;
jit_dbg_reader_data *priv_data;

@@ -791,14 +788,12 @@ jit_object_close_impl (struct gdb_symbol_callbacks *cb,
OBJF_NOT_FILENAME);
objfile->per_bfd->gdbarch = target_gdbarch ();

- j = NULL;
- for (i = obj->symtabs; i; i = j)
- {
- j = i->next;
- finalize_symtab (i, objfile);
- }
+ for (gdb_symtab *symtab : obj->symtabs)
+ finalize_symtab (symtab, objfile);
+
add_objfile_entry (objfile, *priv_data);
- xfree (obj);
+
+ delete obj;
}

/* Try to read CODE_ENTRY using the loaded jit reader (if any).
--
2.24.1