[4/7] Class-fy partial_die_info

Message ID 86lggkcz1x.fsf@gmail.com
State New, archived
Headers

Commit Message

Yao Qi Jan. 26, 2018, 5:25 p.m. UTC
  Tom Tromey <tom@tromey.com> writes:

> I wonder if it would make sense to have an "operator new" implementation
> that allocates directly on an obstack.  It could use

Yes, we can have an "operator new",

> std::is_trivially_destructible to enforce the rule that objects on an
> obstack can't really be destroyed.  This would eliminate the separate
> XOBNEW, which is maybe a potential source of errors; and would also make
> it harder to accidentally add a destructor to objects allocated this way

but why dtor must be trivial?  We can have "operator new" and "operator
delete", the former allocate spaces on obstack and the latter doesn't
de-allocate space.  It doesn't matter dtor is trivial or not.  I may
miss something here.

Further, I think IWBN to have a  class which has new/delete operator,
and other classes can inherit it.  What do you think the patch below?
  

Comments

Tom Tromey Jan. 26, 2018, 8:55 p.m. UTC | #1
>> std::is_trivially_destructible to enforce the rule that objects on an
>> obstack can't really be destroyed.  This would eliminate the separate
>> XOBNEW, which is maybe a potential source of errors; and would also make
>> it harder to accidentally add a destructor to objects allocated this way

Yao> but why dtor must be trivial?  We can have "operator new" and "operator
Yao> delete", the former allocate spaces on obstack and the latter doesn't
Yao> de-allocate space.  It doesn't matter dtor is trivial or not.  I may
Yao> miss something here.

My thinking was that if something is allocated on an obstack, then
presumably the destructor will never be run.  So, it's best to avoid
confusion by requiring a trivial destructor.

I suppose it would be possible to track objects and destroy them, but if
that's done, then it sort of eliminates the point of an obstack -- it
would be just as convenient at that point to use the ordinary new.

Yao> Further, I think IWBN to have a  class which has new/delete operator,
Yao> and other classes can inherit it.  What do you think the patch below?

I suppose this makes sense if you know that all objects of a given type
must be allocated on an obstack.

Tom
  

Patch

diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c
index 96026a8..5c45bdf 100644
--- a/gdb/dwarf2read.c
+++ b/gdb/dwarf2read.c
@@ -380,7 +380,7 @@  struct tu_stats
 /* Collection of data recorded per objfile.
    This hangs off of dwarf2_objfile_data_key.  */
 
-struct dwarf2_per_objfile
+struct dwarf2_per_objfile : public allocate_on_obstack
 {
   /* Construct a dwarf2_per_objfile for OBJFILE.  NAMES points to the
      dwarf2 section names, or is NULL if the standard ELF names are
@@ -2467,10 +2467,9 @@  dwarf2_has_info (struct objfile *objfile,
   if (dwarf2_per_objfile == NULL)
     {
       /* Initialize per-objfile state.  */
-      struct dwarf2_per_objfile *data
-	= XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
-
-      dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
+      dwarf2_per_objfile
+	= new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
+								     names);
       set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
     }
   return (!dwarf2_per_objfile->info.is_virtual
@@ -25168,10 +25167,7 @@  dwarf2_free_objfile (struct objfile *objfile)
   struct dwarf2_per_objfile *dwarf2_per_objfile
     = get_dwarf2_per_objfile (objfile);
 
-  if (dwarf2_per_objfile == NULL)
-    return;
-
-  dwarf2_per_objfile->~dwarf2_per_objfile ();
+  delete dwarf2_per_objfile;
 }
 
 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
diff --git a/gdb/gdb_obstack.h b/gdb/gdb_obstack.h
index 12a90c3..b239ce6 100644
--- a/gdb/gdb_obstack.h
+++ b/gdb/gdb_obstack.h
@@ -78,4 +78,18 @@  struct auto_obstack : obstack
   { obstack_free (this, obstack_base (this)); }
 };
 
+/* Objects are allocated on obstack instead of heap.  */
+
+struct allocate_on_obstack
+{
+  void* operator new (size_t size, struct obstack *obstack)
+  {
+    return obstack_alloc (obstack, size);
+  }
+
+  void operator delete (void* memory)
+  {
+  }
+};
+
 #endif