blob: a3daad3c4ac0a303d44443f033eeba283bad7e16 [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
3 * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include <pj/hash.h>
20#include <pj/log.h>
21#include <pj/string.h>
22#include <pj/pool.h>
23#include <pj/os.h>
24#include <pj/ctype.h>
Benny Prijono40f2f642006-01-30 18:40:05 +000025#include <pj/assert.h>
Benny Prijono9033e312005-11-21 02:08:39 +000026
27/**
28 * The hash multiplier used to calculate hash value.
29 */
30#define PJ_HASH_MULTIPLIER 33
31
32
33struct pj_hash_entry
34{
35 struct pj_hash_entry *next;
36 const void *key;
37 pj_uint32_t hash;
38 pj_uint32_t keylen;
39 void *value;
40};
41
42
43struct pj_hash_table_t
44{
45 pj_hash_entry **table;
46 unsigned count, rows;
47 pj_hash_iterator_t iterator;
48};
49
50
51
Benny Prijono42c5b9e2006-05-10 19:24:40 +000052PJ_DEF(pj_uint32_t) pj_hash_calc(pj_uint32_t hash, const void *key,
53 unsigned keylen)
Benny Prijono9033e312005-11-21 02:08:39 +000054{
55 PJ_CHECK_STACK();
56
57 if (keylen==PJ_HASH_KEY_STRING) {
58 const unsigned char *p = key;
59 for ( ; *p; ++p ) {
Benny Prijono42c5b9e2006-05-10 19:24:40 +000060 hash = (hash * PJ_HASH_MULTIPLIER) + *p;
Benny Prijono9033e312005-11-21 02:08:39 +000061 }
Benny Prijono9033e312005-11-21 02:08:39 +000062 } else {
63 const unsigned char *p = key,
64 *end = p + keylen;
65 for ( ; p!=end; ++p) {
Benny Prijono42c5b9e2006-05-10 19:24:40 +000066 hash = (hash * PJ_HASH_MULTIPLIER) + *p;
Benny Prijono9033e312005-11-21 02:08:39 +000067 }
68 }
69 return hash;
70}
71
72PJ_DEF(pj_uint32_t) pj_hash_calc_tolower( pj_uint32_t hval,
73 char *result,
74 const pj_str_t *key)
75{
76 long i;
77
Benny Prijonob6eab2c2006-07-03 22:08:47 +000078#if defined(PJ_HASH_USE_OWN_TOLOWER) && PJ_HASH_USE_OWN_TOLOWER != 0
79 for (i=0; i<key->slen; ++i) {
80 pj_uint8_t c = key->ptr[i];
81 if (c & 64)
82 result[i] = (char)(c | 32);
83 else
84 result[i] = (char)c;
85 hval = hval * PJ_HASH_MULTIPLIER + result[i];
86 }
87#else
Benny Prijono9033e312005-11-21 02:08:39 +000088 for (i=0; i<key->slen; ++i) {
89 result[i] = (char)pj_tolower(key->ptr[i]);
90 hval = hval * PJ_HASH_MULTIPLIER + result[i];
91 }
Benny Prijonob6eab2c2006-07-03 22:08:47 +000092#endif
Benny Prijono9033e312005-11-21 02:08:39 +000093
94 return hval;
95}
96
97
98PJ_DEF(pj_hash_table_t*) pj_hash_create(pj_pool_t *pool, unsigned size)
99{
100 pj_hash_table_t *h;
101 unsigned table_size;
102
Benny Prijono40f2f642006-01-30 18:40:05 +0000103 /* Check that PJ_HASH_ENTRY_SIZE is correct. */
104 PJ_ASSERT_RETURN(sizeof(pj_hash_entry)==PJ_HASH_ENTRY_SIZE, NULL);
105
Benny Prijono9033e312005-11-21 02:08:39 +0000106 h = pj_pool_alloc(pool, sizeof(pj_hash_table_t));
107 h->count = 0;
108
Benny Prijonoc81dfef2006-01-07 18:41:22 +0000109 PJ_LOG( 6, ("hashtbl", "hash table %p created from pool %s", h, pj_pool_getobjname(pool)));
Benny Prijono9033e312005-11-21 02:08:39 +0000110
111 /* size must be 2^n - 1.
112 round-up the size to this rule, except when size is 2^n, then size
113 will be round-down to 2^n-1.
114 */
115 table_size = 8;
116 do {
117 table_size <<= 1;
Benny Prijono3ae5f062006-10-03 17:13:22 +0000118 } while (table_size < size);
Benny Prijono9033e312005-11-21 02:08:39 +0000119 table_size -= 1;
120
121 h->rows = table_size;
122 h->table = pj_pool_calloc(pool, table_size+1, sizeof(pj_hash_entry*));
123 return h;
124}
125
126static pj_hash_entry **find_entry( pj_pool_t *pool, pj_hash_table_t *ht,
127 const void *key, unsigned keylen,
Benny Prijono40f2f642006-01-30 18:40:05 +0000128 void *val, pj_uint32_t *hval,
129 void *entry_buf)
Benny Prijono9033e312005-11-21 02:08:39 +0000130{
131 pj_uint32_t hash;
132 pj_hash_entry **p_entry, *entry;
133
Benny Prijono40f2f642006-01-30 18:40:05 +0000134 if (hval && *hval != 0) {
135 hash = *hval;
Benny Prijono9033e312005-11-21 02:08:39 +0000136 } else {
Benny Prijono40f2f642006-01-30 18:40:05 +0000137 /* This slightly differs with pj_hash_calc() because we need
138 * to get the keylen when keylen is PJ_HASH_KEY_STRING.
139 */
140 hash=0;
141 if (keylen==PJ_HASH_KEY_STRING) {
142 const unsigned char *p = key;
143 for ( ; *p; ++p ) {
144 hash = hash * PJ_HASH_MULTIPLIER + *p;
145 }
146 keylen = p - (const unsigned char*)key;
147 } else {
148 const unsigned char *p = key,
149 *end = p + keylen;
150 for ( ; p!=end; ++p) {
151 hash = hash * PJ_HASH_MULTIPLIER + *p;
152 }
Benny Prijono9033e312005-11-21 02:08:39 +0000153 }
Benny Prijono40f2f642006-01-30 18:40:05 +0000154
155 /* Report back the computed hash. */
156 if (hval)
157 *hval = hash;
Benny Prijono9033e312005-11-21 02:08:39 +0000158 }
159
160 /* scan the linked list */
161 for (p_entry = &ht->table[hash & ht->rows], entry=*p_entry;
162 entry;
163 p_entry = &entry->next, entry = *p_entry)
164 {
165 if (entry->hash==hash && entry->keylen==keylen &&
Benny Prijono40f2f642006-01-30 18:40:05 +0000166 pj_memcmp(entry->key, key, keylen)==0)
Benny Prijono9033e312005-11-21 02:08:39 +0000167 {
168 break;
169 }
170 }
171
172 if (entry || val==NULL)
173 return p_entry;
174
Benny Prijono40f2f642006-01-30 18:40:05 +0000175 /* Entry not found, create a new one.
176 * If entry_buf is specified, use it. Otherwise allocate from pool.
177 */
178 if (entry_buf) {
179 entry = entry_buf;
180 } else {
181 /* Pool must be specified! */
182 PJ_ASSERT_RETURN(pool != NULL, NULL);
183
184 entry = pj_pool_alloc(pool, sizeof(pj_hash_entry));
185 PJ_LOG(6, ("hashtbl",
186 "%p: New p_entry %p created, pool used=%u, cap=%u",
187 ht, entry, pj_pool_get_used_size(pool),
188 pj_pool_get_capacity(pool)));
189 }
Benny Prijono9033e312005-11-21 02:08:39 +0000190 entry->next = NULL;
191 entry->hash = hash;
192 entry->key = key;
193 entry->keylen = keylen;
194 entry->value = val;
195 *p_entry = entry;
196
197 ++ht->count;
198
199 return p_entry;
200}
201
202PJ_DEF(void *) pj_hash_get( pj_hash_table_t *ht,
Benny Prijono40f2f642006-01-30 18:40:05 +0000203 const void *key, unsigned keylen,
204 pj_uint32_t *hval)
Benny Prijono9033e312005-11-21 02:08:39 +0000205{
206 pj_hash_entry *entry;
Benny Prijono40f2f642006-01-30 18:40:05 +0000207 entry = *find_entry( NULL, ht, key, keylen, NULL, hval, NULL);
Benny Prijono9033e312005-11-21 02:08:39 +0000208 return entry ? entry->value : NULL;
209}
210
211PJ_DEF(void) pj_hash_set( pj_pool_t *pool, pj_hash_table_t *ht,
Benny Prijono40f2f642006-01-30 18:40:05 +0000212 const void *key, unsigned keylen, pj_uint32_t hval,
Benny Prijono9033e312005-11-21 02:08:39 +0000213 void *value )
214{
215 pj_hash_entry **p_entry;
216
Benny Prijono40f2f642006-01-30 18:40:05 +0000217 p_entry = find_entry( pool, ht, key, keylen, value, &hval, NULL);
Benny Prijono9033e312005-11-21 02:08:39 +0000218 if (*p_entry) {
219 if (value == NULL) {
220 /* delete entry */
Benny Prijonoc81dfef2006-01-07 18:41:22 +0000221 PJ_LOG(6, ("hashtbl", "%p: p_entry %p deleted", ht, *p_entry));
Benny Prijono9033e312005-11-21 02:08:39 +0000222 *p_entry = (*p_entry)->next;
223 --ht->count;
224
225 } else {
226 /* overwrite */
227 (*p_entry)->value = value;
Benny Prijono40f2f642006-01-30 18:40:05 +0000228 PJ_LOG(6, ("hashtbl", "%p: p_entry %p value set to %p", ht,
229 *p_entry, value));
230 }
231 }
232}
233
234PJ_DEF(void) pj_hash_set_np( pj_hash_table_t *ht,
235 const void *key, unsigned keylen,
236 pj_uint32_t hval, void *entry_buf, void *value)
237{
238 pj_hash_entry **p_entry;
239
240 p_entry = find_entry( NULL, ht, key, keylen, value, &hval, entry_buf );
241 if (*p_entry) {
242 if (value == NULL) {
243 /* delete entry */
244 PJ_LOG(6, ("hashtbl", "%p: p_entry %p deleted", ht, *p_entry));
245 *p_entry = (*p_entry)->next;
246 --ht->count;
247
248 } else {
249 /* overwrite */
250 (*p_entry)->value = value;
251 PJ_LOG(6, ("hashtbl", "%p: p_entry %p value set to %p", ht,
252 *p_entry, value));
Benny Prijono9033e312005-11-21 02:08:39 +0000253 }
254 }
255}
256
257PJ_DEF(unsigned) pj_hash_count( pj_hash_table_t *ht )
258{
259 return ht->count;
260}
261
262PJ_DEF(pj_hash_iterator_t*) pj_hash_first( pj_hash_table_t *ht,
263 pj_hash_iterator_t *it )
264{
265 it->index = 0;
266 it->entry = NULL;
267
268 for (; it->index < ht->rows; ++it->index) {
269 it->entry = ht->table[it->index];
270 if (it->entry) {
271 break;
272 }
273 }
274
275 return it->entry ? it : NULL;
276}
277
278PJ_DEF(pj_hash_iterator_t*) pj_hash_next( pj_hash_table_t *ht,
279 pj_hash_iterator_t *it )
280{
281 it->entry = it->entry->next;
282 if (it->entry) {
283 return it;
284 }
285
286 for (++it->index; it->index < ht->rows; ++it->index) {
287 it->entry = ht->table[it->index];
288 if (it->entry) {
289 break;
290 }
291 }
292
293 return it->entry ? it : NULL;
294}
295
296PJ_DEF(void*) pj_hash_this( pj_hash_table_t *ht, pj_hash_iterator_t *it )
297{
298 PJ_CHECK_STACK();
299 PJ_UNUSED_ARG(ht);
300 return it->entry->value;
301}
302
303#if 0
304void pj_hash_dump_collision( pj_hash_table_t *ht )
305{
306 unsigned min=0xFFFFFFFF, max=0;
307 unsigned i;
308 char line[120];
309 int len, totlen = 0;
310
311 for (i=0; i<ht->rows; ++i) {
312 unsigned count = 0;
313 pj_hash_entry *entry = ht->table[i];
314 while (entry) {
315 ++count;
316 entry = entry->next;
317 }
318 if (count < min)
319 min = count;
320 if (count > max)
321 max = count;
322 len = pj_snprintf( line+totlen, sizeof(line)-totlen, "%3d:%3d ", i, count);
323 if (len < 1)
324 break;
325 totlen += len;
326
327 if ((i+1) % 10 == 0) {
328 line[totlen] = '\0';
329 PJ_LOG(4,(__FILE__, line));
330 }
331 }
332
333 PJ_LOG(4,(__FILE__,"Count: %d, min: %d, max: %d\n", ht->count, min, max));
334}
335#endif
336
337