Code

+ User_data destroy callback added to match_create_callback() in utils_match
authorPavel Rochnyack <pavel2000@ngs.ru>
Sat, 7 May 2016 07:01:33 +0000 (13:01 +0600)
committerFlorian Forster <octo@collectd.org>
Sun, 27 Nov 2016 06:54:38 +0000 (07:54 +0100)
src/daemon/utils_match.c
src/daemon/utils_match.h

index 35102b6dec76b78e5062156dda586c3d773daae6..2a39e4d0552b439623de8b18bf59c2aab9b3eda4 100644 (file)
@@ -33,7 +33,6 @@
 
 #include <regex.h>
 
-#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 */
index d43ae3b340a578e2be647c53afde1152fa2fb7ce..31dc66d804b84cfc18685e37076d47aa26b503ca 100644 (file)
@@ -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