[04/10] Change storage of demangle_component

Message ID 20240421-canon-fixes-v1-4-4dc4791d270d@tromey.com
State New
Headers
Series Fix some C++ name canonicalizer problems |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 fail Patch failed to apply
linaro-tcwg-bot/tcwg_gdb_build--master-arm fail Patch failed to apply

Commit Message

Tom Tromey April 21, 2024, 5 p.m. UTC
  This changes demangle_component objects to be stored on the obstack
that is part of demangle_info.  It also arranges for a demangle_info
object to be kept alive by cp_merge_demangle_parse_infos.  This way,
other data on the obstack can be kept while an "outer" demangle_info
needs it.
---
 gdb/cp-name-parser.y | 78 +++++-----------------------------------------------
 gdb/cp-support.c     |  2 +-
 gdb/cp-support.h     | 15 ++++------
 3 files changed, 14 insertions(+), 81 deletions(-)
  

Comments

John Baldwin April 22, 2024, 5:17 p.m. UTC | #1
On 4/21/24 10:00 AM, Tom Tromey wrote:
> This changes demangle_component objects to be stored on the obstack
> that is part of demangle_info.  It also arranges for a demangle_info
> object to be kept alive by cp_merge_demangle_parse_infos.  This way,
> other data on the obstack can be kept while an "outer" demangle_info
> needs it.

The mechanical changes to switch to using better memory management look
ok to me.  I did not do a detailed review of the objects' relationships
to understand the "outer" comment in the log.

(Not quite sure where on the line this falls for Acked-By vs
Approved-By)

Acked-By: John Baldwin <jhb@FreeBSD.org>
  

Patch

diff --git a/gdb/cp-name-parser.y b/gdb/cp-name-parser.y
index 2f73be35f37..6003ed0b01e 100644
--- a/gdb/cp-name-parser.y
+++ b/gdb/cp-name-parser.y
@@ -48,17 +48,6 @@ 
 #define GDB_YY_REMAP_PREFIX cpname
 #include "yy-remap.h"
 
-/* The components built by the parser are allocated ahead of time,
-   and cached in this structure.  */
-
-#define ALLOC_CHUNK 100
-
-struct demangle_info {
-  int used;
-  struct demangle_info *next;
-  struct demangle_component comps[ALLOC_CHUNK];
-};
-
 %}
 
 %union
@@ -92,7 +81,7 @@  struct cpname_state
 
   const char *lexptr, *prev_lexptr, *error_lexptr, *global_errmsg;
 
-  struct demangle_info *demangle_info;
+  demangle_parse_info *demangle_info;
 
   /* The parse tree created by the parser is stored here after a
      successful parse.  */
@@ -136,23 +125,7 @@  struct cpname_state
 struct demangle_component *
 cpname_state::d_grab ()
 {
-  struct demangle_info *more;
-
-  if (demangle_info->used >= ALLOC_CHUNK)
-    {
-      if (demangle_info->next == NULL)
-	{
-	  more = XNEW (struct demangle_info);
-	  more->next = NULL;
-	  demangle_info->next = more;
-	}
-      else
-	more = demangle_info->next;
-
-      more->used = 0;
-      demangle_info = more;
-    }
-  return &demangle_info->comps[demangle_info->used++];
+  return obstack_new<demangle_component> (&demangle_info->obstack);
 }
 
 /* Flags passed to d_qualify.  */
@@ -1933,20 +1906,6 @@  yyerror (cpname_state *state, const char *msg)
   state->global_errmsg = msg ? msg : "parse error";
 }
 
-/* Allocate a chunk of the components we'll need to build a tree.  We
-   generally allocate too many components, but the extra memory usage
-   doesn't hurt because the trees are temporary and the storage is
-   reused.  More may be allocated later, by d_grab.  */
-static struct demangle_info *
-allocate_info (void)
-{
-  struct demangle_info *info = XNEW (struct demangle_info);
-
-  info->next = NULL;
-  info->used = 0;
-  return info;
-}
-
 /* See cp-support.h.  */
 
 gdb::unique_xmalloc_ptr<char>
