3 #include <sys/syscall.h>
17 char *global_host_prefix = "";
20 // http://stackoverflow.com/questions/7666509/hash-function-for-string
21 uint32_t simple_hash(const char *name)
27 while((i = *s++)) hash = ((hash << 5) + hash) + i;
29 // fprintf(stderr, "HASH: %lu %s\n", hash, name);
36 // http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
37 uint32_t simple_hash(const char *name) {
38 unsigned char *s = (unsigned char *)name;
39 uint32_t hval = 0x811c9dc5;
43 // multiply by the 32 bit FNV magic prime mod 2^32
45 hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
47 // xor the bottom with the current octet
48 hval ^= (uint32_t)*s++;
51 // fprintf(stderr, "HASH: %u = %s\n", hval, name);
56 // http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
58 uint32_t simple_hash(const char *name) {
59 unsigned char *s = (unsigned char *)name;
72 // fprintf(stderr, "HASH: %u = %s\n", h, name);
78 void strreverse(char* begin, char* end)
82 aux = *end, *end-- = *begin, *begin++ = aux;
85 char *mystrsep(char **ptr, char *s)
88 while ( p && !p[0] && *ptr ) p = strsep(ptr, s);
94 // skip leading spaces
95 while(*s && isspace(*s)) s++;
96 if(!*s || *s == '#') return NULL;
98 // skip tailing spaces
99 int c = strlen(s) - 1;
100 while(c >= 0 && isspace(s[c])) {
104 if(c < 0) return NULL;
109 void *mymmap(const char *filename, unsigned long size, int flags)
115 fd = open(filename, O_RDWR|O_CREAT|O_NOATIME, 0664);
117 if(lseek(fd, size, SEEK_SET) == (long)size) {
118 if(write(fd, "", 1) == 1) {
120 if(ftruncate(fd, size))
121 error("Cannot truncate file '%s' to size %ld. Will use the larger file.", filename, size);
123 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags, fd, 0);
125 if(madvise(mem, size, MADV_SEQUENTIAL|MADV_DONTFORK|MADV_WILLNEED) != 0)
126 error("Cannot advise the kernel about the memory usage of file '%s'.", filename);
129 else error("Cannot write to file '%s' at position %ld.", filename, size);
131 else error("Cannot seek file '%s' to size %ld.", filename, size);
135 else error("Cannot create/open file '%s'.", filename);
140 int savememory(const char *filename, void *mem, unsigned long size)
142 char tmpfilename[FILENAME_MAX + 1];
144 snprintf(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long)getpid());
146 int fd = open(tmpfilename, O_RDWR|O_CREAT|O_NOATIME, 0664);
148 error("Cannot create/open file '%s'.", filename);
152 if(write(fd, mem, size) != (long)size) {
153 error("Cannot write to file '%s' %ld bytes.", filename, (long)size);
161 if(rename(tmpfilename, filename)) {
162 error("Cannot rename '%s' to '%s'", tmpfilename, filename);
169 int fd_is_valid(int fd) {
170 return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
174 ***************************************************************************
175 * Get number of clock ticks per second.
176 ***************************************************************************
184 if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
188 hz = (unsigned int) ticks;
193 return syscall(SYS_gettid);