xref: /netbsd-src/external/gpl3/binutils/dist/gprofng/src/CatchOutOfMemory.cc (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
1*cb63e24eSchristos /* Copyright (C) 2021-2024 Free Software Foundation, Inc.
24f645668Schristos    Contributed by Oracle.
34f645668Schristos 
44f645668Schristos    This file is part of GNU Binutils.
54f645668Schristos 
64f645668Schristos    This program is free software; you can redistribute it and/or modify
74f645668Schristos    it under the terms of the GNU General Public License as published by
84f645668Schristos    the Free Software Foundation; either version 3, or (at your option)
94f645668Schristos    any later version.
104f645668Schristos 
114f645668Schristos    This program is distributed in the hope that it will be useful,
124f645668Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
134f645668Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
144f645668Schristos    GNU General Public License for more details.
154f645668Schristos 
164f645668Schristos    You should have received a copy of the GNU General Public License
174f645668Schristos    along with this program; if not, write to the Free Software
184f645668Schristos    Foundation, 51 Franklin Street - Fifth Floor, Boston,
194f645668Schristos    MA 02110-1301, USA.  */
204f645668Schristos 
214f645668Schristos #include "config.h"
224f645668Schristos #include <new>          // std::bad_alloc
234f645668Schristos #include <stdio.h>      // fprintf
244f645668Schristos #include <stdlib.h>     // exit
254f645668Schristos #include "DbeApplication.h"
264f645668Schristos 
274f645668Schristos static char *name = NULL;
284f645668Schristos 
294f645668Schristos /**
304f645668Schristos  * Out Of Memory exception handler
314f645668Schristos  */
324f645668Schristos void
out_of_mem()334f645668Schristos out_of_mem ()
344f645668Schristos {
354f645668Schristos   fprintf (stderr, "%s: %s: %s\n", "Error", name ? name : "", "Out of memory\n");
364f645668Schristos   exit (2); // Out of memory
374f645668Schristos   // throw bad_alloc();
384f645668Schristos }
394f645668Schristos 
404f645668Schristos /**
414f645668Schristos  * Calls real_main inside try{...}catch(std::bad_alloc *)
424f645668Schristos  */
434f645668Schristos int
catch_out_of_memory(int (* real_main)(int,char * []),int argc,char * argv[])444f645668Schristos catch_out_of_memory (int (*real_main)(int, char*[]), int argc, char *argv[])
454f645668Schristos {
464f645668Schristos   int i = 0;
474f645668Schristos   name = argv[0];
484f645668Schristos   std::set_new_handler (out_of_mem);
494f645668Schristos   try
504f645668Schristos     {
514f645668Schristos       i = real_main (argc, argv);
524f645668Schristos     }
534f645668Schristos   catch (std::bad_alloc */*ba*/)
544f645668Schristos     {
554f645668Schristos       exit (2); // Out of memory
564f645668Schristos     }
574f645668Schristos   delete theDbeApplication;
584f645668Schristos   return i;
594f645668Schristos }
60