From: Frank Lahm Date: Wed, 6 Jun 2012 14:58:11 +0000 (+0200) Subject: Add destructor X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=netatalk.git;a=commitdiff_plain;h=d61ad8359000e747d4c2d0708a983fb70f08aa8e Add destructor --- diff --git a/etc/afpd/dd.c b/etc/afpd/dd.c index 8c3d1350..1bb95896 100644 --- a/etc/afpd/dd.c +++ b/etc/afpd/dd.c @@ -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 @@ -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); diff --git a/etc/afpd/dd.h b/etc/afpd/dd.h index ed8dc5fb..e072e067 100644 --- a/etc/afpd/dd.h +++ b/etc/afpd/dd.h @@ -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 */