Apache HTTPD
apr_hash.c
Go to the documentation of this file.
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "apr_private.h"
18
19#include "apr_general.h"
20#include "apr_pools.h"
21#include "apr_time.h"
22
23#include "apr_hash.h"
24
25#if APR_HAVE_STDLIB_H
26#include <stdlib.h>
27#endif
28#if APR_HAVE_STRING_H
29#include <string.h>
30#endif
31
32#if APR_POOL_DEBUG && APR_HAVE_STDIO_H
33#include <stdio.h>
34#endif
35
36/*
37 * The internal form of a hash table.
38 *
39 * The table is an array indexed by the hash of the key; collisions
40 * are resolved by hanging a linked list of hash entries off each
41 * element of the array. Although this is a really simple design it
42 * isn't too bad given that pools have a low allocation overhead.
43 */
44
46
49 unsigned int hash;
50 const void *key;
52 const void *val;
53};
54
55/*
56 * Data structure for iterating through a hash table.
57 *
58 * We keep a pointer to the next hash entry here to allow the current
59 * hash entry to be freed or otherwise mangled between calls to
60 * apr_hash_next().
61 */
65 unsigned int index;
66};
67
68/*
69 * The size of the array is always a power of two. We use the maximum
70 * index rather than the size so that we can use bitwise-AND for
71 * modular arithmetic.
72 * The count of hash entries may be greater depending on the chosen
73 * collision rate.
74 */
75struct apr_hash_t {
78 apr_hash_index_t iterator; /* For apr_hash_first(NULL, ...) */
79 unsigned int count, max, seed;
81 apr_hash_entry_t *free; /* List of recycled entries */
82};
83
84#define INITIAL_MAX 15 /* tunable == 2^n - 1 */
85
86
87/*
88 * Hash creation functions.
89 */
90
92{
93 return apr_pcalloc(ht->pool, sizeof(*ht->array) * (max + 1));
94}
95
97{
100
101 ht = apr_palloc(pool, sizeof(apr_hash_t));
102 ht->pool = pool;
103 ht->free = NULL;
104 ht->count = 0;
105 ht->max = INITIAL_MAX;
106 ht->seed = (unsigned int)((now >> 32) ^ now ^ (apr_uintptr_t)pool ^
108 ht->array = alloc_array(ht, ht->max);
109 ht->hash_func = NULL;
110
111 return ht;
112}
113
116{
119 return ht;
120}
121
122
123/*
124 * Hash iteration functions.
125 */
126
128{
129 hi->this = hi->next;
130 while (!hi->this) {
131 if (hi->index > hi->ht->max)
132 return NULL;
133
134 hi->this = hi->ht->array[hi->index++];
135 }
136 hi->next = hi->this->next;
137 return hi;
138}
139
141{
143 if (p)
144 hi = apr_palloc(p, sizeof(*hi));
145 else
146 hi = &ht->iterator;
147
148 hi->ht = ht;
149 hi->index = 0;
150 hi->this = NULL;
151 hi->next = NULL;
152 return apr_hash_next(hi);
153}
154
156 const void **key,
158 void **val)
159{
160 if (key) *key = hi->this->key;
161 if (klen) *klen = hi->this->klen;
162 if (val) *val = (void *)hi->this->val;
163}
164
166{
167 const void *key;
168
170 return key;
171}
172
180
182{
183 void *val;
184
186 return val;
187}
188
189/*
190 * Expanding a hash table
191 */
192
194{
197 unsigned int new_max;
198
199 new_max = ht->max * 2 + 1;
201 for (hi = apr_hash_first(NULL, ht); hi; hi = apr_hash_next(hi)) {
202 unsigned int i = hi->this->hash & new_max;
203 hi->this->next = new_array[i];
204 new_array[i] = hi->this;
205 }
206 ht->array = new_array;
207 ht->max = new_max;
208}
209
210static unsigned int hashfunc_default(const char *char_key, apr_ssize_t *klen,
211 unsigned int hash)
212{
213 const unsigned char *key = (const unsigned char *)char_key;
214 const unsigned char *p;
216
217 /*
218 * This is the popular `times 33' hash algorithm which is used by
219 * perl and also appears in Berkeley DB. This is one of the best
220 * known hash functions for strings because it is both computed
221 * very fast and distributes very well.
222 *
223 * The originator may be Dan Bernstein but the code in Berkeley DB
224 * cites Chris Torek as the source. The best citation I have found
225 * is "Chris Torek, Hash function for text in C, Usenet message
226 * <[email protected]> in comp.lang.c , October, 1990." in Rich
227 * Salz's USENIX 1992 paper about INN which can be found at
228 * <http://citeseer.nj.nec.com/salz92internetnews.html>.
229 *
230 * The magic of number 33, i.e. why it works better than many other
231 * constants, prime or not, has never been adequately explained by
232 * anyone. So I try an explanation: if one experimentally tests all
233 * multipliers between 1 and 256 (as I did while writing a low-level
234 * data structure library some time ago) one detects that even
235 * numbers are not useable at all. The remaining 128 odd numbers
236 * (except for the number 1) work more or less all equally well.
237 * They all distribute in an acceptable way and this way fill a hash
238 * table with an average percent of approx. 86%.
239 *
240 * If one compares the chi^2 values of the variants (see
241 * Bob Jenkins ``Hashing Frequently Asked Questions'' at
242 * http://burtleburtle.net/bob/hash/hashfaq.html for a description
243 * of chi^2), the number 33 not even has the best value. But the
244 * number 33 and a few other equally good numbers like 17, 31, 63,
245 * 127 and 129 have nevertheless a great advantage to the remaining
246 * numbers in the large set of possible multipliers: their multiply
247 * operation can be replaced by a faster operation based on just one
248 * shift plus either a single addition or subtraction operation. And
249 * because a hash function has to both distribute good _and_ has to
250 * be very fast to compute, those few numbers should be preferred.
251 *
252 * -- Ralf S. Engelschall <[email protected]>
253 */
254
255 if (*klen == APR_HASH_KEY_STRING) {
256 for (p = key; *p; p++) {
257 hash = hash * 33 + *p;
258 }
259 *klen = p - key;
260 }
261 else {
262 for (p = key, i = *klen; i; i--, p++) {
263 hash = hash * 33 + *p;
264 }
265 }
266
267 return hash;
268}
269
272{
273 return hashfunc_default(char_key, klen, 0);
274}
275
276/*
277 * This is where we keep the details of the hash function and control
278 * the maximum collision rate.
279 *
280 * If val is non-NULL it creates and initializes a new hash entry if
281 * there isn't already one there; it returns an updatable pointer so
282 * that hash entries can be removed.
283 */
284
286 const void *key,
288 const void *val)
289{
291 unsigned int hash;
292
293 if (ht->hash_func)
294 hash = ht->hash_func(key, &klen);
295 else
297
298 /* scan linked list */
299 for (hep = &ht->array[hash & ht->max], he = *hep;
300 he; hep = &he->next, he = *hep) {
301 if (he->hash == hash
302 && he->klen == klen
303 && memcmp(he->key, key, klen) == 0)
304 break;
305 }
306 if (he || !val)
307 return hep;
308
309 /* add a new entry for non-NULL values */
310 if ((he = ht->free) != NULL)
311 ht->free = he->next;
312 else
313 he = apr_palloc(ht->pool, sizeof(*he));
314 he->next = NULL;
315 he->hash = hash;
316 he->key = key;
317 he->klen = klen;
318 he->val = val;
319 *hep = he;
320 ht->count++;
321 return hep;
322}
323
325 const apr_hash_t *orig)
326{
327 apr_hash_t *ht;
329 unsigned int i, j;
330
331 ht = apr_palloc(pool, sizeof(apr_hash_t) +
332 sizeof(*ht->array) * (orig->max + 1) +
333 sizeof(apr_hash_entry_t) * orig->count);
334 ht->pool = pool;
335 ht->free = NULL;
336 ht->count = orig->count;
337 ht->max = orig->max;
338 ht->seed = orig->seed;
339 ht->hash_func = orig->hash_func;
340 ht->array = (apr_hash_entry_t **)((char *)ht + sizeof(apr_hash_t));
341
342 new_vals = (apr_hash_entry_t *)((char *)(ht) + sizeof(apr_hash_t) +
343 sizeof(*ht->array) * (orig->max + 1));
344 j = 0;
345 for (i = 0; i <= ht->max; i++) {
348 while (orig_entry) {
349 *new_entry = &new_vals[j++];
350 (*new_entry)->hash = orig_entry->hash;
351 (*new_entry)->key = orig_entry->key;
352 (*new_entry)->klen = orig_entry->klen;
353 (*new_entry)->val = orig_entry->val;
354 new_entry = &((*new_entry)->next);
355 orig_entry = orig_entry->next;
356 }
357 *new_entry = NULL;
358 }
359 return ht;
360}
361
363 const void *key,
365{
367 he = *find_entry(ht, key, klen, NULL);
368 if (he)
369 return (void *)he->val;
370 else
371 return NULL;
372}
373
375 const void *key,
377 const void *val)
378{
380 hep = find_entry(ht, key, klen, val);
381 if (*hep) {
382 if (!val) {
383 /* delete entry */
385 *hep = (*hep)->next;
386 old->next = ht->free;
387 ht->free = old;
388 --ht->count;
389 }
390 else {
391 /* replace entry */
392 (*hep)->val = val;
393 /* check that the collision rate isn't too high */
394 if (ht->count > ht->max) {
396 }
397 }
398 }
399 /* else key not present and val==NULL */
400}
401
403{
404 return ht->count;
405}
406
408{
411 apr_hash_set(ht, hi->this->key, hi->this->klen, NULL);
412}
413
415 const apr_hash_t *overlay,
416 const apr_hash_t *base)
417{
418 return apr_hash_merge(p, overlay, base, NULL, NULL);
419}
420
422 const apr_hash_t *overlay,
423 const apr_hash_t *base,
424 void * (*merger)(apr_pool_t *p,
425 const void *key,
427 const void *h1_val,
428 const void *h2_val,
429 const void *data),
430 const void *data)
431{
436 unsigned int i, j, k, hash;
437
438#if APR_POOL_DEBUG
439 /* we don't copy keys and values, so it's necessary that
440 * overlay->a.pool and base->a.pool have a life span at least
441 * as long as p
442 */
445 "apr_hash_merge: overlay's pool is not an ancestor of p\n");
446 abort();
447 }
448 if (!apr_pool_is_ancestor(base->pool, p)) {
450 "apr_hash_merge: base's pool is not an ancestor of p\n");
451 abort();
452 }
453#endif
454
455 res = apr_palloc(p, sizeof(apr_hash_t));
456 res->pool = p;
457 res->free = NULL;
458 res->hash_func = base->hash_func;
459 res->count = base->count;
460 res->max = (overlay->max > base->max) ? overlay->max : base->max;
461 if (base->count + overlay->count > res->max) {
462 res->max = res->max * 2 + 1;
463 }
464 res->seed = base->seed;
465 res->array = alloc_array(res, res->max);
466 if (base->count + overlay->count) {
468 (base->count + overlay->count));
469 }
470 j = 0;
471 for (k = 0; k <= base->max; k++) {
472 for (iter = base->array[k]; iter; iter = iter->next) {
473 i = iter->hash & res->max;
474 new_vals[j].klen = iter->klen;
475 new_vals[j].key = iter->key;
476 new_vals[j].val = iter->val;
477 new_vals[j].hash = iter->hash;
478 new_vals[j].next = res->array[i];
479 res->array[i] = &new_vals[j];
480 j++;
481 }
482 }
483
484 for (k = 0; k <= overlay->max; k++) {
485 for (iter = overlay->array[k]; iter; iter = iter->next) {
486 if (res->hash_func)
487 hash = res->hash_func(iter->key, &iter->klen);
488 else
489 hash = hashfunc_default(iter->key, &iter->klen, res->seed);
490 i = hash & res->max;
491 for (ent = res->array[i]; ent; ent = ent->next) {
492 if ((ent->klen == iter->klen) &&
493 (memcmp(ent->key, iter->key, iter->klen) == 0)) {
494 if (merger) {
495 ent->val = (*merger)(p, iter->key, iter->klen,
496 iter->val, ent->val, data);
497 }
498 else {
499 ent->val = iter->val;
500 }
501 break;
502 }
503 }
504 if (!ent) {
505 new_vals[j].klen = iter->klen;
506 new_vals[j].key = iter->key;
507 new_vals[j].val = iter->val;
508 new_vals[j].hash = hash;
509 new_vals[j].next = res->array[i];
510 res->array[i] = &new_vals[j];
511 res->count++;
512 j++;
513 }
514 }
515 }
516 return res;
517}
518
519/* This is basically the following...
520 * for every element in hash table {
521 * comp elemeny.key, element.value
522 * }
523 *
524 * Like with apr_table_do, the comp callback is called for each and every
525 * element of the hash table.
526 */
528 void *rec, const apr_hash_t *ht)
529{
532 int rv, dorv = 1;
533
534 hix.ht = (apr_hash_t *)ht;
535 hix.index = 0;
536 hix.this = NULL;
537 hix.next = NULL;
538
539 if ((hi = apr_hash_next(&hix))) {
540 /* Scan the entire table */
541 do {
542 rv = (*comp)(rec, hi->this->key, hi->this->klen, hi->this->val);
543 } while (rv && (hi = apr_hash_next(hi)));
544
545 if (rv == 0) {
546 dorv = 0;
547 }
548 }
549 return dorv;
550}
551
APR Miscellaneous library routines.
static apr_hash_entry_t ** find_entry(apr_hash_t *ht, const void *key, apr_ssize_t klen, const void *val)
Definition apr_hash.c:285
static void expand_array(apr_hash_t *ht)
Definition apr_hash.c:193
static unsigned int hashfunc_default(const char *char_key, apr_ssize_t *klen, unsigned int hash)
Definition apr_hash.c:210
static apr_hash_entry_t ** alloc_array(apr_hash_t *ht, unsigned int max)
Definition apr_hash.c:91
#define INITIAL_MAX
Definition apr_hash.c:84
APR Hash Tables.
APR memory allocation.
#define hash(h, r, b, n)
Definition apr_random.c:51
APR Time Library.
ap_conf_vector_t * base
apr_pool_t apr_dbd_t apr_dbd_results_t ** res
Definition apr_dbd.h:287
apr_size_t size
apr_uint32_t val
Definition apr_atomic.h:66
const char int apr_pool_t * pool
Definition apr_cstr.h:84
const char * key
void * data
unsigned int(* apr_hashfunc_t)(const char *key, apr_ssize_t *klen)
Definition apr_hash.h:65
const apr_hash_t const apr_hash_t void *(* merger)(apr_pool_t *p, const void *key, apr_ssize_t klen, const void *h1_val, const void *h2_val, const void *data)
Definition apr_hash.h:234
apr_hash_t * ht
Definition apr_hash.h:148
apr_ssize_t * klen
Definition apr_hash.h:71
#define APR_HASH_KEY_STRING
Definition apr_hash.h:47
apr_hashfunc_t hash_func
Definition apr_hash.h:87
const apr_hash_t * overlay
Definition apr_hash.h:214
void * rec
Definition apr_hash.h:270
int() apr_hash_do_callback_fn_t(void *rec, const void *key, apr_ssize_t klen, const void *value)
Definition apr_hash.h:253
APR_DECLARE(void)
Definition apr_hash.c:155
APR_DECLARE_NONSTD(void) apr_terminate(void)
Definition start.c:173
#define APR_POOL_IMPLEMENT_ACCESSOR(type)
Definition apr_pools.h:91
#define apr_pcalloc(p, size)
Definition apr_pools.h:465
void apr_skiplistnode ** iter
void apr_skiplistnode apr_skiplist_compare comp
apr_size_t apr_size_t max
Definition apr_time.h:220
apr_int64_t apr_time_t
Definition apr_time.h:45
apr_pool_t * p
Definition md_event.c:32
return NULL
Definition mod_so.c:359
int i
Definition mod_so.c:347
Definition apr_hash.c:47
unsigned int hash
Definition apr_hash.c:49
apr_hash_entry_t * next
Definition apr_hash.c:48
const void * key
Definition apr_hash.c:50
apr_ssize_t klen
Definition apr_hash.c:51
const void * val
Definition apr_hash.c:52
apr_hash_entry_t * next
Definition apr_hash.c:64
unsigned int index
Definition apr_hash.c:65
apr_hash_t * ht
Definition apr_hash.c:63
apr_pool_t * pool
Definition apr_hash.c:76
apr_hashfunc_t hash_func
Definition apr_hash.c:80
apr_hash_entry_t ** array
Definition apr_hash.c:77
unsigned int seed
Definition apr_hash.c:79
apr_hash_index_t iterator
Definition apr_hash.c:78
unsigned int max
Definition apr_hash.c:79
apr_hash_entry_t * free
Definition apr_hash.c:81
unsigned int count
Definition apr_hash.c:79
apr_skiplistnode * next
struct ent * next
char key
static apr_time_t now
Definition testtime.c:33
typedef int(WSAAPI *apr_winapi_fpt_WSAPoll)(IN OUT LPWSAPOLLFD fdArray