From 51ac9db596ea9f0affa9f7db25bb179cf70beac4 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Fri, 21 Jun 2019 14:10:14 -0400 Subject: [PATCH] dwarf2read: C++ify dwo_file This patch changes dwo_file to be allocated/deallocated with new/delete, so that we can start using C++ features in it, and in struct dwo_sections. The free_dwo_file function becomes the destructor of struct dwo_file (and will disappear in upcoming patches, which will use gdb_bfd_ref_ptr for dbfd and an std::vector for sections.types). gdb/ChangeLog: * dwarf2read.h (struct dwarf2_per_objfile) : Change type to htab_up. * dwarf2read.c (struct dwo_file): Initialize fields. <~dwo_file>: New. (free_dwo_file): Remove, move content to ~dwo_file. (struct dwo_file_deleter): Remove. (dwo_file_up>: Remove custom deleter. (free_dwo_files): Remove. (dwarf2_per_objfile::~dwarf2_per_objfile): Don't explicitly free dwo_files. (process_skeletonless_type_units): Call unique_ptr::get. (allocate_dwo_file_hash_table): Add deleter to created hash table. Change return type to htab_up. (lookup_dwo_file_slot): Don't memset dwo_file, call unique_ptr::get. (create_dwo_unit_in_dwp_v1): Allocate dwo_file with new. (create_dwo_unit_in_dwp_v2): Likewise. (open_and_init_dwo_file): Likewise. (free_dwo_file_from_slot): Remove. --- gdb/ChangeLog | 22 ++++++++++ gdb/dwarf2read.c | 112 ++++++++++++++++------------------------------- gdb/dwarf2read.h | 2 +- 3 files changed, 61 insertions(+), 75 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index b640aa4245..ef3ddbc29a 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,25 @@ +2019-06-21 Simon Marchi + + * dwarf2read.h (struct dwarf2_per_objfile) : Change + type to htab_up. + * dwarf2read.c (struct dwo_file): Initialize fields. + <~dwo_file>: New. + (free_dwo_file): Remove, move content to ~dwo_file. + (struct dwo_file_deleter): Remove. + (dwo_file_up>: Remove custom deleter. + (free_dwo_files): Remove. + (dwarf2_per_objfile::~dwarf2_per_objfile): Don't explicitly free + dwo_files. + (process_skeletonless_type_units): Call unique_ptr::get. + (allocate_dwo_file_hash_table): Add deleter to created hash + table. Change return type to htab_up. + (lookup_dwo_file_slot): Don't memset dwo_file, call + unique_ptr::get. + (create_dwo_unit_in_dwp_v1): Allocate dwo_file with new. + (create_dwo_unit_in_dwp_v2): Likewise. + (open_and_init_dwo_file): Likewise. + (free_dwo_file_from_slot): Remove. + 2019-06-21 Simon Marchi * dwarf2read.h (struct dwarf2_section_info) dwo_file_up; +typedef std::unique_ptr dwo_file_up; static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile); @@ -2147,8 +2145,6 @@ dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_, locate_sections (obfd, sec, *names); } -static void free_dwo_files (htab_t dwo_files, struct objfile *objfile); - dwarf2_per_objfile::~dwarf2_per_objfile () { /* Cached DIE trees use xmalloc and the comp_unit_obstack. */ @@ -2168,9 +2164,6 @@ dwarf2_per_objfile::~dwarf2_per_objfile () VEC_free (dwarf2_section_info_def, types); - if (dwo_files != NULL) - free_dwo_files (dwo_files, objfile); - /* Everything else should be on the objfile obstack. */ } @@ -8407,7 +8400,7 @@ process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile) if (get_dwp_file (dwarf2_per_objfile) == NULL && dwarf2_per_objfile->dwo_files != NULL) { - htab_traverse_noresize (dwarf2_per_objfile->dwo_files, + htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (), process_dwo_file_for_skeletonless_type_units, dwarf2_per_objfile); } @@ -11775,16 +11768,23 @@ eq_dwo_file (const void *item_lhs, const void *item_rhs) /* Allocate a hash table for DWO files. */ -static htab_t +static htab_up allocate_dwo_file_hash_table (struct objfile *objfile) { - return htab_create_alloc_ex (41, - hash_dwo_file, - eq_dwo_file, - NULL, - &objfile->objfile_obstack, - hashtab_obstack_allocate, - dummy_obstack_deallocate); + auto delete_dwo_file = [] (void *item) + { + struct dwo_file *dwo_file = (struct dwo_file *) item; + + delete dwo_file; + }; + + return htab_up (htab_create_alloc_ex (41, + hash_dwo_file, + eq_dwo_file, + delete_dwo_file, + &objfile->objfile_obstack, + hashtab_obstack_allocate, + dummy_obstack_deallocate)); } /* Lookup DWO file DWO_NAME. */ @@ -11801,10 +11801,10 @@ lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile, dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile); - memset (&find_entry, 0, sizeof (find_entry)); find_entry.dwo_name = dwo_name; find_entry.comp_dir = comp_dir; - slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT); + slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry, + INSERT); return slot; } @@ -12451,7 +12451,7 @@ create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile, fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n", virtual_dwo_name.c_str ()); } - dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file); + dwo_file = new struct dwo_file; dwo_file->dwo_name = (const char *) obstack_copy0 (&objfile->objfile_obstack, virtual_dwo_name.c_str (), @@ -12649,7 +12649,7 @@ create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile, fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n", virtual_dwo_name.c_str ()); } - dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file); + dwo_file = new struct dwo_file; dwo_file->dwo_name = (const char *) obstack_copy0 (&objfile->objfile_obstack, virtual_dwo_name.c_str (), @@ -12959,7 +12959,6 @@ open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu, const char *dwo_name, const char *comp_dir) { struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile; - struct objfile *objfile = dwarf2_per_objfile->objfile; gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir)); if (dbfd == NULL) @@ -12969,10 +12968,7 @@ open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu, return NULL; } - /* We use a unique pointer here, despite the obstack allocation, - because a dwo_file needs some cleanup if it is abandoned. */ - dwo_file_up dwo_file (OBSTACK_ZALLOC (&objfile->objfile_obstack, - struct dwo_file)); + dwo_file_up dwo_file (new struct dwo_file); dwo_file->dwo_name = dwo_name; dwo_file->comp_dir = comp_dir; dwo_file->dbfd = dbfd.release (); @@ -13486,38 +13482,6 @@ queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu) htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu); } -/* Free all resources associated with DWO_FILE. - Close the DWO file and munmap the sections. */ - -static void -free_dwo_file (struct dwo_file *dwo_file) -{ - /* Note: dbfd is NULL for virtual DWO files. */ - gdb_bfd_unref (dwo_file->dbfd); - - VEC_free (dwarf2_section_info_def, dwo_file->sections.types); -} - -/* Traversal function for free_dwo_files. */ - -static int -free_dwo_file_from_slot (void **slot, void *info) -{ - struct dwo_file *dwo_file = (struct dwo_file *) *slot; - - free_dwo_file (dwo_file); - - return 1; -} - -/* Free all resources associated with DWO_FILES. */ - -static void -free_dwo_files (htab_t dwo_files, struct objfile *objfile) -{ - htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile); -} - /* Read in various DIEs. */ /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes). diff --git a/gdb/dwarf2read.h b/gdb/dwarf2read.h index c06093c997..4a1cd9cf00 100644 --- a/gdb/dwarf2read.h +++ b/gdb/dwarf2read.h @@ -197,7 +197,7 @@ public: /* A table mapping DW_AT_dwo_name values to struct dwo_file objects. This is NULL if the table hasn't been allocated yet. */ - htab_t dwo_files {}; + htab_up dwo_files; /* True if we've checked for whether there is a DWP file. */ bool dwp_checked = false; -- 2.34.1