From b7ff56db631be7416cf228dff89cb23d753e4ec8 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sun, 20 Nov 2016 09:00:50 +0100 Subject: fixed glib warnings about using g_mem_set_vtable() and revised memory limiting * we were basing the glib allocators on throwing std::bad_alloc just like the C++ operators. However, this always was unsafe since we were throwing exceptions across plain-C frames (Glib). Also, the memory vtable has been deprecated in Glib, resulting in ugly warnings. * Instead, we now let the C++ new/delete operators work like Glib by basing them on g_malloc/g_slice. This means they will assert and the application will terminate abnormally in case of OOM. OOMs cannot be handled properly anyway, so it is more important to have a good memory limiting mechanism. * Memory limiting has been completely revised. Instead of approximating undo stack sizes using virtual methods (which is unprecise and comes with a performance penalty), we now use a common base class SciTECO::Object to count the memory required by all objects allocated within SciTECO. This is less precise than using global replacement new/deletes which would allow us to control allocations in all C++ code including Scintilla, but they are only supported as of C++14 (GCC 5) and adding compile-time checks would be cumbersome. In any case, we're missing Glib allocations (esp. strings). * As a platform-specific extension, on Linux/glibc we use mallinfo() to count the exact memory usage of the process. On Windows, we use GetProcessMemoryInfo() -- the latter implementation is currently UNTESTED. * We use g_malloc() for new/delete operators when there is malloc_trim() since g_slice does not free heap chunks properly (probably does its own mmap()ing), rendering malloc_trim() ineffective. We've also benchmarked g_slice on Linux/glib (malloc_trim() shouldn't be available elsewhere) and found that it brings no significant performance benefit. On all other platforms, we use g_slice since it is assumed that it at least does not hurt. The new g_slice based allocators should be tested on MSVCRT since I assume that they bring a significant performance benefit on Windows. * Memory limiting does now work in batch mode as well and is still enabled by default. * The old UndoTokenWithSize CRTP hack could be removed. UndoStack operations should be a bit faster now. But on the other hand, there will be an overhead due to repeated memory limit checking on every processed character. --- src/main.cpp | 57 ++------------------------------------------------------- 1 file changed, 2 insertions(+), 55 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index e9be929..bc82093 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -23,7 +23,6 @@ #include #include #include -#include #include #include @@ -81,12 +80,10 @@ static gboolean mung_profile = TRUE; sig_atomic_t sigint_occurred = FALSE; extern "C" { -static gpointer g_malloc_exception(gsize n_bytes); -static gpointer g_calloc_exception(gsize n_blocks, gsize n_block_bytes); -static gpointer g_realloc_exception(gpointer mem, gsize n_bytes); static void sigint_handler(int signal); -} + +} /* extern "C" */ #if defined(G_OS_UNIX) || defined(G_OS_HAIKU) @@ -342,45 +339,6 @@ initialize_environment(const gchar *program) * 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) -{ - 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) -{ - 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) -{ - gpointer p = realloc(mem, n_bytes); - - if (!p) - throw g_bad_alloc(); - return p; -} - static void sigint_handler(int signal) { @@ -403,15 +361,6 @@ 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 */ - }; - gchar *mung_filename; #ifdef DEBUG_PAUSE @@ -422,8 +371,6 @@ main(int argc, char **argv) signal(SIGINT, sigint_handler); signal(SIGTERM, sigint_handler); - g_mem_set_vtable(&vtable); - mung_filename = process_options(argc, argv); /* * All remaining arguments in argv are arguments -- cgit v1.2.3