]> git.proxmox.com Git - systemd.git/blob - src/basic/util.h
New upstream version 242
[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 static inline const char* yes_no(bool b) {
9 return b ? "yes" : "no";
10 }
11
12 static inline const char* true_false(bool b) {
13 return b ? "true" : "false";
14 }
15
16 static inline const char* one_zero(bool b) {
17 return b ? "1" : "0";
18 }
19
20 static inline const char* enable_disable(bool b) {
21 return b ? "enable" : "disable";
22 }
23
24 extern int saved_argc;
25 extern char **saved_argv;
26
27 static inline void save_argc_argv(int argc, char **argv) {
28 saved_argc = argc;
29 saved_argv = argv;
30 }
31
32 bool kexec_loaded(void);
33
34 int prot_from_flags(int flags) _const_;
35
36 bool in_initrd(void);
37 void in_initrd_force(bool value);
38
39 int on_ac_power(void);
40
41 static inline unsigned u64log2(uint64_t n) {
42 #if __SIZEOF_LONG_LONG__ == 8
43 return (n > 1) ? (unsigned) __builtin_clzll(n) ^ 63U : 0;
44 #else
45 #error "Wut?"
46 #endif
47 }
48
49 static inline unsigned u32ctz(uint32_t n) {
50 #if __SIZEOF_INT__ == 4
51 return n != 0 ? __builtin_ctz(n) : 32;
52 #else
53 #error "Wut?"
54 #endif
55 }
56
57 static inline unsigned log2i(int x) {
58 assert(x > 0);
59
60 return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1;
61 }
62
63 static inline unsigned log2u(unsigned x) {
64 assert(x > 0);
65
66 return sizeof(unsigned) * 8 - __builtin_clz(x) - 1;
67 }
68
69 static inline unsigned log2u_round_up(unsigned x) {
70 assert(x > 0);
71
72 if (x == 1)
73 return 0;
74
75 return log2u(x - 1) + 1;
76 }
77
78 int container_get_leader(const char *machine, pid_t *pid);
79
80 int version(void);
81
82 int str_verscmp(const char *s1, const char *s2);
83
84 void disable_coredumps(void);