Re: [PATCH 4/7] Class-fy partial_die_info

Yao Qi
2018-01-26T17:25:39Z

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?

--
Yao (齐尧)
From 9f20b2690cd1c83a3bdcd41ea59f07dfef0da522 Mon Sep 17 00:00:00 2001
From: Yao Qi <yao.qi@linaro.org>
Date: Fri, 26 Jan 2018 11:57:34 +0000
Subject: [PATCH] New class allocate_on_obstack

This patch adds a new class allocate_on_obstack, and let dwarf2_per_objfile
inherit it, so that dwarf2_per_objfile is automatically allocated on
obstack, and "delete dwarf2_per_objfile" doesn't de-allocate any space.

gdb:

2018-01-26 Yao Qi <yao.qi@linaro.org>

* dwarf2read.c (dwarf2_per_objfile): Inherit allocate_on_obstack.
(dwarf2_free_objfile): Use delete.
* gdb_obstack.h (allocate_on_obstack): New.

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