[PATCH] Check DECL_CONTEXT of new/delete operators.

Jonathan Wakely jwakely.gcc@gmail.com
Tue Apr 7 08:26:46 GMT 2020


On Mon, 6 Apr 2020 at 13:45, Nathan Sidwell <nathan@acm.org> wrote:
>
> On 4/6/20 4:34 AM, Martin Liška wrote:
>
> >
> > May I please ping Jason, Nathan and Jonathan who can help us here?
>
> On IRC Martin clarified the question as essentially 'how do you pair up
> operator new and operator delete calls?' (so you may delete them if the
> object is not used).
>
> I am not sure you're permitted to remove those calls in general.  All I
> can find is [expr.new]/12
> 'An implementation is allowed to omit a call to a replaceable global
> allocation function (17.6.2.1, 17.6.2.2). When it does so, the storage
> is instead provided by the implementation or provided by extending the
> allocation of another new-expression.'

At https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94295#c6 Richard Smith
summarised the rules as "new-expressions, like std::allocator, may
obtain storage by calling 'operator new', but it's unspecified how
often it's called and with what arguments."

So if our optimisation is removing the calls to base::operator new and
base::operator delete, but not the B::operator new call, then it seems
to be working at the wrong level. It should be eliding any calls to
operator new and operator delete at the point of the new-expression
and delete-expression, not leaving one call to operator new present
and then removing another one (which leaves the call "partially
removed").


More information about the Gcc-patches mailing list