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