aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main.cpp
diff options
context:
space:
mode:
authorRobin Haberkorn <robin.haberkorn@googlemail.com>2013-05-12 13:57:34 +0200
committerRobin Haberkorn <robin.haberkorn@googlemail.com>2014-02-15 15:21:53 +0100
commitabfabda5f54ac903bc990340a684e14e5e2b68a5 (patch)
tree1f736cc477f4a90214f01f1a3586aee7e5282b84 /src/main.cpp
parent161b624b4f27a994a33e427d2b5646d80afd0822 (diff)
downloadsciteco-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
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)
{