| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 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> |
| 25 |
#include <pj/assert.h> |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#define PJ_HASH_MULTIPLIER 33 |
| 31 |
|
| 32 |
|
| 33 |
struct pj_hash_entry |
| 34 |
{ |
| 35 |
struct pj_hash_entry *next; |
| 36 |
void *key; |
| 37 |
pj_uint32_t hash; |
| 38 |
pj_uint32_t keylen; |
| 39 |
void *value; |
| 40 |
}; |
| 41 |
|
| 42 |
|
| 43 |
struct pj_hash_table_t |
| 44 |
{ |
| 45 |
pj_hash_entry **table; |
| 46 |
unsigned count, rows; |
| 47 |
pj_hash_iterator_t iterator; |
| 48 |
}; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
PJ_DEF(pj_uint32_t) pj_hash_calc(pj_uint32_t hash, const void *key, |
| 53 |
unsigned keylen) |
| 54 |
{ |
| 55 |
PJ_CHECK_STACK(); |
| 56 |
|
| 57 |
if (keylen==PJ_HASH_KEY_STRING) { |
| 58 |
const pj_uint8_t *p = (const pj_uint8_t*)key; |
| 59 |
for ( ; *p; ++p ) { |
| 60 |
hash = (hash * PJ_HASH_MULTIPLIER) + *p; |
| 61 |
} |
| 62 |
} else { |
| 63 |
const pj_uint8_t *p = (const pj_uint8_t*)key, |
| 64 |
*end = p + keylen; |
| 65 |
for ( ; p!=end; ++p) { |
| 66 |
hash = (hash * PJ_HASH_MULTIPLIER) + *p; |
| 67 |
} |
| 68 |
} |
| 69 |
return hash; |
| 70 |
} |
| 71 |
|
| 72 |
PJ_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 |
|
| 78 |
for (i=0; i<key->slen; ++i) { |
| 79 |
int lower = pj_tolower(key->ptr[i]); |
| 80 |
if (result) |
| 81 |
result[i] = (char)lower; |
| 82 |
|
| 83 |
hval = hval * PJ_HASH_MULTIPLIER + lower; |
| 84 |
} |
| 85 |
|
| 86 |
return hval; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
PJ_DEF(pj_hash_table_t*) pj_hash_create(pj_pool_t *pool, unsigned size) |
| 91 |
{ |
| 92 |
pj_hash_table_t *h; |
| 93 |
unsigned table_size; |
| 94 |
|
| 95 |
|
| 96 |
PJ_ASSERT_RETURN(sizeof(pj_hash_entry)<=PJ_HASH_ENTRY_BUF_SIZE, NULL); |
| 97 |
|
| 98 |
h = PJ_POOL_ALLOC_T(pool, pj_hash_table_t); |
| 99 |
h->count = 0; |
| 100 |
|
| 101 |
PJ_LOG( 6, ("hashtbl", "hash table %p created from pool %s", h, pj_pool_getobjname(pool))); |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
table_size = 8; |
| 108 |
do { |
| 109 |
table_size <<= 1; |
| 110 |
} while (table_size < size); |
| 111 |
table_size -= 1; |
| 112 |
|
| 113 |
h->rows = table_size; |
| 114 |
h->table = (pj_hash_entry**) |
| 115 |
pj_pool_calloc(pool, table_size+1, sizeof(pj_hash_entry*)); |
| 116 |
return h; |
| 117 |
} |
| 118 |
|
| 119 |
static pj_hash_entry **find_entry( pj_pool_t *pool, pj_hash_table_t *ht, |
| 120 |
const void *key, unsigned keylen, |
| 121 |
void *val, pj_uint32_t *hval, |
| 122 |
void *entry_buf, pj_bool_t lower) |
| 123 |
{ |
| 124 |
pj_uint32_t hash; |
| 125 |
pj_hash_entry **p_entry, *entry; |
| 126 |
|
| 127 |
if (hval && *hval != 0) { |
| 128 |
hash = *hval; |
| 129 |
if (keylen==PJ_HASH_KEY_STRING) { |
| 130 |
keylen = (unsigned)pj_ansi_strlen((const char*)key); |
| 131 |
} |
| 132 |
} else { |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
hash=0; |
| 137 |
if (keylen==PJ_HASH_KEY_STRING) { |
| 138 |
const pj_uint8_t *p = (const pj_uint8_t*)key; |
| 139 |
for ( ; *p; ++p ) { |
| 140 |
if (lower) |
| 141 |
hash = hash * PJ_HASH_MULTIPLIER + pj_tolower(*p); |
| 142 |
else |
| 143 |
hash = hash * PJ_HASH_MULTIPLIER + *p; |
| 144 |
} |
| 145 |
keylen = (unsigned)(p - (const unsigned char*)key); |
| 146 |
} else { |
| 147 |
const pj_uint8_t *p = (const pj_uint8_t*)key, |
| 148 |
*end = p + keylen; |
| 149 |
for ( ; p!=end; ++p) { |
| 150 |
if (lower) |
| 151 |
hash = hash * PJ_HASH_MULTIPLIER + pj_tolower(*p); |
| 152 |
else |
| 153 |
hash = hash * PJ_HASH_MULTIPLIER + *p; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
if (hval) |
| 159 |
*hval = hash; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
for (p_entry = &ht->table[hash & ht->rows], entry=*p_entry; |
| 164 |
entry; |
| 165 |
p_entry = &entry->next, entry = *p_entry) |
| 166 |
{ |
| 167 |
if (entry->hash==hash && entry->keylen==keylen && |
| 168 |
((lower && pj_ansi_strnicmp((const char*)entry->key, |
| 169 |
(const char*)key, keylen)==0) || |
| 170 |
(!lower && pj_memcmp(entry->key, key, keylen)==0))) |
| 171 |
{ |
| 172 |
break; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
if (entry || val==NULL) |
| 177 |
return p_entry; |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
if (entry_buf) { |
| 183 |
entry = (pj_hash_entry*)entry_buf; |
| 184 |
} else { |
| 185 |
|
| 186 |
PJ_ASSERT_RETURN(pool != NULL, NULL); |
| 187 |
|
| 188 |
entry = PJ_POOL_ALLOC_T(pool, pj_hash_entry); |
| 189 |
PJ_LOG(6, ("hashtbl", |
| 190 |
"%p: New p_entry %p created, pool used=%lu, cap=%lu", |
| 191 |
ht, entry, (unsigned long)pj_pool_get_used_size(pool), |
| 192 |
(unsigned long)pj_pool_get_capacity(pool))); |
| 193 |
} |
| 194 |
entry->next = NULL; |
| 195 |
entry->hash = hash; |
| 196 |
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 |
} |
| 202 |
entry->keylen = keylen; |
| 203 |
entry->value = val; |
| 204 |
*p_entry = entry; |
| 205 |
|
| 206 |
++ht->count; |
| 207 |
|
| 208 |
return p_entry; |
| 209 |
} |
| 210 |
|
| 211 |
PJ_DEF(void *) pj_hash_get( pj_hash_table_t *ht, |
| 212 |
const void *key, unsigned keylen, |
| 213 |
pj_uint32_t *hval) |
| 214 |
{ |
| 215 |
pj_hash_entry *entry; |
| 216 |
entry = *find_entry( NULL, ht, key, keylen, NULL, hval, NULL, PJ_FALSE); |
| 217 |
return entry ? entry->value : NULL; |
| 218 |
} |
| 219 |
|
| 220 |
PJ_DEF(void *) pj_hash_get_lower( pj_hash_table_t *ht, |
| 221 |
const void *key, unsigned keylen, |
| 222 |
pj_uint32_t *hval) |
| 223 |
{ |
| 224 |
pj_hash_entry *entry; |
| 225 |
entry = *find_entry( NULL, ht, key, keylen, NULL, hval, NULL, PJ_TRUE); |
| 226 |
return entry ? entry->value : NULL; |
| 227 |
} |
| 228 |
|
| 229 |
static void hash_set( pj_pool_t *pool, pj_hash_table_t *ht, |
| 230 |
const void *key, unsigned keylen, pj_uint32_t hval, |
| 231 |
void *value, void *entry_buf, pj_bool_t lower ) |
| 232 |
{ |
| 233 |
pj_hash_entry **p_entry; |
| 234 |
|
| 235 |
p_entry = find_entry( pool, ht, key, keylen, value, &hval, entry_buf, |
| 236 |
lower); |
| 237 |
if (*p_entry) { |
| 238 |
if (value == NULL) { |
| 239 |
|
| 240 |
PJ_LOG(6, ("hashtbl", "%p: p_entry %p deleted", ht, *p_entry)); |
| 241 |
*p_entry = (*p_entry)->next; |
| 242 |
--ht->count; |
| 243 |
|
| 244 |
} else { |
| 245 |
|
| 246 |
(*p_entry)->value = value; |
| 247 |
PJ_LOG(6, ("hashtbl", "%p: p_entry %p value set to %p", ht, |
| 248 |
*p_entry, value)); |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
PJ_DEF(void) pj_hash_set( pj_pool_t *pool, pj_hash_table_t *ht, |
| 254 |
const void *key, unsigned keylen, pj_uint32_t hval, |
| 255 |
void *value ) |
| 256 |
{ |
| 257 |
hash_set(pool, ht, key, keylen, hval, value, NULL, PJ_FALSE); |
| 258 |
} |
| 259 |
|
| 260 |
PJ_DEF(void) pj_hash_set_lower( pj_pool_t *pool, pj_hash_table_t *ht, |
| 261 |
const void *key, unsigned keylen, |
| 262 |
pj_uint32_t hval, void *value ) |
| 263 |
{ |
| 264 |
hash_set(pool, ht, key, keylen, hval, value, NULL, PJ_TRUE); |
| 265 |
} |
| 266 |
|
| 267 |
PJ_DEF(void) pj_hash_set_np( pj_hash_table_t *ht, |
| 268 |
const void *key, unsigned keylen, |
| 269 |
pj_uint32_t hval, pj_hash_entry_buf entry_buf, |
| 270 |
void *value) |
| 271 |
{ |
| 272 |
hash_set(NULL, ht, key, keylen, hval, value, (void *)entry_buf, PJ_FALSE); |
| 273 |
} |
| 274 |
|
| 275 |
PJ_DEF(void) pj_hash_set_np_lower( pj_hash_table_t *ht, |
| 276 |
const void *key, unsigned keylen, |
| 277 |
pj_uint32_t hval, |
| 278 |
pj_hash_entry_buf entry_buf, |
| 279 |
void *value) |
| 280 |
{ |
| 281 |
hash_set(NULL, ht, key, keylen, hval, value, (void *)entry_buf, PJ_TRUE); |
| 282 |
} |
| 283 |
|
| 284 |
PJ_DEF(unsigned) pj_hash_count( pj_hash_table_t *ht ) |
| 285 |
{ |
| 286 |
return ht->count; |
| 287 |
} |
| 288 |
|
| 289 |
PJ_DEF(pj_hash_iterator_t*) pj_hash_first( pj_hash_table_t *ht, |
| 290 |
pj_hash_iterator_t *it ) |
| 291 |
{ |
| 292 |
it->index = 0; |
| 293 |
it->entry = NULL; |
| 294 |
|
| 295 |
for (; it->index <= ht->rows; ++it->index) { |
| 296 |
it->entry = ht->table[it->index]; |
| 297 |
if (it->entry) { |
| 298 |
break; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
return it->entry ? it : NULL; |
| 303 |
} |
| 304 |
|
| 305 |
PJ_DEF(pj_hash_iterator_t*) pj_hash_next( pj_hash_table_t *ht, |
| 306 |
pj_hash_iterator_t *it ) |
| 307 |
{ |
| 308 |
it->entry = it->entry->next; |
| 309 |
if (it->entry) { |
| 310 |
return it; |
| 311 |
} |
| 312 |
|
| 313 |
for (++it->index; it->index <= ht->rows; ++it->index) { |
| 314 |
it->entry = ht->table[it->index]; |
| 315 |
if (it->entry) { |
| 316 |
break; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
return it->entry ? it : NULL; |
| 321 |
} |
| 322 |
|
| 323 |
PJ_DEF(void*) pj_hash_this( pj_hash_table_t *ht, pj_hash_iterator_t *it ) |
| 324 |
{ |
| 325 |
PJ_CHECK_STACK(); |
| 326 |
PJ_UNUSED_ARG(ht); |
| 327 |
return it->entry->value; |
| 328 |
} |
| 329 |
|
| 330 |
#if 0 |
| 331 |
void pj_hash_dump_collision( pj_hash_table_t *ht ) |
| 332 |
{ |
| 333 |
unsigned min=0xFFFFFFFF, max=0; |
| 334 |
unsigned i; |
| 335 |
char line[120]; |
| 336 |
int len, totlen = 0; |
| 337 |
|
| 338 |
for (i=0; i<=ht->rows; ++i) { |
| 339 |
unsigned count = 0; |
| 340 |
pj_hash_entry *entry = ht->table[i]; |
| 341 |
while (entry) { |
| 342 |
++count; |
| 343 |
entry = entry->next; |
| 344 |
} |
| 345 |
if (count < min) |
| 346 |
min = count; |
| 347 |
if (count > max) |
| 348 |
max = count; |
| 349 |
len = pj_snprintf( line+totlen, sizeof(line)-totlen, "%3d:%3d ", i, count); |
| 350 |
if (len < 1) |
| 351 |
break; |
| 352 |
totlen += len; |
| 353 |
|
| 354 |
if ((i+1) % 10 == 0) { |
| 355 |
line[totlen] = '\0'; |
| 356 |
PJ_LOG(4,(__FILE__, line)); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
PJ_LOG(4,(__FILE__,"Count: %d, min: %d, max: %d\n", ht->count, min, max)); |
| 361 |
} |
| 362 |
#endif |
| 363 |
|
| 364 |
|