From: Jan Vrany <jan.vrany@labware.com>
To: gdb-patches@sourceware.org
Cc: Jan Vrany <jan.vrany@labware.com>
Subject: [PATCH 1/2] gdb: allocate blockvector on heap
Date: Mon, 13 Oct 2025 19:23:17 +0100 [thread overview]
Message-ID: <20251013182318.1045138-2-jan.vrany@labware.com> (raw)
In-Reply-To: <20251013182318.1045138-1-jan.vrany@labware.com>
This patch changes blockvector to be allocated on the heap, using
'new'.
The code is little awkward since blockvector uses flexible array member
to hold on blocks. So in order to make it working, this commit overloads
'new' and 'delete' operators to take the number of blocks as parameter
and allocates/deallocates memory using std::malloc and std::free.
This will go away in following commit that will change blockvector to
use std::vector rather than flexible array member.
---
gdb/block.h | 18 ++++++++++++++++++
gdb/buildsym.c | 5 +----
gdb/jit.c | 8 +-------
gdb/mdebugread.c | 38 +++++++++++---------------------------
4 files changed, 31 insertions(+), 38 deletions(-)
diff --git a/gdb/block.h b/gdb/block.h
index 8a3ea822d04..d1ca41d7482 100644
--- a/gdb/block.h
+++ b/gdb/block.h
@@ -420,6 +420,24 @@ struct global_block : public block
struct blockvector
{
+ void *operator new (size_t size, size_t num_blocks)
+ {
+ size_t size_in_bytes = size + (num_blocks - 1) * (sizeof (struct block *));
+ return std::malloc (size_in_bytes);
+ }
+
+ void operator delete (void *memory)
+ {
+ std::free (memory);
+ }
+
+ blockvector (int nblocks)
+ : m_map (nullptr),
+ m_num_blocks (nblocks)
+ {
+ std::memset (m_blocks, 0, nblocks * sizeof (struct block *));
+ }
+
/* Return a view on the blocks of this blockvector. */
gdb::array_view<struct block *> blocks ()
{
diff --git a/gdb/buildsym.c b/gdb/buildsym.c
index 2a8e95e078b..a61ef68ada1 100644
--- a/gdb/buildsym.c
+++ b/gdb/buildsym.c
@@ -427,10 +427,7 @@ buildsym_compunit::make_blockvector ()
{
}
- blockvector = (struct blockvector *)
- obstack_alloc (&m_objfile->objfile_obstack,
- (sizeof (struct blockvector)
- + (i - 1) * sizeof (struct block *)));
+ blockvector = new (i) struct blockvector (i);
/* Copy the blocks into the blockvector. This is done in reverse
order, which happens to put the blocks into the proper order
diff --git a/gdb/jit.c b/gdb/jit.c
index ca817e85c71..d95fe542a29 100644
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -517,7 +517,6 @@ jit_symtab_close_impl (struct gdb_symbol_callbacks *cb,
static void
finalize_symtab (struct gdb_symtab *stab, struct objfile *objfile)
{
- size_t blockvector_size;
CORE_ADDR begin, end;
struct blockvector *bv;
@@ -554,18 +553,13 @@ finalize_symtab (struct gdb_symtab *stab, struct objfile *objfile)
filetab->set_linetable (new_table);
}
- blockvector_size = (sizeof (struct blockvector)
- + (actual_nblocks - 1) * sizeof (struct block *));
- bv = (struct blockvector *) obstack_alloc (&objfile->objfile_obstack,
- blockvector_size);
+ bv = new (actual_nblocks) blockvector (actual_nblocks);
cust->set_blockvector (bv);
/* At the end of this function, (begin, end) will contain the PC range this
entire blockvector spans. */
- bv->set_map (nullptr);
begin = stab->blocks.front ().begin;
end = stab->blocks.front ().end;
- bv->set_num_blocks (actual_nblocks);
/* First run over all the gdb_block objects, creating a real block
object for each. Simultaneously, keep setting the real_block
diff --git a/gdb/mdebugread.c b/gdb/mdebugread.c
index 9504e5e1670..3cedf0e4fc2 100644
--- a/gdb/mdebugread.c
+++ b/gdb/mdebugread.c
@@ -242,8 +242,6 @@ static struct compunit_symtab *new_symtab (const char *, int, struct objfile *);
static struct linetable *new_linetable (int);
-static struct blockvector *new_bvect (int);
-
static struct type *parse_type (int, union aux_ext *, unsigned int, int *,
int, const char *);
@@ -4501,18 +4499,19 @@ add_block (struct block *b, struct symtab *s)
{
/* Cast away "const", but that's ok because we're building the
symtab and blockvector here. */
- struct blockvector *bv
+ struct blockvector *old_bv
= (struct blockvector *) s->compunit ()->blockvector ();
- bv = (struct blockvector *) xrealloc ((void *) bv,
- (sizeof (struct blockvector)
- + bv->num_blocks ()
- * sizeof (struct block)));
- if (bv != s->compunit ()->blockvector ())
- s->compunit ()->set_blockvector (bv);
+ struct blockvector *new_bv = new (old_bv->num_blocks () + 1)
+ blockvector (old_bv->num_blocks () + 1);
+
+ s->compunit ()->set_blockvector (old_bv);
- bv->set_block (bv->num_blocks (), b);
- bv->set_num_blocks (bv->num_blocks () + 1);
+ for (int i = 0; i < old_bv->num_blocks (); i++)
+ new_bv->set_block (i, old_bv->block (i));
+ new_bv->set_block (new_bv->num_blocks () - 1, b);
+
+ delete old_bv;
}
/* Add a new linenumber entry (LINENO,ADR) to a linevector LT.
@@ -4636,7 +4635,7 @@ new_symtab (const char *name, int maxlines, struct objfile *objfile)
lang = cust->language ();
/* All symtabs must have at least two blocks. */
- bv = new_bvect (2);
+ bv = new (2) blockvector (2);
bv->set_block (GLOBAL_BLOCK, new_block (objfile, NON_FUNCTION_BLOCK, lang));
bv->set_block (STATIC_BLOCK, new_block (objfile, NON_FUNCTION_BLOCK, lang));
bv->static_block ()->set_superblock (bv->global_block ());
@@ -4704,21 +4703,6 @@ shrink_linetable (struct linetable *lt)
* sizeof (lt->item))));
}
-/* Allocate and zero a new blockvector of NBLOCKS blocks. */
-
-static struct blockvector *
-new_bvect (int nblocks)
-{
- struct blockvector *bv;
- int size;
-
- size = sizeof (struct blockvector) + nblocks * sizeof (struct block *);
- bv = (struct blockvector *) xzalloc (size);
- bv->set_num_blocks (nblocks);
-
- return bv;
-}
-
/* Allocate and zero a new block of language LANGUAGE, and set its
BLOCK_MULTIDICT. If function is non-zero, assume the block is
associated to a function, and make sure that the symbols are stored
--
2.51.0
next prev parent reply other threads:[~2025-10-13 18:25 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-13 18:23 [PATCH 0/2] use std::vector<> to hold on blocks in struct blockvector Jan Vrany
2025-10-13 18:23 ` Jan Vrany [this message]
2025-10-14 18:41 ` [PATCH 1/2] gdb: allocate blockvector on heap Simon Marchi
2025-10-13 18:23 ` [PATCH 2/2] gdb: use std::vector<> to hold on blocks in struct blockvector Jan Vrany
2025-10-14 15:00 ` Andrew Burgess
2025-10-14 19:41 ` Simon Marchi
2025-10-15 21:34 ` Jan Vraný
2025-10-14 19:29 ` Simon Marchi
2025-10-14 20:05 ` Tom Tromey
2025-10-14 20:19 ` Simon Marchi
2025-10-14 20:40 ` Jan Vraný
2025-10-15 2:00 ` Simon Marchi
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=20251013182318.1045138-2-jan.vrany@labware.com \
--to=jan.vrany@labware.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