Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Jan Vrany <jan.vrany@labware.com>
To: gdb-patches@sourceware.org
Cc: Jan Vrany <jan.vrany@labware.com>
Subject: [RFC v5 06/18] gdb: use std::vector<> to hold on blocks in struct blockvector
Date: Mon, 23 Jun 2025 17:10:01 +0100	[thread overview]
Message-ID: <20250623161013.650814-7-jan.vrany@labware.com> (raw)
In-Reply-To: <20250623161013.650814-1-jan.vrany@labware.com>

This commit changes internal implementation of struct blockvector to use
std::vector<> rather than flexible array.

The main motivation for this change is to simplify adding blocks to
existing symtab. This feature will be used later by Python API to build
objfiles, compunits and symtabs dynamically (similarly to JIT reader
API).

To do so, this commit

 1. introduces obstack_allocator, an implementation of Allocator
    concept that allocates memory on obstack.
 2. uses std::vector<> with the above allocator to hold on blocks
 3. updates users.

As a side-effect of this change, blockvectors allocated in mdebugread.c
are obstack-allocated rather than xzalloc()ated which seems to be the
correct thing to do. Also, code got simpler.

The downside is higher memory consumption. The size of std::vector with
obstack allocator is 32 bytes (GCC 14) compared to 8 bytes used
currently to store the number of blocks (m_num_blocks). Stopping gdb at
its main(), followed by "maint expand-symtabs" results in 4593
compunit symtabs so in this case the overhead is 24*4593 = 110232 bytes
which I hope is acceptable.

Maybe more concerning is the fact that one may waste obstack memory when
excessively adding blocks. However, blockvectors are not added blocks
after initial allocation at the moment (except in mdebugread.c) so this
is not a problem for existing code. To to mitigate this issue code
allocating may capacity - a number of blocks the blockvector may hold
without reallocating.
---
 gdb/block.c              | 59 ++++++++++++++++++++++++++++++++++++++++
 gdb/block.h              | 56 +++++++++++++++++++++++++++-----------
 gdb/buildsym.c           |  6 +---
 gdb/jit.c                |  8 +-----
 gdb/mdebugread.c         | 32 ++--------------------
 gdbsupport/gdb_obstack.h | 52 +++++++++++++++++++++++++++++++++++
 6 files changed, 156 insertions(+), 57 deletions(-)

diff --git a/gdb/block.c b/gdb/block.c
index 54d768d7255..5e2c121cb3f 100644
--- a/gdb/block.c
+++ b/gdb/block.c
@@ -17,6 +17,7 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
+#include <cstring>
 #include "block.h"
 #include "symtab.h"
 #include "symfile.h"
@@ -844,6 +845,64 @@ bool blockvector::maybe_contains (CORE_ADDR addr) const
   return true;
 }
 
+static bool
+block_ordering_predicate(struct block *b1, struct block *b2)
+{
+  CORE_ADDR start1 = b1->start ();
+  CORE_ADDR start2 = b2->start ();
+
+  if (start1 != start2)
+    return start1 < start2;
+  return (b2->end () < b1->end ());
+}
+
+/* See block.h.  */
+
+void
+blockvector::add_block (struct block *block)
+{
+  if (num_blocks() <= FIRST_LOCAL_BLOCK)
+    {
+      /* No blocks (except global and static block).  */
+      m_blocks.push_back (block);
+    }
+  else
+    {
+      /* blockvector already contains some blocks.  Insert new block
+   to a correct place.  */
+      auto first = m_blocks.begin () + FIRST_LOCAL_BLOCK;
+      auto last = m_blocks.end ();
+
+      auto insert_before = std::upper_bound (first,
+               last,
+               block,
+               block_ordering_predicate);
+
+      m_blocks.insert (insert_before, block);
+    }
+}
+
+/* See block.h.  */
+
+void
+blockvector::sort ()
+{
+  if (num_blocks() > FIRST_LOCAL_BLOCK)
+    {
+      std::sort (blocks ().begin () + FIRST_LOCAL_BLOCK,
+     blocks ().end (),
+     block_ordering_predicate);
+    }
+}
+
+/* See block.h.  */
+
+struct blockvector *
+allocate_blockvector(struct obstack *obstack, int nblocks, int capacity)
+{
+  return new (obstack) blockvector(obstack, nblocks, capacity);
+}
+
 /* Implement 'maint info blocks' command.  If passed an argument then
    print a list of all blocks at the given address.  With no arguments
    then list all blocks at the current address of the current inferior.  */
