xref: /netbsd-src/external/gpl3/binutils/dist/gprofng/src/CatchOutOfMemory.cc (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
1 /* Copyright (C) 2021-2024 Free Software Foundation, Inc.
2    Contributed by Oracle.
3 
4    This file is part of GNU Binutils.
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3, or (at your option)
9    any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, 51 Franklin Street - Fifth Floor, Boston,
19    MA 02110-1301, USA.  */
20 
21 #include "config.h"
22 #include <new>          // std::bad_alloc
23 #include <stdio.h>      // fprintf
24 #include <stdlib.h>     // exit
25 #include "DbeApplication.h"
26 
27 static char *name = NULL;
28 
29 /**
30  * Out Of Memory exception handler
31  */
32 void
out_of_mem()33 out_of_mem ()
34 {
35   fprintf (stderr, "%s: %s: %s\n", "Error", name ? name : "", "Out of memory\n");
36   exit (2); // Out of memory
37   // throw bad_alloc();
38 }
39 
40 /**
41  * Calls real_main inside try{...}catch(std::bad_alloc *)
42  */
43 int
catch_out_of_memory(int (* real_main)(int,char * []),int argc,char * argv[])44 catch_out_of_memory (int (*real_main)(int, char*[]), int argc, char *argv[])
45 {
46   int i = 0;
47   name = argv[0];
48   std::set_new_handler (out_of_mem);
49   try
50     {
51       i = real_main (argc, argv);
52     }
53   catch (std::bad_alloc */*ba*/)
54     {
55       exit (2); // Out of memory
56     }
57   delete theDbeApplication;
58   return i;
59 }
60