1 #ifndef NETDATA_INLINED_H
2 #define NETDATA_INLINED_H
6 #ifdef HAVE_STMT_EXPR_NON_EXISTING
7 // GCC extension to define a function as a preprocessor macro
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++; \
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; \
31 #else /* ! HAVE_STMT_EXPR */
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;
40 hval ^= (uint32_t) *s++;
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;
49 if (unlikely(c >= 'A' && c <= 'Z')) c += 'a' - 'A';
56 #endif /* HAVE_STMT_EXPR */
58 static inline int str2i(const char *s) {
60 register char c, negative = (*s == '-');
62 for(c = (negative)?*(++s):*s; c >= '0' && c <= '9' ; c = *(++s)) {
67 if(unlikely(negative))
73 static inline long str2l(const char *s) {
75 register char c, negative = (*s == '-');
77 for(c = (negative)?*(++s):*s; c >= '0' && c <= '9' ; c = *(++s)) {
82 if(unlikely(negative))
88 static inline unsigned long str2ul(const char *s) {
89 register unsigned long n = 0;
91 for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
98 static inline unsigned long long str2ull(const char *s) {
99 register unsigned long long n = 0;
101 for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
108 static inline int read_single_number_file(const char *filename, unsigned long long *result) {
109 char buffer[1024 + 1];
111 int fd = open(filename, O_RDONLY, 0666);
112 if(unlikely(fd == -1)) {
117 ssize_t r = read(fd, buffer, 1024);
118 if(unlikely(r == -1)) {
125 *result = str2ull(buffer);
129 #endif //NETDATA_INLINED_H