]>
git.proxmox.com Git - mirror_lxcfs.git/blob - src/memory_utils.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #ifndef __LXCFS_MEMORY_UTILS_H
4 #define __LXCFS_MEMORY_UTILS_H
13 #include <sys/types.h>
18 #define define_cleanup_function(type, cleaner) \
19 static inline void cleaner##_function(type *ptr) \
25 #define call_cleaner(cleaner) __attribute__((__cleanup__(cleaner##_function)))
27 #define close_prot_errno_disarm(fd) \
35 #define close_prot_errno_replace(fd, new_fd) \
43 static inline void close_prot_errno_disarm_function(int *fd
)
45 close_prot_errno_disarm(*fd
);
47 #define __do_close call_cleaner(close_prot_errno_disarm)
49 define_cleanup_function(FILE *, fclose
);
50 #define __do_fclose call_cleaner(fclose)
52 define_cleanup_function(DIR *, closedir
);
53 #define __do_closedir call_cleaner(closedir)
55 #define free_disarm(ptr) \
61 static inline void free_disarm_function(void *ptr
)
63 free_disarm(*(void **)ptr
);
65 #define __do_free call_cleaner(free_disarm)
67 static inline void free_string_list(char **list
)
70 for (int i
= 0; list
[i
]; i
++)
75 define_cleanup_function(char **, free_string_list
);
76 #define __do_free_string_list call_cleaner(free_string_list)
78 static inline void *memdup(const void *data
, size_t len
)
82 copy
= len
? malloc(len
) : NULL
;
83 return copy
? memcpy(copy
, data
, len
) : NULL
;
86 #define zalloc(__size__) (calloc(1, __size__))
88 #endif /* __LXCFS_MEMORY_UTILS_H */