]> arthur.barton.de Git - netdata.git/blobdiff - src/inlined.h
dns_query_time plugin: replace "." with "_" in dimensions
[netdata.git] / src / inlined.h
index 788385bf6416c7438fbffb87435cc649e7c354d1..0dc11c950951b3d9dfcf4bda8a8fd615cc5be058 100644 (file)
@@ -3,38 +3,25 @@
 
 #include "common.h"
 
-#ifdef HAVE_STMT_EXPR_NON_EXISTING
-// GCC extension to define a function as a preprocessor macro
-
-#define simple_hash(name) ({                                         \
-    register unsigned char *__hash_source = (unsigned char *)(name); \
-    register uint32_t __hash_value = 0x811c9dc5;                     \
-    while (*__hash_source) {                                         \
-        __hash_value *= 16777619;                                    \
-        __hash_value ^= (uint32_t) *__hash_source++;                 \
-    }                                                                \
-    __hash_value;                                                    \
-})
-
-#define simple_uhash(name) ({                                        \
-    register unsigned char *__hash_source = (unsigned char *)(name); \
-    register uint32_t __hash_value = 0x811c9dc5, __hash_char;        \
-    while ((__hash_char = *__hash_source++)) {                       \
-        if (unlikely(__hash_char >= 'A' && __hash_char <= 'Z'))      \
-            __hash_char += 'a' - 'A';                                \
-        __hash_value *= 16777619;                                    \
-        __hash_value ^= __hash_char;                                 \
-    }                                                                \
-    __hash_value;                                                    \
-})
-
-#else /* ! HAVE_STMT_EXPR */
+#ifdef KERNEL_32BIT
+typedef uint32_t kernel_uint_t;
+#define str2kernel_uint_t(string) str2uint32_t(string)
+#define KERNEL_UINT_FORMAT "%u"
+#else
+typedef uint64_t kernel_uint_t;
+#define str2kernel_uint_t(string) str2uint64_t(string)
+#define KERNEL_UINT_FORMAT "%" PRIu64
+#endif
+
+#define str2pid_t(string) str2uint32_t(string)
+
 
 // for faster execution, allow the compiler to inline
-// these functions that are called to hash strings
+// these functions that are called thousands of times per second
+
 static inline uint32_t simple_hash(const char *name) {
-    register unsigned char *s = (unsigned char *) name;
-    register uint32_t hval = 0x811c9dc5;
+    unsigned char *s = (unsigned char *) name;
+    uint32_t hval = 0x811c9dc5;
     while (*s) {
         hval *= 16777619;
         hval ^= (uint32_t) *s++;
@@ -43,8 +30,8 @@ static inline uint32_t simple_hash(const char *name) {
 }
 
 static inline uint32_t simple_uhash(const char *name) {
-    register unsigned char *s = (unsigned char *) name;
-    register uint32_t hval = 0x811c9dc5, c;
+    unsigned char *s = (unsigned char *) name;
+    uint32_t hval = 0x811c9dc5, c;
     while ((c = *s++)) {
         if (unlikely(c >= 'A' && c <= 'Z')) c += 'a' - 'A';
         hval *= 16777619;
@@ -53,11 +40,37 @@ static inline uint32_t simple_uhash(const char *name) {
     return hval;
 }
 
-#endif /* HAVE_STMT_EXPR */
+static inline int simple_hash_strcmp(const char *name, const char *b, uint32_t *hash) {
+    unsigned char *s = (unsigned char *) name;
+    uint32_t hval = 0x811c9dc5;
+    int ret = 0;
+    while (*s) {
+        if(!ret) ret = *s - *b++;
+        hval *= 16777619;
+        hval ^= (uint32_t) *s++;
+    }
+    *hash = hval;
+    return ret;
+}
+
+static inline int str2i(const char *s) {
+    int n = 0;
+    char c, negative = (*s == '-');
+
+    for(c = (negative)?*(++s):*s; c >= '0' && c <= '9' ; c = *(++s)) {
+        n *= 10;
+        n += c - '0';
+    }
+
+    if(unlikely(negative))
+        return -n;
+
+    return n;
+}
 
 static inline long str2l(const char *s) {
-    register long n = 0;
-    register char c, negative = (*s == '-');
+    long n = 0;
+    char c, negative = (*s == '-');
 
     for(c = (negative)?*(++s):*s; c >= '0' && c <= '9' ; c = *(++s)) {
         n *= 10;
@@ -70,9 +83,29 @@ static inline long str2l(const char *s) {
     return n;
 }
 
+static inline uint32_t str2uint32_t(const char *s) {
+    uint32_t n = 0;
+    char c;
+    for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
+        n *= 10;
+        n += c - '0';
+    }
+    return n;
+}
+
+static inline uint64_t str2uint64_t(const char *s) {
+    uint64_t n = 0;
+    char c;
+    for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
+        n *= 10;
+        n += c - '0';
+    }
+    return n;
+}
+
 static inline unsigned long str2ul(const char *s) {
-    register unsigned long n = 0;
-    register char c;
+    unsigned long n = 0;
+    char c;
     for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
         n *= 10;
         n += c - '0';
@@ -81,8 +114,8 @@ static inline unsigned long str2ul(const char *s) {
 }
 
 static inline unsigned long long str2ull(const char *s) {
-    register unsigned long long n = 0;
-    register char c;
+    unsigned long long n = 0;
+    char c;
     for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
         n *= 10;
         n += c - '0';
@@ -90,8 +123,32 @@ static inline unsigned long long str2ull(const char *s) {
     return n;
 }
 
+#ifdef NETDATA_STRCMP_OVERRIDE
+#ifdef strcmp
+#undef strcmp
+#endif
+#define strcmp(a, b) strsame(a, b)
+#endif // NETDATA_STRCMP_OVERRIDE
+
+static inline int strsame(const char *a, const char *b) {
+    if(unlikely(a == b)) return 0;
+    while(*a && *a == *b) { a++; b++; }
+    return *a - *b;
+}
+
+static inline char *strncpyz(char *dst, const char *src, size_t n) {
+    char *p = dst;
+
+    while (*src && n--)
+        *dst++ = *src++;
+
+    *dst = '\0';
+
+    return p;
+}
+
 static inline int read_single_number_file(const char *filename, unsigned long long *result) {
-    char buffer[1024 + 1];
+    char buffer[30 + 1];
 
     int fd = open(filename, O_RDONLY, 0666);
     if(unlikely(fd == -1)) {
@@ -99,7 +156,7 @@ static inline int read_single_number_file(const char *filename, unsigned long lo
         return 1;
     }
 
-    ssize_t r = read(fd, buffer, 1024);
+    ssize_t r = read(fd, buffer, 30);
     if(unlikely(r == -1)) {
         *result = 0;
         close(fd);
@@ -107,7 +164,8 @@ static inline int read_single_number_file(const char *filename, unsigned long lo
     }
 
     close(fd);
-    *result = strtoull(buffer, NULL, 0);
+    buffer[30] = '\0';
+    *result = str2ull(buffer);
     return 0;
 }