From: Simon Marchi <simon.marchi@polymtl.ca>
To: Tom Tromey <tom@tromey.com>
Cc: Simon Marchi <simon.marchi@ericsson.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH 1/2] Introduce obstack_new, poison other "typed" obstack functions
Date: Thu, 26 Apr 2018 02:58:00 -0000 [thread overview]
Message-ID: <129280589d474492d52e33e7997b47a7@polymtl.ca> (raw)
In-Reply-To: <87sh7i7w2i.fsf@tromey.com>
Hi Tom,
Thanks for taking a look.
On 2018-04-25 18:36, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi <simon.marchi@ericsson.com> writes:
>
> Simon> This patch introduces a utility to make this pattern simpler:
> Simon> foo *f = obstack_new<foo> ();
>
> What about just having those types that can use this inherit from
> allocate_on_obstack? Then you can write:
>
> foo *f = new (obstack) foo ();
I thought about this. The downside I see is that it forces new to
allocate on an obstack. What if you want to use the standard new for
the same type in another context? Maybe this doesn't happen in practice
though.
> It would be nice if we could have a global operator new that does this,
> but I don't think it is possible to have one that is limited to
> is_trivially_constructible classes.
That goes beyond my C++ knowledge.
> Maybe is_trivially_destructible is also needed somehow since an obstack
> isn't going to run those destructors. Not sure how to manage this
> either, right now allocate_on_obstack just assumes you know what you're
> up to.
I remember you having some back and forth with Yao about this. Here's
my take on it. It is possible to allocate objects that need to be
destroyed on obstacks, the important thing is to call the destructor
manually at some point before the obstack is freed. It's done for
mapped_index right now, for example. You just can't allocate such an
object on an obstack and forget about it, assuming it will be freed by
magic (it's just like when you "new" something, you have to keep track
of it to destroy it at some point). When doing this, you obviously lose
one nice feature of the obstack, not having to care about deallocation.
So if that was the main reason an obstack is used, you might as well
reconsider using an obstack at all [1]. But if the obstack is used for
the more efficient memory allocation, then it would still make sense to
use an obstack and track the objects separately so that they can be
destroyed properly. What we need is (not sure this is realistic):
everywhere we allocate an object on an obstack and don't keep track of
it for further destruction, have a static_assert there that the type
should be trivially destructible. If you happen to make said type non
trivially destructible, you'll get an error and realize "oh right, the
objects created here are never destroyed".
Or maybe there are better C++ ways of getting the same advantages as
with an obstack (efficient allocation for a bunch of objects
allocated/freed at the same time, data locality)?
[1] For example, is there a reason why mapped_index is allocated on the
objfile obstack, and not newed just like mapped_debug_names?
Simon
next prev parent reply other threads:[~2018-04-26 2:58 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-04-25 19:40 Simon Marchi
2018-04-25 19:40 ` [PATCH 2/2] Use XOBNEW when possible Simon Marchi
2018-04-25 22:27 ` Tom Tromey
2018-04-26 2:59 ` Simon Marchi
2018-04-25 22:36 ` [PATCH 1/2] Introduce obstack_new, poison other "typed" obstack functions Tom Tromey
2018-04-26 2:58 ` Simon Marchi [this message]
2018-04-26 17:42 ` Tom Tromey
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=129280589d474492d52e33e7997b47a7@polymtl.ca \
--to=simon.marchi@polymtl.ca \
--cc=gdb-patches@sourceware.org \
--cc=simon.marchi@ericsson.com \
--cc=tom@tromey.com \
/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