]> arthur.barton.de Git - netdata.git/blob - src/common.c
replaced simple_hash with the FNV-1a 32bit algorithm
[netdata.git] / src / common.c
1 // enable O_NOATIME
2 #define _GNU_SOURCE
3
4 #include <string.h>
5 #include <ctype.h>
6 #include <errno.h>
7 #include <unistd.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <sys/mman.h>
12
13
14 #include "log.h"
15 #include "common.h"
16
17 /*
18 // http://stackoverflow.com/questions/7666509/hash-function-for-string
19 uint32_t simple_hash(const char *name)
20 {
21         const char *s = name;
22         uint32_t hash = 5381;
23         int i;
24
25         while((i = *s++)) hash = ((hash << 5) + hash) + i;
26
27         // fprintf(stderr, "HASH: %lu %s\n", hash, name);
28
29         return hash;
30 }
31 */
32
33 // http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
34 uint32_t simple_hash(const char *name) {
35         unsigned char *s = (unsigned char *)name;
36         uint32_t hval = 0x811c9dc5;
37
38         // FNV-1a algorithm
39         while (*s) {
40                 // multiply by the 32 bit FNV magic prime mod 2^32
41                 // gcc optimized
42                 hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
43
44                 // xor the bottom with the current octet
45                 hval ^= (uint32_t)*s++;
46         }
47
48         // fprintf(stderr, "HASH: %u = %s\n", hval, name);
49         return hval;
50 }
51
52
53 void strreverse(char* begin, char* end)
54 {
55     char aux;
56     while (end > begin)
57         aux = *end, *end-- = *begin, *begin++ = aux;
58 }
59
60 char *mystrsep(char **ptr, char *s)
61 {
62         char *p = "";
63         while ( p && !p[0] && *ptr ) p = strsep(ptr, s);
64         return(p);
65 }
66
67 // like strsep() but:
68 // it trims spaces before and after each value
69 // it accepts quoted values in single or double quotes
70 char *qstrsep(char **ptr)
71 {
72         if(!*ptr || !**ptr) return NULL;
73         
74         char *s, *p = *ptr;
75
76         // skip leading spaces
77         while(isspace(*p)) p++;
78
79         // if the first char is a quote, assume quoted
80         if(*p == '"' || *p == '\'') {
81                 char q = *p;
82                 s = ++p;
83                 while(*p && *p != q) p++;
84
85                 if(*p == q) {
86                         *p = '\0';
87                         p++;
88                 }
89
90                 *ptr = p;
91                 return s;
92         }
93
94         s = p;
95         while(*p && !isspace(*p)) p++;
96         if(!*p) *ptr = NULL;
97         else {
98                 *p = '\0';
99                 *ptr = ++p;
100         }
101
102         return s;
103 }
104
105 char *trim(char *s)
106 {
107         // skip leading spaces
108         while(*s && isspace(*s)) s++;
109         if(!*s || *s == '#') return NULL;
110
111         // skip tailing spaces
112         int c = strlen(s) - 1;
113         while(c >= 0 && isspace(s[c])) {
114                 s[c] = '\0';
115                 c--;
116         }
117         if(c < 0) return NULL;
118         if(!*s) return NULL;
119         return s;
120 }
121
122 void *mymmap(const char *filename, unsigned long size, int flags)
123 {
124         int fd;
125         void *mem = NULL;
126
127         errno = 0;
128         fd = open(filename, O_RDWR|O_CREAT|O_NOATIME, 0664);
129         if(fd != -1) {
130                 if(lseek(fd, size, SEEK_SET) == (long)size) {
131                         if(write(fd, "", 1) == 1) {
132
133                                 if(ftruncate(fd, size))
134                                         error("Cannot truncate file '%s' to size %ld. Will use the larger file.", filename, size);
135
136                                 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags, fd, 0);
137                                 if(mem) {
138                                         if(madvise(mem, size, MADV_SEQUENTIAL|MADV_DONTFORK|MADV_WILLNEED) != 0)
139                                                 error("Cannot advise the kernel about the memory usage of file '%s'.", filename);
140                                 }
141                         }
142                         else error("Cannot write to file '%s' at position %ld.", filename, size);
143                 }
144                 else error("Cannot seek file '%s' to size %ld.", filename, size);
145
146                 close(fd);
147         }
148         else error("Cannot create/open file '%s'.", filename);
149
150         return mem;
151 }
152
153 int savememory(const char *filename, void *mem, unsigned long size)
154 {
155         char tmpfilename[FILENAME_MAX + 1];
156
157         snprintf(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long)getpid());
158
159         int fd = open(tmpfilename, O_RDWR|O_CREAT|O_NOATIME, 0664);
160         if(fd < 0) {
161                 error("Cannot create/open file '%s'.", filename);
162                 return -1;
163         }
164
165         if(write(fd, mem, size) != (long)size) {
166                 error("Cannot write to file '%s' %ld bytes.", filename, (long)size);
167                 close(fd);
168                 return -1;
169         }
170
171         close(fd);
172
173         int ret = 0;
174         if(rename(tmpfilename, filename)) {
175                 error("Cannot rename '%s' to '%s'", tmpfilename, filename);
176                 ret = -1;
177         }
178
179         return ret;
180 }
181
182 int fd_is_valid(int fd) {
183     return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
184 }
185