]> arthur.barton.de Git - netatalk.git/commitdiff
Add destructor
authorFrank Lahm <franklahm@googlemail.com>
Wed, 6 Jun 2012 14:58:11 +0000 (16:58 +0200)
committerFrank Lahm <franklahm@googlemail.com>
Wed, 15 Aug 2012 18:12:55 +0000 (20:12 +0200)
etc/afpd/dd.c
etc/afpd/dd.h

index 8c3d13502f9cfdfe151b6155bfc81f77f275e157..1bb95896431a36535907c844b82426002489292e 100644 (file)
@@ -42,10 +42,12 @@ static const char *neststrings[] = {
     "                        "
 };
 
-void *_dd_add_obj(dd_t *dd, void *talloc_chunk, void *obj, size_t size)
+int _dd_add_obj(dd_t *dd, void *talloc_chunk, void *obj, size_t size, int (*destructor)(void *))
 {
     AFP_ASSERT(talloc_chunk);
-    
+
+    if (destructor)
+        talloc_set_destructor(talloc_chunk, destructor);
     if (dd->dd_talloc_array == NULL) {
         dd->dd_talloc_array = talloc_array(dd, void *, 1);
     } else {
@@ -89,6 +91,12 @@ static int dd_dump(dd_t *dd, int nestinglevel)
     printf("%s}\n", neststrings[nestinglevel]);
 }
 
+static int bstring_destructor(void *bstr)
+{
+    bdestroy(*(bstring *)bstr);
+    return 0;
+}
+
 #ifdef SPOT_TEST_MAIN
 #include <stdarg.h>
 
@@ -104,25 +112,25 @@ int main(int argc, char **argv)
     LOG(logtype_default, log_info, "Start");
 
     i = 2;
-    dd_add_obj(dd, &i, int64_t);
+    dd_add_obj(dd, &i, int64_t, NULL);
 
     bstring str = bfromcstr("hello world");
-    dd_add_obj(dd, &str, bstring);
+    dd_add_obj(dd, &str, bstring, bstring_destructor);
 
     bool b = true;
-    dd_add_obj(dd, &b, bool);
+    dd_add_obj(dd, &b, bool, NULL);
 
     b = false;
-    dd_add_obj(dd, &b, bool);
+    dd_add_obj(dd, &b, bool, NULL);
 
     i = 1;
-    dd_add_obj(dd, &i, int64_t);
+    dd_add_obj(dd, &i, int64_t, NULL);
 
     /* add a nested array */
     dd_t *nested = talloc_zero(dd, dd_t);
-    dd_add_obj(nested, &i, int64_t);
-    dd_add_obj(nested, &str, bstring);
-    dd_add_obj(dd, &nested, dd_t);
+    dd_add_obj(nested, &i, int64_t, NULL);
+    dd_add_obj(nested, &str, bstring, bstring_destructor);
+    dd_add_obj(dd, &nested, dd_t, NULL);
 
     dd_dump(dd, 0);
 
index ed8dc5fb1cad43d8cf7379f961a6841dae22d8d9..e072e067de25681fc653b4bef0f3749535b28701 100644 (file)
@@ -16,6 +16,9 @@
 #include "config.h"
 #endif /* HAVE_CONFIG_H */
 
+#ifndef DD_H
+#define DD_H
+
 /* dynamic datastore */
 typedef struct {
     void **dd_talloc_array;
@@ -23,7 +26,11 @@ typedef struct {
 
 #define dd_init(dd) (dd)->dd_talloc_array = NULL;
 
-#define dd_add_obj(dd, obj, type)                                   \
-    _dd_add_obj((dd), talloc((dd), type), (obj), sizeof(type));
+#define dd_add_obj(dd, obj, type, destructor)                              \
+    _dd_add_obj((dd), talloc((dd), type), (obj), sizeof(type), (destructor));
+
+#define dd_get_count(dd) talloc_array_length(dd->dd_talloc_array)
+
+extern int _dd_add_obj(dd_t *dd, void *talloc_chunk, void *obj, size_t size, int (*destructor)(void *));
 
-#define dd_get_count(dd) talloc_array_length(dd)
+#endif  /* DD_H */