GCC Code Coverage Report


Directory: ./
File: include/utils.h
Date: 2026-01-20 23:48:36
Exec Total Coverage
Lines: 20 20 100.0%
Functions: 1 1 100.0%
Branches: 12 12 100.0%

Line Branch Exec Source
1 #ifndef NFTABLES_UTILS_H
2 #define NFTABLES_UTILS_H
3
4 #include <asm/byteorder.h>
5 #include <stdarg.h>
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <assert.h>
9 #include <limits.h>
10 #include <list.h>
11 #include <gmputil.h>
12
13 #ifdef HAVE_VISIBILITY_HIDDEN
14 # define __visible __attribute__((visibility("default")))
15 # define EXPORT_SYMBOL(x) typeof(x) (x) __visible;
16 #else
17 # define EXPORT_SYMBOL
18 #endif
19
20 #define BITS_PER_BYTE 8
21
22 #define pr_debug(fmt, arg...) printf(fmt, ##arg)
23
24 #if defined(HAVE_LIBGMP)
25 #define pr_gmp_debug(fmt, arg...) gmp_printf(fmt, ##arg)
26 #else
27 #define pr_gmp_debug(fmt, arg...) ({ if (false) {}; 0; })
28 #endif
29
30 #define __fmtstring(x, y) __attribute__((format(printf, x, y)))
31
32 #define __must_check __attribute__((warn_unused_result))
33 #define __noreturn __attribute__((__noreturn__))
34
35 #define BUG(fmt, arg...) ({ fprintf(stderr, "BUG: " fmt "\n", ##arg); assert(0); abort(); })
36
37 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
38 #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
39
40 #define __must_be_array(a) \
41 BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(typeof(a), typeof(&a[0])))
42
43 #define assert_refcount_safe(refcnt) do { \
44 if ((refcnt) == 0) \
45 BUG("refcount was 0"); \
46 if ((refcnt) >= INT_MAX) \
47 BUG("refcount saturated"); \
48 } while (0)
49
50 #define container_of(ptr, type, member) ({ \
51 typeof( ((type *)0)->member ) *__mptr = (ptr); \
52 (type *)( (void *)__mptr - offsetof(type,member) );})
53
54 /**
55 * Return a pointer to a constant variable of a size smaller than the variable.
56 */
57 #ifdef __LITTLE_ENDIAN_BITFIELD
58 #define constant_data_ptr(val, len) \
59 ((void *)&(val))
60 #elif defined(__BIG_ENDIAN_BITFIELD)
61 #define constant_data_ptr(val, len) \
62 ((void *)&(val) + sizeof(val) - div_round_up(len, BITS_PER_BYTE))
63 #else
64 #error "byteorder undefined"
65 #endif
66
67 #define field_sizeof(t, f) (sizeof(((t *)NULL)->f))
68 #define array_size(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
69 #define div_round_up(n, d) (((n) + (d) - 1) / (d))
70 #define round_up(n, b) (div_round_up(n, b) * b)
71
72 #define min(_x, _y) ({ \
73 _x < _y ? _x : _y; })
74
75 #define max(_x, _y) ({ \
76 _x > _y ? _x : _y; })
77
78 #define SNPRINTF_BUFFER_SIZE(ret, len, offset) \
79 ({ \
80 const int _ret = (ret); \
81 size_t *const _len = (len); \
82 size_t *const _offset = (offset); \
83 bool _not_truncated = true; \
84 size_t _ret2; \
85 \
86 assert(_ret >= 0); \
87 \
88 if ((size_t) _ret >= *_len) { \
89 /* Truncated.
90 *
91 * We will leave "len" at zero and increment
92 * "offset" to point one byte after the buffer
93 * (after the terminating NUL byte). */ \
94 _ret2 = *_len; \
95 _not_truncated = false; \
96 } else \
97 _ret2 = (size_t) _ret; \
98 \
99 *_offset += _ret2; \
100 *_len -= _ret2; \
101 \
102 _not_truncated; \
103 })
104
105 #define MSEC_PER_SEC 1000L
106
107 /**
108 * fls - find last (most-significant) bit set
109 * @x: the word to search
110 *
111 * This is defined the same way as ffs.
112 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
113 */
114 95103 static inline int fls(uint32_t x)
115 {
116 95103 int r = 32;
117
118
2/2
✓ Branch 0 (2→3) taken 2 times.
✓ Branch 1 (2→4) taken 95101 times.
95103 if (!x)
119 2 return 0;
120
2/2
✓ Branch 0 (4→5) taken 92939 times.
✓ Branch 1 (4→6) taken 2162 times.
95101 if (!(x & 0xffff0000u)) {
121 92939 x <<= 16;
122 92939 r -= 16;
123 }
124
2/2
✓ Branch 0 (6→7) taken 2130 times.
✓ Branch 1 (6→8) taken 92971 times.
95101 if (!(x & 0xff000000u)) {
125 2130 x <<= 8;
126 2130 r -= 8;
127 }
128
2/2
✓ Branch 0 (8→9) taken 89744 times.
✓ Branch 1 (8→10) taken 5357 times.
95101 if (!(x & 0xf0000000u)) {
129 89744 x <<= 4;
130 89744 r -= 4;
131 }
132
2/2
✓ Branch 0 (10→11) taken 86158 times.
✓ Branch 1 (10→12) taken 8943 times.
95101 if (!(x & 0xc0000000u)) {
133 86158 x <<= 2;
134 86158 r -= 2;
135 }
136
2/2
✓ Branch 0 (12→13) taken 85058 times.
✓ Branch 1 (12→14) taken 10043 times.
95101 if (!(x & 0x80000000u)) {
137 85058 x <<= 1;
138 85058 r -= 1;
139 }
140 95101 return r;
141 }
142
143 extern void __memory_allocation_error(const char *filename, uint32_t line) __noreturn;
144
145 #define memory_allocation_error() \
146 __memory_allocation_error(__FILE__, __LINE__);
147
148 extern void *xmalloc(size_t size);
149 extern void *xmalloc_array(size_t nmemb, size_t size);
150 extern void *xrealloc(void *ptr, size_t size);
151 extern void *xzalloc(size_t size);
152 extern void *xzalloc_array(size_t nmemb, size_t size);
153 extern char *xstrdup(const char *s);
154 extern void xstrunescape(const char *in, char *out);
155 extern int round_pow_2(unsigned int value);
156
157 #endif /* NFTABLES_UTILS_H */
158