root/trunk/hash.c

Revision 41, 2.0 kB (checked in by jesus, 8 years ago)

autoconf support

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /* ======================================================================
2  * Copyright (c) 2000 George Schlossnagle
3  * All rights reserved.
4  * The following code was written by George Schlossnagle <george@lethargy.org>
5  * This code was written to facilitate clustered logging via Spread,
6  * particularly in conjunction with the mod_log_spread module for Apache.
7  * More information on Spread can be found at http://www.spread.org/
8  * More information on mod_log_spread can be found at
9  * http://www.backhand.org/mod_log_spread/
10  * Please refer to the LICENSE file before using this software.
11  * ======================================================================
12 */
13
14 #include "sld_config.h"
15 #include "hash.h"
16
17 int __fhash_size = 1024;
18 static int myprime[20] = {
19         3,5,7,11,13,17,23,31,37,41,43,47,53,59,61,67,71,73,83,87
20 };
21
22 int gethash(void *vhostheader, hash_element *hash) {
23   int a, i;
24   hash_element *elem;
25   char *hostheader = (char *)vhostheader;
26   a = hashpjw(hostheader,FHASH_SIZE);
27   if(hash[a].fd == -1) {
28     return -1;  /* return -1 if element is not here */
29   }
30   for(i=0;i<FHASH_SIZE; i++) {
31     elem = &hash[(a+(i*myprime[a%20]))%FHASH_SIZE];
32     /* return -1 if element is not possibly in hsah*/
33     if (elem->fd == -1)
34       return -1;
35     /*return fd if the element matches */
36     if(!strcmp(hostheader,elem->hostheader))
37       return elem->fd;
38   }
39   return -1;
40 }
41
42 void inshash(hash_element b, hash_element *hash) {
43   int a, i, off;
44   a = hashpjw(b.hostheader,FHASH_SIZE);
45   if(hash[a].fd == -1) {
46     hash[a] = b;
47     return;
48   }
49   for(i=0;i<FHASH_SIZE; i++)
50     off = (a+(i*myprime[a%20]))%FHASH_SIZE;
51     if((hash[off].fd) == -1) {
52       hash[off] = b;
53       return;
54     }
55   close(hash[a].fd);
56   hash[a] = b;
57 }
58
59 int hashpjw(const void *key, const int size) {
60   const char *ptr;
61   unsigned int val = 0;
62   ptr = key;
63   while (*ptr != '\0') {
64     int tmp;
65         val = ((val << 1) + ((*ptr)*31 >> 5)) >> 1;
66     if ((tmp = (val & 0xf0000000)) != 0) {
67       val = val ^ (tmp >> 24);
68       val = val ^ tmp;
69     }
70     ptr++;
71   }
72   return (int) (val % size);
73 }
74
Note: See TracBrowser for help on using the browser.