Garbage collector allows to register variables which
then all will be freed by calling gc_free().
---
 src/common.c |   34 ++++++++++++++++++++++++++++++++++
 src/common.h |   20 ++++++++++++++++++++
 2 files changed, 54 insertions(+)

Index: collectd-4.9.1/src/common.h
===================================================================
--- collectd-4.9.1.orig/src/common.h    2010-02-05 20:19:05.000000000 +0100
+++ collectd-4.9.1/src/common.h 2010-02-05 21:30:10.000000000 +0100
@@ -47,6 +47,14 @@
                || (strcasecmp ("no", (s)) == 0) \
                || (strcasecmp ("off", (s)) == 0))
 
+struct gc_entry {
+       void *ptr;
+       struct gc_entry *next;
+};
+struct gc_list {
+       struct gc_entry *list;
+};
+
 char *sstrncpy (char *dest, const char *src, size_t n);
 int ssnprintf (char *dest, size_t n, const char *format, ...);
 char *sstrdup(const char *s);
@@ -289,4 +297,16 @@
  * (in the range [1-65535]). Returns less than zero on error. */
 int service_name_to_port_number (const char *service_name);
 
+
+/* initialize garbage collector */
+struct gc_list *gc_init();
+
+/* register variable 'ptr' to garbage collector */
+void gc_register(struct gc_list *gc, void *ptr);
+
+/* free all entries of garbage collector */
+void gc_free(struct gc_list *gc);
+
+
+
 #endif /* COMMON_H */
Index: collectd-4.9.1/src/common.c
===================================================================
--- collectd-4.9.1.orig/src/common.c    2010-02-05 20:19:03.000000000 +0100
+++ collectd-4.9.1/src/common.c 2010-02-05 21:31:15.000000000 +0100
@@ -1133,3 +1133,37 @@
                return (service_number);
        return (-1);
 } /* int service_name_to_port_number */
+
+struct gc_list *gc_init() {
+       struct gc_list * ret;
+       ret = (struct gc_list *) malloc(sizeof(struct gc_list));
+       ret->list = NULL;
+       return ret;
+}
+
+void gc_register(struct gc_list *gc, void *ptr) {
+       struct gc_entry *item;
+       struct gc_entry *tmp;
+
+       tmp = gc->list;
+       item = (struct gc_entry *) malloc(sizeof(struct gc_entry));
+       item->next = tmp;
+       item->ptr = ptr;
+       gc->list = item;
+}
+
+void gc_free(struct gc_list *gc) {
+       struct gc_entry *item;
+       if (! gc)
+               return;
+       item = gc->list;
+       sfree(gc);
+
+       while (item != NULL) {
+               struct gc_entry *next = item->next;
+               sfree(item->ptr);
+               sfree(item);
+               item = next;
+       }
+}
+

-- 

_______________________________________________
collectd mailing list
[email protected]
http://mailman.verplant.org/listinfo/collectd

Reply via email to