From: Pavel Rochnyack Date: Sat, 7 May 2016 07:01:33 +0000 (+0600) Subject: + User_data destroy callback added to match_create_callback() in utils_match X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=8ae4f48f3666ada75afaf4bd5f492a72b1934747;p=collectd.git + User_data destroy callback added to match_create_callback() in utils_match --- diff --git a/src/daemon/utils_match.c b/src/daemon/utils_match.c index 914b6e27..14e71c73 100644 --- a/src/daemon/utils_match.c +++ b/src/daemon/utils_match.c @@ -33,7 +33,6 @@ #include -#define UTILS_MATCH_FLAGS_FREE_USER_DATA 0x01 #define UTILS_MATCH_FLAGS_EXCLUDE_REGEX 0x02 struct cu_match_s @@ -45,6 +44,7 @@ struct cu_match_s int (*callback) (const char *str, char * const *matches, size_t matches_num, void *user_data); void *user_data; + void (*free) (void *user_data); }; /* @@ -225,13 +225,18 @@ static int default_callback (const char __attribute__((unused)) *str, return (0); } /* int default_callback */ +static void match_simple_free (void *data) +{ + free (data); +} /* void match_simple_free */ + /* * Public functions */ cu_match_t *match_create_callback (const char *regex, const char *excluderegex, int (*callback) (const char *str, char * const *matches, size_t matches_num, void *user_data), - void *user_data) + void *user_data, void (*free_user_data) (void *user_data)) { cu_match_t *obj; int status; @@ -265,6 +270,7 @@ cu_match_t *match_create_callback (const char *regex, const char *excluderegex, obj->callback = callback; obj->user_data = user_data; + obj->free = free_user_data; return (obj); } /* cu_match_t *match_create_callback */ @@ -281,15 +287,12 @@ cu_match_t *match_create_simple (const char *regex, user_data->ds_type = match_ds_type; obj = match_create_callback (regex, excluderegex, - default_callback, user_data); + default_callback, user_data, match_simple_free); if (obj == NULL) { sfree (user_data); return (NULL); } - - obj->flags |= UTILS_MATCH_FLAGS_FREE_USER_DATA; - return (obj); } /* cu_match_t *match_create_simple */ @@ -310,10 +313,8 @@ void match_destroy (cu_match_t *obj) if (obj == NULL) return; - if (obj->flags & UTILS_MATCH_FLAGS_FREE_USER_DATA) - { - sfree (obj->user_data); - } + if ((obj->user_data != NULL) && (obj->free != NULL)) + (*obj->free) (obj->user_data); sfree (obj); } /* void match_destroy */ diff --git a/src/daemon/utils_match.h b/src/daemon/utils_match.h index a1d10020..d0ebd542 100644 --- a/src/daemon/utils_match.h +++ b/src/daemon/utils_match.h @@ -93,11 +93,13 @@ typedef struct cu_match_value_s cu_match_value_t; * callback. * The optional `excluderegex' allows to exclude the line from the match, if * the excluderegex matches. + * When `match_destroy' is called the `user_data' pointer is freed using + * the `free_user_data' callback - if it is not NULL. */ cu_match_t *match_create_callback (const char *regex, const char *excluderegex, int (*callback) (const char *str, char * const *matches, size_t matches_num, void *user_data), - void *user_data); + void *user_data, void (*free_user_data) (void *user_data)); /* * NAME