-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathutils.hpp
More file actions
228 lines (178 loc) · 7.1 KB
/
utils.hpp
File metadata and controls
228 lines (178 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* Utilities.
*
* Author: Steffen Vogel <post@steffenvogel.de>
* Author: Daniel Krebs <github@daniel-krebs.net>
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <filesystem>
#include <list>
#include <string>
#include <vector>
#include <cassert>
#include <cstdint>
#include <cstdlib>
#include <sched.h>
#include <signal.h>
#include <sys/types.h>
#include <openssl/sha.h>
#include <villas/config.hpp>
#ifdef __GNUC__
#define LIKELY(x) __builtin_expect((x), 1)
#define UNLIKELY(x) __builtin_expect((x), 0)
#else
#define LIKELY(x) (x)
#define UNLIKELY(x) (x)
#endif
// Check assertion and exit if failed.
#ifndef assert
#define assert(exp) \
do { \
if (!EXPECT(exp, 0)) \
error("Assertion failed: '%s' in %s(), %s:%d", XSTR(exp), __FUNCTION__, \
__BASE_FILE__, __LINE__); \
} while (0)
#endif
// CPP stringification
#define XSTR(x) STR(x)
#define STR(x) #x
#define CONCAT_DETAIL(x, y) x##y
#define CONCAT(x, y) CONCAT_DETAIL(x, y)
#define UNIQUE(x) CONCAT(x, __COUNTER__)
#ifdef ALIGN
#undef ALIGN
#endif
#define ALIGN(x, a) ALIGN_MASK(x, (uintptr_t)(a)-1)
#define ALIGN_MASK(x, m) (((uintptr_t)(x) + (m)) & ~(m))
#define IS_ALIGNED(x, a) (ALIGN(x, a) == (uintptr_t)x)
#define SWAP(x, y) \
do { \
auto t = x; \
x = y; \
y = t; \
} while (0)
// Round-up integer division
#define CEIL(x, y) (((x) + (y)-1) / (y))
// Get nearest up-rounded power of 2
#define LOG2_CEIL(x) (1 << (villas::utils::log2i((x)-1) + 1))
// Check if the number is a power of 2
#define IS_POW2(x) (((x) != 0) && !((x) & ((x)-1)))
// Calculate the number of elements in an array.
#define ARRAY_LEN(a) (sizeof(a) / sizeof(a)[0])
// Return the bigger value
#ifdef MAX
#undef MAX
#endif
#define MAX(a, b) \
({ \
__typeof__(a) _a = (a); \
__typeof__(b) _b = (b); \
_a > _b ? _a : _b; \
})
// Return the smaller value
#ifdef MIN
#undef MIN
#endif
#define MIN(a, b) \
({ \
__typeof__(a) _a = (a); \
__typeof__(b) _b = (b); \
_a < _b ? _a : _b; \
})
#define MIN3(a, b, c) MIN(MIN((a), (b)), (c))
#ifndef offsetof
#define offsetof(type, member) __builtin_offsetof(type, member)
#endif
#ifndef container_of
#define container_of(ptr, type, member) \
({ \
const typeof(((type *)0)->member) *__mptr = (ptr); \
(type *)((char *)__mptr - offsetof(type, member)); \
})
#endif
#define BITS_PER_LONGLONG (sizeof(long long) * 8)
// Some helper macros
#define BITMASK(h, l) \
(((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONGLONG - 1 - (h))))
#define BIT(nr) (1UL << (nr))
namespace villas {
namespace utils {
std::vector<std::string> tokenize(std::string s, const std::string &delimiter);
template <typename T> void assertExcept(bool condition, const T &exception) {
if (not condition)
throw exception;
}
// Register a exit callback for program termination: SIGINT, SIGKILL & SIGALRM.
int signalsInit(void (*cb)(int signal, siginfo_t *sinfo, void *ctx),
std::list<int> cbSignals = {},
std::list<int> ignoreSignals = {SIGCHLD})
__attribute__((warn_unused_result));
// Fill buffer with random data.
ssize_t readRandom(char *buf, size_t len);
// Remove ANSI control sequences for colored output.
char *decolor(char *str);
// Normal random variate generator using the Box-Muller method
//
// @param m Mean
// @param s Standard deviation
// @return Normal variate random variable (Gaussian)
double boxMuller(float m, float s);
// Double precission uniform random variable
double randf();
// Concat formatted string to an existing string.
//
// This function uses realloc() to resize the destination.
// Please make sure to only on dynamic allocated destionations!!!
//
// @param dest A pointer to a malloc() allocated memory region
// @param fmt A format string like for printf()
// @param ... Optional parameters like for printf()
// @retval The the new value of the dest buffer.
char *strcatf(char **dest, const char *fmt, ...)
__attribute__((format(printf, 2, 3)));
// Variadic version of strcatf()
char *vstrcatf(char **dest, const char *fmt, va_list va)
__attribute__((format(printf, 2, 0)));
char *strf(const char *fmt, ...);
char *vstrf(const char *fmt, va_list va);
// Allocate and copy memory.
void *memdup(const void *src, size_t bytes);
// Call quit() in the main thread.
void die();
// Get log2 of long long integers
int log2i(long long x);
// Send signal \p sig to main thread.
void killme(int sig);
pid_t spawn(const char *name, char *const argv[]);
// Determines the string length as printed on the screen (ignores escable sequences).
size_t strlenp(const char *str);
// Calculate SHA1 hash of complete file \p f and place it into \p sha1.
//
// @param sha1[out] Must be SHA_DIGEST_LENGTH (20) in size.
// @retval 0 Everything was okay.
int sha1sum(FILE *f, unsigned char *sha1);
// Check if process is running inside a Docker container.
bool isDocker();
// Check if process is running inside a Kubernetes container.
bool isKubernetes();
// Check if process is running inside a containerized environment.
bool isContainer();
// Check if the process is running in a privileged environment (has SYS_ADMIN capability).
bool isPrivileged();
// helper type for std::visit
template <class... Ts> struct overloaded : Ts... {
using Ts::operator()...;
};
// Explicit deduction guide (not needed as of C++20)
template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
void write_to_file(std::string data, const std::filesystem::path file);
std::vector<std::string>
read_names_in_directory(const std::filesystem::path &directory);
namespace base64 {
using byte = std::uint8_t;
std::string encode(const std::vector<byte> &input);
std::vector<byte> decode(const std::string &input);
} // namespace base64
} // namespace utils
} // namespace villas