4 #include <sys/syscall.h>
18 char *global_host_prefix = "";
21 // http://stackoverflow.com/questions/7666509/hash-function-for-string
22 uint32_t simple_hash(const char *name)
28 while((i = *s++)) hash = ((hash << 5) + hash) + i;
30 // fprintf(stderr, "HASH: %lu %s\n", hash, name);
37 // http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
38 uint32_t simple_hash(const char *name) {
39 unsigned char *s = (unsigned char *)name;
40 uint32_t hval = 0x811c9dc5;
44 // multiply by the 32 bit FNV magic prime mod 2^32
46 hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
48 // xor the bottom with the current octet
49 hval ^= (uint32_t)*s++;
52 // fprintf(stderr, "HASH: %u = %s\n", hval, name);
57 // http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
59 uint32_t simple_hash(const char *name) {
60 unsigned char *s = (unsigned char *)name;
73 // fprintf(stderr, "HASH: %u = %s\n", h, name);
79 void strreverse(char* begin, char* end)
83 aux = *end, *end-- = *begin, *begin++ = aux;
86 char *mystrsep(char **ptr, char *s)
89 while ( p && !p[0] && *ptr ) p = strsep(ptr, s);
95 // skip leading spaces
96 while(*s && isspace(*s)) s++;
97 if(!*s || *s == '#') return NULL;
99 // skip tailing spaces
100 int c = strlen(s) - 1;
101 while(c >= 0 && isspace(s[c])) {
105 if(c < 0) return NULL;
110 void *mymmap(const char *filename, unsigned long size, int flags)
116 fd = open(filename, O_RDWR|O_CREAT|O_NOATIME, 0664);
118 if(lseek(fd, size, SEEK_SET) == (long)size) {
119 if(write(fd, "", 1) == 1) {
121 if(ftruncate(fd, size))
122 error("Cannot truncate file '%s' to size %ld. Will use the larger file.", filename, size);
124 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags, fd, 0);
126 if(madvise(mem, size, MADV_SEQUENTIAL|MADV_DONTFORK|MADV_WILLNEED) != 0)
127 error("Cannot advise the kernel about the memory usage of file '%s'.", filename);
130 else error("Cannot write to file '%s' at position %ld.", filename, size);
132 else error("Cannot seek file '%s' to size %ld.", filename, size);
136 else error("Cannot create/open file '%s'.", filename);
141 int savememory(const char *filename, void *mem, unsigned long size)
143 char tmpfilename[FILENAME_MAX + 1];
145 snprintf(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long)getpid());
147 int fd = open(tmpfilename, O_RDWR|O_CREAT|O_NOATIME, 0664);
149 error("Cannot create/open file '%s'.", filename);
153 if(write(fd, mem, size) != (long)size) {
154 error("Cannot write to file '%s' %ld bytes.", filename, (long)size);
162 if(rename(tmpfilename, filename)) {
163 error("Cannot rename '%s' to '%s'", tmpfilename, filename);
170 int fd_is_valid(int fd) {
171 return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
175 ***************************************************************************
176 * Get number of clock ticks per second.
177 ***************************************************************************
185 if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
189 hz = (unsigned int) ticks;
194 return syscall(SYS_gettid);