diff --git a/gdb/block.h b/gdb/block.h
index 4a962eff30e..89ceeb4268b 100644
--- a/gdb/block.h
+++ b/gdb/block.h
@@ -20,6 +20,7 @@
 #ifndef GDB_BLOCK_H
 #define GDB_BLOCK_H
 
+#include <algorithm>
 #include "dictionary.h"
 #include "gdbsupport/array-view.h"
 #include "gdbsupport/next-iterator.h"
@@ -416,41 +417,60 @@ struct global_block : public block
 
 struct blockvector
 {
+  void* operator new (size_t size, struct obstack *obstack)
+  {
+    return obstack_alloc (obstack, size);
+  }
+
+  void* operator new[] (size_t size, struct obstack *obstack)
+  {
+    return obstack_alloc (obstack, size);
+  }
+
+  void operator delete (void *memory) {}
+  void operator delete[] (void *memory) {}
+
+  blockvector (struct obstack *obstack, int nblocks, int capacity = 0)
+    : m_map (nullptr),
+      m_blocks (0, nullptr, obstack_allocator<struct block *> (obstack))
+  {
+    m_blocks.reserve (std::max (nblocks, capacity));
+    m_blocks.resize (nblocks, nullptr);
+  }
+
   /* Return a view on the blocks of this blockvector.  */
   gdb::array_view<struct block *> blocks ()
   {
-    return gdb::array_view<struct block *> (m_blocks, m_num_blocks);
+    return gdb::array_view<struct block *> (m_blocks.data (), m_blocks.size ());
   }
 
   /* Const version of the above.  */
   gdb::array_view<const struct block *const> blocks () const
   {
-    const struct block **blocks = (const struct block **) m_blocks;
-    return gdb::array_view<const struct block *const> (blocks, m_num_blocks);
+    const struct block **blocks = (const struct block **) m_blocks.data ();
+    return gdb::array_view<const struct block *const> (blocks, m_blocks.size ());
   }
 
   /* Return the block at index I.  */
   struct block *block (size_t i)
-  { return this->blocks ()[i]; }
+  { return m_blocks[i]; }
 
   /* Const version of the above.  */
   const struct block *block (size_t i) const
-  { return this->blocks ()[i]; }
+  { return m_blocks[i]; }
 
   /* Set the block at index I.  */
   void set_block (int i, struct block *block)
   { m_blocks[i] = block; }
 
-  /* Set the number of blocks of this blockvector.
-
-     The storage of blocks is done using a flexible array member, so the number
-     of blocks set here must agree with what was effectively allocated.  */
-  void set_num_blocks (int num_blocks)
-  { m_num_blocks = num_blocks; }
+  /* Add BLOCK, making sure blocks are ordered by code-addresses
+     as required. Update global and static block start and end
+     adresses accordingly.  */
+  void add_block(struct block *block);
 
   /* Return the number of blocks in this blockvector.  */
   int num_blocks () const
-  { return m_num_blocks; }
+  { return m_blocks.size (); }
 
   /* Return the global block of this blockvector.  */
   struct global_block *global_block ()
@@ -487,19 +507,23 @@ struct blockvector
      it definitely does not.  */
   bool maybe_contains (CORE_ADDR addr) const;
 
+  void sort ();
+
 private:
   /* An address map mapping addresses to blocks in this blockvector.
      This pointer is zero if the blocks' start and end addresses are
      enough.  */
   addrmap_fixed *m_map;
 
-  /* Number of blocks in the list.  */
-  int m_num_blocks;
-
   /* The blocks themselves.  */
-  struct block *m_blocks[1];
+  std::vector<struct block *, obstack_allocator<struct block *>> m_blocks;
 };
 
