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
229
230
231
232
233
234
|
// SPDX-License-Identifier: GPL-3.0-or-later
#ifndef NETDATA_INLINED_H
#define NETDATA_INLINED_H 1
#include "libnetdata.h"
#ifdef KERNEL_32BIT
typedef uint32_t kernel_uint_t;
#define str2kernel_uint_t(string) str2uint32_t(string)
#define KERNEL_UINT_FORMAT "%u"
#else
typedef uint64_t kernel_uint_t;
#define str2kernel_uint_t(string) str2uint64_t(string)
#define KERNEL_UINT_FORMAT "%" PRIu64
#endif
#define str2pid_t(string) str2uint32_t(string)
// for faster execution, allow the compiler to inline
// these functions that are called thousands of times per second
static inline uint32_t simple_hash(const char *name) {
unsigned char *s = (unsigned char *) name;
uint32_t hval = 0x811c9dc5;
while (*s) {
hval *= 16777619;
hval ^= (uint32_t) *s++;
}
return hval;
}
static inline uint32_t simple_uhash(const char *name) {
unsigned char *s = (unsigned char *) name;
uint32_t hval = 0x811c9dc5, c;
while ((c = *s++)) {
if (unlikely(c >= 'A' && c <= 'Z')) c += 'a' - 'A';
hval *= 16777619;
hval ^= c;
}
return hval;
}
static inline int simple_hash_strcmp(const char *name, const char *b, uint32_t *hash) {
unsigned char *s = (unsigned char *) name;
uint32_t hval = 0x811c9dc5;
int ret = 0;
while (*s) {
if(!ret) ret = *s - *b++;
hval *= 16777619;
hval ^= (uint32_t) *s++;
}
*hash = hval;
return ret;
}
static inline int str2i(const char *s) {
int n = 0;
char c, negative = (*s == '-');
for(c = (negative)?*(++s):*s; c >= '0' && c <= '9' ; c = *(++s)) {
n *= 10;
n += c - '0';
}
if(unlikely(negative))
return -n;
return n;
}
static inline long str2l(const char *s) {
long n = 0;
char c, negative = (*s == '-');
for(c = (negative)?*(++s):*s; c >= '0' && c <= '9' ; c = *(++s)) {
n *= 10;
n += c - '0';
}
if(unlikely(negative))
return -n;
return n;
}
static inline uint32_t str2uint32_t(const char *s) {
uint32_t n = 0;
char c;
for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
n *= 10;
n += c - '0';
}
return n;
}
static inline uint64_t str2uint64_t(const char *s) {
uint64_t n = 0;
char c;
for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
n *= 10;
n += c - '0';
}
return n;
}
static inline unsigned long str2ul(const char *s) {
unsigned long n = 0;
char c;
for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
n *= 10;
n += c - '0';
}
return n;
}
static inline unsigned long long str2ull(const char *s) {
unsigned long long n = 0;
char c;
for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
n *= 10;
n += c - '0';
}
return n;
}
static inline long long str2ll(const char *s, char **endptr) {
int negative = 0;
if(unlikely(*s == '-')) {
s++;
negative = 1;
}
else if(unlikely(*s == '+'))
s++;
long long n = 0;
char c;
for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
n *= 10;
n += c - '0';
}
if(unlikely(endptr))
*endptr = (char *)s;
if(unlikely(negative))
return -n;
else
return n;
}
static inline char *strncpyz(char *dst, const char *src, size_t n) {
char *p = dst;
while (*src && n--)
*dst++ = *src++;
*dst = '\0';
return p;
}
static inline void sanitize_json_string(char *dst, const char *src, size_t dst_size) {
while (*src != '\0' && dst_size > 1) {
if (*src < 0x1F) {
*dst++ = '_';
src++;
dst_size--;
}
else if (*src == '\\' || *src == '\"') {
*dst++ = '\\';
*dst++ = *src++;
dst_size -= 2;
}
else {
*dst++ = *src++;
dst_size--;
}
}
*dst = '\0';
}
static inline int read_file(const char *filename, char *buffer, size_t size) {
if(unlikely(!size)) return 3;
int fd = open(filename, O_RDONLY, 0666);
if(unlikely(fd == -1)) {
buffer[0] = '\0';
return 1;
}
ssize_t r = read(fd, buffer, size);
if(unlikely(r == -1)) {
buffer[0] = '\0';
close(fd);
return 2;
}
buffer[r] = '\0';
close(fd);
return 0;
}
static inline int read_single_number_file(const char *filename, unsigned long long *result) {
char buffer[30 + 1];
int ret = read_file(filename, buffer, 30);
if(unlikely(ret)) {
*result = 0;
return ret;
}
buffer[30] = '\0';
*result = str2ull(buffer);
return 0;
}
static inline int read_single_signed_number_file(const char *filename, long long *result) {
char buffer[30 + 1];
int ret = read_file(filename, buffer, 30);
|