aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/main.cpp b/src/main.cpp
index d6b8ab3..ce2cdb4 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -24,6 +24,7 @@
#include <string.h>
#include <stdlib.h>
#include <signal.h>
+#include <stdexcept>
#include <glib.h>
#include <glib/gprintf.h>
@@ -77,6 +78,13 @@ static gchar *mung_file = NULL;
sig_atomic_t sigint_occurred = FALSE;
extern "C" {
+static gpointer g_malloc_exception(gsize n_bytes)
+ throw (std::bad_alloc);
+static gpointer g_calloc_exception(gsize n_blocks, gsize n_block_bytes)
+ throw (std::bad_alloc);
+static gpointer g_realloc_exception(gpointer mem, gsize n_bytes)
+ throw (std::bad_alloc);
+
static void sigint_handler(int signal);
}
@@ -235,8 +243,19 @@ main(int argc, char **argv)
static GotoTable cmdline_goto_table;
static QRegisterTable local_qregs;
+ static GMemVTable vtable = {
+ g_malloc_exception, /* malloc */
+ g_realloc_exception, /* realloc */
+ free, /* free */
+ g_calloc_exception, /* calloc */
+ malloc, /* try_malloc */
+ realloc /* try_realloc */
+ };
+
signal(SIGINT, sigint_handler);
+ g_mem_set_vtable(&vtable);
+
process_options(argc, argv);
interface.main(argc, argv);
/* remaining arguments are arguments to the munged file */
@@ -324,6 +343,45 @@ main(int argc, char **argv)
* Callbacks
*/
+class g_bad_alloc : public std::bad_alloc {
+public:
+ const char *
+ what() const throw()
+ {
+ return "glib allocation";
+ }
+};
+
+static gpointer
+g_malloc_exception(gsize n_bytes) throw (std::bad_alloc)
+{
+ gpointer p = malloc(n_bytes);
+
+ if (!p)
+ throw g_bad_alloc();
+ return p;
+}
+
+static gpointer
+g_calloc_exception(gsize n_blocks, gsize n_block_bytes) throw (std::bad_alloc)
+{
+ gpointer p = calloc(n_blocks, n_block_bytes);
+
+ if (!p)
+ throw g_bad_alloc();
+ return p;
+}
+
+static gpointer
+g_realloc_exception(gpointer mem, gsize n_bytes) throw (std::bad_alloc)
+{
+ gpointer p = realloc(mem, n_bytes);
+
+ if (!p)
+ throw g_bad_alloc();
+ return p;
+}
+
static void
sigint_handler(int signal)
{