memory_utils: add call_cleaner() helper

This allows to trivially declare cleanup attributes on the fly. Signed-off-by: 's avatarChristian Brauner <christian.brauner@ubuntu.com>
parent 2b1ec4b9
......@@ -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)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment