diff options
author | Burt P <pburt0@gmail.com> | 2019-07-25 20:43:25 -0500 |
---|---|---|
committer | Leandro A. F. Pereira <leandro@hardinfo.org> | 2019-07-29 19:44:59 -0700 |
commit | 6938cbfd0c8d64aa93633fc7423879de757fd45c (patch) | |
tree | 359dd94243c72f29da8d848dbe5f23dd84b35fbc /deps | |
parent | 9d043ceb42468af2f18b823e7dd5d026ef24b0b1 (diff) |
replace idle_free() with auto_free() from sysobj
* auto_free() works in both report mode and gui mode.
* auto_free() only creates one recuring event for the whole
system rather than idle_free()'s one event per call.
* auto_free_ex() allows specifying the free function to use.
* auto_free() is thread-aware, free_auto_free_thread_final()
can be used to clean up on thread exit.
* auto_free() has some tweakability.
Signed-off-by: Burt P <pburt0@gmail.com>
Diffstat (limited to 'deps')
-rw-r--r-- | deps/sysobj_early/include/auto_free.h | 58 | ||||
-rw-r--r-- | deps/sysobj_early/src/auto_free.c | 164 |
2 files changed, 222 insertions, 0 deletions
diff --git a/deps/sysobj_early/include/auto_free.h b/deps/sysobj_early/include/auto_free.h new file mode 100644 index 00000000..e038783d --- /dev/null +++ b/deps/sysobj_early/include/auto_free.h @@ -0,0 +1,58 @@ +/* + * sysobj - https://github.com/bp0/verbose-spork + * Copyright (C) 2018 Burt P. <pburt0@gmail.com> + * + * 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 2 + * 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef _AUTO_FREE_H_ +#define _AUTO_FREE_H_ + +#include <glib.h> + +/* DEBUG_AUTO_FREE messages level: + * 0 - none + * 1 - some + * 2 - much + */ +#ifndef DEBUG_AUTO_FREE +#define DEBUG_AUTO_FREE 0 +#endif +/* the period between free_auto_free()s in the main loop */ +#define AF_SECONDS 11 +/* the minimum time between auto_free(p) and free(p) */ +#define AF_DELAY_SECONDS 10 + +#if (DEBUG_AUTO_FREE > 0) +#define auto_free(p) auto_free_(p, __FILE__, __LINE__, __FUNCTION__) +#define auto_free_ex(p, f) auto_free_ex_(p, f, __FILE__, __LINE__, __FUNCTION__) +#else +#define auto_free(p) auto_free_(p, NULL, 0, NULL) +#define auto_free_ex(p, f) auto_free_ex_(p, f, NULL, 0, NULL) +#endif +gpointer auto_free_(gpointer p, const char *file, int line, const char *func); +gpointer auto_free_ex_(gpointer p, GDestroyNotify f, const char *file, int line, const char *func); + +/* free all the auto_free marked items in the current thread */ +void free_auto_free(); + +/* call at thread termination */ +void free_auto_free_thread_final(); + +/* call at program termination */ +void free_auto_free_final(); + +#endif diff --git a/deps/sysobj_early/src/auto_free.c b/deps/sysobj_early/src/auto_free.c new file mode 100644 index 00000000..78f11690 --- /dev/null +++ b/deps/sysobj_early/src/auto_free.c @@ -0,0 +1,164 @@ +/* + * sysobj - https://github.com/bp0/verbose-spork + * Copyright (C) 2018 Burt P. <pburt0@gmail.com> + * + * 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 2 + * 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include "auto_free.h" +#include <stdio.h> +//#include "sysobj.h" + +static GMutex free_lock; +static GSList *free_list = NULL; +static gboolean free_final = FALSE; +static GTimer *auto_free_timer = NULL; +static guint free_event_source = 0; +#define _elapsed() (auto_free_timer ? g_timer_elapsed(auto_free_timer, NULL) : 0) + +#define auto_free_msg(msg, ...) fprintf (stderr, "[%s] " msg "\n", __FUNCTION__, ##__VA_ARGS__) /**/ + +typedef struct { + gpointer ptr; + GThread *thread; + GDestroyNotify f_free; + double stamp; + + const char *file; + int line; + const char *func; +} auto_free_item; + +gboolean free_auto_free_sf(gpointer trash) { + (void)trash; + if (free_final) { + free_event_source = 0; + return G_SOURCE_REMOVE; + } + free_auto_free(); + //sysobj_stats.auto_free_next = sysobj_elapsed() + AF_SECONDS; + return G_SOURCE_CONTINUE; +} + +gpointer auto_free_ex_(gpointer p, GDestroyNotify f, const char *file, int line, const char *func) { + if (!p) return p; + + /* an auto_free() after free_auto_free_final()? + * Changed mind, I guess, just go with it. */ + if (free_final) + free_final = FALSE; + + if (!auto_free_timer) { + auto_free_timer = g_timer_new(); + g_timer_start(auto_free_timer); + } + + if (!free_event_source) { + /* if there is a main loop, then this will call + * free_auto_free() in idle time every AF_SECONDS seconds. + * If there is no main loop, then free_auto_free() + * will be called at sysobj_cleanup() and when exiting + * threads, as in sysobj_foreach(). */ + free_event_source = g_timeout_add_seconds(AF_SECONDS, (GSourceFunc)free_auto_free_sf, NULL); + //sysobj_stats.auto_free_next = sysobj_elapsed() + AF_SECONDS; + } + + auto_free_item *z = g_new0(auto_free_item, 1); + z->ptr = p; + z->f_free = f; + z->thread = g_thread_self(); + z->file = file; + z->line = line; + z->func = func; + z->stamp = _elapsed(); + g_mutex_lock(&free_lock); + free_list = g_slist_prepend(free_list, z); + //sysobj_stats.auto_free_len++; + g_mutex_unlock(&free_lock); + return p; +} + +gpointer auto_free_(gpointer p, const char *file, int line, const char *func) { + return auto_free_ex_(p, (GDestroyNotify)g_free, file, line, func); +} + +static struct { GDestroyNotify fptr; char *name; } + free_function_tab[] = { + { (GDestroyNotify) g_free, "g_free" }, + // ... + { NULL, "(null)" }, +}; + +static void free_auto_free_ex(gboolean thread_final) { + GThread *this_thread = g_thread_self(); + GSList *l = NULL, *n = NULL; + long long unsigned fc = 0; + double now = _elapsed(); + + if (!free_list) return; + + g_mutex_lock(&free_lock); + if (DEBUG_AUTO_FREE) { + unsigned long long ll = g_slist_length(free_list); + auto_free_msg("%llu total items in queue, but will free from thread %p only... ", ll, this_thread); + } + for(l = free_list; l; l = n) { + auto_free_item *z = (auto_free_item*)l->data; + n = l->next; + double age = now - z->stamp; + if (free_final || (z->thread == this_thread && (thread_final || age > AF_DELAY_SECONDS) ) ) { + if (DEBUG_AUTO_FREE == 2) { + char fptr[128] = "", *fname; + for(int i = 0; i < (int)G_N_ELEMENTS(free_function_tab); i++) + if (z->f_free == free_function_tab[i].fptr) + fname = free_function_tab[i].name; + if (!fname) { + snprintf(fname, 127, "%p", z->f_free); + fname = fptr; + } + if (z->file || z->func) + auto_free_msg("free: %s(%p) age:%lfs from %s:%d %s()", fname, z->ptr, age, z->file, z->line, z->func); + else + auto_free_msg("free: %s(%p) age:%lfs", fname, z->ptr, age); + } + + z->f_free(z->ptr); + g_free(z); + free_list = g_slist_delete_link(free_list, l); + fc++; + } + } + if (DEBUG_AUTO_FREE) + auto_free_msg("... freed %llu (from thread %p)", fc, this_thread); + //sysobj_stats.auto_freed += fc; + //sysobj_stats.auto_free_len -= fc; + g_mutex_unlock(&free_lock); +} + +void free_auto_free_thread_final() { + free_auto_free_ex(TRUE); +} + +void free_auto_free_final() { + free_final = TRUE; + free_auto_free_ex(TRUE); + g_timer_destroy(auto_free_timer); + auto_free_timer = NULL; +} + +void free_auto_free() { + free_auto_free_ex(FALSE); +} |