root/trunk/hash.c

Revision 11, 1.9 kB (checked in by george, 17 years ago)

Updated hash function to use getrlimit/setrlimit.

  • 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 <stdlib.h>
15 #include <stdio.h>
16
17 #include "hash.h"
18
19 extern int nr_open;
20 static int myprime[20] = {
21         3,5,7,11,13,17,23,31,37,41,43,47,53,59,61,67,71,73,83,87
22 };
23
24 int gethash(void *hostheader, hash_element *hash) {
25   int a, i;
26   hash_element *elem;
27   a = hashpjw(hostheader,nr_open);
28   if(hash[a].fd == -1) return -1;  /* return -1 if element is not here */
29   for(i=0;i<nr_open; i++) {
30     elem = &hash[(a+(i*myprime[a%20]))%nr_open];
31     /* return -1 if element is not possibly in hsah*/
32     if (elem->fd == -1)
33       return -1;
34     /*return fd if the element matches */
35     if(!strcmp(hostheader,elem->hostheader))
36       return elem->fd;
37   }
38   return -1;
39 }
40
41 void inshash(hash_element b, hash_element *hash) {
42   int a, i;
43   a = hashpjw(b.hostheader,nr_open);
44   for(i=0;i<nr_open; i++)
45     if((hash[(a+(i*myprime[a%20]))%nr_open].fd) == -1) {
46       hash[(a+(i*myprime[a%20]))%nr_open] = b;
47       return;
48     }
49 }
50
51 int hashpjw(const void *key, const int size) {
52   const char *ptr;
53   unsigned int val = 0;
54   ptr = key;
55   while (*ptr != '\0') {
56     int tmp;
57     val = (val << 4) + (*ptr);
58     if ((tmp = (val & 0xf0000000)) != 0) {
59       val = val ^ (tmp >> 24);
60       val = val ^ tmp;
61     }
62     ptr++;
63   }
64   return (int) (val % size);
65 }
66
Note: See TracBrowser for help on using the browser.