]> arthur.barton.de Git - netdata.git/blob - src/inlined.h
Merge pull request #1998 from ktsaou/master
[netdata.git] / src / inlined.h
1 #ifndef NETDATA_INLINED_H
2 #define NETDATA_INLINED_H
3
4 #include "common.h"
5
6 #ifdef KERNEL_32BIT
7 typedef uint32_t kernel_uint_t;
8 #define str2kernel_uint_t(string) str2uint32_t(string)
9 #define KERNEL_UINT_FORMAT "%u"
10 #else
11 typedef uint64_t kernel_uint_t;
12 #define str2kernel_uint_t(string) str2uint64_t(string)
13 #define KERNEL_UINT_FORMAT "%" PRIu64
14 #endif
15
16 #define str2pid_t(string) str2uint32_t(string)
17
18
19 // for faster execution, allow the compiler to inline
20 // these functions that are called thousands of times per second
21
22 static inline uint32_t simple_hash(const char *name) {
23     unsigned char *s = (unsigned char *) name;
24     uint32_t hval = 0x811c9dc5;
25     while (*s) {
26         hval *= 16777619;
27         hval ^= (uint32_t) *s++;
28     }
29     return hval;
30 }
31
32 static inline uint32_t simple_uhash(const char *name) {
33     unsigned char *s = (unsigned char *) name;
34     uint32_t hval = 0x811c9dc5, c;
35     while ((c = *s++)) {
36         if (unlikely(c >= 'A' && c <= 'Z')) c += 'a' - 'A';
37         hval *= 16777619;
38         hval ^= c;
39     }
40     return hval;
41 }
42
43 static inline int simple_hash_strcmp(const char *name, const char *b, uint32_t *hash) {
44     unsigned char *s = (unsigned char *) name;
45     uint32_t hval = 0x811c9dc5;
46     int ret = 0;
47     while (*s) {
48         if(!ret) ret = *s - *b++;
49         hval *= 16777619;
50         hval ^= (uint32_t) *s++;
51     }
52     *hash = hval;
53     return ret;
54 }
55
56 static inline int str2i(const char *s) {
57     int n = 0;
58     char c, negative = (*s == '-');
59
60     for(c = (negative)?*(++s):*s; c >= '0' && c <= '9' ; c = *(++s)) {
61         n *= 10;
62         n += c - '0';
63     }
64
65     if(unlikely(negative))
66         return -n;
67
68     return n;
69 }
70
71 static inline long str2l(const char *s) {
72     long n = 0;
73     char c, negative = (*s == '-');
74
75     for(c = (negative)?*(++s):*s; c >= '0' && c <= '9' ; c = *(++s)) {
76         n *= 10;
77         n += c - '0';
78     }
79
80     if(unlikely(negative))
81         return -n;
82
83     return n;
84 }
85
86 static inline uint32_t str2uint32_t(const char *s) {
87     uint32_t n = 0;
88     char c;
89     for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
90         n *= 10;
91         n += c - '0';
92     }
93     return n;
94 }
95
96 static inline uint64_t str2uint64_t(const char *s) {
97     uint64_t n = 0;
98     char c;
99     for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
100         n *= 10;
101         n += c - '0';
102     }
103     return n;
104 }
105
106 static inline unsigned long str2ul(const char *s) {
107     unsigned long n = 0;
108     char c;
109     for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
110         n *= 10;
111         n += c - '0';
112     }
113     return n;
114 }
115
116 static inline unsigned long long str2ull(const char *s) {
117     unsigned long long n = 0;
118     char c;
119     for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
120         n *= 10;
121         n += c - '0';
122     }
123     return n;
124 }
125
126 #ifdef NETDATA_STRCMP_OVERRIDE
127 #ifdef strcmp
128 #undef strcmp
129 #endif
130 #define strcmp(a, b) strsame(a, b)
131 #endif // NETDATA_STRCMP_OVERRIDE
132
133 static inline int strsame(const char *a, const char *b) {
134     if(unlikely(a == b)) return 0;
135     while(*a && *a == *b) { a++; b++; }
136     return *a - *b;
137 }
138
139 static inline char *strncpyz(char *dst, const char *src, size_t n) {
140     char *p = dst;
141
142     while (*src && n--)
143         *dst++ = *src++;
144
145     *dst = '\0';
146
147     return p;
148 }
149
150 static inline int read_single_number_file(const char *filename, unsigned long long *result) {
151     char buffer[30 + 1];
152
153     int fd = open(filename, O_RDONLY, 0666);
154     if(unlikely(fd == -1)) {
155         *result = 0;
156         return 1;
157     }
158
159     ssize_t r = read(fd, buffer, 30);
160     if(unlikely(r == -1)) {
161         *result = 0;
162         close(fd);
163         return 2;
164     }
165
166     close(fd);
167     buffer[30] = '\0';
168     *result = str2ull(buffer);
169     return 0;
170 }
171
172 #endif //NETDATA_INLINED_H