]> arthur.barton.de Git - netdata.git/blob - src/inlined.h
a18564f58d43ed41d0af73be79c4baaa3e794b1c
[netdata.git] / src / inlined.h
1 #ifndef NETDATA_INLINED_H
2 #define NETDATA_INLINED_H
3
4 #include "common.h"
5
6 #ifdef HAVE_STMT_EXPR_NON_EXISTING
7 // GCC extension to define a function as a preprocessor macro
8
9 #define simple_hash(name) ({                                         \
10     register unsigned char *__hash_source = (unsigned char *)(name); \
11     register uint32_t __hash_value = 0x811c9dc5;                     \
12     while (*__hash_source) {                                         \
13         __hash_value *= 16777619;                                    \
14         __hash_value ^= (uint32_t) *__hash_source++;                 \
15     }                                                                \
16     __hash_value;                                                    \
17 })
18
19 #define simple_uhash(name) ({                                        \
20     register unsigned char *__hash_source = (unsigned char *)(name); \
21     register uint32_t __hash_value = 0x811c9dc5, __hash_char;        \
22     while ((__hash_char = *__hash_source++)) {                       \
23         if (unlikely(__hash_char >= 'A' && __hash_char <= 'Z'))      \
24             __hash_char += 'a' - 'A';                                \
25         __hash_value *= 16777619;                                    \
26         __hash_value ^= __hash_char;                                 \
27     }                                                                \
28     __hash_value;                                                    \
29 })
30
31 #else /* ! HAVE_STMT_EXPR */
32
33 // for faster execution, allow the compiler to inline
34 // these functions that are called to hash strings
35 static inline uint32_t simple_hash(const char *name) {
36     register unsigned char *s = (unsigned char *) name;
37     register uint32_t hval = 0x811c9dc5;
38     while (*s) {
39         hval *= 16777619;
40         hval ^= (uint32_t) *s++;
41     }
42     return hval;
43 }
44
45 static inline uint32_t simple_uhash(const char *name) {
46     register unsigned char *s = (unsigned char *) name;
47     register uint32_t hval = 0x811c9dc5, c;
48     while ((c = *s++)) {
49         if (unlikely(c >= 'A' && c <= 'Z')) c += 'a' - 'A';
50         hval *= 16777619;
51         hval ^= c;
52     }
53     return hval;
54 }
55
56 #endif /* HAVE_STMT_EXPR */
57
58 static inline int str2i(const char *s) {
59     register int n = 0;
60     register 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 long str2l(const char *s) {
74     register long n = 0;
75     register char c, negative = (*s == '-');
76
77     for(c = (negative)?*(++s):*s; c >= '0' && c <= '9' ; c = *(++s)) {
78         n *= 10;
79         n += c - '0';
80     }
81
82     if(unlikely(negative))
83         return -n;
84
85     return n;
86 }
87
88 static inline unsigned long str2ul(const char *s) {
89     register unsigned long n = 0;
90     register char c;
91     for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
92         n *= 10;
93         n += c - '0';
94     }
95     return n;
96 }
97
98 static inline unsigned long long str2ull(const char *s) {
99     register unsigned long long n = 0;
100     register char c;
101     for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
102         n *= 10;
103         n += c - '0';
104     }
105     return n;
106 }
107
108 static inline int read_single_number_file(const char *filename, unsigned long long *result) {
109     char buffer[1024 + 1];
110
111     int fd = open(filename, O_RDONLY, 0666);
112     if(unlikely(fd == -1)) {
113         *result = 0;
114         return 1;
115     }
116
117     ssize_t r = read(fd, buffer, 1024);
118     if(unlikely(r == -1)) {
119         *result = 0;
120         close(fd);
121         return 2;
122     }
123
124     close(fd);
125     *result = str2ull(buffer);
126     return 0;
127 }
128
129 #endif //NETDATA_INLINED_H