From patchwork Mon Oct 15 20:51:05 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: Install error handler for out-of-memory when using STL containers Re: PR fortran/51727: make module files reproducible, question on C++ in gcc Date: Mon, 15 Oct 2012 10:51:05 -0000 From: =?utf-8?q?Tobias_Schl=C3=BCter_=3Ctobias=2Eschlueter=40physik=2Euni-m?= =?utf-8?q?uenchen=2Ede=3E?= X-Patchwork-Id: 191664 Message-Id: <507C7739.3070003@physik.uni-muenchen.de> To: Jakub Jelinek Cc: gcc-patches Hi, On 2012-10-14 23:44, Jakub Jelinek wrote: > On Mon, Oct 15, 2012 at 12:35:27AM +0300, Janne Blomqvist wrote: >> On Sat, Oct 13, 2012 at 4:26 PM, Tobias Schlüter >>> I'm putting forward two patches. One uses a C++ map to very concisely build >>> up and handle the ordered list of symbols. This has three problems: >>> 1) gfortran maintainers may not want C++isms (even though in this case >>> it's very localized, and in my opinion very transparent), and > > Even if you prefer a C++isms, why don't you go for "hash-table.h"? > std::map at least with the default allocator will just crash the compiler > if malloc returns NULL (remember that we build with -fno-exceptions), > while when you use hash-table.h (or hashtab.h) you get proper OOM diagnostics. 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? Cheers, - Tobi 2012-10-15 Tobias Schlüter * toplev.c: Add '#include '. (cxx_out_of_memory): New function. (general_init): Install cxx_out_of_memory as handler for out-of-memory condition. 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 + 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 ();