Browse code

- hash cleanup/cosmetics: - use the "raw" hash functions from hashes.h as base for the other hashes (e.g. tm hash). This doesn't change the hash functions, just removes some extra copies. - split old hashes.h into hashes.h (more generic stuff) and str_hash.h - new_hash() is safer now (nobody uses it, but just in case...)

Andrei Pelinescu-Onciul authored on 13/12/2006 22:50:46
Showing 7 changed files
... ...
@@ -40,6 +40,8 @@
40 40
 #include "error.h"
41 41
 #include "stdlib.h"
42 42
 #include "hashes.h"
43
+#include "clist.h"
44
+#include "mem/mem.h"
43 45
 
44 46
 int setflag( struct sip_msg* msg, flag_t flag ) {
45 47
 	msg->flags |= 1 << flag;
... ...
@@ -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 126
 	h=((h)+(h>>11))+((h>>13)+(h>>23));
127 127
 	return (h)&(TABLE_ENTRIES-1);
128 128
 }
129
+#endif
129 130
 
130 131
 
131 132
 
... ...
@@ -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 27
 #define _hashes_h
28 28
 
29 29
 #include "str.h"
30
-#include "mem/mem.h"
31
-#include "clist.h"
32 30
 
33 31
 
34 32
 
... ...
@@ -142,84 +141,4 @@ inline static unsigned int get_hash2_raw2(str* key1, str* key2)
142 142
 
143 143
 
144 144
 
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 145
 #endif
... ...
@@ -72,7 +72,7 @@
72 72
 #include "mem/mem.h"
73 73
 #include "select.h"
74 74
 #include "onsend.h"
75
-#include "hashes.h"
75
+#include "str_hash.h"
76 76
 #include "ut.h"
77 77
 
78 78
 #define RT_HASH_SIZE	8 /* route names hash */
... ...
@@ -37,7 +37,7 @@
37 37
 #include "error.h"
38 38
 #include "route_struct.h"
39 39
 #include "parser/msg_parser.h"
40
-#include "hashes.h"
40
+#include "str_hash.h"
41 41
 
42 42
 /*#include "cfg_parser.h" */
43 43
 
44 44
new file mode 100644
... ...
@@ -0,0 +1,115 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ * Copyright (C) 2006 iptelorg GmbH 
4
+ *
5
+ * Permission to use, copy, modify, and distribute this software for any
6
+ * purpose with or without fee is hereby granted, provided that the above
7
+ * copyright notice and this permission notice appear in all copies.
8
+ *
9
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
+ */
17
+/*
18
+ * History:
19
+ * --------
20
+ *  2006-02-02  created by andrei
21
+ *  2006-11-24  added numeric string optimized hash function (andrei)
22
+ *  2006-12-13  split into hashes.h (more generic) and str_hash.h (andrei)
23
+ */
24
+
25
+
26
+#ifndef _str_hashs_h
27
+#define _str_hashs_h
28
+
29
+#include "str.h"
30
+#include "hashes.h"
31
+#include "mem/mem.h"
32
+#include "clist.h"
33
+#include <string.h>
34
+
35
+
36
+/* generic, simple str keyed hash */
37
+
38
+struct str_hash_entry{
39
+	struct str_hash_entry* next;
40
+	struct str_hash_entry* prev;
41
+	str key;
42
+	unsigned int flags;
43
+	union{
44
+		void* p;
45
+		char* s;
46
+		int   n;
47
+		char  data[sizeof(void*)];
48
+	}u;
49
+};
50
+
51
+
52
+struct str_hash_head{
53
+	struct str_hash_entry* next;
54
+	struct str_hash_entry* prev;
55
+};
56
+
57
+
58
+struct str_hash_table{
59
+	struct str_hash_head* table;
60
+	int size;
61
+};
62
+
63
+
64
+
65
+/* returns 0 on success, <0 on failure */
66
+inline static int str_hash_alloc(struct str_hash_table* ht, int size)
67
+{
68
+	ht->table=pkg_malloc(sizeof(struct str_hash_head)*size);
69
+	if (ht->table==0)
70
+		return -1;
71
+	ht->size=size;
72
+	return 0;
73
+}
74
+
75
+
76
+
77
+inline static void str_hash_init(struct str_hash_table* ht)
78
+{
79
+	int r;
80
+	
81
+	for (r=0; r<ht->size; r++) clist_init(&(ht->table[r]), next, prev);
82
+}
83
+
84
+
85
+
86
+inline static void str_hash_add(struct str_hash_table* ht, 
87
+								struct str_hash_entry* e)
88
+{
89
+	int h;
90
+	
91
+	h=get_hash1_raw(e->key.s, e->key.len) % ht->size;
92
+	clist_insert(&ht->table[h], e, next, prev);
93
+}
94
+
95
+
96
+
97
+inline static struct str_hash_entry* str_hash_get(struct str_hash_table* ht,
98
+									char* key, int len)
99
+{
100
+	int h;
101
+	struct str_hash_entry* e;
102
+	
103
+	h=get_hash1_raw(key, len) % ht->size;
104
+	clist_foreach(&ht->table[h], e, next){
105
+		if ((e->key.len==len) && (memcmp(e->key.s, key, len)==0))
106
+			return e;
107
+	}
108
+	return 0;
109
+}
110
+
111
+
112
+#define str_hash_del(e) clist_rm(e, next, prev)
113
+
114
+#endif