1
/* $Id: hash.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
#include <pj/string.h>
26
#include <pj/assert.h>
29
* The hash multiplier used to calculate hash value.
31
#define PJ_HASH_MULTIPLIER 33
36
struct pj_hash_entry *next;
44
struct pj_hash_table_t
46
pj_hash_entry **table;
48
pj_hash_iterator_t iterator;
53
PJ_DEF(pj_uint32_t) pj_hash_calc(pj_uint32_t hash, const void *key,
58
if (keylen==PJ_HASH_KEY_STRING) {
59
const pj_uint8_t *p = (const pj_uint8_t*)key;
61
hash = (hash * PJ_HASH_MULTIPLIER) + *p;
64
const pj_uint8_t *p = (const pj_uint8_t*)key,
66
for ( ; p!=end; ++p) {
67
hash = (hash * PJ_HASH_MULTIPLIER) + *p;
73
PJ_DEF(pj_uint32_t) pj_hash_calc_tolower( pj_uint32_t hval,
79
#if defined(PJ_HASH_USE_OWN_TOLOWER) && PJ_HASH_USE_OWN_TOLOWER != 0
80
for (i=0; i<key->slen; ++i) {
81
pj_uint8_t c = key->ptr[i];
83
result[i] = (char)(c | 32);
86
hval = hval * PJ_HASH_MULTIPLIER + result[i];
89
for (i=0; i<key->slen; ++i) {
90
result[i] = (char)pj_tolower(key->ptr[i]);
91
hval = hval * PJ_HASH_MULTIPLIER + result[i];
99
PJ_DEF(pj_hash_table_t*) pj_hash_create(pj_pool_t *pool, unsigned size)
104
/* Check that PJ_HASH_ENTRY_BUF_SIZE is correct. */
105
PJ_ASSERT_RETURN(sizeof(pj_hash_entry)<=PJ_HASH_ENTRY_BUF_SIZE, NULL);
107
h = PJ_POOL_ALLOC_T(pool, pj_hash_table_t);
110
PJ_LOG( 6, ("hashtbl", "hash table %p created from pool %s", h, pj_pool_getobjname(pool)));
112
/* size must be 2^n - 1.
113
round-up the size to this rule, except when size is 2^n, then size
114
will be round-down to 2^n-1.
119
} while (table_size < size);
122
h->rows = table_size;
123
h->table = (pj_hash_entry**)
124
pj_pool_calloc(pool, table_size+1, sizeof(pj_hash_entry*));
128
static pj_hash_entry **find_entry( pj_pool_t *pool, pj_hash_table_t *ht,
129
const void *key, unsigned keylen,
130
void *val, pj_uint32_t *hval,
134
pj_hash_entry **p_entry, *entry;
136
if (hval && *hval != 0) {
138
if (keylen==PJ_HASH_KEY_STRING) {
139
keylen = pj_ansi_strlen((const char*)key);
142
/* This slightly differs with pj_hash_calc() because we need
143
* to get the keylen when keylen is PJ_HASH_KEY_STRING.
146
if (keylen==PJ_HASH_KEY_STRING) {
147
const pj_uint8_t *p = (const pj_uint8_t*)key;
149
hash = hash * PJ_HASH_MULTIPLIER + *p;
151
keylen = p - (const unsigned char*)key;
153
const pj_uint8_t *p = (const pj_uint8_t*)key,
155
for ( ; p!=end; ++p) {
156
hash = hash * PJ_HASH_MULTIPLIER + *p;
160
/* Report back the computed hash. */
165
/* scan the linked list */
166
for (p_entry = &ht->table[hash & ht->rows], entry=*p_entry;
168
p_entry = &entry->next, entry = *p_entry)
170
if (entry->hash==hash && entry->keylen==keylen &&
171
pj_memcmp(entry->key, key, keylen)==0)
177
if (entry || val==NULL)
180
/* Entry not found, create a new one.
181
* If entry_buf is specified, use it. Otherwise allocate from pool.
184
entry = (pj_hash_entry*)entry_buf;
186
/* Pool must be specified! */
187
PJ_ASSERT_RETURN(pool != NULL, NULL);
189
entry = PJ_POOL_ALLOC_T(pool, pj_hash_entry);
190
PJ_LOG(6, ("hashtbl",
191
"%p: New p_entry %p created, pool used=%u, cap=%u",
192
ht, entry, pj_pool_get_used_size(pool),
193
pj_pool_get_capacity(pool)));
198
entry->key = pj_pool_alloc(pool, keylen);
199
pj_memcpy(entry->key, key, keylen);
201
entry->key = (void*)key;
203
entry->keylen = keylen;
212
PJ_DEF(void *) pj_hash_get( pj_hash_table_t *ht,
213
const void *key, unsigned keylen,
216
pj_hash_entry *entry;
217
entry = *find_entry( NULL, ht, key, keylen, NULL, hval, NULL);
218
return entry ? entry->value : NULL;
221
PJ_DEF(void) pj_hash_set( pj_pool_t *pool, pj_hash_table_t *ht,
222
const void *key, unsigned keylen, pj_uint32_t hval,
225
pj_hash_entry **p_entry;
227
p_entry = find_entry( pool, ht, key, keylen, value, &hval, NULL);
231
PJ_LOG(6, ("hashtbl", "%p: p_entry %p deleted", ht, *p_entry));
232
*p_entry = (*p_entry)->next;
237
(*p_entry)->value = value;
238
PJ_LOG(6, ("hashtbl", "%p: p_entry %p value set to %p", ht,
244
PJ_DEF(void) pj_hash_set_np( pj_hash_table_t *ht,
245
const void *key, unsigned keylen,
246
pj_uint32_t hval, pj_hash_entry_buf entry_buf,
249
pj_hash_entry **p_entry;
251
p_entry = find_entry( NULL, ht, key, keylen, value, &hval,
256
PJ_LOG(6, ("hashtbl", "%p: p_entry %p deleted", ht, *p_entry));
257
*p_entry = (*p_entry)->next;
262
(*p_entry)->value = value;
263
PJ_LOG(6, ("hashtbl", "%p: p_entry %p value set to %p", ht,
269
PJ_DEF(unsigned) pj_hash_count( pj_hash_table_t *ht )
274
PJ_DEF(pj_hash_iterator_t*) pj_hash_first( pj_hash_table_t *ht,
275
pj_hash_iterator_t *it )
280
for (; it->index <= ht->rows; ++it->index) {
281
it->entry = ht->table[it->index];
287
return it->entry ? it : NULL;
290
PJ_DEF(pj_hash_iterator_t*) pj_hash_next( pj_hash_table_t *ht,
291
pj_hash_iterator_t *it )
293
it->entry = it->entry->next;
298
for (++it->index; it->index <= ht->rows; ++it->index) {
299
it->entry = ht->table[it->index];
305
return it->entry ? it : NULL;
308
PJ_DEF(void*) pj_hash_this( pj_hash_table_t *ht, pj_hash_iterator_t *it )
312
return it->entry->value;
316
void pj_hash_dump_collision( pj_hash_table_t *ht )
318
unsigned min=0xFFFFFFFF, max=0;
323
for (i=0; i<=ht->rows; ++i) {
325
pj_hash_entry *entry = ht->table[i];
334
len = pj_snprintf( line+totlen, sizeof(line)-totlen, "%3d:%3d ", i, count);
339
if ((i+1) % 10 == 0) {
341
PJ_LOG(4,(__FILE__, line));
345
PJ_LOG(4,(__FILE__,"Count: %d, min: %d, max: %d\n", ht->count, min, max));