]> arthur.barton.de Git - netdata.git/blob - src/inlined.h
tomcat plugin: some code optimization
[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 #ifdef strcmp
95 #undef strcmp
96 #endif
97 #define strcmp(a, b) strsame(a, b)
98 static inline int strsame(const char *a, const char *b) {
99     if(unlikely(a == b)) return 0;
100     while(*a && *a == *b) { a++; b++; }
101     return *a - *b;
102 }
103 #endif // NETDATA_STRSAME
104
105 static inline int read_single_number_file(const char *filename, unsigned long long *result) {
106     char buffer[30 + 1];
107
108     int fd = open(filename, O_RDONLY, 0666);
109     if(unlikely(fd == -1)) {
110         *result = 0;
111         return 1;
112     }
113
114     ssize_t r = read(fd, buffer, 30);
115     if(unlikely(r == -1)) {
116         *result = 0;
117         close(fd);
118         return 2;
119     }
120
121     close(fd);
122     buffer[30] = '\0';
123     *result = str2ull(buffer);
124     return 0;
125 }
126
127 #endif //NETDATA_INLINED_H