public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
* [binutils-gdb] Require trivial destructor in allocate_on_obstack
@ 2024-03-21 18:29 Tom Tromey
  0 siblings, 0 replies; only message in thread
From: Tom Tromey @ 2024-03-21 18:29 UTC (permalink / raw)
  To: gdb-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=7f032bbedf3e66f6695d4df0d149c2e8033224da

commit 7f032bbedf3e66f6695d4df0d149c2e8033224da
Author: Tom Tromey <tromey@adacore.com>
Date:   Mon Feb 26 10:19:07 2024 -0700

    Require trivial destructor in allocate_on_obstack
    
    This patch makes allocate_on_obstack a little bit safer, by enforcing
    the rule that objects allocated on an obstack must have a trivial
    destructor.
    
    The static assert is done in a method -- doing it inside the class
    itself won't work because the class is incomplete at that point.

Diff:
---
 gdb/addrmap.h             | 2 +-
 gdb/block.c               | 2 +-
 gdb/block.h               | 2 +-
 gdb/dwarf2/cooked-index.h | 2 +-
 gdb/gdbtypes.h            | 4 ++--
 gdb/symtab.h              | 2 +-
 gdbsupport/gdb_obstack.h  | 6 +++++-
 7 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/gdb/addrmap.h b/gdb/addrmap.h
index 0d61046cebd..55dea36b877 100644
--- a/gdb/addrmap.h
+++ b/gdb/addrmap.h
@@ -81,7 +81,7 @@ struct addrmap_mutable;
 
 /* Fixed address maps.  */
 struct addrmap_fixed : public addrmap,
-		       public allocate_on_obstack
+		       public allocate_on_obstack<addrmap_fixed>
 {
 public:
 
diff --git a/gdb/block.c b/gdb/block.c
index 079053cd79e..6d0d33fa85e 100644
--- a/gdb/block.c
+++ b/gdb/block.c
@@ -31,7 +31,7 @@
    C++ files, namely using declarations and the current namespace in
    scope.  */
 
-struct block_namespace_info : public allocate_on_obstack
+struct block_namespace_info : public allocate_on_obstack<block_namespace_info>
 {
   const char *scope = nullptr;
   struct using_direct *using_decl = nullptr;
diff --git a/gdb/block.h b/gdb/block.h
index 4c29f6599ef..ae676c4ba67 100644
--- a/gdb/block.h
+++ b/gdb/block.h
@@ -105,7 +105,7 @@ struct blockranges
    This implies that within the body of one function
    the blocks appear in the order of a depth-first tree walk.  */
 
-struct block : public allocate_on_obstack
+struct block : public allocate_on_obstack<block>
 {
   /* Return this block's start address.  */
   CORE_ADDR start () const
diff --git a/gdb/dwarf2/cooked-index.h b/gdb/dwarf2/cooked-index.h
index 1c8fb8fa28f..e73bd7c73c3 100644
--- a/gdb/dwarf2/cooked-index.h
+++ b/gdb/dwarf2/cooked-index.h
@@ -104,7 +104,7 @@ extern bool language_requires_canonicalization (enum language lang);
    This is an "open" class and the members are all directly
    accessible.  It is read-only after the index has been fully read
    and processed.  */
-struct cooked_index_entry : public allocate_on_obstack
+struct cooked_index_entry : public allocate_on_obstack<cooked_index_entry>
 {
   cooked_index_entry (sect_offset die_offset_, enum dwarf_tag tag_,
 		      cooked_index_flag flags_,
diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h
index 435a040544a..f80bd7e071a 100644
--- a/gdb/gdbtypes.h
+++ b/gdb/gdbtypes.h
@@ -211,7 +211,7 @@ struct variant_part;
    control other variant parts as well.  This struct corresponds to
    DW_TAG_variant in DWARF.  */
 
-struct variant : allocate_on_obstack
+struct variant : allocate_on_obstack<variant>
 {
   /* * The discriminant ranges for this variant.  */
   gdb::array_view<discriminant_range> discriminants;
@@ -243,7 +243,7 @@ struct variant : allocate_on_obstack
    and holds an array of variants.  This struct corresponds to
    DW_TAG_variant_part in DWARF.  */
 
-struct variant_part : allocate_on_obstack
+struct variant_part : allocate_on_obstack<variant_part>
 {
   /* * The index of the discriminant field in the outer type.  This is
      an index into the type's array of fields.  If this is -1, there
diff --git a/gdb/symtab.h b/gdb/symtab.h
index e23eaed43ec..bf9a3cfb79f 100644
--- a/gdb/symtab.h
+++ b/gdb/symtab.h
@@ -1244,7 +1244,7 @@ extern gdb::array_view<const struct symbol_impl> symbol_impls;
 
 /* This structure is space critical.  See space comments at the top.  */
 
-struct symbol : public general_symbol_info, public allocate_on_obstack
+struct symbol : public general_symbol_info, public allocate_on_obstack<symbol>
 {
   symbol ()
     /* Class-initialization of bitfields is only allowed in C++20.  */
diff --git a/gdbsupport/gdb_obstack.h b/gdbsupport/gdb_obstack.h
index 20f888660f9..7b3bb05bc00 100644
--- a/gdbsupport/gdb_obstack.h
+++ b/gdbsupport/gdb_obstack.h
@@ -133,14 +133,18 @@ struct auto_obstack : obstack
   { obstack_free (this, obstack_base (this)); }
 };
 
-/* Objects are allocated on obstack instead of heap.  */
+/* Objects are allocated on obstack instead of heap.  This is a mixin
+   that uses CRTP to ensure that the type in question is trivially
+   destructible.  */
 
+template<typename T>
 struct allocate_on_obstack
 {
   allocate_on_obstack () = default;
 
   void* operator new (size_t size, struct obstack *obstack)
   {
+    static_assert (IsFreeable<T>::value);
     return obstack_alloc (obstack, size);
   }

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-03-21 18:29 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-21 18:29 [binutils-gdb] Require trivial destructor in allocate_on_obstack Tom Tromey

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).