]> arthur.barton.de Git - netdata.git/blob - src/inlined.h
c862bf805a7340f45008fe8b617b11dbd1455407
[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
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 read_single_number_file(const char *filename, unsigned long long *result) {
59     char buffer[1024 + 1];
60
61     int fd = open(filename, O_RDONLY, 0666);
62     if(unlikely(fd == -1)) {
63         *result = 0;
64         return 1;
65     }
66
67     ssize_t r = read(fd, buffer, 1024);
68     if(unlikely(r == -1)) {
69         *result = 0;
70         close(fd);
71         return 2;
72     }
73
74     close(fd);
75     *result = strtoull(buffer, NULL, 0);
76     return 0;
77 }
78
79 #endif //NETDATA_INLINED_H