@@ -1959,20 +1918,6 @@  cp_comp_to_string (struct demangle_component *result, int estimated_len)
   return gdb::unique_xmalloc_ptr<char> (res);
 }
 
-/* Destructor for demangle_parse_info.  */
-
-demangle_parse_info::~demangle_parse_info ()
-{
-  /* Free any allocated chunks of memory for the parse.  */
-  while (info != NULL)
-    {
-      struct demangle_info *next = info->next;
-
-      free (info);
-      info = next;
-    }
-}
-
 /* Merge the two parse trees given by DEST and SRC.  The parse tree
    in SRC is attached to DEST at the node represented by TARGET.
 
@@ -1986,21 +1931,14 @@  demangle_parse_info::~demangle_parse_info ()
 void
 cp_merge_demangle_parse_infos (struct demangle_parse_info *dest,
 			       struct demangle_component *target,
-			       struct demangle_parse_info *src)
+			       std::unique_ptr<demangle_parse_info> src)
 
 {
-  struct demangle_info *di;
-
   /* Copy the SRC's parse data into DEST.  */
   *target = *src->tree;
-  di = dest->info;
-  while (di->next != NULL)
-    di = di->next;
-  di->next = src->info;
-
-  /* Clear the (pointer to) SRC's parse data so that it is not freed when
-     cp_demangled_parse_info_free is called.  */
-  src->info = NULL;
+
+  /* Make sure SRC is owned by DEST.  */
+  dest->infos.push_back (std::move (src));
 }
 
 /* Convert a demangled name to a demangle_component tree.  On success,
@@ -2018,10 +1956,8 @@  cp_demangled_name_to_comp (const char *demangled_name,
   state.error_lexptr = NULL;
   state.global_errmsg = NULL;
 
-  state.demangle_info = allocate_info ();
-
   auto result = std::make_unique<demangle_parse_info> ();
-  result->info = state.demangle_info;
+  state.demangle_info = result.get ();
 
   if (yyparse (&state))
     {
diff --git a/gdb/cp-support.c b/gdb/cp-support.c
index e6e811ddf50..4c64e4963e4 100644
--- a/gdb/cp-support.c
+++ b/gdb/cp-support.c
@@ -258,7 +258,7 @@  inspect_type (struct demangle_parse_info *info,
 	  if (i != NULL)
 	    {
 	      /* Merge the two trees.  */
-	      cp_merge_demangle_parse_infos (info, ret_comp, i.get ());
+	      cp_merge_demangle_parse_infos (info, ret_comp, std::move (i));
 
 	      /* Replace any newly introduced typedefs -- but not
 		 if the type is anonymous (that would lead to infinite
diff --git a/gdb/cp-support.h b/gdb/cp-support.h
index d0bedcd7b80..765c4435f41 100644
--- a/gdb/cp-support.h
+++ b/gdb/cp-support.h
@@ -58,18 +58,15 @@  struct using_direct;
 
 struct demangle_parse_info
 {
-  demangle_parse_info () = default;
-
-  ~demangle_parse_info ();
-
-  /* The memory used during the parse.  */
-  struct demangle_info *info = nullptr;
-
   /* The result of the parse.  */
   struct demangle_component *tree = nullptr;
 
-  /* Any temporary memory used during typedef replacement.  */
+  /* Any memory used during processing.  */
   auto_obstack obstack;
+
+  /* Any other objects referred to by this object, and whose storage
+     lifetime must be linked.  */
+  std::vector<std::unique_ptr<demangle_parse_info>> infos;
 };
 
 
@@ -182,7 +179,7 @@  extern gdb::unique_xmalloc_ptr<char> cp_comp_to_string
 
 extern void cp_merge_demangle_parse_infos (struct demangle_parse_info *,
 					   struct demangle_component *,
-					   struct demangle_parse_info *);
+					   std::unique_ptr<demangle_parse_info>);
 
 /* The list of "maint cplus" commands.  */