blob: 0495fadc0aaf73493d027fd1b9ff48a9e1157d95 [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
52PJ_DEF(pj_uint32_t) pj_hash_calc(pj_uint32_t hash, const void *key, unsigned keylen)
53{
54 PJ_CHECK_STACK();
55
56 if (keylen==PJ_HASH_KEY_STRING) {
57 const unsigned char *p = key;
58 for ( ; *p; ++p ) {
59 hash = hash * PJ_HASH_MULTIPLIER + *p;
60 }
Benny Prijono9033e312005-11-21 02:08:39 +000061 } else {
62 const unsigned char *p = key,
63 *end = p + keylen;
64 for ( ; p!=end; ++p) {
65 hash = hash * PJ_HASH_MULTIPLIER + *p;
66 }
67 }
68 return hash;
69}
70
71PJ_DEF(pj_uint32_t) pj_hash_calc_tolower( pj_uint32_t hval,
72 char *result,
73 const pj_str_t *key)
74{
75 long i;
76
77 for (i=0; i<key->slen; ++i) {
78 result[i] = (char)pj_tolower(key->ptr[i]);
79 hval = hval * PJ_HASH_MULTIPLIER + result[i];
80 }
81
82 return hval;
83}
84
85
86PJ_DEF(pj_hash_table_t*) pj_hash_create(pj_pool_t *pool, unsigned size)
87{
88 pj_hash_table_t *h;
89 unsigned table_size;
90
Benny Prijono40f2f642006-01-30 18:40:05 +000091 /* Check that PJ_HASH_ENTRY_SIZE is correct. */
92 PJ_ASSERT_RETURN(sizeof(pj_hash_entry)==PJ_HASH_ENTRY_SIZE, NULL);
93
Benny Prijono9033e312005-11-21 02:08:39 +000094 h = pj_pool_alloc(pool, sizeof(pj_hash_table_t));
95 h->count = 0;
96
Benny Prijonoc81dfef2006-01-07 18:41:22 +000097 PJ_LOG( 6, ("hashtbl", "hash table %p created from pool %s", h, pj_pool_getobjname(pool)));
Benny Prijono9033e312005-11-21 02:08:39 +000098
99 /* size must be 2^n - 1.
100 round-up the size to this rule, except when size is 2^n, then size
101 will be round-down to 2^n-1.
102 */
103 table_size = 8;
104 do {
105 table_size <<= 1;
106 } while (table_size <= size);
107 table_size -= 1;
108
109 h->rows = table_size;
110 h->table = pj_pool_calloc(pool, table_size+1, sizeof(pj_hash_entry*));
111 return h;
112}
113
114static pj_hash_entry **find_entry( pj_pool_t *pool, pj_hash_table_t *ht,
115 const void *key, unsigned keylen,
Benny Prijono40f2f642006-01-30 18:40:05 +0000116 void *val, pj_uint32_t *hval,
117 void *entry_buf)
Benny Prijono9033e312005-11-21 02:08:39 +0000118{
119 pj_uint32_t hash;
120 pj_hash_entry **p_entry, *entry;
121
Benny Prijono40f2f642006-01-30 18:40:05 +0000122 if (hval && *hval != 0) {
123 hash = *hval;
Benny Prijono9033e312005-11-21 02:08:39 +0000124 } else {
Benny Prijono40f2f642006-01-30 18:40:05 +0000125 /* This slightly differs with pj_hash_calc() because we need
126 * to get the keylen when keylen is PJ_HASH_KEY_STRING.
127 */
128 hash=0;
129 if (keylen==PJ_HASH_KEY_STRING) {
130 const unsigned char *p = key;
131 for ( ; *p; ++p ) {
132 hash = hash * PJ_HASH_MULTIPLIER + *p;
133 }
134 keylen = p - (const unsigned char*)key;
135 } else {
136 const unsigned char *p = key,
137 *end = p + keylen;
138 for ( ; p!=end; ++p) {
139 hash = hash * PJ_HASH_MULTIPLIER + *p;
140 }
Benny Prijono9033e312005-11-21 02:08:39 +0000141 }
Benny Prijono40f2f642006-01-30 18:40:05 +0000142
143 /* Report back the computed hash. */
144 if (hval)
145 *hval = hash;
Benny Prijono9033e312005-11-21 02:08:39 +0000146 }
147
148 /* scan the linked list */
149 for (p_entry = &ht->table[hash & ht->rows], entry=*p_entry;
150 entry;
151 p_entry = &entry->next, entry = *p_entry)
152 {
153 if (entry->hash==hash && entry->keylen==keylen &&
Benny Prijono40f2f642006-01-30 18:40:05 +0000154 pj_memcmp(entry->key, key, keylen)==0)
Benny Prijono9033e312005-11-21 02:08:39 +0000155 {
156 break;
157 }
158 }
159
160 if (entry || val==NULL)
161 return p_entry;
162
Benny Prijono40f2f642006-01-30 18:40:05 +0000163 /* Entry not found, create a new one.
164 * If entry_buf is specified, use it. Otherwise allocate from pool.
165 */
166 if (entry_buf) {
167 entry = entry_buf;
168 } else {
169 /* Pool must be specified! */
170 PJ_ASSERT_RETURN(pool != NULL, NULL);
171
172 entry = pj_pool_alloc(pool, sizeof(pj_hash_entry));
173 PJ_LOG(6, ("hashtbl",
174 "%p: New p_entry %p created, pool used=%u, cap=%u",
175 ht, entry, pj_pool_get_used_size(pool),
176 pj_pool_get_capacity(pool)));
177 }
Benny Prijono9033e312005-11-21 02:08:39 +0000178 entry->next = NULL;
179 entry->hash = hash;
180 entry->key = key;
181 entry->keylen = keylen;
182 entry->value = val;
183 *p_entry = entry;
184
185 ++ht->count;
186
187 return p_entry;
188}
189
190PJ_DEF(void *) pj_hash_get( pj_hash_table_t *ht,
Benny Prijono40f2f642006-01-30 18:40:05 +0000191 const void *key, unsigned keylen,
192 pj_uint32_t *hval)
Benny Prijono9033e312005-11-21 02:08:39 +0000193{
194 pj_hash_entry *entry;
Benny Prijono40f2f642006-01-30 18:40:05 +0000195
196 if (hval) *hval = 0;
197 entry = *find_entry( NULL, ht, key, keylen, NULL, hval, NULL);
Benny Prijono9033e312005-11-21 02:08:39 +0000198 return entry ? entry->value : NULL;
199}
200
201PJ_DEF(void) pj_hash_set( pj_pool_t *pool, pj_hash_table_t *ht,
Benny Prijono40f2f642006-01-30 18:40:05 +0000202 const void *key, unsigned keylen, pj_uint32_t hval,
Benny Prijono9033e312005-11-21 02:08:39 +0000203 void *value )
204{
205 pj_hash_entry **p_entry;
206
Benny Prijono40f2f642006-01-30 18:40:05 +0000207 p_entry = find_entry( pool, ht, key, keylen, value, &hval, NULL);
Benny Prijono9033e312005-11-21 02:08:39 +0000208 if (*p_entry) {
209 if (value == NULL) {
210 /* delete entry */
Benny Prijonoc81dfef2006-01-07 18:41:22 +0000211 PJ_LOG(6, ("hashtbl", "%p: p_entry %p deleted", ht, *p_entry));
Benny Prijono9033e312005-11-21 02:08:39 +0000212 *p_entry = (*p_entry)->next;
213 --ht->count;
214
215 } else {
216 /* overwrite */
217 (*p_entry)->value = value;
Benny Prijono40f2f642006-01-30 18:40:05 +0000218 PJ_LOG(6, ("hashtbl", "%p: p_entry %p value set to %p", ht,
219 *p_entry, value));
220 }
221 }
222}
223
224PJ_DEF(void) pj_hash_set_np( pj_hash_table_t *ht,
225 const void *key, unsigned keylen,
226 pj_uint32_t hval, void *entry_buf, void *value)
227{
228 pj_hash_entry **p_entry;
229
230 p_entry = find_entry( NULL, ht, key, keylen, value, &hval, entry_buf );
231 if (*p_entry) {
232 if (value == NULL) {
233 /* delete entry */
234 PJ_LOG(6, ("hashtbl", "%p: p_entry %p deleted", ht, *p_entry));
235 *p_entry = (*p_entry)->next;
236 --ht->count;
237
238 } else {
239 /* overwrite */
240 (*p_entry)->value = value;
241 PJ_LOG(6, ("hashtbl", "%p: p_entry %p value set to %p", ht,
242 *p_entry, value));
Benny Prijono9033e312005-11-21 02:08:39 +0000243 }
244 }
245}
246
247PJ_DEF(unsigned) pj_hash_count( pj_hash_table_t *ht )
248{
249 return ht->count;
250}
251
252PJ_DEF(pj_hash_iterator_t*) pj_hash_first( pj_hash_table_t *ht,
253 pj_hash_iterator_t *it )
254{
255 it->index = 0;
256 it->entry = NULL;
257
258 for (; it->index < ht->rows; ++it->index) {
259 it->entry = ht->table[it->index];
260 if (it->entry) {
261 break;
262 }
263 }
264
265 return it->entry ? it : NULL;
266}
267
268PJ_DEF(pj_hash_iterator_t*) pj_hash_next( pj_hash_table_t *ht,
269 pj_hash_iterator_t *it )
270{
271 it->entry = it->entry->next;
272 if (it->entry) {
273 return it;
274 }
275
276 for (++it->index; it->index < ht->rows; ++it->index) {
277 it->entry = ht->table[it->index];
278 if (it->entry) {
279 break;
280 }
281 }
282
283 return it->entry ? it : NULL;
284}
285
286PJ_DEF(void*) pj_hash_this( pj_hash_table_t *ht, pj_hash_iterator_t *it )
287{
288 PJ_CHECK_STACK();
289 PJ_UNUSED_ARG(ht);
290 return it->entry->value;
291}
292
293#if 0
294void pj_hash_dump_collision( pj_hash_table_t *ht )
295{
296 unsigned min=0xFFFFFFFF, max=0;
297 unsigned i;
298 char line[120];
299 int len, totlen = 0;
300
301 for (i=0; i<ht->rows; ++i) {
302 unsigned count = 0;
303 pj_hash_entry *entry = ht->table[i];
304 while (entry) {
305 ++count;
306 entry = entry->next;
307 }
308 if (count < min)
309 min = count;
310 if (count > max)
311 max = count;
312 len = pj_snprintf( line+totlen, sizeof(line)-totlen, "%3d:%3d ", i, count);
313 if (len < 1)
314 break;
315 totlen += len;
316
317 if ((i+1) % 10 == 0) {
318 line[totlen] = '\0';
319 PJ_LOG(4,(__FILE__, line));
320 }
321 }
322
323 PJ_LOG(4,(__FILE__,"Count: %d, min: %d, max: %d\n", ht->count, min, max));
324}
325#endif
326
327