浏览代码

- 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 19 年之前
父节点
当前提交
e8138515fe
共有 7 个文件被更改,包括 133 次插入92 次删除
  1. 2 0
      flags.c
  2. 8 6
      hash_func.c
  3. 5 2
      hash_func.h
  4. 1 82
      hashes.h
  5. 1 1
      route.c
  6. 1 1
      route.h
  7. 115 0
      str_hash.h

+ 2 - 0
flags.c

@@ -40,6 +40,8 @@
 #include "error.h"
 #include "stdlib.h"
 #include "hashes.h"
+#include "clist.h"
+#include "mem/mem.h"
 
 int setflag( struct sip_msg* msg, flag_t flag ) {
 	msg->flags |= 1 << flag;

+ 8 - 6
hash_func.c

@@ -45,9 +45,9 @@ extern unsigned short int crc_16_tab[];
 #include "ut.h"
 
 
-int new_hash( str call_id, str cseq_nr )
+unsigned int new_hash( str call_id, str cseq_nr )
 {
-	int hash_code = 0;
+	unsigned int hash_code = 0;
 	int i,j, k, third;
 	int ci_len, cs_len;
 	char *ci, *cs;
@@ -71,12 +71,12 @@ int new_hash( str call_id, str cseq_nr )
 	for ( i=ci_len-1, j=2*third, k=third;
 		k>0 ; i--, j--, k-- ) {
 		hash_code+=crc_16_tab[(unsigned char)(*(ci+i)) /*+7*/ ]+
-			ccitt_tab[*(ci+k)+63]+
-			ccitt_tab[*(ci+j)+13];
+			ccitt_tab[(unsigned char)*(ci+k)+63]+
+			ccitt_tab[(unsigned char)*(ci+j)+13];
 	}
 	for( i=0 ; i<cs_len ; i++ )
 		//hash_code+=crc_32_tab[(cseq_nr.s[i]+hash_code)%243];
-		hash_code+=ccitt_tab[*(cs+i)+123];
+		hash_code+=ccitt_tab[(unsigned char)*(cs+i)+123];
 
 	/* hash_code conditioning */
 #ifdef _BUG
@@ -92,11 +92,12 @@ int new_hash( str call_id, str cseq_nr )
 	hash_code &= (TABLE_ENTRIES-1); /* TABLE_ENTRIES = 2^k */
 #endif
 	hash_code=hash_code%(TABLE_ENTRIES-1)+1;
-   	return hash_code;
+	return hash_code;
 }
 
 
 
+#if 0
 int new_hash2( str call_id, str cseq_nr )
 {
 #define h_inc h+=v^(v>>3)
@@ -126,6 +127,7 @@ int new_hash2( str call_id, str cseq_nr )
 	h=((h)+(h>>11))+((h>>13)+(h>>23));
 	return (h)&(TABLE_ENTRIES-1);
 }
+#endif
 
 
 

+ 5 - 2
hash_func.h

@@ -31,13 +31,16 @@
 #define _HASH_H
 
 #include "str.h"
+#include "hashes.h"
 
 /* always use a power of 2 for hash table size */
 #define T_TABLE_POWER    16 
 #define TABLE_ENTRIES    (1 << (T_TABLE_POWER))
 
-int new_hash( str  call_id, str cseq_nr );
-int new_hash2( str  call_id, str cseq_nr );
+unsigned int new_hash( str  call_id, str cseq_nr );
+
+#define new_hash2(call_id, cseq_nr) \
+	(get_hash2_raw(&(call_id), &(cseq_nr)) & (TABLE_ENTRIES-1))
 
 
 #define hash( cid, cseq) new_hash2( cid, cseq )

+ 1 - 82
hashes.h

@@ -20,6 +20,7 @@
  * --------
  *  2006-02-02  created by andrei
  *  2006-11-24  added numeric string optimized hash function (andrei)
+ *  2006-12-13  split into hashes.h (more generic) and str_hash.h (andrei)
  */
 
 
@@ -27,8 +28,6 @@
 #define _hashes_h
 
 #include "str.h"
-#include "mem/mem.h"
-#include "clist.h"
 
 
 
@@ -142,84 +141,4 @@ inline static unsigned int get_hash2_raw2(str* key1, str* key2)
 
 
 
-/* generic, simple str keyed hash */
-
-struct str_hash_entry{
-	struct str_hash_entry* next;
-	struct str_hash_entry* prev;
-	str key;
-	unsigned int flags;
-	union{
-		void* p;
-		char* s;
-		int   n;
-		char  data[sizeof(void*)];
-	}u;
-};
-
-
-struct str_hash_head{
-	struct str_hash_entry* next;
-	struct str_hash_entry* prev;
-};
-
-
-struct str_hash_table{
-	struct str_hash_head* table;
-	int size;
-};
-
-
-
-/* returns 0 on success, <0 on failure */
-inline static int str_hash_alloc(struct str_hash_table* ht, int size)
-{
-	ht->table=pkg_malloc(sizeof(struct str_hash_head)*size);
-	if (ht->table==0)
-		return -1;
-	ht->size=size;
-	return 0;
-}
-
-
-
-inline static void str_hash_init(struct str_hash_table* ht)
-{
-	int r;
-	
-	for (r=0; r<ht->size; r++) clist_init(&(ht->table[r]), next, prev);
-}
-
-
-
-inline static void str_hash_add(struct str_hash_table* ht, 
-								struct str_hash_entry* e)
-{
-	int h;
-	
-	h=get_hash1_raw(e->key.s, e->key.len) % ht->size;
-	clist_insert(&ht->table[h], e, next, prev);
-}
-
-
-
-inline static struct str_hash_entry* str_hash_get(struct str_hash_table* ht,
-									char* key, int len)
-{
-	int h;
-	struct str_hash_entry* e;
-	
-	h=get_hash1_raw(key, len) % ht->size;
-	clist_foreach(&ht->table[h], e, next){
-		if ((e->key.len==len) && (memcmp(e->key.s, key, len)==0))
-			return e;
-	}
-	return 0;
-}
-
-
-#define str_hash_del(e) clist_rm(e, next, prev)
-
-
-
 #endif

+ 1 - 1
route.c

@@ -72,7 +72,7 @@
 #include "mem/mem.h"
 #include "select.h"
 #include "onsend.h"
-#include "hashes.h"
+#include "str_hash.h"
 #include "ut.h"
 
 #define RT_HASH_SIZE	8 /* route names hash */

+ 1 - 1
route.h

@@ -37,7 +37,7 @@
 #include "error.h"
 #include "route_struct.h"
 #include "parser/msg_parser.h"
-#include "hashes.h"
+#include "str_hash.h"
 
 /*#include "cfg_parser.h" */
 

+ 115 - 0
str_hash.h

@@ -0,0 +1,115 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2006 iptelorg GmbH 
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+/*
+ * History:
+ * --------
+ *  2006-02-02  created by andrei
+ *  2006-11-24  added numeric string optimized hash function (andrei)
+ *  2006-12-13  split into hashes.h (more generic) and str_hash.h (andrei)
+ */
+
+
+#ifndef _str_hashs_h
+#define _str_hashs_h
+
+#include "str.h"
+#include "hashes.h"
+#include "mem/mem.h"
+#include "clist.h"
+#include <string.h>
+
+
+/* generic, simple str keyed hash */
+
+struct str_hash_entry{
+	struct str_hash_entry* next;
+	struct str_hash_entry* prev;
+	str key;
+	unsigned int flags;
+	union{
+		void* p;
+		char* s;
+		int   n;
+		char  data[sizeof(void*)];
+	}u;
+};
+
+
+struct str_hash_head{
+	struct str_hash_entry* next;
+	struct str_hash_entry* prev;
+};
+
+
+struct str_hash_table{
+	struct str_hash_head* table;
+	int size;
+};
+
+
+
+/* returns 0 on success, <0 on failure */
+inline static int str_hash_alloc(struct str_hash_table* ht, int size)
+{
+	ht->table=pkg_malloc(sizeof(struct str_hash_head)*size);
+	if (ht->table==0)
+		return -1;
+	ht->size=size;
+	return 0;
+}
+
+
+
+inline static void str_hash_init(struct str_hash_table* ht)
+{
+	int r;
+	
+	for (r=0; r<ht->size; r++) clist_init(&(ht->table[r]), next, prev);
+}
+
+
+
+inline static void str_hash_add(struct str_hash_table* ht, 
+								struct str_hash_entry* e)
+{
+	int h;
+	
+	h=get_hash1_raw(e->key.s, e->key.len) % ht->size;
+	clist_insert(&ht->table[h], e, next, prev);
+}
+
+
+
+inline static struct str_hash_entry* str_hash_get(struct str_hash_table* ht,
+									char* key, int len)
+{
+	int h;
+	struct str_hash_entry* e;
+	
+	h=get_hash1_raw(key, len) % ht->size;
+	clist_foreach(&ht->table[h], e, next){
+		if ((e->key.len==len) && (memcmp(e->key.s, key, len)==0))
+			return e;
+	}
+	return 0;
+}
+
+
+#define str_hash_del(e) clist_rm(e, next, prev)
+
+#endif