]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/memory_utils.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #ifndef __LXC_MEMORY_UTILS_H
4 #define __LXC_MEMORY_UTILS_H
12 #include <sys/types.h>
16 #include "error_utils.h"
18 #define define_cleanup_function(type, cleaner) \
19 static inline void cleaner##_function(type *ptr) \
25 #define call_cleaner(cleaner) \
26 __attribute__((__cleanup__(cleaner##_function))) __attribute__((unused))
28 #define close_prot_errno_disarm(fd) \
36 #define close_prot_errno_move(fd, new_fd) \
45 static inline void close_prot_errno_disarm_function(int *fd
)
47 close_prot_errno_disarm(*fd
);
49 #define __do_close call_cleaner(close_prot_errno_disarm)
51 define_cleanup_function(FILE *, fclose
);
52 #define __do_fclose call_cleaner(fclose)
54 define_cleanup_function(DIR *, closedir
);
55 #define __do_closedir call_cleaner(closedir)
57 #define free_disarm(ptr) \
59 if (!IS_ERR_OR_NULL(ptr)) { \
65 static inline void free_disarm_function(void *ptr
)
67 free_disarm(*(void **)ptr
);
69 #define __do_free call_cleaner(free_disarm)
71 static inline void free_string_list(char **list
)
73 if (list
&& !IS_ERR(list
)) {
74 for (int i
= 0; list
[i
]; i
++)
79 define_cleanup_function(char **, free_string_list
);
80 #define __do_free_string_list call_cleaner(free_string_list)
82 static inline void *memdup(const void *data
, size_t len
)
86 copy
= len
? malloc(len
) : NULL
;
87 return copy
? memcpy(copy
, data
, len
) : NULL
;
90 #define zalloc(__size__) (calloc(1, __size__))
92 #define free_move_ptr(a, b) \
95 (a) = move_ptr((b)); \
98 #define close_move_fd(a, b) \
101 (a) = move_fd((b)); \
104 #define close_equal(a, b) \
106 if (a >= 0 && a != b) \
113 #define free_equal(a, b) \
121 #endif /* __LXC_MEMORY_UTILS_H */