+/* Allocate new blockvector for NBLOCKS blocks with enough storage to
+   hold up to CAPACITY blocks.  CAPACITY defaults to NBLOCKS.  */
+struct blockvector *allocate_blockvector(struct obstack *obstack,
+					 int nblocks, int capacity = 0);
+
 extern const struct blockvector *blockvector_for_pc (CORE_ADDR,
 					       const struct block **);
 
diff --git a/gdb/buildsym.c b/gdb/buildsym.c
index 6dc079f29b1..a9ad8a997d3 100644
--- a/gdb/buildsym.c
+++ b/gdb/buildsym.c
@@ -429,10 +429,7 @@ buildsym_compunit::make_blockvector ()
     {
     }
 
-  blockvector = (struct blockvector *)
-    obstack_alloc (&m_objfile->objfile_obstack,
-		   (sizeof (struct blockvector)
-		    + (i - 1) * sizeof (struct block *)));
+  blockvector = allocate_blockvector(&m_objfile->objfile_obstack, i);
 
   /* Copy the blocks into the blockvector.  This is done in reverse
      order, which happens to put the blocks into the proper order
@@ -440,7 +437,6 @@ buildsym_compunit::make_blockvector ()
      each block into the list after its subblocks in order to make
      sure this is true.  */
 
-  blockvector->set_num_blocks (i);
   for (next = m_pending_blocks; next; next = next->next)
     blockvector->set_block (--i, next->block);
 
diff --git a/gdb/jit.c b/gdb/jit.c
index 1944e8afa22..a2512b9b9a9 100644
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -518,7 +518,6 @@ static void
 finalize_symtab (struct gdb_symtab *stab, struct objfile *objfile)
 {
   struct compunit_symtab *cust;
-  size_t blockvector_size;
   CORE_ADDR begin, end;
   struct blockvector *bv;
 
@@ -553,18 +552,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 = allocate_blockvector(&objfile->objfile_obstack, 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 51e1907c683..25139e8ab1e 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 *);
 
@@ -4502,17 +4500,8 @@ 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 *) 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);
-
-  bv->set_block (bv->num_blocks (), b);
-  bv->set_num_blocks (bv->num_blocks () + 1);
+    = const_cast<struct blockvector*> (s->compunit ()->blockvector ());
+  bv->add_block (b);
 }
 
 /* Add a new linenumber entry (LINENO,ADR) to a linevector LT.
@@ -4635,7 +4624,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 = allocate_blockvector(&objfile->objfile_obstack, 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 ());
@@ -4703,21 +4692,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
diff --git a/gdbsupport/gdb_obstack.h b/gdbsupport/gdb_obstack.h
index 755b90767aa..2ac7288226f 100644
--- a/gdbsupport/gdb_obstack.h
+++ b/gdbsupport/gdb_obstack.h
@@ -20,6 +20,8 @@
 #ifndef GDBSUPPORT_GDB_OBSTACK_H
 #define GDBSUPPORT_GDB_OBSTACK_H
 
+#include <limits>
+#include <type_traits>
 #include "obstack.h"
 
 /* Utility macros - wrap obstack alloc into something more robust.  */
@@ -157,4 +159,54 @@ struct allocate_on_obstack
   void operator delete[] (void *memory) {}
 };
 
