]> arthur.barton.de Git - netdata.git/blob - src/inlined.h
remove statement expressions - they are not faster
[netdata.git] / src / inlined.h
1 #ifndef NETDATA_INLINED_H
2 #define NETDATA_INLINED_H
3
4 #include "common.h"
5
6 // for faster execution, allow the compiler to inline
7 // these functions that are called thousands of times per second
8
9 static inline uint32_t simple_hash(const char *name) {
10     register unsigned char *s = (unsigned char *) name;
11     register uint32_t hval = 0x811c9dc5;
12     while (*s) {
13         hval *= 16777619;
14         hval ^= (uint32_t) *s++;
15     }
16     return hval;
17 }
18
19 static inline uint32_t simple_uhash(const char *name) {
20     register unsigned char *s = (unsigned char *) name;
21     register uint32_t hval = 0x811c9dc5, c;
22     while ((c = *s++)) {
23         if (unlikely(c >= 'A' && c <= 'Z')) c += 'a' - 'A';
24         hval *= 16777619;
25         hval ^= c;
26     }
27     return hval;
28 }
29
30 static inline int str2i(const char *s) {
31     register int n = 0;
32     register char c, negative = (*s == '-');
33
34     for(c = (negative)?*(++s):*s; c >= '0' && c <= '9' ; c = *(++s)) {
35         n *= 10;
36         n += c - '0';
37     }
38
39     if(unlikely(negative))
40         return -n;
41
42     return n;
43 }
44
45 static inline long str2l(const char *s) {
46     register long n = 0;
47     register char c, negative = (*s == '-');
48
49     for(c = (negative)?*(++s):*s; c >= '0' && c <= '9' ; c = *(++s)) {
50         n *= 10;
51         n += c - '0';
52     }
53
54     if(unlikely(negative))
55         return -n;
56
57     return n;
58 }
59
60 static inline unsigned long str2ul(const char *s) {
61     register unsigned long n = 0;
62     register char c;
63     for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
64         n *= 10;
65         n += c - '0';
66     }
67     return n;
68 }
69
70 static inline unsigned long long str2ull(const char *s) {
71     register unsigned long long n = 0;
72     register char c;
73     for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
74         n *= 10;
75         n += c - '0';
76     }
77     return n;
78 }
79
80 static inline int read_single_number_file(const char *filename, unsigned long long *result) {
81     char buffer[1024 + 1];
82
83     int fd = open(filename, O_RDONLY, 0666);
84     if(unlikely(fd == -1)) {
85         *result = 0;
86         return 1;
87     }
88
89     ssize_t r = read(fd, buffer, 1024);
90     if(unlikely(r == -1)) {
91         *result = 0;
92         close(fd);
93         return 2;
94     }
95
96     close(fd);
97     *result = str2ull(buffer);
98     return 0;
99 }
100
101 #endif //NETDATA_INLINED_H