blob: 69b577d305e4688c5389e6f770811807ac6fbd8e [file] [log] [blame]
Benny Prijonodd859a62005-11-01 16:42:51 +00001/* $Header: /pjproject-0.3/pjlib/src/pj/hash.c 8 10/14/05 12:26a Bennylp $ */
2/* $Log: /pjproject-0.3/pjlib/src/pj/hash.c $
3 *
4 * 8 10/14/05 12:26a Bennylp
5 * Finished error code framework, some fixes in ioqueue, etc. Pretty
6 * major.
7 *
8 * 7 9/17/05 10:37a Bennylp
9 * Major reorganization towards version 0.3.
10 *
11 */
12#include <pj/hash.h>
13#include <pj/log.h>
14#include <pj/string.h>
15#include <pj/pool.h>
16#include <pj/os.h>
17
18/**
19 * The hash multiplier used to calculate hash value.
20 */
21#define PJ_HASH_MULTIPLIER 33
22
23
24struct pj_hash_entry
25{
26 struct pj_hash_entry *next;
27 const void *key;
28 pj_uint32_t hash;
29 pj_uint32_t keylen;
30 void *value;
31};
32
33
34struct pj_hash_table_t
35{
36 pj_hash_entry **table;
37 unsigned count, rows;
38 pj_hash_iterator_t iterator;
39};
40
41
42
43PJ_DEF(pj_uint32_t) pj_hash_calc(pj_uint32_t hash, const void *key, unsigned keylen)
44{
45 PJ_CHECK_STACK();
46
47 if (keylen==PJ_HASH_KEY_STRING) {
48 const unsigned char *p = key;
49 for ( ; *p; ++p ) {
50 hash = hash * PJ_HASH_MULTIPLIER + *p;
51 }
52 keylen = p - (const unsigned char*)key;
53 } else {
54 const unsigned char *p = key,
55 *end = p + keylen;
56 for ( ; p!=end; ++p) {
57 hash = hash * PJ_HASH_MULTIPLIER + *p;
58 }
59 }
60 return hash;
61}
62
63
64PJ_DEF(pj_hash_table_t*) pj_hash_create(pj_pool_t *pool, unsigned size)
65{
66 pj_hash_table_t *h;
67 unsigned table_size;
68
69 h = pj_pool_alloc(pool, sizeof(pj_hash_table_t));
70 h->count = 0;
71
72 PJ_LOG( 5, ("hashtbl", "hash table %p created from pool %s", h, pj_pool_getobjname(pool)));
73
74 /* size must be 2^n - 1.
75 round-up the size to this rule, except when size is 2^n, then size
76 will be round-down to 2^n-1.
77 */
78 table_size = 8;
79 do {
80 table_size <<= 1;
81 } while (table_size <= size);
82 table_size -= 1;
83
84 h->rows = table_size;
85 h->table = pj_pool_calloc(pool, table_size+1, sizeof(pj_hash_entry*));
86 return h;
87}
88
89static pj_hash_entry **find_entry( pj_pool_t *pool, pj_hash_table_t *ht,
90 const void *key, unsigned keylen,
91 void *val)
92{
93 pj_uint32_t hash;
94 pj_hash_entry **p_entry, *entry;
95
96 hash=0;
97 if (keylen==PJ_HASH_KEY_STRING) {
98 const unsigned char *p = key;
99 for ( ; *p; ++p ) {
100 hash = hash * PJ_HASH_MULTIPLIER + *p;
101 }
102 keylen = p - (const unsigned char*)key;
103 } else {
104 const unsigned char *p = key,
105 *end = p + keylen;
106 for ( ; p!=end; ++p) {
107 hash = hash * PJ_HASH_MULTIPLIER + *p;
108 }
109 }
110
111 /* scan the linked list */
112 for (p_entry = &ht->table[hash & ht->rows], entry=*p_entry;
113 entry;
114 p_entry = &entry->next, entry = *p_entry)
115 {
116 if (entry->hash==hash && entry->keylen==keylen &&
117 memcmp(entry->key, key, keylen)==0)
118 {
119 break;
120 }
121 }
122
123 if (entry || val==NULL)
124 return p_entry;
125
126 /* create a new entry */
127 entry = pj_pool_alloc(pool, sizeof(pj_hash_entry));
128 PJ_LOG(5, ("hashtbl", "%p: New p_entry %p created, pool used=%u, cap=%u", ht, entry,
129 pj_pool_get_used_size(pool), pj_pool_get_capacity(pool)));
130 entry->next = NULL;
131 entry->hash = hash;
132 entry->key = key;
133 entry->keylen = keylen;
134 entry->value = val;
135 *p_entry = entry;
136
137 ++ht->count;
138
139 return p_entry;
140}
141
142PJ_DEF(void *) pj_hash_get( pj_hash_table_t *ht,
143 const void *key, unsigned keylen )
144{
145 pj_hash_entry *entry;
146 entry = *find_entry( NULL, ht, key, keylen, NULL);
147 return entry ? entry->value : NULL;
148}
149
150PJ_DEF(void) pj_hash_set( pj_pool_t *pool, pj_hash_table_t *ht,
151 const void *key, unsigned keylen,
152 void *value )
153{
154 pj_hash_entry **p_entry;
155
156 p_entry = find_entry( pool, ht, key, keylen, value );
157 if (*p_entry) {
158 if (value == NULL) {
159 /* delete entry */
160 PJ_LOG(5, ("hashtbl", "%p: p_entry %p deleted", ht, *p_entry));
161 *p_entry = (*p_entry)->next;
162 --ht->count;
163
164 } else {
165 /* overwrite */
166 (*p_entry)->value = value;
167 PJ_LOG(5, ("hashtbl", "%p: p_entry %p value set to %p", ht, *p_entry, value));
168 }
169 }
170}
171
172PJ_DEF(unsigned) pj_hash_count( pj_hash_table_t *ht )
173{
174 return ht->count;
175}
176
177PJ_DEF(pj_hash_iterator_t*) pj_hash_first( pj_hash_table_t *ht,
178 pj_hash_iterator_t *it )
179{
180 it->index = 0;
181 it->entry = NULL;
182
183 for (; it->index < ht->rows; ++it->index) {
184 it->entry = ht->table[it->index];
185 if (it->entry) {
186 break;
187 }
188 }
189
190 return it->entry ? it : NULL;
191}
192
193PJ_DEF(pj_hash_iterator_t*) pj_hash_next( pj_hash_table_t *ht,
194 pj_hash_iterator_t *it )
195{
196 it->entry = it->entry->next;
197 if (it->entry) {
198 return it;
199 }
200
201 for (++it->index; it->index < ht->rows; ++it->index) {
202 it->entry = ht->table[it->index];
203 if (it->entry) {
204 break;
205 }
206 }
207
208 return it->entry ? it : NULL;
209}
210
211PJ_DEF(void*) pj_hash_this( pj_hash_table_t *ht, pj_hash_iterator_t *it )
212{
213 PJ_CHECK_STACK();
214 PJ_UNUSED_ARG(ht);
215 return it->entry->value;
216}
217
218#if 0
219void pj_hash_dump_collision( pj_hash_table_t *ht )
220{
221 unsigned min=0xFFFFFFFF, max=0;
222 unsigned i;
223 char line[120];
224 int len, totlen = 0;
225
226 for (i=0; i<ht->rows; ++i) {
227 unsigned count = 0;
228 pj_hash_entry *entry = ht->table[i];
229 while (entry) {
230 ++count;
231 entry = entry->next;
232 }
233 if (count < min)
234 min = count;
235 if (count > max)
236 max = count;
237 len = pj_snprintf( line+totlen, sizeof(line)-totlen, "%3d:%3d ", i, count);
238 if (len < 1)
239 break;
240 totlen += len;
241
242 if ((i+1) % 10 == 0) {
243 line[totlen] = '\0';
244 PJ_LOG(4,(__FILE__, line));
245 }
246 }
247
248 PJ_LOG(4,(__FILE__,"Count: %d, min: %d, max: %d\n", ht->count, min, max));
249}
250#endif
251
252