blob: 0c2bda34f116b89ef9ddc00ff239f946028bcf1b [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono9033e312005-11-21 02:08:39 +00004 *
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;
Benny Prijono0e1d35c2007-09-08 07:12:44 +000036 void *key;
Benny Prijono9033e312005-11-21 02:08:39 +000037 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) {
Benny Prijonof260e462007-04-30 21:03:32 +000058 const pj_uint8_t *p = (const pj_uint8_t*)key;
Benny Prijono9033e312005-11-21 02:08:39 +000059 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 {
Benny Prijonof260e462007-04-30 21:03:32 +000063 const pj_uint8_t *p = (const pj_uint8_t*)key,
64 *end = p + keylen;
Benny Prijono9033e312005-11-21 02:08:39 +000065 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 Prijono17e58ed2007-05-28 11:49:46 +0000103 /* Check that PJ_HASH_ENTRY_BUF_SIZE is correct. */
104 PJ_ASSERT_RETURN(sizeof(pj_hash_entry)<=PJ_HASH_ENTRY_BUF_SIZE, NULL);
Benny Prijono40f2f642006-01-30 18:40:05 +0000105
Benny Prijonof260e462007-04-30 21:03:32 +0000106 h = PJ_POOL_ALLOC_T(pool, pj_hash_table_t);
Benny Prijono9033e312005-11-21 02:08:39 +0000107 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;
Benny Prijonof260e462007-04-30 21:03:32 +0000122 h->table = (pj_hash_entry**)
123 pj_pool_calloc(pool, table_size+1, sizeof(pj_hash_entry*));
Benny Prijono9033e312005-11-21 02:08:39 +0000124 return h;
125}
126
127static pj_hash_entry **find_entry( pj_pool_t *pool, pj_hash_table_t *ht,
128 const void *key, unsigned keylen,
Benny Prijono40f2f642006-01-30 18:40:05 +0000129 void *val, pj_uint32_t *hval,
130 void *entry_buf)
Benny Prijono9033e312005-11-21 02:08:39 +0000131{
132 pj_uint32_t hash;
133 pj_hash_entry **p_entry, *entry;
134
Benny Prijono40f2f642006-01-30 18:40:05 +0000135 if (hval && *hval != 0) {
136 hash = *hval;
Benny Prijono07066b02007-04-11 07:48:03 +0000137 if (keylen==PJ_HASH_KEY_STRING) {
138 keylen = pj_ansi_strlen((const char*)key);
139 }
Benny Prijono9033e312005-11-21 02:08:39 +0000140 } else {
Benny Prijono40f2f642006-01-30 18:40:05 +0000141 /* This slightly differs with pj_hash_calc() because we need
142 * to get the keylen when keylen is PJ_HASH_KEY_STRING.
143 */
144 hash=0;
145 if (keylen==PJ_HASH_KEY_STRING) {
Benny Prijonof260e462007-04-30 21:03:32 +0000146 const pj_uint8_t *p = (const pj_uint8_t*)key;
Benny Prijono40f2f642006-01-30 18:40:05 +0000147 for ( ; *p; ++p ) {
148 hash = hash * PJ_HASH_MULTIPLIER + *p;
149 }
150 keylen = p - (const unsigned char*)key;
151 } else {
Benny Prijonof260e462007-04-30 21:03:32 +0000152 const pj_uint8_t *p = (const pj_uint8_t*)key,
153 *end = p + keylen;
Benny Prijono40f2f642006-01-30 18:40:05 +0000154 for ( ; p!=end; ++p) {
155 hash = hash * PJ_HASH_MULTIPLIER + *p;
156 }
Benny Prijono9033e312005-11-21 02:08:39 +0000157 }
Benny Prijono40f2f642006-01-30 18:40:05 +0000158
159 /* Report back the computed hash. */
160 if (hval)
161 *hval = hash;
Benny Prijono9033e312005-11-21 02:08:39 +0000162 }
163
164 /* scan the linked list */
165 for (p_entry = &ht->table[hash & ht->rows], entry=*p_entry;
166 entry;
167 p_entry = &entry->next, entry = *p_entry)
168 {
169 if (entry->hash==hash && entry->keylen==keylen &&
Benny Prijono40f2f642006-01-30 18:40:05 +0000170 pj_memcmp(entry->key, key, keylen)==0)
Benny Prijono9033e312005-11-21 02:08:39 +0000171 {
172 break;
173 }
174 }
175
176 if (entry || val==NULL)
177 return p_entry;
178
Benny Prijono40f2f642006-01-30 18:40:05 +0000179 /* Entry not found, create a new one.
180 * If entry_buf is specified, use it. Otherwise allocate from pool.
181 */
182 if (entry_buf) {
Benny Prijonof260e462007-04-30 21:03:32 +0000183 entry = (pj_hash_entry*)entry_buf;
Benny Prijono40f2f642006-01-30 18:40:05 +0000184 } else {
185 /* Pool must be specified! */
186 PJ_ASSERT_RETURN(pool != NULL, NULL);
187
Benny Prijonof260e462007-04-30 21:03:32 +0000188 entry = PJ_POOL_ALLOC_T(pool, pj_hash_entry);
Benny Prijono40f2f642006-01-30 18:40:05 +0000189 PJ_LOG(6, ("hashtbl",
190 "%p: New p_entry %p created, pool used=%u, cap=%u",
191 ht, entry, pj_pool_get_used_size(pool),
192 pj_pool_get_capacity(pool)));
193 }
Benny Prijono9033e312005-11-21 02:08:39 +0000194 entry->next = NULL;
195 entry->hash = hash;
Benny Prijono0e1d35c2007-09-08 07:12:44 +0000196 if (pool) {
197 entry->key = pj_pool_alloc(pool, keylen);
198 pj_memcpy(entry->key, key, keylen);
199 } else {
200 entry->key = (void*)key;
201 }
Benny Prijono9033e312005-11-21 02:08:39 +0000202 entry->keylen = keylen;
203 entry->value = val;
204 *p_entry = entry;
205
206 ++ht->count;
207
208 return p_entry;
209}
210
211PJ_DEF(void *) pj_hash_get( pj_hash_table_t *ht,
Benny Prijono40f2f642006-01-30 18:40:05 +0000212 const void *key, unsigned keylen,
213 pj_uint32_t *hval)
Benny Prijono9033e312005-11-21 02:08:39 +0000214{
215 pj_hash_entry *entry;
Benny Prijono40f2f642006-01-30 18:40:05 +0000216 entry = *find_entry( NULL, ht, key, keylen, NULL, hval, NULL);
Benny Prijono9033e312005-11-21 02:08:39 +0000217 return entry ? entry->value : NULL;
218}
219
220PJ_DEF(void) pj_hash_set( pj_pool_t *pool, pj_hash_table_t *ht,
Benny Prijono40f2f642006-01-30 18:40:05 +0000221 const void *key, unsigned keylen, pj_uint32_t hval,
Benny Prijono9033e312005-11-21 02:08:39 +0000222 void *value )
223{
224 pj_hash_entry **p_entry;
225
Benny Prijono40f2f642006-01-30 18:40:05 +0000226 p_entry = find_entry( pool, ht, key, keylen, value, &hval, NULL);
Benny Prijono9033e312005-11-21 02:08:39 +0000227 if (*p_entry) {
228 if (value == NULL) {
229 /* delete entry */
Benny Prijonoc81dfef2006-01-07 18:41:22 +0000230 PJ_LOG(6, ("hashtbl", "%p: p_entry %p deleted", ht, *p_entry));
Benny Prijono9033e312005-11-21 02:08:39 +0000231 *p_entry = (*p_entry)->next;
232 --ht->count;
233
234 } else {
235 /* overwrite */
236 (*p_entry)->value = value;
Benny Prijono40f2f642006-01-30 18:40:05 +0000237 PJ_LOG(6, ("hashtbl", "%p: p_entry %p value set to %p", ht,
238 *p_entry, value));
239 }
240 }
241}
242
243PJ_DEF(void) pj_hash_set_np( pj_hash_table_t *ht,
244 const void *key, unsigned keylen,
Benny Prijono17e58ed2007-05-28 11:49:46 +0000245 pj_uint32_t hval, pj_hash_entry_buf entry_buf,
246 void *value)
Benny Prijono40f2f642006-01-30 18:40:05 +0000247{
248 pj_hash_entry **p_entry;
249
Benny Prijono17e58ed2007-05-28 11:49:46 +0000250 p_entry = find_entry( NULL, ht, key, keylen, value, &hval,
251 (void*)entry_buf );
Benny Prijono40f2f642006-01-30 18:40:05 +0000252 if (*p_entry) {
253 if (value == NULL) {
254 /* delete entry */
255 PJ_LOG(6, ("hashtbl", "%p: p_entry %p deleted", ht, *p_entry));
256 *p_entry = (*p_entry)->next;
257 --ht->count;
258
259 } else {
260 /* overwrite */
261 (*p_entry)->value = value;
262 PJ_LOG(6, ("hashtbl", "%p: p_entry %p value set to %p", ht,
263 *p_entry, value));
Benny Prijono9033e312005-11-21 02:08:39 +0000264 }
265 }
266}
267
268PJ_DEF(unsigned) pj_hash_count( pj_hash_table_t *ht )
269{
270 return ht->count;
271}
272
273PJ_DEF(pj_hash_iterator_t*) pj_hash_first( pj_hash_table_t *ht,
274 pj_hash_iterator_t *it )
275{
276 it->index = 0;
277 it->entry = NULL;
278
279 for (; it->index < ht->rows; ++it->index) {
280 it->entry = ht->table[it->index];
281 if (it->entry) {
282 break;
283 }
284 }
285
286 return it->entry ? it : NULL;
287}
288
289PJ_DEF(pj_hash_iterator_t*) pj_hash_next( pj_hash_table_t *ht,
290 pj_hash_iterator_t *it )
291{
292 it->entry = it->entry->next;
293 if (it->entry) {
294 return it;
295 }
296
297 for (++it->index; it->index < ht->rows; ++it->index) {
298 it->entry = ht->table[it->index];
299 if (it->entry) {
300 break;
301 }
302 }
303
304 return it->entry ? it : NULL;
305}
306
307PJ_DEF(void*) pj_hash_this( pj_hash_table_t *ht, pj_hash_iterator_t *it )
308{
309 PJ_CHECK_STACK();
310 PJ_UNUSED_ARG(ht);
311 return it->entry->value;
312}
313
314#if 0
315void pj_hash_dump_collision( pj_hash_table_t *ht )
316{
317 unsigned min=0xFFFFFFFF, max=0;
318 unsigned i;
319 char line[120];
320 int len, totlen = 0;
321
322 for (i=0; i<ht->rows; ++i) {
323 unsigned count = 0;
324 pj_hash_entry *entry = ht->table[i];
325 while (entry) {
326 ++count;
327 entry = entry->next;
328 }
329 if (count < min)
330 min = count;
331 if (count > max)
332 max = count;
333 len = pj_snprintf( line+totlen, sizeof(line)-totlen, "%3d:%3d ", i, count);
334 if (len < 1)
335 break;
336 totlen += len;
337
338 if ((i+1) % 10 == 0) {
339 line[totlen] = '\0';
340 PJ_LOG(4,(__FILE__, line));
341 }
342 }
343
344 PJ_LOG(4,(__FILE__,"Count: %d, min: %d, max: %d\n", ht->count, min, max));
345}
346#endif
347
348