diff options
author | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2024-09-28 00:17:24 +0200 |
---|---|---|
committer | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2024-09-28 00:17:24 +0200 |
commit | b3ae8e299517556ec6e9ad06d91353f5b66d0827 (patch) | |
tree | a53c879c047e02a3474a6a1707bea553053f7d76 | |
parent | c4a1c3a21818965f669ebc6a081f08323a6919f1 (diff) | |
download | sciteco-b3ae8e299517556ec6e9ad06d91353f5b66d0827.tar.gz |
fixed memory limiting if the process' memory usage is larger than 2GB and overflow checking
* teco_memory_usage is now an unsigned integer.
* Unfortunately we currently rely on the variable being int-sized since we use
atomic operations.
This means on 64-bit systems, limiting will not work as expected if you set the limit larger
than 4GB.
Not sure whether this should be fixed.
* Calling teco_memory_check() with a non-null request-size was totally broken and could
result in bogus failures.
This is currently used exclusively for checking backwards searches.
-rw-r--r-- | src/memory.c | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/src/memory.c b/src/memory.c index 8de015d..605c221 100644 --- a/src/memory.c +++ b/src/memory.c @@ -288,7 +288,7 @@ * Current memory usage. * Access must be synchronized using atomic operations. */ -static gint teco_memory_usage = 0; +static guint teco_memory_usage = 0; /* * NOTE: This implementation based on malloc_usable_size() might @@ -658,7 +658,7 @@ gsize teco_memory_limit = 500*1000*1000; gboolean teco_memory_set_limit(gsize new_limit, GError **error) { - gsize memory_usage = g_atomic_int_get(&teco_memory_usage); + gsize memory_usage = (guint)g_atomic_int_get(&teco_memory_usage); if (G_UNLIKELY(new_limit && memory_usage > new_limit)) { g_autofree gchar *usage_str = g_format_size(memory_usage); @@ -691,18 +691,19 @@ teco_memory_set_limit(gsize new_limit, GError **error) gboolean teco_memory_check(gsize request, GError **error) { - gsize memory_usage = g_atomic_int_get(&teco_memory_usage) + request; + gsize memory_usage = (guint)g_atomic_int_get(&teco_memory_usage); + gsize requested_memory_usage = memory_usage+request; /* * Check for overflows. * NOTE: Glib 2.48 has g_size_checked_add(). */ - if (G_UNLIKELY(memory_usage < request)) + if (G_UNLIKELY(requested_memory_usage < memory_usage)) /* guaranteed to fail if memory limiting is enabled */ - memory_usage = G_MAXSIZE; + requested_memory_usage = G_MAXSIZE; - if (G_UNLIKELY(teco_memory_limit && memory_usage >= teco_memory_limit)) { - g_autofree gchar *limit_str = g_format_size(memory_usage); + if (G_UNLIKELY(teco_memory_limit && requested_memory_usage >= teco_memory_limit)) { + g_autofree gchar *limit_str = g_format_size(requested_memory_usage); g_set_error(error, TECO_ERROR, TECO_ERROR_MEMLIMIT, "Memory limit (%s) exceeded. See <EJ> command.", |