This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: [PATCH] Add configure flag for operator new (std::nothrow)
- From: Marc Glisse <marc dot glisse at inria dot fr>
- To: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- Cc: Daniel Gutson <daniel dot gutson at tallertechnologies dot com>, gcc-patches <gcc-patches at gcc dot gnu dot org>, Aurelio Remonda <aurelio dot remonda at tallertechnologies dot com>, Martin Sebor <msebor at gmail dot com>, libstdc++ <libstdc++ at gcc dot gnu dot org>
- Date: Fri, 6 Nov 2015 08:19:10 +0100 (CET)
- Subject: Re: [PATCH] Add configure flag for operator new (std::nothrow)
- Authentication-results: sourceware.org; auth=none
- References: <1446554133-3090-1-git-send-email-aurelio dot remonda at tallertechnologies dot com> <56391843 dot 1070807 at gmail dot com> <CAF5HaEVeD4G1Mj8GwbpLyZ8V+GWNRAy1=5qbfVHgrZ=GpkbHbg at mail dot gmail dot com> <CAH6eHdQ_8cL8rqsX5u3NrNouf6E2_LtRPQ5F-V-rgTQ3FZTRug at mail dot gmail dot com> <CAF5HaEX5Ju0uXcFP4cLRvh_wWOBaqezbM6WdNUFp+CbzzKNjdg at mail dot gmail dot com> <CAH6eHdRf296VZwDaz8cS742RKEWid76un6-7ogxyzcTyWzj=rw at mail dot gmail dot com> <CAF5HaEVx-Y9JJQ-nnUL3-kuwbCx0_rh+GOrxAFGFYBF2u6=iKw at mail dot gmail dot com> <CAH6eHdQYLqFLkMBJqe=4oSj20ago6F6rGXaDQ8umEEWsBy8k8g at mail dot gmail dot com> <CAF5HaEVF12CH+Z6BssUwmS-TVxGsjfXWdvvGUZ2OHuUhOhhwHA at mail dot gmail dot com> <CAH6eHdREg8F-oexR7LgjjoZ+_ebR8B3bY1W5Ex3UX3WD72Jyfw at mail dot gmail dot com>
- Reply-to: libstdc++ at gcc dot gnu dot org
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