diff options
Diffstat (limited to 'deps/sysobj_early/src/auto_free.c')
-rw-r--r-- | deps/sysobj_early/src/auto_free.c | 61 |
1 files changed, 45 insertions, 16 deletions
diff --git a/deps/sysobj_early/src/auto_free.c b/deps/sysobj_early/src/auto_free.c index 78f11690..3d75c169 100644 --- a/deps/sysobj_early/src/auto_free.c +++ b/deps/sysobj_early/src/auto_free.c @@ -19,15 +19,26 @@ */ #include "auto_free.h" +#if (AF_USE_SYSOBJ) +#include "sysobj.h" +#else #include <stdio.h> -//#include "sysobj.h" +#define sysobj_elapsed() af_elapsed() +#define sysobj_stats af_stats +static struct { + double auto_free_next; + unsigned long long + auto_freed, + auto_free_len; +} af_stats; +#endif 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 af_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__) /**/ @@ -49,7 +60,7 @@ gboolean free_auto_free_sf(gpointer trash) { return G_SOURCE_REMOVE; } free_auto_free(); - //sysobj_stats.auto_free_next = sysobj_elapsed() + AF_SECONDS; + sysobj_stats.auto_free_next = sysobj_elapsed() + AF_SECONDS; return G_SOURCE_CONTINUE; } @@ -73,7 +84,7 @@ gpointer auto_free_ex_(gpointer p, GDestroyNotify f, const char *file, int line, * 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; + sysobj_stats.auto_free_next = sysobj_elapsed() + AF_SECONDS; } auto_free_item *z = g_new0(auto_free_item, 1); @@ -83,22 +94,41 @@ gpointer auto_free_ex_(gpointer p, GDestroyNotify f, const char *file, int line, z->file = file; z->line = line; z->func = func; - z->stamp = _elapsed(); + z->stamp = af_elapsed(); g_mutex_lock(&free_lock); free_list = g_slist_prepend(free_list, z); - //sysobj_stats.auto_free_len++; + 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); +gpointer auto_free_on_exit_ex_(gpointer p, GDestroyNotify f, const char *file, int line, const char *func) { + if (!p) return p; + + 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 = -1.0; + 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; } static struct { GDestroyNotify fptr; char *name; } free_function_tab[] = { { (GDestroyNotify) g_free, "g_free" }, - // ... +#if (AF_USE_SYSOBJ) + { (GDestroyNotify) sysobj_free, "sysobj_free" }, + { (GDestroyNotify) class_free, "class_free" }, + { (GDestroyNotify) sysobj_filter_free, "sysobj_filter_free" }, + { (GDestroyNotify) sysobj_virt_free, "sysobj_virt_free" }, +#endif { NULL, "(null)" }, }; @@ -106,18 +136,17 @@ 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(); + double now = af_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); - } + if (DEBUG_AUTO_FREE) + auto_free_msg("%llu total items in queue, but will free from thread %p only... ", sysobj_stats.auto_free_len, this_thread); for(l = free_list; l; l = n) { auto_free_item *z = (auto_free_item*)l->data; n = l->next; + if (!free_final && z->stamp < 0) continue; double age = now - z->stamp; if (free_final || (z->thread == this_thread && (thread_final || age > AF_DELAY_SECONDS) ) ) { if (DEBUG_AUTO_FREE == 2) { @@ -143,8 +172,8 @@ static void free_auto_free_ex(gboolean thread_final) { } 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; + sysobj_stats.auto_freed += fc; + sysobj_stats.auto_free_len -= fc; g_mutex_unlock(&free_lock); } |