From: Jonathan Wakely <jwakely.gcc@gmail.com>
To: Martin Sebor <msebor@gmail.com>
Cc: Aurelio Remonda <aurelio.remonda@tallertechnologies.com>,
"libstdc++" <libstdc++@gcc.gnu.org>,
gcc-patches <gcc-patches@gcc.gnu.org>,
daniel.gutston@tallertechnologies.com
Subject: Re: [PATCH] Add configure flag for operator new (std::nothrow)
Date: Wed, 04 Nov 2015 06:15:00 -0000 [thread overview]
Message-ID: <CAH6eHdTfmaDrWRo3FK+0sd8OmOv9JZ=P=e42wmQqEjv5rXfbVg@mail.gmail.com> (raw)
In-Reply-To: <56391843.1070807@gmail.com>
On 4 November 2015 at 01:55, Martin Sebor 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
> or terminate the process. It's not allowed to return otherwise.
>
> Besides violating the requirement of the C++ standard, replacing
> 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.
I share your concerns, but I'm also sympathetic to the changes that
the Taller Technologies team are trying to make, to allow libstdc++ to
be more useful in exception-free systems.
At the very least the patch to doc/xml/manual/configure.xml must
document that this option enables behaviour that violates the standard
and so produces a non-conforming implementation. It should probably
also explain how to use the resulting implementation, so that users on
embedded systems who might want to enable this know how to write a
suitable new-handler.
next prev parent reply other threads:[~2015-11-04 6:15 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
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 [this message]
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='CAH6eHdTfmaDrWRo3FK+0sd8OmOv9JZ=P=e42wmQqEjv5rXfbVg@mail.gmail.com' \
--to=jwakely.gcc@gmail.com \
--cc=aurelio.remonda@tallertechnologies.com \
--cc=daniel.gutston@tallertechnologies.com \
--cc=gcc-patches@gcc.gnu.org \
--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).