From abfabda5f54ac903bc990340a684e14e5e2b68a5 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sun, 12 May 2013 13:57:34 +0200 Subject: glib allocation functions throw std::bad_alloc exceptions now; catch all bad_allocs and convert them to State::Error * will allow some degree of OOM handling * currently does not work since the exception specifications prevent bad_allocs from propagating. exception specification usage must be completely revised --- src/main.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'src/main.cpp') 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 #include #include +#include #include #include @@ -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) { -- cgit v1.2.3