+/* Implementation of Allocator concept using obstack to
+   allocate memory. This allows standard containers to be
+   used with obstack.  */
+
+template <typename T>
+class obstack_allocator
+{
+public:
+  typedef T value_type;
+
+  obstack_allocator (struct obstack *obstack)
+    : m_obstack(obstack)
+    {}
+
+  template <typename U> constexpr obstack_allocator (const obstack_allocator<U>& allocator) noexcept
+    : m_obstack(allocator.m_obstack)
+  {}
+
+  T* allocate (std::size_t n)
+  {
+    if (n > std::numeric_limits<std::size_t>::max () / sizeof (T))
+      throw std::bad_array_new_length ();
+
+    if (auto p = static_cast<T*> (obstack_alloc (m_obstack, n * sizeof (T))))
+      {
+	return p;
+      }
+
+    throw std::bad_alloc ();
+  }
+
+  void deallocate(T* p, std::size_t n) noexcept
+  {}
+
+private:
+
+  struct obstack *m_obstack;
+};
+
+template <class T, class U>
+bool operator==(const obstack_allocator<T> &t, const obstack_allocator<U> &u)
+{
+  return (std::is_same<T, U>::value_type) && (t.m_obstack == u.m_obstack);
+}
+template <class T, class U>
+bool operator!=(const obstack_allocator<T> &t, const obstack_allocator<U> &u)
+{
+  return ! (t == u);
+}
+
 #endif /* GDBSUPPORT_GDB_OBSTACK_H */
-- 
2.47.2


  parent reply	other threads:[~2025-06-23 16:12 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-23 16:09 [RFC v5 00/19] Add Python "JIT" API Jan Vrany
2025-06-23 16:09 ` [RFC v5 01/18] gdb: introduce expand_symtabs_maybe_overlapping Jan Vrany
2025-06-24 15:22   ` Tom Tromey
2025-06-26 15:05     ` Jan Vraný
2025-06-23 16:09 ` [RFC v5 02/18] gdb: introduce compunit_symtab::maybe_contains Jan Vrany
2025-06-23 16:09 ` [RFC v5 03/18] gdb: update is_addr_in_objfile to support "dynamic" objfiles Jan Vrany
2025-06-23 16:09 ` [RFC v5 04/18] gdb: introduce new function create_function_type Jan Vrany
2025-06-24 15:29   ` Tom Tromey
2025-06-26 11:12     ` Jan Vraný
2025-06-27 14:21       ` Tom Tromey
2025-06-27 14:30         ` Jan Vraný
2025-06-23 16:10 ` [RFC v5 05/18] gdb/python: add function () method to gdb.Type object Jan Vrany
2025-06-24 16:11   ` Tom Tromey
2025-06-26 11:13     ` Jan Vraný
2025-06-23 16:10 ` Jan Vrany [this message]
2025-06-23 16:10 ` [RFC v5 07/18] gdb/python: add gdb.Compunit Jan Vrany
2025-06-23 16:10 ` [RFC v5 08/18] gdb/python: allow instantiation of gdb.Objfile from Python Jan Vrany
2025-06-23 16:10 ` [RFC v5 09/18] gdb/python: add unlink () method to gdb.Objfile object Jan Vrany
2025-06-23 16:10 ` [RFC v5 10/18] gdb/python: allow instantiation of gdb.Compunit from Python Jan Vrany
2025-06-23 16:10 ` [RFC v5 11/18] gdb/python: allow instantiation of gdb.Symtab " Jan Vrany
2025-06-23 16:10 ` [RFC v5 12/18] gdb/python: allow instantiation of gdb.Block " Jan Vrany
2025-06-23 16:10 ` [RFC v5 13/18] gdb/python: allow instantiation of gdb.Symbol " Jan Vrany
2025-06-23 16:10 ` [RFC v5 14/18] gdb/python: add add_symbol () method to gdb.Block Jan Vrany
2025-08-29 14:10   ` Andrew Burgess
2025-08-29 14:14     ` Andrew Burgess
2025-06-23 16:10 ` [RFC v5 15/18] gdb/python: add more attributes to gdb.LinetableEntry objects Jan Vrany
2025-08-29 14:00   ` Andrew Burgess
2025-09-02 11:03     ` Jan Vraný
2025-06-23 16:10 ` [RFC v5 16/18] gdb/python: allow instantiation of gdb.LineTableEntry objects Jan Vrany
2025-06-23 16:10 ` [RFC v5 17/18] gdb/python: allow instantiation of gdb.LineTable objects Jan Vrany
2025-06-23 16:10 ` [RFC v5 18/18] gdb/python: add section in documentation on implementing JIT interface Jan Vrany

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=20250623161013.650814-7-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