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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "apr_private.h"
19 #include "apr_general.h"
20 #include "apr_pools.h"
33 * The internal form of a hash table.
35 * The table is an array indexed by the hash of the key; collisions
36 * are resolved by hanging a linked list of hash entries off each
37 * element of the array. Although this is a really simple design it
38 * isn't too bad given that pools have a low allocation overhead.
41 typedef struct apr_hash_entry_t apr_hash_entry_t;
43 struct apr_hash_entry_t {
44 apr_hash_entry_t *next;
52 * Data structure for iterating through a hash table.
54 * We keep a pointer to the next hash entry here to allow the current
55 * hash entry to be freed or otherwise mangled between calls to
58 struct apr_hash_index_t {
60 apr_hash_entry_t *this, *next;
65 * The size of the array is always a power of two. We use the maximum
66 * index rather than the size so that we can use bitwise-AND for
68 * The count of hash entries may be greater depending on the chosen
73 apr_hash_entry_t **array;
74 apr_hash_index_t iterator; /* For apr_hash_first(NULL, ...) */
75 unsigned int count, max;
76 apr_hash_entry_t *free; /* List of recycled entries */
79 #define INITIAL_MAX 15 /* tunable == 2^n - 1 */
83 * Hash creation functions.
86 static apr_hash_entry_t **alloc_array(apr_hash_t *ht, unsigned int max)
88 return apr_pcalloc(ht->pool, sizeof(*ht->array) * (max + 1));
91 APR_DECLARE(apr_hash_t *) apr_hash_make(apr_pool_t *pool)
94 ht = apr_palloc(pool, sizeof(apr_hash_t));
98 ht->max = INITIAL_MAX;
99 ht->array = alloc_array(ht, ht->max);
105 * Hash iteration functions.
108 APR_DECLARE(apr_hash_index_t *) apr_hash_next(apr_hash_index_t *hi)
112 if (hi->index > hi->ht->max)
115 hi->this = hi->ht->array[hi->index++];
117 hi->next = hi->this->next;
121 APR_DECLARE(apr_hash_index_t *) apr_hash_first(apr_pool_t *p, apr_hash_t *ht)
123 apr_hash_index_t *hi;
125 hi = apr_palloc(p, sizeof(*hi));
133 return apr_hash_next(hi);
136 APR_DECLARE(void) apr_hash_this(apr_hash_index_t *hi,
141 if (key) *key = hi->this->key;
142 if (klen) *klen = hi->this->klen;
143 if (val) *val = (void *)hi->this->val;
148 * Expanding a hash table
151 static void expand_array(apr_hash_t *ht)
153 apr_hash_index_t *hi;
154 apr_hash_entry_t **new_array;
155 unsigned int new_max;
157 new_max = ht->max * 2 + 1;
158 new_array = alloc_array(ht, new_max);
159 for (hi = apr_hash_first(NULL, ht); hi; hi = apr_hash_next(hi)) {
160 unsigned int i = hi->this->hash & new_max;
161 hi->this->next = new_array[i];
162 new_array[i] = hi->this;
164 ht->array = new_array;
169 * This is where we keep the details of the hash function and control
170 * the maximum collision rate.
172 * If val is non-NULL it creates and initializes a new hash entry if
173 * there isn't already one there; it returns an updatable pointer so
174 * that hash entries can be removed.
177 static apr_hash_entry_t **find_entry(apr_hash_t *ht,
182 apr_hash_entry_t **hep, *he;
183 const unsigned char *p;
188 * This is the popular `times 33' hash algorithm which is used by
189 * perl and also appears in Berkeley DB. This is one of the best
190 * known hash functions for strings because it is both computed
191 * very fast and distributes very well.
193 * The originator may be Dan Bernstein but the code in Berkeley DB
194 * cites Chris Torek as the source. The best citation I have found
195 * is "Chris Torek, Hash function for text in C, Usenet message
196 * <27038@mimsy.umd.edu> in comp.lang.c , October, 1990." in Rich
197 * Salz's USENIX 1992 paper about INN which can be found at
198 * <http://citeseer.nj.nec.com/salz92internetnews.html>.
200 * The magic of number 33, i.e. why it works better than many other
201 * constants, prime or not, has never been adequately explained by
202 * anyone. So I try an explanation: if one experimentally tests all
203 * multipliers between 1 and 256 (as I did while writing a low-level
204 * data structure library some time ago) one detects that even
205 * numbers are not useable at all. The remaining 128 odd numbers
206 * (except for the number 1) work more or less all equally well.
207 * They all distribute in an acceptable way and this way fill a hash
208 * table with an average percent of approx. 86%.
210 * If one compares the chi^2 values of the variants (see
211 * Bob Jenkins ``Hashing Frequently Asked Questions'' at
212 * http://burtleburtle.net/bob/hash/hashfaq.html for a description
213 * of chi^2), the number 33 not even has the best value. But the
214 * number 33 and a few other equally good numbers like 17, 31, 63,
215 * 127 and 129 have nevertheless a great advantage to the remaining
216 * numbers in the large set of possible multipliers: their multiply
217 * operation can be replaced by a faster operation based on just one
218 * shift plus either a single addition or subtraction operation. And
219 * because a hash function has to both distribute good _and_ has to
220 * be very fast to compute, those few numbers should be preferred.
222 * -- Ralf S. Engelschall <rse@engelschall.com>
225 if (klen == APR_HASH_KEY_STRING) {
226 for (p = key; *p; p++) {
227 hash = hash * 33 + *p;
229 klen = p - (const unsigned char *)key;
232 for (p = key, i = klen; i; i--, p++) {
233 hash = hash * 33 + *p;
237 /* scan linked list */
238 for (hep = &ht->array[hash & ht->max], he = *hep;
239 he; hep = &he->next, he = *hep) {
242 && memcmp(he->key, key, klen) == 0)
248 /* add a new entry for non-NULL values */
249 if ((he = ht->free) != NULL)
252 he = apr_palloc(ht->pool, sizeof(*he));
263 APR_DECLARE(apr_hash_t *) apr_hash_copy(apr_pool_t *pool,
264 const apr_hash_t *orig)
267 apr_hash_entry_t *new_vals;
270 ht = apr_palloc(pool, sizeof(apr_hash_t) +
271 sizeof(*ht->array) * (orig->max + 1) +
272 sizeof(apr_hash_entry_t) * orig->count);
275 ht->count = orig->count;
277 ht->array = (apr_hash_entry_t **)((char *)ht + sizeof(apr_hash_t));
279 new_vals = (apr_hash_entry_t *)((char *)(ht) + sizeof(apr_hash_t) +
280 sizeof(*ht->array) * (orig->max + 1));
282 for (i = 0; i <= ht->max; i++) {
283 apr_hash_entry_t **new_entry = &(ht->array[i]);
284 apr_hash_entry_t *orig_entry = orig->array[i];
286 *new_entry = &new_vals[j++];
287 (*new_entry)->hash = orig_entry->hash;
288 (*new_entry)->key = orig_entry->key;
289 (*new_entry)->klen = orig_entry->klen;
290 (*new_entry)->val = orig_entry->val;
291 new_entry = &((*new_entry)->next);
292 orig_entry = orig_entry->next;
299 APR_DECLARE(void *) apr_hash_get(apr_hash_t *ht,
303 apr_hash_entry_t *he;
304 he = *find_entry(ht, key, klen, NULL);
306 return (void *)he->val;
311 APR_DECLARE(void) apr_hash_set(apr_hash_t *ht,
316 apr_hash_entry_t **hep;
317 hep = find_entry(ht, key, klen, val);
321 apr_hash_entry_t *old = *hep;
323 old->next = ht->free;
330 /* check that the collision rate isn't too high */
331 if (ht->count > ht->max) {
336 /* else key not present and val==NULL */
339 APR_DECLARE(unsigned int) apr_hash_count(apr_hash_t *ht)
344 APR_DECLARE(apr_hash_t*) apr_hash_overlay(apr_pool_t *p,
345 const apr_hash_t *overlay,
346 const apr_hash_t *base)
348 return apr_hash_merge(p, overlay, base, NULL, NULL);
351 APR_DECLARE(apr_hash_t *) apr_hash_merge(apr_pool_t *p,
352 const apr_hash_t *overlay,
353 const apr_hash_t *base,
354 void * (*merger)(apr_pool_t *p,
363 apr_hash_entry_t *new_vals = NULL;
364 apr_hash_entry_t *iter;
365 apr_hash_entry_t *ent;
369 /* we don't copy keys and values, so it's necessary that
370 * overlay->a.pool and base->a.pool have a life span at least
373 if (!apr_pool_is_ancestor(overlay->pool, p)) {
375 "apr_hash_overlay: overlay's pool is not an ancestor of p\n");
378 if (!apr_pool_is_ancestor(base->pool, p)) {
380 "apr_hash_overlay: base's pool is not an ancestor of p\n");
385 res = apr_palloc(p, sizeof(apr_hash_t));
388 res->count = base->count;
389 res->max = (overlay->max > base->max) ? overlay->max : base->max;
390 if (base->count + overlay->count > res->max) {
391 res->max = res->max * 2 + 1;
393 res->array = alloc_array(res, res->max);
394 if (base->count + overlay->count) {
395 new_vals = apr_palloc(p, sizeof(apr_hash_entry_t) *
396 (base->count + overlay->count));
399 for (k = 0; k <= base->max; k++) {
400 for (iter = base->array[k]; iter; iter = iter->next) {
401 i = iter->hash & res->max;
402 new_vals[j].klen = iter->klen;
403 new_vals[j].key = iter->key;
404 new_vals[j].val = iter->val;
405 new_vals[j].hash = iter->hash;
406 new_vals[j].next = res->array[i];
407 res->array[i] = &new_vals[j];
412 for (k = 0; k <= overlay->max; k++) {
413 for (iter = overlay->array[k]; iter; iter = iter->next) {
414 i = iter->hash & res->max;
415 for (ent = res->array[i]; ent; ent = ent->next) {
416 if ((ent->klen == iter->klen) &&
417 (memcmp(ent->key, iter->key, iter->klen) == 0)) {
419 ent->val = (*merger)(p, iter->key, iter->klen,
420 iter->val, ent->val, data);
423 ent->val = iter->val;
429 new_vals[j].klen = iter->klen;
430 new_vals[j].key = iter->key;
431 new_vals[j].val = iter->val;
432 new_vals[j].hash = iter->hash;
433 new_vals[j].next = res->array[i];
434 res->array[i] = &new_vals[j];
443 APR_POOL_IMPLEMENT_ACCESSOR(hash)