diff options
author | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2013-05-12 13:57:34 +0200 |
---|---|---|
committer | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2014-02-15 15:21:53 +0100 |
commit | abfabda5f54ac903bc990340a684e14e5e2b68a5 (patch) | |
tree | 1f736cc477f4a90214f01f1a3586aee7e5282b84 | |
parent | 161b624b4f27a994a33e427d2b5646d80afd0822 (diff) | |
download | sciteco-abfabda5f54ac903bc990340a684e14e5e2b68a5.tar.gz |
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
-rw-r--r-- | src/main.cpp | 58 | ||||
-rw-r--r-- | src/parser.cpp | 15 |
2 files changed, 69 insertions, 4 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) { diff --git a/src/parser.cpp b/src/parser.cpp index 8921942..f64b246 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -21,6 +21,7 @@ #include <stdarg.h> #include <string.h> +#include <stdexcept> #include <glib.h> #include <glib/gprintf.h> @@ -83,10 +84,16 @@ Execute::step(const gchar *macro, gint stop_pos) #endif try { - if (interface.is_interrupted()) - throw State::Error("Interrupted"); - - State::input(macro[macro_pc]); + /* convert bad_alloc exceptions */ + try { + if (interface.is_interrupted()) + throw State::Error("Interrupted"); + + State::input(macro[macro_pc]); + } catch (std::bad_alloc &alloc) { + throw State::Error("bad_alloc: %s", + alloc.what()); + } } catch (State::Error &error) { error.pos = macro_pc; String::get_coord(macro, error.pos, |