diff options
author | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2021-05-30 02:38:43 +0200 |
---|---|---|
committer | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2021-05-30 03:12:56 +0200 |
commit | 432ad24e382681f1c13b07e8486e91063dd96e2e (patch) | |
tree | 51838adac822767bd5884b9383cd4c72f29d3840 /src/memory.cpp | |
parent | 524bc3960e6a6e5645ce904e20f72479e24e0a23 (diff) | |
download | sciteco-432ad24e382681f1c13b07e8486e91063dd96e2e.tar.gz |
THE GREAT CEEIFICATION EVENT
This is a total conversion of SciTECO to plain C (GNU C11).
The chance was taken to improve a lot of internal datastructures,
fix fundamental bugs and lay the foundations of future features.
The GTK user interface is now in an useable state!
All changes have been squashed together.
The language itself has almost not changed at all, except for:
* Detection of string terminators (usually Escape) now takes
the string building characters into account.
A string is only terminated outside of string building characters.
In other words, you can now for instance write
I^EQ[Hello$world]$
This removes one of the last bits of shellisms which is out of
place in SciTECO where no tokenization/lexing is performed.
Consequently, the current termination character can also be
escaped using ^Q/^R.
This is used by auto completions to make sure that strings
are inserted verbatim and without unwanted sideeffects.
* All strings can now safely contain null-characters
(see also: 8-bit cleanliness).
The null-character itself (^@) is not (yet) a valid SciTECO
command, though.
An incomplete list of changes:
* We got rid of the BSD headers for RB trees and lists/queues.
The problem with them was that they used a form of metaprogramming
only to gain a bit of type safety. It also resulted in less
readble code. This was a C++ desease.
The new code avoids metaprogramming only to gain type safety.
The BSD tree.h has been replaced by rb3ptr by Jens Stimpfle
(https://github.com/jstimpfle/rb3ptr).
This implementation is also more memory efficient than BSD's.
The BSD list.h and queue.h has been replaced with a custom
src/list.h.
* Fixed crashes, performance issues and compatibility issues with
the Gtk 3 User Interface.
It is now more or less ready for general use.
The GDK lock is no longer used to avoid using deprecated functions.
On the downside, the new implementation (driving the Gtk event loop
stepwise) is even slower than the old one.
A few glitches remain (see TODO), but it is hoped that they will
be resolved by the Scintilla update which will be performed soon.
* A lot of program units have been split up, so they are shorter
and easier to maintain: core-commands.c, qreg-commands.c,
goto-commands.c, file-utils.h.
* Parser states are simply structs of callbacks now.
They still use a kind of polymorphy using a preprocessor trick.
TECO_DEFINE_STATE() takes an initializer list that will be
merged with the default list of field initializers.
To "subclass" states, you can simply define new macros that add
initializers to existing macros.
* Parsers no longer have a "transitions" table but the input_cb()
may use switch-case statements.
There are also teco_machine_main_transition_t now which can
be used to implement simple transitions. Additionally, you
can specify functions to execute during transitions.
This largely avoids long switch-case-statements.
* Parsers are embeddable/reusable now, at least in parse-only mode.
This does not currently bring any advantages but may later
be used to write a Scintilla lexer for TECO syntax highlighting.
Once parsers are fully embeddable, it will also be possible
to run TECO macros in a kind of coroutine which would allow
them to process string arguments in real time.
* undo.[ch] still uses metaprogramming extensively but via
the C preprocessor of course. On the downside, most undo
token generators must be initiated explicitly (theoretically
we could have used embedded functions / trampolines to
instantiate automatically but this has turned out to be
dangereous).
There is a TECO_DEFINE_UNDO_CALL() to generate closures for
arbitrary functions now (ie. to call an arbitrary function
at undo-time). This simplified a lot of code and is much
shorter than manually pushing undo tokens in many cases.
* Instead of the ridiculous C++ Curiously Recurring Template
Pattern to achieve static polymorphy for user interface
implementations, we now simply declare all functions to
implement in interface.h and link in the implementations.
This is possible since we no longer hace to define
interface subclasses (all state is static variables in
the interface's *.c files).
* Headers are now significantly shorter than in C++ since
we can often hide more of our "class" implementations.
* Memory counting is based on dlmalloc for most platforms now.
Unfortunately, there is no malloc implementation that
provides an efficient constant-time memory counter that
is guaranteed to decrease when freeing memory.
But since we use a defined malloc implementation now,
malloc_usable_size() can be used safely for tracking memory use.
malloc() replacement is very tricky on Windows, so we
use a poll thread on Windows. This can also be enabled
on other supported platforms using --disable-malloc-replacement.
All in all, I'm still not pleased with the state of memory
limiting. It is a mess.
* Error handling uses GError now. This has the advantage that
the GError codes can be reused once we support error catching
in the SciTECO language.
* Added a few more test suite cases.
* Haiku is no longer supported as builds are instable and
I did not manage to debug them - quite possibly Haiku bugs
were responsible.
* Glib v2.44 or later are now required.
The GTK UI requires Gtk+ v3.12 or later now.
The GtkFlowBox fallback and sciteco-wrapper workaround are
no longer required.
* We now extensively use the GCC/Clang-specific g_auto
feature (automatic deallocations when leaving the current
code block).
* Updated copyright to 2021.
SciTECO has been in continuous development, even though there
have been no commits since 2018.
* Since these changes are so significant, the target release has
been set to v2.0.
It is planned that beginning with v3.0, the language will be
kept stable.
Diffstat (limited to 'src/memory.cpp')
-rw-r--r-- | src/memory.cpp | 350 |
1 files changed, 0 insertions, 350 deletions
diff --git a/src/memory.cpp b/src/memory.cpp deleted file mode 100644 index fd7adf7..0000000 --- a/src/memory.cpp +++ /dev/null @@ -1,350 +0,0 @@ -/* - * Copyright (C) 2012-2017 Robin Haberkorn - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -/* for malloc_usable_size() */ -#ifdef HAVE_MALLOC_H -#include <malloc.h> -#endif -#ifdef HAVE_MALLOC_NP_H -#include <malloc_np.h> -#endif - -#include <new> - -#include <glib.h> - -#include "sciteco.h" -#include "memory.h" -#include "error.h" -#include "undo.h" - -#ifdef HAVE_WINDOWS_H -/* here it shouldn't cause conflicts with other headers */ -#define WIN32_LEAN_AND_MEAN -#include <windows.h> -#include <psapi.h> -#endif - -namespace SciTECO { - -/* - * Define this to prefix each heap object allocated - * by the custom allocators with a magic value. - * This helps to detect non-matching calls to the - * overridden new/delete operators which can cause - * underruns of the memory counter. - */ -//#define DEBUG_MAGIC ((guintptr)0xDEAD15DE5E1BEAF0) - -MemoryLimit memlimit; - -/* - * A discussion of memory measurement techniques on Linux - * and UNIXoid operating systems is in order, since this - * problem turned out to be rather tricky. - * - * - UNIX has resource limits, which could be used to enforce - * the memory limit, but in case they are hit, malloc() - * will return NULL, so g_malloc() would abort(). - * Wrapping malloc() to work around that has the same - * problems described below. - * - glibc has malloc hooks, but they are non-portable and - * deprecated. - * - It is possible to effectively wrap malloc() by overriding - * the libc's implementation, which will even work when - * statically linking in libc since malloc() is usually - * delcared `weak`. - * - When wrapping malloc(), malloc_usable_size() could be - * used to count the memory consumption. - * This is libc-specific, but available at least in - * glibc and jemalloc (FreeBSD). - * - glibc exports symbols for the original malloc() implementation - * like __libc_malloc() that could be used for wrapping. - * This is undocumented and libc-specific, though. - * - The GNU ld --wrap option allows us to intercept calls, - * but obviously won't work for shared libraries. - * - The portable dlsym() could be used to look up the original - * library symbol, but it may and does call malloc functions, - * eg. calloc() on glibc. - * In other words, there is no way to portably and reliably - * wrap malloc() and friends when linking dynamically. - * - Another difficulty is that, when free() is overridden, every - * function that can __independently__ allocate memory that - * can be passed to free() must also be overridden. - * Otherwise the measurement is not precise and there can even - * be underruns. Thus we'd have to guard against underruns. - * - malloc() and friends are MT-safe, so any replacement function - * would have to be MT-safe as well to avoid memory corruption. - * E.g. even in single-threaded builds, glib might use - * threads internally. - * - There is also the old-school technique of calculating the size - * of the program break, ie. the effective size of the DATA segment. - * This works under the assumption that all allocations are - * performed by extending the program break, as is __traditionally__ - * done by malloc() and friends. - * - Unfortunately, modern malloc() implementations sometimes - * mmap() memory, especially for large allocations. - * SciTECO mostly allocates small chunks. - * Unfortunately, some malloc implementations like jemalloc - * only claim memory using mmap(), thus rendering sbrk(0) - * useless. - * - Furthermore, some malloc-implementations like glibc will - * only shrink the program break when told so explicitly - * using malloc_trim(0). - * - The sbrk(0) method thus depends on implementation details - * of the libc. - * - glibc and some other platforms have mallinfo(). - * But at least on glibc it can get unbearably slow on programs - * with a lot of (virtual/resident) memory. - * Besides, mallinfo's API is broken on 64-bit systems, effectively - * limiting the enforcable memory limit to 4GB. - * Other glibc-specific introspection functions like malloc_info() - * can be even slower because of the syscalls required. - * - Linux has /proc/self/stat and /proc/self/statm but polling them - * is very inefficient. - * - FreeBSD/jemalloc has mallctl("stats.allocated") which even when - * optimized is significantly slower than the fallback but generally - * acceptable. - * - On all other platforms we (have to) rely on the fallback - * implementation based on C++ allocators/deallocators. - * They have been improved significantly to count as much memory - * as possible, even using libc-specific APIs like malloc_usable_size(). - * Since this has been proven to work sufficiently well even on FreeBSD, - * there is no longer any UNIX-specific implementation. - * Even the malloc_usable_size() workaround for old or non-GNU - * compilers is still faster than mallctl() on FreeBSD. - * This might need to change in the future. - * - Beginning with C++14 (or earlier with -fsized-deallocation), - * it is possible to globally replace sized allocation/deallocation - * functions, which could be used to avoid the malloc_usable_size() - * workaround. Unfortunately, this may not be used for arrays, - * since the compiler may have to call non-sized variants if the - * original allocation size is unknown - and there is no way to detect - * that when the new[] call is made. - * What's worse is that at least G++ STL is broken seriously and - * some versions will call the non-sized delete() even when sized-deallocation - * is available. Again, this cannot be detected at new() time. - * Therefore, I had to remove the sized-deallocation based - * optimization. - */ - -#ifdef G_OS_WIN32 -/* - * Uses the Windows-specific GetProcessMemoryInfo(), - * so the entire process heap is measured. - * - * FIXME: Unfortunately, this is much slower than the portable - * fallback implementation. - * It may be possible to overwrite malloc() and friends, - * counting the chunks with the MSVCRT-specific _minfo(). - * Since we will always run against MSVCRT, the disadvantages - * discussed above for the UNIX-case may not be important. - * We might also just use the fallback implementation with some - * additional support for _msize(). - */ - -gsize -MemoryLimit::get_usage(void) -{ - PROCESS_MEMORY_COUNTERS info; - - /* - * This __should__ not fail since the current process has - * PROCESS_ALL_ACCESS, but who knows... - * Since memory limiting cannot be turned off when this - * happens, we can just as well terminate abnormally. - */ - if (G_UNLIKELY(!GetProcessMemoryInfo(GetCurrentProcess(), - &info, sizeof(info)))) { - gchar *msg = g_win32_error_message(GetLastError()); - g_error("Cannot get memory usage: %s", msg); - /* shouldn't be reached */ - g_free(msg); - return 0; - } - - return info.WorkingSetSize; -} - -#else -/* - * Portable fallback-implementation relying on C++11 sized allocators. - * - * Unfortunately, in the worst case, this will only measure the heap used - * by C++ objects in SciTECO's sources; not even Scintilla, nor all - * g_malloc() calls. - * Usually, we will be able to use global non-sized deallocators with - * libc-specific support to get more accurate results, though. - */ - -#define MEMORY_USAGE_FALLBACK - -/** - * Current memory usage in bytes. - * - * @bug This only works in single-threaded applications. - * Should SciTECO or Scintilla ever use multiple threads, - * it will be necessary to use atomic operations. - */ -static gsize memory_usage = 0; - -gsize -MemoryLimit::get_usage(void) -{ - return memory_usage; -} - -#endif /* MEMORY_USAGE_FALLBACK */ - -void -MemoryLimit::set_limit(gsize new_limit) -{ - gsize memory_usage = get_usage(); - - if (G_UNLIKELY(new_limit && memory_usage > new_limit)) { - gchar *usage_str = g_format_size(memory_usage); - gchar *limit_str = g_format_size(new_limit); - - Error err("Cannot set undo memory limit (%s): " - "Current usage too large (%s).", - limit_str, usage_str); - - g_free(limit_str); - g_free(usage_str); - throw err; - } - - undo.push_var(limit) = new_limit; -} - -void -MemoryLimit::check(void) -{ - if (G_UNLIKELY(limit && get_usage() > limit)) { - gchar *limit_str = g_format_size(limit); - - Error err("Memory limit (%s) exceeded. See <EJ> command.", - limit_str); - - g_free(limit_str); - throw err; - } -} - -/* - * The object-specific sized deallocators allow memory - * counting portably, even in strict C++11 mode. - * Once we depend on C++14, they and the entire `Object` - * class hack may be avoided. - * But see above - due to broken STLs, this may not actually - * be safe! - */ - -void * -Object::operator new(size_t size) noexcept -{ -#ifdef MEMORY_USAGE_FALLBACK - memory_usage += size; -#endif - -#ifdef DEBUG_MAGIC - guintptr *ptr = (guintptr *)g_malloc(sizeof(guintptr) + size); - *ptr = DEBUG_MAGIC; - return ptr + 1; -#else - /* - * Since we've got the sized-delete operator - * below, we could allocate via g_slice. - * - * Using g_slice however would render malloc_trim() - * ineffective. Also, it has been shown to be - * unnecessary on Linux/glibc. - * Glib is guaranteed to use the system malloc(), - * so g_malloc() cooperates with malloc_trim(). - * - * On Windows (even Windows 2000), the slice allocator - * did not show any significant performance boost - * either. Also, since g_slice never seems to return - * memory to the OS and we cannot force it to do so, - * it will not cooperate with the Windows-specific - * memory measurement and it is hard to recover - * from memory limit exhaustions. - */ - return g_malloc(size); -#endif -} - -void -Object::operator delete(void *ptr, size_t size) noexcept -{ -#ifdef DEBUG_MAGIC - if (ptr) { - ptr = (guintptr *)ptr - 1; - g_assert(*(guintptr *)ptr == DEBUG_MAGIC); - } -#endif - - g_free(ptr); - -#ifdef MEMORY_USAGE_FALLBACK - memory_usage -= size; -#endif -} - -} /* namespace SciTECO */ - -/* - * In strict C++11, we can still use global non-sized - * deallocators. - * - * On their own, they bring little benefit, but with - * some libc-specific functionality, they can be used - * to improve the fallback memory measurements to include - * all allocations (including Scintilla). - * This comes with a moderate runtime penalty. - * - * Unfortunately, even in C++14, defining replacement - * sized deallocators may be very dangerous, so this - * seems to be as best as we can get (see above). - */ - -void * -operator new(size_t size) -{ - void *ptr = g_malloc(size); - -#if defined(MEMORY_USAGE_FALLBACK) && defined(HAVE_MALLOC_USABLE_SIZE) - /* NOTE: g_malloc() should always use the system malloc(). */ - SciTECO::memory_usage += malloc_usable_size(ptr); -#endif - - return ptr; -} - -void -operator delete(void *ptr) noexcept -{ -#if defined(MEMORY_USAGE_FALLBACK) && defined(HAVE_MALLOC_USABLE_SIZE) - if (ptr) - SciTECO::memory_usage -= malloc_usable_size(ptr); -#endif - g_free(ptr); -} |