diff mbox

Ping: [PATCH] Install error handler for out-of-memory when using STL containers

Message ID 508D4F1F.6040301@physik.uni-muenchen.de
State New
Headers show

Commit Message

Tobias Schlüter Oct. 28, 2012, 3:28 p.m. UTC
Ping.  This issue stands in the way of a very simple solution of PR 
fortran/51727.  I've re-attached the patch for your convenience.

On 15 Oct 2012 at 22:51:05 +0200 Tobias Schlüter wrote:
> The attached patch adds out-of-memory diagnostics for code using STL
> containers by using set_new_handler. Since the intended allocation
> size is not available to a new_handler, I had to forego a more
> detailed error message such as the one from xmalloc_failed().
> fatal_error() and abort() don't give a meaningful location when the
> new_handler is called, so I chose to put together the error message
> manually as is done in xmalloc_failed(). I would have found it more
> appealing to have operator new call xmalloc() unless a custom
> allocator is given, but I don't think there's a standard way of doing
> this.
>
> Built and tested on the C and Fortran testsuites. Ok for trunk?

Best regards,
- Tobi

2012-10-15  Tobias Schlüter  <tobi@gcc.gnu.org>

	* toplev.c: Add '#include <new>'.
	(cxx_out_of_memory): New function.
	(general_init): Install cxx_out_of_memory as handler for
	out-of-memory condition.
2012-10-15  Tobias Schlüter  <tobi@gcc.gnu.org>

	* toplev.c: Add '#include <new>'.
	(cxx_out_of_memory): New function.
	(general_init): Install cxx_out_of_memory as handler for
	out-of-memory condition.

Comments

Paul Richard Thomas Oct. 29, 2012, 9:02 a.m. UTC | #1
Hi Tobi,

It looks straightforward to me but I am not convinced that a fortran
maintainer should be giving the green light on this :-)

Cheers

Paul

On 28 October 2012 16:28, Tobias Schlüter
<tobias.schlueter@physik.uni-muenchen.de> wrote:
>
> Ping.  This issue stands in the way of a very simple solution of PR
> fortran/51727.  I've re-attached the patch for your convenience.
>
> On 15 Oct 2012 at 22:51:05 +0200 Tobias Schlüter wrote:
>>
>> The attached patch adds out-of-memory diagnostics for code using STL
>> containers by using set_new_handler. Since the intended allocation
>> size is not available to a new_handler, I had to forego a more
>> detailed error message such as the one from xmalloc_failed().
>> fatal_error() and abort() don't give a meaningful location when the
>> new_handler is called, so I chose to put together the error message
>> manually as is done in xmalloc_failed(). I would have found it more
>> appealing to have operator new call xmalloc() unless a custom
>> allocator is given, but I don't think there's a standard way of doing
>> this.
>>
>> Built and tested on the C and Fortran testsuites. Ok for trunk?
>
>
> Best regards,
> - Tobi
>
> 2012-10-15  Tobias Schlüter  <tobi@gcc.gnu.org>
>
>         * toplev.c: Add '#include <new>'.
>         (cxx_out_of_memory): New function.
>         (general_init): Install cxx_out_of_memory as handler for
>         out-of-memory condition.
Mike Stump Oct. 29, 2012, 5:08 p.m. UTC | #2
On Oct 29, 2012, at 2:02 AM, Paul Richard Thomas <paul.richard.thomas@gmail.com> wrote:
> It looks straightforward to me but I am not convinced that a fortran
> maintainer should be giving the green light on this :-)

Well, seems reasonable to me as well…  I'd let Diego or Jason approve it.  :-)
Tobias Schlüter Nov. 3, 2012, 9:17 a.m. UTC | #3
Hi,

any comments appreciated, also if you think it's unnecessary.  This 
issue came up when I submitted a patch for PR fortran/51727 that uses 
std::map.  If this isn't approved within the next few days, I'll fix PR 
fortran/51727 in a way that doesn't use C++ and thus needs more 
boilerplate code (see 
<http://gcc.gnu.org/ml/gcc-patches/2012-10/msg01284.html> for context.)

I'm CCing Ian as the last person who approved a patch to toplev.c and 
also Jakub as a global write maintainer and as the person who brought up 
this subject.

Link to patch: <http://gcc.gnu.org/ml/gcc-patches/2012-10/msg02515.html>

On 2012-10-28 16:28, Tobias Schlüter wrote:
>
> Ping.  This issue stands in the way of a very simple solution of PR
> fortran/51727.  I've re-attached the patch for your convenience.
>
> On 15 Oct 2012 at 22:51:05 +0200 Tobias Schlüter wrote:
>> The attached patch adds out-of-memory diagnostics for code using STL
>> containers by using set_new_handler. Since the intended allocation
>> size is not available to a new_handler, I had to forego a more
>> detailed error message such as the one from xmalloc_failed().
>> fatal_error() and abort() don't give a meaningful location when the
>> new_handler is called, so I chose to put together the error message
>> manually as is done in xmalloc_failed(). I would have found it more
>> appealing to have operator new call xmalloc() unless a custom
>> allocator is given, but I don't think there's a standard way of doing
>> this.
>>
>> Built and tested on the C and Fortran testsuites. Ok for trunk?
>
> Best regards,
> - Tobi
>
> 2012-10-15  Tobias Schlüter  <tobi@gcc.gnu.org>
>
>      * toplev.c: Add '#include <new>'.
>      (cxx_out_of_memory): New function.
>      (general_init): Install cxx_out_of_memory as handler for
>      out-of-memory condition.

Cheers,
- Tobi
diff mbox

Patch

diff --git a/gcc/toplev.c b/gcc/toplev.c
index 2c9329f..2e6248a 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -89,6 +89,8 @@  along with GCC; see the file COPYING3.  If not see
 				   declarations for e.g. AIX 4.x.  */
 #endif
 
+#include <new>
+
 static void general_init (const char *);
 static void do_compile (void);
 static void process_options (void);
@@ -1061,6 +1063,21 @@  open_auxiliary_file (const char *ext)
   return file;
 }
 
+
+/* Error handler for use with C++ memory allocation.  Will be
+   installed via std::set_new_handler().  */
+
+static void
+cxx_out_of_memory()
+{
+  fprintf (stderr,
+	   "\n%s%sout of memory\n",
+	   progname, *progname ? ": " : "");
+
+  xexit (1);
+}
+
+
 /* Initialization of the front end environment, before command line
    options are parsed.  Signal handlers, internationalization etc.
    ARGV0 is main's argv[0].  */
@@ -1074,6 +1091,8 @@  general_init (const char *argv0)
     --p;
   progname = p;
 
+  std::set_new_handler (cxx_out_of_memory);
+
   xmalloc_set_program_name (progname);
 
   hex_init ();