From: Christian Brauner Date: Sun, 15 Mar 2020 00:28:22 +0000 (+0100) Subject: memory_utils: add call_cleaner() helper X-Git-Tag: lxc-4.0.0~30^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e16ad728f827f87175918cc3cfa6e67f46a3acd1;p=thirdparty%2Flxc.git memory_utils: add call_cleaner() helper This allows to trivially declare cleanup attributes on the fly. Signed-off-by: Christian Brauner --- diff --git a/src/lxc/memory_utils.h b/src/lxc/memory_utils.h index 196c957a5..5968eee1e 100644 --- a/src/lxc/memory_utils.h +++ b/src/lxc/memory_utils.h @@ -12,13 +12,15 @@ #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) {