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-Tag: collectd-5.7.0~26 X-Git-Url: https://git.octo.it/?p=collectd.git;a=commitdiff_plain;h=bd1624ece305c788d504c81ade52043c427deca2 + 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 35102b6d..2a39e4d0 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); }; /* @@ -226,13 +226,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; @@ -266,6 +271,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 */ @@ -282,15 +288,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 */ @@ -313,10 +316,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 d43ae3b3..31dc66d8 100644 --- a/src/daemon/utils_match.h +++ b/src/daemon/utils_match.h @@ -94,11 +94,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