]>
git.proxmox.com Git - mirror_lxcfs.git/blob - src/macro.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #ifndef __LXCFS_MACRO_H
4 #define __LXCFS_MACRO_H
10 #include <linux/types.h>
14 /* filesystem magic values */
15 #ifndef CGROUP_SUPER_MAGIC
16 #define CGROUP_SUPER_MAGIC 0x27e0eb
19 #ifndef CGROUP2_SUPER_MAGIC
20 #define CGROUP2_SUPER_MAGIC 0x63677270
23 #define lxcfs_debug_stream(stream, format, ...) \
25 fprintf(stream, "%s: %d: %s: " format "\n", __FILE__, \
26 __LINE__, __func__, ##__VA_ARGS__); \
29 #define lxcfs_error(format, ...) lxcfs_debug_stream(stderr, format, ##__VA_ARGS__)
32 #define lxcfs_debug(format, ...) lxcfs_error(format, ##__VA_ARGS__)
34 #define lxcfs_debug(format, ...) \
40 #define lxcfs_v(format, ...) lxcfs_error(format, ##__VA_ARGS__);
42 #define lxcfs_v(format, ...)
45 #define lxcfs_info(format, ...) \
47 fprintf(stderr, format "\n", ##__VA_ARGS__); \
50 #define log_error_errno(__ret__, __errno__, format, ...) \
53 lxcfs_error(format, ##__VA_ARGS__); \
57 #define log_error(__ret__, format, ...) \
59 lxcfs_error(format, ##__VA_ARGS__); \
63 #define STRLITERALLEN(x) (sizeof(""x"") - 1)
65 /* Calculate the number of chars needed to represent a given integer as a C
66 * string. Include room for '-' to indicate negative numbers and the \0 byte.
67 * This is based on systemd.
69 #define INTTYPE_TO_STRLEN(type) \
70 (2 + (sizeof(type) <= 1 \
78 : sizeof(int[-2 * (sizeof(type) > 8)])))
80 #define move_ptr(ptr) \
82 __typeof__(ptr) __internal_ptr__ = (ptr); \
89 int __internal_fd__ = (fd); \
94 #define ret_errno(__errno__) \
100 #define ret_set_errno(__ret__, __errno__) \
106 #define lxc_iterate_parts(__iterator, __splitme, __separators) \
107 for (char *__p = NULL, *__it = strtok_r(__splitme, __separators, &__p); \
108 (__iterator = __it); \
109 __iterator = __it = strtok_r(NULL, __separators, &__p))
111 #define log_exit(format, ...) \
113 fprintf(stderr, format, ##__VA_ARGS__); \
114 _exit(EXIT_FAILURE); \
118 #define log_debug(__ret__, format, ...) \
120 lxcfs_debug_stream(stderr, format, ##__VA_ARGS__); \
124 #define log_debug(__ret__, format, ...) ({ __ret__; })
127 #define PTR_TO_INT(p) ((int)((intptr_t)(p)))
128 #define INT_TO_PTR(u) ((void *)((intptr_t)(u)))
129 #define PTR_TO_UINT64(p) ((uint64_t)((uintptr_t)(p)))
130 #define INTTYPE_TO_PTR(u) ((void *)((uintptr_t)(u)))
132 #define __visible __attribute__((visibility("default")))
134 #define __lxcfs_fuse_ops
136 #ifndef __returns_twice
137 #define __returns_twice __attribute__((returns_twice))
140 #define STRINGIFY(a) __STRINGIFY(a)
141 #define __STRINGIFY(a) #a
143 /* Taken over modified from the kernel sources. */
144 #define NBITS 32 /* bits in uint32_t */
145 #define DIV_ROUND_UP(n, d) (((n) + (d)-1) / (d))
146 #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, NBITS)
148 static inline void set_bit(__u32 bit
, __u32
*bitarr
)
150 bitarr
[bit
/ NBITS
] |= ((__u32
)1 << (bit
% NBITS
));
153 static inline void clear_bit(__u32 bit
, __u32
*bitarr
)
155 bitarr
[bit
/ NBITS
] &= ~((__u32
)1 << (bit
% NBITS
));
158 static inline bool is_set(__u32 bit
, __u32
*bitarr
)
160 return (bitarr
[bit
/ NBITS
] & ((__u32
)1 << (bit
% NBITS
))) != 0;
163 #endif /* __LXCFS_MACRO_H */