public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Daniel Gutson <daniel.gutson@tallertechnologies.com>
To: Martin Sebor <msebor@gmail.com>
Cc: Aurelio Remonda <aurelio.remonda@tallertechnologies.com>,
	libstdc++@gcc.gnu.org,
		"gcc-patches@gcc.gnu.org" <gcc-patches@gcc.gnu.org>,
	daniel.gutston@tallertechnologies.com,
		Jonathan Wakely <jwakely.gcc@gmail.com>
Subject: Re: [PATCH] Add configure flag for operator new (std::nothrow)
Date: Tue, 03 Nov 2015 20:41:00 -0000	[thread overview]
Message-ID: <CAF5HaEVeD4G1Mj8GwbpLyZ8V+GWNRAy1=5qbfVHgrZ=GpkbHbg@mail.gmail.com> (raw)
In-Reply-To: <56391843.1070807@gmail.com>

On Tue, Nov 3, 2015 at 5:25 PM, Martin Sebor <msebor@gmail.com> wrote:
> On 11/03/2015 05:35 AM, Aurelio Remonda wrote:
>>
>> Currently, whenever operator new (std::nothrow) fails to allocate memory,
>> it'll
>> check if there is a new-handler function available. If there is, it'll
>> call
>> the handler and then try to allocate again. Otherwise, it'll return a null
>> pointer.
>>
>> This retrying behavior may not always be desirable. If the handler cannot
>> fix
>> the memory allocation issue, we may end up being stuck in an infinite
>> loop.
>> Whereas returning nullptr may be a valid alternative to keep calling the
>> new_handler.
>> The workaround to end the loop, we would have to call
>> std::set_new_handler(nullptr)
>> from within the handler itself, which gets complicated if the handler has
>> to be
>> re-setted afterwards.
>>
>> This patch adds the new_nothrow_no_retry configuration flag, which, if
>> enabled,
>> will change the retrying behavior of operator new (std::nothrow) so that
>> it only calls
>> the handler once when it fails to allocate memory and the return nullptr.
>
>
> The purpose of the loop is to give the new handler an opportunity
> to free up enough memory to let the allocation succeed. Since the
> handler doesn't get passed the size of the request it has no easy
> way of determining how much memory to free. The loop lets it free
> up increasingly more memory. If it can't free up any memory it is
> expected/required to either indicate failure by throwing bad_alloc

Since this is a nothrow new, we thought that probably the system
might not be exceptions-friendly (such as certain embedded systems),
so we wanted to provide the new_handler the ability to do something else
other than trying to allocate memory and keep the function iterating.
In fact, our idea is that since the nothrow new can indeed return nullptr,
let the new_handler do something else and leave the no-memory-consequence
to the caller.
This new flag enables just that.

> or terminate the process. It's not allowed to return otherwise.
>
> Besides violating the requirement of the C++ standard, replacing

Could you please point us to the relevant section where this behavior
is enforced? We couldn't find it so far.

> the loop with an if statement would disable that aspect of the
> feature for the whole system (or for all processes that link with
> the libstdc++ that was configured this way). It would effectively
> be imposing a system-wide policy without providing a mechanism
> for correctly written programs to opt out. In addition, as
> a configuration option, it could not be easily tested. I would
> therefore advise against making such a change.
>
> Martin



-- 

Daniel F. Gutson
Chief Engineering Officer, SPD

San Lorenzo 47, 3rd Floor, Office 5
Córdoba, Argentina

Phone:   +54 351 4217888 / +54 351 4218211
Skype:    dgutson
LinkedIn: http://ar.linkedin.com/in/danielgutson

  reply	other threads:[~2015-11-03 20:41 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 [this message]
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
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='CAF5HaEVeD4G1Mj8GwbpLyZ8V+GWNRAy1=5qbfVHgrZ=GpkbHbg@mail.gmail.com' \
    --to=daniel.gutson@tallertechnologies.com \
    --cc=aurelio.remonda@tallertechnologies.com \
    --cc=daniel.gutston@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).