memory_utils: adapt to new infrastructure

parent 5a076633
...@@ -21,16 +21,37 @@ ...@@ -21,16 +21,37 @@
#define call_cleaner(cleaner) __attribute__((__cleanup__(cleaner##_function))) #define call_cleaner(cleaner) __attribute__((__cleanup__(cleaner##_function)))
#define close_prot_errno_disarm(fd) \
if (fd >= 0) { \
int _e_ = errno; \
close(fd); \
errno = _e_; \
fd = -EBADF; \
}
static inline void close_prot_errno_disarm_function(int *fd)
{
close_prot_errno_disarm(*fd);
}
#define __do_close_prot_errno call_cleaner(close_prot_errno_disarm)
define_cleanup_function(FILE *, fclose);
#define __do_fclose call_cleaner(fclose)
define_cleanup_function(DIR *, closedir);
#define __do_closedir call_cleaner(closedir)
#define free_disarm(ptr) \ #define free_disarm(ptr) \
({ \ ({ \
free(ptr); \ free(ptr); \
move_ptr(ptr); \ move_ptr(ptr); \
}) })
static inline void __auto_free__(void *p) static inline void free_disarm_function(void *ptr)
{ {
free(*(void **)p); free_disarm(*(void **)ptr);
} }
#define __do_free call_cleaner(free_disarm)
static inline void free_string_list(char **list) static inline void free_string_list(char **list)
{ {
...@@ -41,38 +62,7 @@ static inline void free_string_list(char **list) ...@@ -41,38 +62,7 @@ static inline void free_string_list(char **list)
} }
} }
define_cleanup_function(char **, free_string_list); define_cleanup_function(char **, free_string_list);
#define __do_free_string_list \ #define __do_free_string_list call_cleaner(free_string_list)
__attribute__((__cleanup__(free_string_list_function)))
static inline void __auto_fclose__(FILE **f)
{
if (*f)
fclose(*f);
}
static inline void __auto_closedir__(DIR **d)
{
if (*d)
closedir(*d);
}
#define close_prot_errno_disarm(fd) \
if (fd >= 0) { \
int _e_ = errno; \
close(fd); \
errno = _e_; \
fd = -EBADF; \
}
static inline void __auto_close__(int *fd)
{
close_prot_errno_disarm(*fd);
}
#define __do_close_prot_errno __attribute__((__cleanup__(__auto_close__)))
#define __do_free __attribute__((__cleanup__(__auto_free__)))
#define __do_fclose __attribute__((__cleanup__(__auto_fclose__)))
#define __do_closedir __attribute__((__cleanup__(__auto_closedir__)))
static inline void *memdup(const void *data, size_t len) static inline void *memdup(const void *data, size_t len)
{ {
......
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