1
/* $Id: hash.c 4537 2013-06-19 06:47:43Z riza $ */
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];
84
lower = (char)(c | 32);
89
hval = hval * PJ_HASH_MULTIPLIER + lower;
92
for (i=0; i<key->slen; ++i) {
93
char lower = (char)pj_tolower(key->ptr[i]);
96
hval = hval * PJ_HASH_MULTIPLIER + lower;
104
PJ_DEF(pj_hash_table_t*) pj_hash_create(pj_pool_t *pool, unsigned size)
109
/* Check that PJ_HASH_ENTRY_BUF_SIZE is correct. */
110
PJ_ASSERT_RETURN(sizeof(pj_hash_entry)<=PJ_HASH_ENTRY_BUF_SIZE, NULL);
112
h = PJ_POOL_ALLOC_T(pool, pj_hash_table_t);
115
PJ_LOG( 6, ("hashtbl", "hash table %p created from pool %s", h, pj_pool_getobjname(pool)));
117
/* size must be 2^n - 1.
118
round-up the size to this rule, except when size is 2^n, then size
119
will be round-down to 2^n-1.
124
} while (table_size < size);
127
h->rows = table_size;
128
h->table = (pj_hash_entry**)
129
pj_pool_calloc(pool, table_size+1, sizeof(pj_hash_entry*));
133
static pj_hash_entry **find_entry( pj_pool_t *pool, pj_hash_table_t *ht,
134
const void *key, unsigned keylen,
135
void *val, pj_uint32_t *hval,
136
void *entry_buf, pj_bool_t lower)
139
pj_hash_entry **p_entry, *entry;
141
if (hval && *hval != 0) {
143
if (keylen==PJ_HASH_KEY_STRING) {
144
keylen = (unsigned)pj_ansi_strlen((const char*)key);
147
/* This slightly differs with pj_hash_calc() because we need
148
* to get the keylen when keylen is PJ_HASH_KEY_STRING.
151
if (keylen==PJ_HASH_KEY_STRING) {
152
const pj_uint8_t *p = (const pj_uint8_t*)key;
155
hash = hash * PJ_HASH_MULTIPLIER + pj_tolower(*p);
157
hash = hash * PJ_HASH_MULTIPLIER + *p;
159
keylen = (unsigned)(p - (const unsigned char*)key);
161
const pj_uint8_t *p = (const pj_uint8_t*)key,
163
for ( ; p!=end; ++p) {
165
hash = hash * PJ_HASH_MULTIPLIER + pj_tolower(*p);
167
hash = hash * PJ_HASH_MULTIPLIER + *p;
171
/* Report back the computed hash. */
176
/* scan the linked list */
177
for (p_entry = &ht->table[hash & ht->rows], entry=*p_entry;
179
p_entry = &entry->next, entry = *p_entry)
181
if (entry->hash==hash && entry->keylen==keylen &&
182
((lower && pj_ansi_strnicmp((const char*)entry->key,
183
(const char*)key, keylen)==0) ||
184
(!lower && pj_memcmp(entry->key, key, keylen)==0)))
190
if (entry || val==NULL)
193
/* Entry not found, create a new one.
194
* If entry_buf is specified, use it. Otherwise allocate from pool.
197
entry = (pj_hash_entry*)entry_buf;
199
/* Pool must be specified! */
200
PJ_ASSERT_RETURN(pool != NULL, NULL);
202
entry = PJ_POOL_ALLOC_T(pool, pj_hash_entry);
203
PJ_LOG(6, ("hashtbl",
204
"%p: New p_entry %p created, pool used=%u, cap=%u",
205
ht, entry, pj_pool_get_used_size(pool),
206
pj_pool_get_capacity(pool)));
211
entry->key = pj_pool_alloc(pool, keylen);
212
pj_memcpy(entry->key, key, keylen);
214
entry->key = (void*)key;
216
entry->keylen = keylen;
225
PJ_DEF(void *) pj_hash_get( pj_hash_table_t *ht,
226
const void *key, unsigned keylen,
229
pj_hash_entry *entry;
230
entry = *find_entry( NULL, ht, key, keylen, NULL, hval, NULL, PJ_FALSE);
231
return entry ? entry->value : NULL;
234
PJ_DEF(void *) pj_hash_get_lower( pj_hash_table_t *ht,
235
const void *key, unsigned keylen,
238
pj_hash_entry *entry;
239
entry = *find_entry( NULL, ht, key, keylen, NULL, hval, NULL, PJ_TRUE);
240
return entry ? entry->value : NULL;
243
static void hash_set( pj_pool_t *pool, pj_hash_table_t *ht,
244
const void *key, unsigned keylen, pj_uint32_t hval,
245
void *value, void *entry_buf, pj_bool_t lower )
247
pj_hash_entry **p_entry;
249
p_entry = find_entry( pool, ht, key, keylen, value, &hval, entry_buf,
254
PJ_LOG(6, ("hashtbl", "%p: p_entry %p deleted", ht, *p_entry));
255
*p_entry = (*p_entry)->next;
260
(*p_entry)->value = value;
261
PJ_LOG(6, ("hashtbl", "%p: p_entry %p value set to %p", ht,
267
PJ_DEF(void) pj_hash_set( pj_pool_t *pool, pj_hash_table_t *ht,
268
const void *key, unsigned keylen, pj_uint32_t hval,
271
hash_set(pool, ht, key, keylen, hval, value, NULL, PJ_FALSE);
274
PJ_DEF(void) pj_hash_set_lower( pj_pool_t *pool, pj_hash_table_t *ht,
275
const void *key, unsigned keylen,
276
pj_uint32_t hval, void *value )
278
hash_set(pool, ht, key, keylen, hval, value, NULL, PJ_TRUE);
281
PJ_DEF(void) pj_hash_set_np( pj_hash_table_t *ht,
282
const void *key, unsigned keylen,
283
pj_uint32_t hval, pj_hash_entry_buf entry_buf,
286
hash_set(NULL, ht, key, keylen, hval, value, (void *)entry_buf, PJ_FALSE);
289
PJ_DEF(void) pj_hash_set_np_lower( pj_hash_table_t *ht,
290
const void *key, unsigned keylen,
292
pj_hash_entry_buf entry_buf,
295
hash_set(NULL, ht, key, keylen, hval, value, (void *)entry_buf, PJ_TRUE);
298
PJ_DEF(unsigned) pj_hash_count( pj_hash_table_t *ht )
303
PJ_DEF(pj_hash_iterator_t*) pj_hash_first( pj_hash_table_t *ht,
304
pj_hash_iterator_t *it )
309
for (; it->index <= ht->rows; ++it->index) {
310
it->entry = ht->table[it->index];
316
return it->entry ? it : NULL;
319
PJ_DEF(pj_hash_iterator_t*) pj_hash_next( pj_hash_table_t *ht,
320
pj_hash_iterator_t *it )
322
it->entry = it->entry->next;
327
for (++it->index; it->index <= ht->rows; ++it->index) {
328
it->entry = ht->table[it->index];
334
return it->entry ? it : NULL;
337
PJ_DEF(void*) pj_hash_this( pj_hash_table_t *ht, pj_hash_iterator_t *it )
341
return it->entry->value;
345
void pj_hash_dump_collision( pj_hash_table_t *ht )
347
unsigned min=0xFFFFFFFF, max=0;
352
for (i=0; i<=ht->rows; ++i) {
354
pj_hash_entry *entry = ht->table[i];
363
len = pj_snprintf( line+totlen, sizeof(line)-totlen, "%3d:%3d ", i, count);
368
if ((i+1) % 10 == 0) {
370
PJ_LOG(4,(__FILE__, line));
374
PJ_LOG(4,(__FILE__,"Count: %d, min: %d, max: %d\n", ht->count, min, max));