]> git.proxmox.com Git - mirror_lxcfs.git/blame - src/macro.h
sysfs: fix cpumasks
[mirror_lxcfs.git] / src / macro.h
CommitLineData
db0463bf 1/* SPDX-License-Identifier: LGPL-2.1+ */
1f5596dd 2
17e0e368
CB
3#ifndef __LXCFS_MACRO_H
4#define __LXCFS_MACRO_H
5
d511b24d 6#include "config.h"
1f5596dd 7
a3c8d33c 8#include <stdbool.h>
5fbea8a6 9#include <stdio.h>
a3c8d33c 10#include <linux/types.h>
5fbea8a6
CB
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
081d7647
CB
23#define lxcfs_debug_stream(stream, format, ...) \
24 do { \
25 fprintf(stream, "%s: %d: %s: " format "\n", __FILE__, \
26 __LINE__, __func__, ##__VA_ARGS__); \
17e0e368
CB
27 } while (false)
28
5faa9af7 29#define lxcfs_error(format, ...) lxcfs_debug_stream(stderr, format, ##__VA_ARGS__)
17e0e368
CB
30
31#ifdef DEBUG
5faa9af7 32#define lxcfs_debug(format, ...) lxcfs_error(format, ##__VA_ARGS__)
17e0e368 33#else
081d7647
CB
34#define lxcfs_debug(format, ...) \
35 do { \
36 } while (false)
17e0e368
CB
37#endif /* DEBUG */
38
83dad6a5 39#ifdef VERBOSE
5faa9af7 40#define lxcfs_v(format, ...) lxcfs_error(format, ##__VA_ARGS__);
83dad6a5
SG
41#else
42#define lxcfs_v(format, ...)
43#endif /* VERBOSE */
44
cc42d0c7
CB
45#define lxcfs_info(format, ...) \
46 do { \
47 fprintf(stderr, format "\n", ##__VA_ARGS__); \
48 } while (false)
49
5fbea8a6
CB
50#define log_error_errno(__ret__, __errno__, format, ...) \
51 ({ \
52 errno = __errno__; \
53 lxcfs_error(format, ##__VA_ARGS__); \
54 __ret__; \
55 })
56
971aa868
CB
57#define log_error(__ret__, format, ...) \
58 ({ \
59 lxcfs_error(format, ##__VA_ARGS__); \
60 __ret__; \
61 })
62
5fbea8a6
CB
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
05b7a16d
CB
80#define move_ptr(ptr) \
81 ({ \
0d64d474 82 __typeof__(ptr) __internal_ptr__ = (ptr); \
05b7a16d
CB
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
5fbea8a6
CB
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
2243c5a9
CB
111#define log_exit(format, ...) \
112 ({ \
113 fprintf(stderr, format, ##__VA_ARGS__); \
4ec5c9da 114 _exit(EXIT_FAILURE); \
2243c5a9
CB
115 })
116
b456d40d 117#ifdef DEBUG
6ba365d9
CB
118#define log_debug(__ret__, format, ...) \
119 ({ \
120 lxcfs_debug_stream(stderr, format, ##__VA_ARGS__); \
121 __ret__; \
b456d40d
CB
122 })
123#else
124#define log_debug(__ret__, format, ...) ({ __ret__; })
125#endif
126
f1744de4
CB
127#define PTR_TO_INT(p) ((int)((intptr_t)(p)))
128#define INT_TO_PTR(u) ((void *)((intptr_t)(u)))
fc743888
CB
129#define PTR_TO_UINT64(p) ((uint64_t)((uintptr_t)(p)))
130#define INTTYPE_TO_PTR(u) ((void *)((uintptr_t)(u)))
99b183fb 131
b0a2dc08
CB
132#define __visible __attribute__((visibility("default")))
133
2d7bcab7
CB
134#define __lxcfs_fuse_ops
135
35acc247
CB
136#ifndef __returns_twice
137#define __returns_twice __attribute__((returns_twice))
138#endif
139
10deeaaf 140#define STRINGIFY(a) __STRINGIFY(a)
141#define __STRINGIFY(a) #a
142
a3c8d33c
CB
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
148static inline void set_bit(__u32 bit, __u32 *bitarr)
149{
150 bitarr[bit / NBITS] |= ((__u32)1 << (bit % NBITS));
151}
152
153static inline void clear_bit(__u32 bit, __u32 *bitarr)
154{
155 bitarr[bit / NBITS] &= ~((__u32)1 << (bit % NBITS));
156}
157
158static inline bool is_set(__u32 bit, __u32 *bitarr)
159{
160 return (bitarr[bit / NBITS] & ((__u32)1 << (bit % NBITS))) != 0;
161}
162
17e0e368 163#endif /* __LXCFS_MACRO_H */