]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
memory_utils: add call_cleaner() helper
authorChristian Brauner <christian.brauner@ubuntu.com>
Sun, 15 Mar 2020 00:28:22 +0000 (01:28 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Sun, 15 Mar 2020 00:37:57 +0000 (01:37 +0100)
This allows to trivially declare cleanup attributes on the fly.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/memory_utils.h

index 196c957a5f66044ee2c74599397a97c40cae878f..5968eee1e0d53ee437e8ea4f863b575bb6491406 100644 (file)
 
 #include "macro.h"
 
-#define define_cleanup_attribute(type, func)     \
-       static inline void func##_ptr(type *ptr) \
-       {                                        \
-               if (*ptr)                        \
-                       func(*ptr);              \
+#define define_cleanup_function(type, cleaner)           \
+       static inline void cleaner##_function(type *ptr) \
+       {                                                \
+               if (*ptr)                                \
+                       cleaner(*ptr);                   \
        }
 
+#define call_cleaner(cleaner) __attribute__((__cleanup__(cleaner##_function)))
+
 #define free_disarm(ptr)       \
        ({                     \
                free(ptr);     \
@@ -38,8 +40,9 @@ static inline void free_string_list(char **list)
                free_disarm(list);
        }
 }
-define_cleanup_attribute(char **, free_string_list);
-#define __do_free_string_list __attribute__((__cleanup__(free_string_list_ptr)))
+define_cleanup_function(char **, free_string_list);
+#define __do_free_string_list \
+       __attribute__((__cleanup__(free_string_list_function)))
 
 static inline void __auto_fclose__(FILE **f)
 {