... | ... |
@@ -45,9 +45,9 @@ extern unsigned short int crc_16_tab[]; |
45 | 45 |
#include "ut.h" |
46 | 46 |
|
47 | 47 |
|
48 |
-int new_hash( str call_id, str cseq_nr ) |
|
48 |
+unsigned int new_hash( str call_id, str cseq_nr ) |
|
49 | 49 |
{ |
50 |
- int hash_code = 0; |
|
50 |
+ unsigned int hash_code = 0; |
|
51 | 51 |
int i,j, k, third; |
52 | 52 |
int ci_len, cs_len; |
53 | 53 |
char *ci, *cs; |
... | ... |
@@ -71,12 +71,12 @@ int new_hash( str call_id, str cseq_nr ) |
71 | 71 |
for ( i=ci_len-1, j=2*third, k=third; |
72 | 72 |
k>0 ; i--, j--, k-- ) { |
73 | 73 |
hash_code+=crc_16_tab[(unsigned char)(*(ci+i)) /*+7*/ ]+ |
74 |
- ccitt_tab[*(ci+k)+63]+ |
|
75 |
- ccitt_tab[*(ci+j)+13]; |
|
74 |
+ ccitt_tab[(unsigned char)*(ci+k)+63]+ |
|
75 |
+ ccitt_tab[(unsigned char)*(ci+j)+13]; |
|
76 | 76 |
} |
77 | 77 |
for( i=0 ; i<cs_len ; i++ ) |
78 | 78 |
//hash_code+=crc_32_tab[(cseq_nr.s[i]+hash_code)%243]; |
79 |
- hash_code+=ccitt_tab[*(cs+i)+123]; |
|
79 |
+ hash_code+=ccitt_tab[(unsigned char)*(cs+i)+123]; |
|
80 | 80 |
|
81 | 81 |
/* hash_code conditioning */ |
82 | 82 |
#ifdef _BUG |
... | ... |
@@ -92,11 +92,12 @@ int new_hash( str call_id, str cseq_nr ) |
92 | 92 |
hash_code &= (TABLE_ENTRIES-1); /* TABLE_ENTRIES = 2^k */ |
93 | 93 |
#endif |
94 | 94 |
hash_code=hash_code%(TABLE_ENTRIES-1)+1; |
95 |
- return hash_code; |
|
95 |
+ return hash_code; |
|
96 | 96 |
} |
97 | 97 |
|
98 | 98 |
|
99 | 99 |
|
100 |
+#if 0 |
|
100 | 101 |
int new_hash2( str call_id, str cseq_nr ) |
101 | 102 |
{ |
102 | 103 |
#define h_inc h+=v^(v>>3) |
... | ... |
@@ -126,6 +127,7 @@ int new_hash2( str call_id, str cseq_nr ) |
126 | 127 |
h=((h)+(h>>11))+((h>>13)+(h>>23)); |
127 | 128 |
return (h)&(TABLE_ENTRIES-1); |
128 | 129 |
} |
130 |
+#endif |
|
129 | 131 |
|
130 | 132 |
|
131 | 133 |
|
... | ... |
@@ -31,13 +31,16 @@ |
31 | 31 |
#define _HASH_H |
32 | 32 |
|
33 | 33 |
#include "str.h" |
34 |
+#include "hashes.h" |
|
34 | 35 |
|
35 | 36 |
/* always use a power of 2 for hash table size */ |
36 | 37 |
#define T_TABLE_POWER 16 |
37 | 38 |
#define TABLE_ENTRIES (1 << (T_TABLE_POWER)) |
38 | 39 |
|
39 |
-int new_hash( str call_id, str cseq_nr ); |
|
40 |
-int new_hash2( str call_id, str cseq_nr ); |
|
40 |
+unsigned int new_hash( str call_id, str cseq_nr ); |
|
41 |
+ |
|
42 |
+#define new_hash2(call_id, cseq_nr) \ |
|
43 |
+ (get_hash2_raw(&(call_id), &(cseq_nr)) & (TABLE_ENTRIES-1)) |
|
41 | 44 |
|
42 | 45 |
|
43 | 46 |
#define hash( cid, cseq) new_hash2( cid, cseq ) |
... | ... |
@@ -20,6 +20,7 @@ |
20 | 20 |
* -------- |
21 | 21 |
* 2006-02-02 created by andrei |
22 | 22 |
* 2006-11-24 added numeric string optimized hash function (andrei) |
23 |
+ * 2006-12-13 split into hashes.h (more generic) and str_hash.h (andrei) |
|
23 | 24 |
*/ |
24 | 25 |
|
25 | 26 |
|
... | ... |
@@ -27,8 +28,6 @@ |
27 | 28 |
#define _hashes_h |
28 | 29 |
|
29 | 30 |
#include "str.h" |
30 |
-#include "mem/mem.h" |
|
31 |
-#include "clist.h" |
|
32 | 31 |
|
33 | 32 |
|
34 | 33 |
|
... | ... |
@@ -142,84 +141,4 @@ inline static unsigned int get_hash2_raw2(str* key1, str* key2) |
142 | 141 |
|
143 | 142 |
|
144 | 143 |
|
145 |
-/* generic, simple str keyed hash */ |
|
146 |
- |
|
147 |
-struct str_hash_entry{ |
|
148 |
- struct str_hash_entry* next; |
|
149 |
- struct str_hash_entry* prev; |
|
150 |
- str key; |
|
151 |
- unsigned int flags; |
|
152 |
- union{ |
|
153 |
- void* p; |
|
154 |
- char* s; |
|
155 |
- int n; |
|
156 |
- char data[sizeof(void*)]; |
|
157 |
- }u; |
|
158 |
-}; |
|
159 |
- |
|
160 |
- |
|
161 |
-struct str_hash_head{ |
|
162 |
- struct str_hash_entry* next; |
|
163 |
- struct str_hash_entry* prev; |
|
164 |
-}; |
|
165 |
- |
|
166 |
- |
|
167 |
-struct str_hash_table{ |
|
168 |
- struct str_hash_head* table; |
|
169 |
- int size; |
|
170 |
-}; |
|
171 |
- |
|
172 |
- |
|
173 |
- |
|
174 |
-/* returns 0 on success, <0 on failure */ |
|
175 |
-inline static int str_hash_alloc(struct str_hash_table* ht, int size) |
|
176 |
-{ |
|
177 |
- ht->table=pkg_malloc(sizeof(struct str_hash_head)*size); |
|
178 |
- if (ht->table==0) |
|
179 |
- return -1; |
|
180 |
- ht->size=size; |
|
181 |
- return 0; |
|
182 |
-} |
|
183 |
- |
|
184 |
- |
|
185 |
- |
|
186 |
-inline static void str_hash_init(struct str_hash_table* ht) |
|
187 |
-{ |
|
188 |
- int r; |
|
189 |
- |
|
190 |
- for (r=0; r<ht->size; r++) clist_init(&(ht->table[r]), next, prev); |
|
191 |
-} |
|
192 |
- |
|
193 |
- |
|
194 |
- |
|
195 |
-inline static void str_hash_add(struct str_hash_table* ht, |
|
196 |
- struct str_hash_entry* e) |
|
197 |
-{ |
|
198 |
- int h; |
|
199 |
- |
|
200 |
- h=get_hash1_raw(e->key.s, e->key.len) % ht->size; |
|
201 |
- clist_insert(&ht->table[h], e, next, prev); |
|
202 |
-} |
|
203 |
- |
|
204 |
- |
|
205 |
- |
|
206 |
-inline static struct str_hash_entry* str_hash_get(struct str_hash_table* ht, |
|
207 |
- char* key, int len) |
|
208 |
-{ |
|
209 |
- int h; |
|
210 |
- struct str_hash_entry* e; |
|
211 |
- |
|
212 |
- h=get_hash1_raw(key, len) % ht->size; |
|
213 |
- clist_foreach(&ht->table[h], e, next){ |
|
214 |
- if ((e->key.len==len) && (memcmp(e->key.s, key, len)==0)) |
|
215 |
- return e; |
|
216 |
- } |
|
217 |
- return 0; |
|
218 |
-} |
|
219 |
- |
|
220 |
- |
|
221 |
-#define str_hash_del(e) clist_rm(e, next, prev) |
|
222 |
- |
|
223 |
- |
|
224 |
- |
|
225 | 144 |
#endif |
44 | 44 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,115 @@ |
1 |
+/* |
|
2 |
+ * $Id$ |
|
3 |
+ * |
|
4 |
+ * Copyright (C) 2006 iptelorg GmbH |
|
5 |
+ * |
|
6 |
+ * Permission to use, copy, modify, and distribute this software for any |
|
7 |
+ * purpose with or without fee is hereby granted, provided that the above |
|
8 |
+ * copyright notice and this permission notice appear in all copies. |
|
9 |
+ * |
|
10 |
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
|
11 |
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
|
12 |
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
|
13 |
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|
14 |
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
|
15 |
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
|
16 |
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|
17 |
+ */ |
|
18 |
+/* |
|
19 |
+ * History: |
|
20 |
+ * -------- |
|
21 |
+ * 2006-02-02 created by andrei |
|
22 |
+ * 2006-11-24 added numeric string optimized hash function (andrei) |
|
23 |
+ * 2006-12-13 split into hashes.h (more generic) and str_hash.h (andrei) |
|
24 |
+ */ |
|
25 |
+ |
|
26 |
+ |
|
27 |
+#ifndef _str_hashs_h |
|
28 |
+#define _str_hashs_h |
|
29 |
+ |
|
30 |
+#include "str.h" |
|
31 |
+#include "hashes.h" |
|
32 |
+#include "mem/mem.h" |
|
33 |
+#include "clist.h" |
|
34 |
+#include <string.h> |
|
35 |
+ |
|
36 |
+ |
|
37 |
+/* generic, simple str keyed hash */ |
|
38 |
+ |
|
39 |
+struct str_hash_entry{ |
|
40 |
+ struct str_hash_entry* next; |
|
41 |
+ struct str_hash_entry* prev; |
|
42 |
+ str key; |
|
43 |
+ unsigned int flags; |
|
44 |
+ union{ |
|
45 |
+ void* p; |
|
46 |
+ char* s; |
|
47 |
+ int n; |
|
48 |
+ char data[sizeof(void*)]; |
|
49 |
+ }u; |
|
50 |
+}; |
|
51 |
+ |
|
52 |
+ |
|
53 |
+struct str_hash_head{ |
|
54 |
+ struct str_hash_entry* next; |
|
55 |
+ struct str_hash_entry* prev; |
|
56 |
+}; |
|
57 |
+ |
|
58 |
+ |
|
59 |
+struct str_hash_table{ |
|
60 |
+ struct str_hash_head* table; |
|
61 |
+ int size; |
|
62 |
+}; |
|
63 |
+ |
|
64 |
+ |
|
65 |
+ |
|
66 |
+/* returns 0 on success, <0 on failure */ |
|
67 |
+inline static int str_hash_alloc(struct str_hash_table* ht, int size) |
|
68 |
+{ |
|
69 |
+ ht->table=pkg_malloc(sizeof(struct str_hash_head)*size); |
|
70 |
+ if (ht->table==0) |
|
71 |
+ return -1; |
|
72 |
+ ht->size=size; |
|
73 |
+ return 0; |
|
74 |
+} |
|
75 |
+ |
|
76 |
+ |
|
77 |
+ |
|
78 |
+inline static void str_hash_init(struct str_hash_table* ht) |
|
79 |
+{ |
|
80 |
+ int r; |
|
81 |
+ |
|
82 |
+ for (r=0; r<ht->size; r++) clist_init(&(ht->table[r]), next, prev); |
|
83 |
+} |
|
84 |
+ |
|
85 |
+ |
|
86 |
+ |
|
87 |
+inline static void str_hash_add(struct str_hash_table* ht, |
|
88 |
+ struct str_hash_entry* e) |
|
89 |
+{ |
|
90 |
+ int h; |
|
91 |
+ |
|
92 |
+ h=get_hash1_raw(e->key.s, e->key.len) % ht->size; |
|
93 |
+ clist_insert(&ht->table[h], e, next, prev); |
|
94 |
+} |
|
95 |
+ |
|
96 |
+ |
|
97 |
+ |
|
98 |
+inline static struct str_hash_entry* str_hash_get(struct str_hash_table* ht, |
|
99 |
+ char* key, int len) |
|
100 |
+{ |
|
101 |
+ int h; |
|
102 |
+ struct str_hash_entry* e; |
|
103 |
+ |
|
104 |
+ h=get_hash1_raw(key, len) % ht->size; |
|
105 |
+ clist_foreach(&ht->table[h], e, next){ |
|
106 |
+ if ((e->key.len==len) && (memcmp(e->key.s, key, len)==0)) |
|
107 |
+ return e; |
|
108 |
+ } |
|
109 |
+ return 0; |
|
110 |
+} |
|
111 |
+ |
|
112 |
+ |
|
113 |
+#define str_hash_del(e) clist_rm(e, next, prev) |
|
114 |
+ |
|
115 |
+#endif |