]> arthur.barton.de Git - netdata.git/blob - src/inlined.h
reverted strcmp()
[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     unsigned char *s = (unsigned char *) name;
11     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     unsigned char *s = (unsigned char *) name;
21     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 simple_hash_strcmp(const char *name, const char *b, uint32_t *hash) {
31     unsigned char *s = (unsigned char *) name;
32     uint32_t hval = 0x811c9dc5;
33     int ret = 0;
34     while (*s) {
35         if(!ret) ret = *s - *b++;
36         hval *= 16777619;
37         hval ^= (uint32_t) *s++;
38     }
39     *hash = hval;
40     return ret;
41 }
42
43 static inline int str2i(const char *s) {
44     int n = 0;
45     char c, negative = (*s == '-');
46
47     for(c = (negative)?*(++s):*s; c >= '0' && c <= '9' ; c = *(++s)) {
48         n *= 10;
49         n += c - '0';
50     }
51
52     if(unlikely(negative))
53         return -n;
54
55     return n;
56 }
57
58 static inline long str2l(const char *s) {
59     long n = 0;
60     char c, negative = (*s == '-');
61
62     for(c = (negative)?*(++s):*s; c >= '0' && c <= '9' ; c = *(++s)) {
63         n *= 10;
64         n += c - '0';
65     }
66
67     if(unlikely(negative))
68         return -n;
69
70     return n;
71 }
72
73 static inline unsigned long str2ul(const char *s) {
74     unsigned long n = 0;
75     char c;
76     for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
77         n *= 10;
78         n += c - '0';
79     }
80     return n;
81 }
82
83 static inline unsigned long long str2ull(const char *s) {
84     unsigned long long n = 0;
85     char c;
86     for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
87         n *= 10;
88         n += c - '0';
89     }
90     return n;
91 }
92
93 #ifdef NETDATA_STRCMP_OVERRIDE
94 static inline int strcmp(const char *a, const char *b) {
95     if(unlikely(a == b)) return 0;
96     while(*a && *a == *b) { a++; b++; }
97     return *a - *b;
98 }
99 #endif // NETDATA_STRSAME
100
101 static inline int read_single_number_file(const char *filename, unsigned long long *result) {
102     char buffer[30 + 1];
103
104     int fd = open(filename, O_RDONLY, 0666);
105     if(unlikely(fd == -1)) {
106         *result = 0;
107         return 1;
108     }
109
110     ssize_t r = read(fd, buffer, 30);
111     if(unlikely(r == -1)) {
112         *result = 0;
113         close(fd);
114         return 2;
115     }
116
117     close(fd);
118     buffer[30] = '\0';
119     *result = str2ull(buffer);
120     return 0;
121 }
122
123 #endif //NETDATA_INLINED_H