]> git.proxmox.com Git - mirror_lxcfs.git/blob - src/macro.h
sysfs: fix cpumasks
[mirror_lxcfs.git] / src / macro.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #ifndef __LXCFS_MACRO_H
4 #define __LXCFS_MACRO_H
5
6 #include "config.h"
7
8 #include <stdbool.h>
9 #include <stdio.h>
10 #include <linux/types.h>
11
12 #define BATCH_SIZE 50
13
14 /* filesystem magic values */
15 #ifndef CGROUP_SUPER_MAGIC
16 #define CGROUP_SUPER_MAGIC 0x27e0eb
17 #endif
18
19 #ifndef CGROUP2_SUPER_MAGIC
20 #define CGROUP2_SUPER_MAGIC 0x63677270
21 #endif
22
23 #define lxcfs_debug_stream(stream, format, ...) \
24 do { \
25 fprintf(stream, "%s: %d: %s: " format "\n", __FILE__, \
26 __LINE__, __func__, ##__VA_ARGS__); \
27 } while (false)
28
29 #define lxcfs_error(format, ...) lxcfs_debug_stream(stderr, format, ##__VA_ARGS__)
30
31 #ifdef DEBUG
32 #define lxcfs_debug(format, ...) lxcfs_error(format, ##__VA_ARGS__)
33 #else
34 #define lxcfs_debug(format, ...) \
35 do { \
36 } while (false)
37 #endif /* DEBUG */
38
39 #ifdef VERBOSE
40 #define lxcfs_v(format, ...) lxcfs_error(format, ##__VA_ARGS__);
41 #else
42 #define lxcfs_v(format, ...)
43 #endif /* VERBOSE */
44
45 #define lxcfs_info(format, ...) \
46 do { \
47 fprintf(stderr, format "\n", ##__VA_ARGS__); \
48 } while (false)
49
50 #define log_error_errno(__ret__, __errno__, format, ...) \
51 ({ \
52 errno = __errno__; \
53 lxcfs_error(format, ##__VA_ARGS__); \
54 __ret__; \
55 })
56
57 #define log_error(__ret__, format, ...) \
58 ({ \
59 lxcfs_error(format, ##__VA_ARGS__); \
60 __ret__; \
61 })
62
63 #define STRLITERALLEN(x) (sizeof(""x"") - 1)
64
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.
68 */
69 #define INTTYPE_TO_STRLEN(type) \
70 (2 + (sizeof(type) <= 1 \
71 ? 3 \
72 : sizeof(type) <= 2 \
73 ? 5 \
74 : sizeof(type) <= 4 \
75 ? 10 \
76 : sizeof(type) <= 8 \
77 ? 20 \
78 : sizeof(int[-2 * (sizeof(type) > 8)])))
79
80 #define move_ptr(ptr) \
81 ({ \
82 __typeof__(ptr) __internal_ptr__ = (ptr); \
83 (ptr) = NULL; \
84 __internal_ptr__; \
85 })
86
87 #define move_fd(fd) \
88 ({ \
89 int __internal_fd__ = (fd); \
90 (fd) = -EBADF; \
91 __internal_fd__; \
92 })
93
94 #define ret_errno(__errno__) \
95 ({ \
96 errno = __errno__; \
97 -__errno__; \
98 })
99
100 #define ret_set_errno(__ret__, __errno__) \
101 ({ \
102 errno = __errno__; \
103 __ret__; \
104 })
105
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))
110
111 #define log_exit(format, ...) \
112 ({ \
113 fprintf(stderr, format, ##__VA_ARGS__); \
114 _exit(EXIT_FAILURE); \
115 })
116
117 #ifdef DEBUG
118 #define log_debug(__ret__, format, ...) \
119 ({ \
120 lxcfs_debug_stream(stderr, format, ##__VA_ARGS__); \
121 __ret__; \
122 })
123 #else
124 #define log_debug(__ret__, format, ...) ({ __ret__; })
125 #endif
126
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)))
131
132 #define __visible __attribute__((visibility("default")))
133
134 #define __lxcfs_fuse_ops
135
136 #ifndef __returns_twice
137 #define __returns_twice __attribute__((returns_twice))
138 #endif
139
140 #define STRINGIFY(a) __STRINGIFY(a)
141 #define __STRINGIFY(a) #a
142
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)
147
148 static inline void set_bit(__u32 bit, __u32 *bitarr)
149 {
150 bitarr[bit / NBITS] |= ((__u32)1 << (bit % NBITS));
151 }
152
153 static inline void clear_bit(__u32 bit, __u32 *bitarr)
154 {
155 bitarr[bit / NBITS] &= ~((__u32)1 << (bit % NBITS));
156 }
157
158 static inline bool is_set(__u32 bit, __u32 *bitarr)
159 {
160 return (bitarr[bit / NBITS] & ((__u32)1 << (bit % NBITS))) != 0;
161 }
162
163 #endif /* __LXCFS_MACRO_H */