Commit | Line | Data |
---|---|---|
503b1c39 PA |
1 | /* Replace operator new/new[], for GDB, the GNU debugger. |
2 | ||
3 | Copyright (C) 2016 Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include "common-defs.h" | |
21 | #include "host-defs.h" | |
22 | #include <new> | |
23 | ||
24 | /* Override operator new / operator new[], in order to internal_error | |
25 | on allocation failure and thus query the user for abort/core | |
26 | dump/continue, just like xmalloc does. We don't do this from a | |
27 | new-handler function instead (std::set_new_handler) because we want | |
28 | to catch allocation errors from within global constructors too. | |
29 | ||
30 | Note that C++ implementations could either have their throw | |
31 | versions call the nothrow versions (libstdc++), or the other way | |
32 | around (clang/libc++). For that reason, we replace both throw and | |
33 | nothrow variants and call malloc directly. */ | |
34 | ||
35 | void * | |
36 | operator new (std::size_t sz) | |
37 | { | |
38 | /* malloc (0) is unpredictable; avoid it. */ | |
39 | if (sz == 0) | |
40 | sz = 1; | |
41 | ||
42 | void *p = malloc (sz); /* ARI: malloc */ | |
43 | if (p == NULL) | |
44 | { | |
45 | /* If the user decides to continue debugging, throw a | |
46 | gdb_quit_bad_alloc exception instead of a regular QUIT | |
47 | gdb_exception. The former extends both std::bad_alloc and a | |
48 | QUIT gdb_exception. This is necessary because operator new | |
49 | can only ever throw std::bad_alloc, or something that extends | |
50 | it. */ | |
51 | TRY | |
52 | { | |
53 | malloc_failure (sz); | |
54 | } | |
55 | CATCH (ex, RETURN_MASK_ALL) | |
56 | { | |
57 | do_cleanups (all_cleanups ()); | |
58 | ||
59 | throw gdb_quit_bad_alloc (ex); | |
60 | } | |
61 | END_CATCH | |
62 | } | |
63 | return p; | |
64 | } | |
65 | ||
66 | void * | |
67 | operator new (std::size_t sz, const std::nothrow_t&) | |
68 | { | |
69 | /* malloc (0) is unpredictable; avoid it. */ | |
70 | if (sz == 0) | |
71 | sz = 1; | |
72 | return malloc (sz); /* ARI: malloc */ | |
73 | } | |
74 | ||
75 | void * | |
76 | operator new[] (std::size_t sz) | |
77 | { | |
78 | return ::operator new (sz); | |
79 | } | |
80 | ||
81 | void* | |
82 | operator new[] (std::size_t sz, const std::nothrow_t&) | |
83 | { | |
84 | return ::operator new (sz, std::nothrow); | |
85 | } |