Patchwork Don't make an extra copy + allocation of the demangled name

login
register
mail settings
Submitter Doug Evans via gdb-patches
Date Oct. 2, 2019, 7:46 p.m.
Message ID <20191002194619.72854-1-cbiesinger@google.com>
Download mbox | patch
Permalink /patch/34799/
State New
Headers show

Comments

Doug Evans via gdb-patches - Oct. 2, 2019, 7:46 p.m.
We can just keep around the malloc()-ed name we got from bfd and free
it later.

gdb/ChangeLog:

2019-10-02  Christian Biesinger  <cbiesinger@google.com>

	* symtab.c (struct demangled_name_entry): Change demangled name
	to a char*, now that we don't allocate it as part of the struct
	anymore.
	(free_demangled_name_entry): New function.
	(create_demangled_names_hash): Pass free function to htab_create_alloc.
	(symbol_set_names): No longer obstack allocate + copy the demangled
	name, just store the allocated name from bfd.
---
 gdb/symtab.c | 35 +++++++++++++++++------------------
 1 file changed, 17 insertions(+), 18 deletions(-)

Patch

diff --git a/gdb/symtab.c b/gdb/symtab.c
index 8a551f1575..a185f922ad 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -715,7 +715,7 @@  struct demangled_name_entry
 {
   const char *mangled;
   ENUM_BITFIELD(language) language : LANGUAGE_BITS;
-  char demangled[1];
+  char* demangled;
 };
 
 /* Hash function for the demangled name hash.  */
@@ -742,6 +742,15 @@  eq_demangled_name_entry (const void *a, const void *b)
   return strcmp (da->mangled, db->mangled) == 0;
 }
 
+static void
+free_demangled_name_entry (void *data)
+{
+  struct demangled_name_entry *e
+    = (struct demangled_name_entry *) data;
+
+  xfree (e->demangled);
+}
+
 /* Create the hash table used for demangled names.  Each hash entry is
    a pair of strings; one for the mangled name and one for the demangled
    name.  The entry is hashed via just the mangled name.  */
@@ -756,7 +765,7 @@  create_demangled_names_hash (struct objfile_per_bfd_storage *per_bfd)
 
   per_bfd->demangled_names_hash.reset (htab_create_alloc
     (256, hash_demangled_name_entry, eq_demangled_name_entry,
-     NULL, xcalloc, xfree));
+     free_demangled_name_entry, xcalloc, xfree));
 }
 
 /* Try to determine the demangled name for a symbol, based on the
@@ -869,8 +878,6 @@  symbol_set_names (struct general_symbol_info *gsymbol,
     {
       char *demangled_name_ptr
 	= symbol_find_demangled_name (gsymbol, linkage_name_copy);
-      gdb::unique_xmalloc_ptr<char> demangled_name (demangled_name_ptr);
-      int demangled_len = demangled_name ? strlen (demangled_name.get ()) : 0;
 
       /* Suppose we have demangled_name==NULL, copy_name==0, and
 	 linkage_name_copy==linkage_name.  In this case, we already have the
@@ -886,39 +893,31 @@  symbol_set_names (struct general_symbol_info *gsymbol,
 	  *slot
 	    = ((struct demangled_name_entry *)
 	       obstack_alloc (&per_bfd->storage_obstack,
-			      offsetof (struct demangled_name_entry, demangled)
-			      + demangled_len + 1));
+			      sizeof (demangled_name_entry)));
 	  (*slot)->mangled = linkage_name;
 	}
       else
 	{
-	  char *mangled_ptr;
-
 	  /* If we must copy the mangled name, put it directly after
-	     the demangled name so we can have a single
+	     the struct so we can have a single
 	     allocation.  */
 	  *slot
 	    = ((struct demangled_name_entry *)
 	       obstack_alloc (&per_bfd->storage_obstack,
-			      offsetof (struct demangled_name_entry, demangled)
-			      + len + demangled_len + 2));
-	  mangled_ptr = &((*slot)->demangled[demangled_len + 1]);
+			      sizeof (demangled_name_entry) + len + 1));
+	  char *mangled_ptr = reinterpret_cast<char*> (*slot + 1);
 	  strcpy (mangled_ptr, linkage_name_copy);
 	  (*slot)->mangled = mangled_ptr;
 	}
+      (*slot)->demangled = demangled_name_ptr;
       (*slot)->language = gsymbol->language;
-
-      if (demangled_name != NULL)
-	strcpy ((*slot)->demangled, demangled_name.get ());
-      else
-	(*slot)->demangled[0] = '\0';
     }
   else if (gsymbol->language == language_unknown
 	   || gsymbol->language == language_auto)
     gsymbol->language = (*slot)->language;
 
   gsymbol->name = (*slot)->mangled;
-  if ((*slot)->demangled[0] != '\0')
+  if ((*slot)->demangled != nullptr)
     symbol_set_demangled_name (gsymbol, (*slot)->demangled,
 			       &per_bfd->storage_obstack);
   else