From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 04/10] Change storage of demangle_component
Date: Sun, 21 Apr 2024 11:00:04 -0600 [thread overview]
Message-ID: <20240421-canon-fixes-v1-4-4dc4791d270d@tromey.com> (raw)
In-Reply-To: <20240421-canon-fixes-v1-0-4dc4791d270d@tromey.com>
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(-)
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. */
--
2.44.0
next prev parent reply other threads:[~2024-04-21 17:00 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-21 17:00 [PATCH 00/10] Fix some C++ name canonicalizer problems Tom Tromey
2024-04-21 17:00 ` [PATCH 01/10] Remove test code from cp-name-parser.y Tom Tromey
2024-04-22 17:11 ` John Baldwin
2024-04-21 17:00 ` [PATCH 02/10] Allow initialization functions in .y files Tom Tromey
2024-04-21 17:00 ` [PATCH 03/10] Clean up demangle_parse_info Tom Tromey
2024-04-22 17:12 ` John Baldwin
2024-04-21 17:00 ` Tom Tromey [this message]
2024-04-22 17:17 ` [PATCH 04/10] Change storage of demangle_component John Baldwin
2024-04-21 17:00 ` [PATCH 05/10] Fix C++ name canonicalizations of character literals Tom Tromey
2024-04-22 17:19 ` John Baldwin
2024-04-21 17:00 ` [PATCH 06/10] Remove some unnecessary allocations from cpname_state::parse_number Tom Tromey
2024-04-22 17:20 ` John Baldwin
2024-04-21 17:00 ` [PATCH 07/10] Fix C++ canonicalization of hex literals Tom Tromey
2024-04-22 17:22 ` John Baldwin
2024-04-21 17:00 ` [PATCH 08/10] Implement C++14 numeric separators Tom Tromey
2024-04-22 17:29 ` John Baldwin
2024-04-24 21:42 ` Tom Tromey
2024-04-30 16:33 ` John Baldwin
2024-04-21 17:00 ` [PATCH 09/10] Allow function types as template parameters in name canonicalizer Tom Tromey
2024-04-22 17:30 ` John Baldwin
2024-04-21 17:00 ` [PATCH 10/10] Add spaceship operator to cp-name-parser.y Tom Tromey
2024-04-22 17:31 ` John Baldwin
2024-05-14 19:29 ` [PATCH 00/10] Fix some C++ name canonicalizer problems Tom Tromey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240421-canon-fixes-v1-4-4dc4791d270d@tromey.com \
--to=tom@tromey.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).