]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/util.h
license: LGPL-2.1+ -> LGPL-2.1-or-later
[thirdparty/systemd.git] / src / basic / util.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <stdint.h>
5
6 #include "macro.h"
7
8 extern int saved_argc;
9 extern char **saved_argv;
10
11 static inline void save_argc_argv(int argc, char **argv) {
12 saved_argc = argc;
13 saved_argv = argv;
14 }
15
16 bool kexec_loaded(void);
17
18 int prot_from_flags(int flags) _const_;
19
20 bool in_initrd(void);
21 void in_initrd_force(bool value);
22
23 int on_ac_power(void);
24
25 static inline unsigned u64log2(uint64_t n) {
26 #if __SIZEOF_LONG_LONG__ == 8
27 return (n > 1) ? (unsigned) __builtin_clzll(n) ^ 63U : 0;
28 #else
29 #error "Wut?"
30 #endif
31 }
32
33 static inline unsigned u32ctz(uint32_t n) {
34 #if __SIZEOF_INT__ == 4
35 return n != 0 ? __builtin_ctz(n) : 32;
36 #else
37 #error "Wut?"
38 #endif
39 }
40
41 static inline unsigned log2i(int x) {
42 assert(x > 0);
43
44 return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1;
45 }
46
47 static inline unsigned log2u(unsigned x) {
48 assert(x > 0);
49
50 return sizeof(unsigned) * 8 - __builtin_clz(x) - 1;
51 }
52
53 static inline unsigned log2u_round_up(unsigned x) {
54 assert(x > 0);
55
56 if (x == 1)
57 return 0;
58
59 return log2u(x - 1) + 1;
60 }
61
62 int container_get_leader(const char *machine, pid_t *pid);
63
64 int version(void);
65
66 int str_verscmp(const char *s1, const char *s2);
67
68 void disable_coredumps(void);