Bug 44647 - std::nothrow and std::bad_alloc are coupled
Summary: std::nothrow and std::bad_alloc are coupled
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 4.4.4
: P3 enhancement
Target Milestone: 4.6.0
Assignee: Jonathan Wakely
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-06-23 14:35 UTC by Sebastian Huber
Modified: 2010-10-05 09:44 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2010-06-23 15:24:06


Attachments
Moves std::bad_alloc implementation into new file bad_alloc.cc (960 bytes, patch)
2010-06-23 15:20 UTC, Sebastian Huber
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Sebastian Huber 2010-06-23 14:35:03 UTC
The definitions of std::nothrow, __new_handler and std::bad_alloc are in the same file.  This is bad since the std::bad_alloc definition pulls in the exception support.  The std::bad_alloc definitions should be placed into a separate file.
Comment 1 Jonathan Wakely 2010-06-23 14:57:07 UTC
(In reply to comment #0)
> The std::bad_alloc definitions should be placed into a
> separate file.

IIUC that wouldn't work, <new> is required to declare bad_alloc, so if bad_alloc was moved to a separate file, the parts of the library which need nothrow_t, new_handler and operator new etc. would still have to include <new>, which would still have to include bad_alloc.

The way to achieve what you want is to put everything except bad_alloc in a separate file, and include that file instead of <new> when bad_alloc is not needed.


Comment 2 Sebastian Huber 2010-06-23 15:02:18 UTC
(In reply to comment #1)
> (In reply to comment #0)
> > The std::bad_alloc definitions should be placed into a
> > separate file.
> 
> IIUC that wouldn't work, <new> is required to declare bad_alloc, so if
> bad_alloc was moved to a separate file, the parts of the library which need
> nothrow_t, new_handler and operator new etc. would still have to include <new>,
> which would still have to include bad_alloc.
> 
> The way to achieve what you want is to put everything except bad_alloc in a
> separate file, and include that file instead of <new> when bad_alloc is not
> needed.
> 

There is nothing wrong with <new>.  The problem is new_handler.cc.

Suppose you use

#include <new>

int *p = new (std::nothrow) int;

this will use the operator new implementation in new_opnt.cc.  This one uses __new_handler which is now coupled with std::bad_alloc.
Comment 3 Jonathan Wakely 2010-06-23 15:14:45 UTC
OK, now I understand what you're suggesting.

Confirmed as a valid enhancement request.
Comment 4 Sebastian Huber 2010-06-23 15:20:43 UTC
Created attachment 20987 [details]
Moves std::bad_alloc implementation into new file bad_alloc.cc

I don't know how to regenerate the Makefile.in.  A simple automake invocation changes a lot in the Makefile.in.
Comment 5 Jonathan Wakely 2010-06-23 15:24:06 UTC
leave it to me (apart from not including the re-generated autoconf file, your patch doesn't update the Copyright dates)
Comment 6 paolo@gcc.gnu.org 2010-10-05 09:42:03 UTC
Author: paolo
Date: Tue Oct  5 09:41:57 2010
New Revision: 164973

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=164973
Log:
2010-10-05  Sebastian Huber  <sebastian.huber@embedded-brains.de>
	    Jonathan Wakely  <jwakely.gcc@gmail.com>

	PR libstdc++/44647
	* libsupc++/Makefile.am (sources): Add bad_alloc.cc.
	* libsupc++/new_handler.cc (bad_alloc): Move to ...
	* libsupc++/bad_alloc.cc (bad_alloc): ... here, new file.
	* libsupc++/Makefile.in: Regenerate.

Added:
    trunk/libstdc++-v3/libsupc++/bad_alloc.cc
Modified:
    trunk/libstdc++-v3/ChangeLog
    trunk/libstdc++-v3/libsupc++/Makefile.am
    trunk/libstdc++-v3/libsupc++/Makefile.in
    trunk/libstdc++-v3/libsupc++/new_handler.cc
Comment 7 Paolo Carlini 2010-10-05 09:43:57 UTC
Done.
Comment 8 Paolo Carlini 2010-10-05 09:44:53 UTC
.