public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Marc Glisse <marc.glisse@inria.fr>
To: Jonathan Wakely <jwakely.gcc@gmail.com>
Cc: Daniel Gutson <daniel.gutson@tallertechnologies.com>,
	    gcc-patches <gcc-patches@gcc.gnu.org>,
	    Aurelio Remonda <aurelio.remonda@tallertechnologies.com>,
	    Martin Sebor <msebor@gmail.com>,
	libstdc++ <libstdc++@gcc.gnu.org>
Subject: Re: [PATCH] Add configure flag for operator new (std::nothrow)
Date: Fri, 06 Nov 2015 07:19:00 -0000	[thread overview]
Message-ID: <alpine.DEB.2.20.1511060800040.2358@laptop-mg.saclay.inria.fr> (raw)
In-Reply-To: <CAH6eHdREg8F-oexR7LgjjoZ+_ebR8B3bY1W5Ex3UX3WD72Jyfw@mail.gmail.com>

On Fri, 6 Nov 2015, Jonathan Wakely wrote:

> On 6 November 2015 at 09:02, Daniel Gutson
> <daniel.gutson@tallertechnologies.com> wrote:
>>
>> El 5/11/2015 22:56, "Jonathan Wakely" <jwakely.gcc@gmail.com> escribió:
>>>
>>> It can just call malloc, and the replacement operator delete can call
>>> free.
>>
>> It can but the actual code of operator new is optimized and more complex
>> (for example by using intrinsics). The user might not want to deal with the
>> STL implementation internals.

The code is more complex for reasons that might not apply to your code.
If you are replacing new, you don't need to look at a new handler, you
can write directly whatever code you want. If you know how malloc(0)
behaves on your platform (or are confident that you will never call new
with size 0), you can skip the initial test.

IIRC I introduced the __builtin_expect in that code, but I personnally
often write the following in my code, assuming that allocation will
never fail:
inline void* operator new(std::size_t n){return malloc(n);}
inline void operator delete(void*p)noexcept{free(p);}
(same with nothrow and arrays)

(technically illegal because of 'inline' but I don't care)

> By this argument users should never replace operator new at all. I
> don't find that convincing.
>
> If they don't want to deal with implementation details then they don't
> have to. The default implementations are optimized because they are
> used by everyone and it would be silly to ship them without making
> easy optimisations, but that doesn't mean that users have to do the
> same thing in their own replacement allocation functions. Using
> __builtin_expect makes a small difference when looping on the result
> of malloc, but if you were to provide a replacement that doesn't loop
> then the difference for a single branch is not very significant.

Actually, it is optimized for the case where it does *not* loop (gcc by
default optimizes by assuming that loops do loop).

-- 
Marc Glisse

  reply	other threads:[~2015-11-06  7:19 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-03 12:35 Aurelio Remonda
2015-11-03 13:26 ` Paolo Carlini
2015-11-03 14:06   ` Aurelio Remonda
2015-11-03 14:12     ` Andreas Schwab
2015-11-03 17:17   ` Aurelio Remonda
2015-11-03 20:25 ` Martin Sebor
2015-11-03 20:41   ` Daniel Gutson
2015-11-03 21:10     ` Martin Sebor
2015-11-03 23:09       ` Mike Stump
2015-11-03 23:50         ` Martin Sebor
2015-11-04  8:07           ` Marc Glisse
2015-11-04  6:20     ` Jonathan Wakely
2015-11-04  6:32       ` Jonathan Wakely
2015-11-05 15:22       ` Daniel Gutson
2015-11-05 17:11         ` Jonathan Wakely
2015-11-05 18:01           ` Daniel Gutson
2015-11-06  1:56             ` Jonathan Wakely
     [not found]               ` <CAF5HaEVF12CH+Z6BssUwmS-TVxGsjfXWdvvGUZ2OHuUhOhhwHA@mail.gmail.com>
2015-11-06  4:24                 ` Jonathan Wakely
2015-11-06  7:19                   ` Marc Glisse [this message]
2015-11-06  9:59               ` Pedro Alves
2015-11-10 13:10                 ` Jonathan Wakely
2015-11-16 18:56                   ` Pedro Alves
2016-02-19 21:56                     ` Daniel Gutson
2016-02-22 15:58                       ` Jonathan Wakely
2016-02-19 21:45                   ` Daniel Gutson
2015-11-17 12:38         ` Sebastian Huber
2015-11-04  6:15   ` Jonathan Wakely
2015-11-04 14:52     ` Martin Sebor

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=alpine.DEB.2.20.1511060800040.2358@laptop-mg.saclay.inria.fr \
    --to=marc.glisse@inria.fr \
    --cc=aurelio.remonda@tallertechnologies.com \
    --cc=daniel.gutson@tallertechnologies.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jwakely.gcc@gmail.com \
    --cc=libstdc++@gcc.gnu.org \
    --cc=msebor@gmail.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).