]> Dogcows Code - chaz/homebank/blobdiff - src/refcount.h
add plugin engine (supports C and Perl plugins)
[chaz/homebank] / src / refcount.h
diff --git a/src/refcount.h b/src/refcount.h
new file mode 100644 (file)
index 0000000..f97b93a
--- /dev/null
@@ -0,0 +1,54 @@
+
+#ifndef __REFCOUNT_H__
+#define __REFCOUNT_H__
+
+#include <glib.h>
+
+
+static inline gpointer rc_alloc(size_t size)
+{
+       gpointer chunk = g_malloc0(size + sizeof(long));
+       (*(long*)chunk) = 1;
+       //g_print("ALLOC: %p (ref %ld)\n", (long*)chunk + 1, *(long*)chunk);
+       return (long*)chunk + 1;
+}
+
+static inline gpointer rc_ref(gpointer p)
+{
+       //g_print("  REF: %p (ref %ld)\n", p, *((long*)p - 1));
+       if (p) {
+               ++(*((long*)p - 1));
+       }
+       return p;
+}
+
+static inline gboolean rc_unref(gpointer p)
+{
+       //g_print("UNREF: %p (ref %ld)\n", p, *((long*)p - 1));
+       if (p && --(*((long*)p - 1)) <= 0) {
+               return TRUE;
+       }
+       return FALSE;
+}
+
+static inline void rc_free(gpointer p)
+{
+       //g_print(" FREE: %p (ref %ld)\n", p, *((long*)p - 1));
+       g_free((long*)p - 1);
+}
+
+static inline gpointer rc_dup(gpointer p, size_t size)
+{
+       if (p) {
+               gpointer chunk = (long*)p - 1;
+               gpointer new_chunk = g_memdup(chunk, size + sizeof(long));
+               *(long*)new_chunk = 1;
+               //g_print("  DUP: %p (ref %ld) -> %p (ref %ld)\n", p, *((long*)p - 1), (long*)new_chunk + 1, *(long*)new_chunk);
+               return (long*)new_chunk + 1;
+       }
+       //g_print("  DUP: NULL\n");
+       return NULL;
+}
+
+
+#endif
This page took 0.020634 seconds and 4 git commands to generate.