2
0

fspr_hash.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. #include "fspr_private.h"
  17. #include "fspr_general.h"
  18. #include "fspr_pools.h"
  19. #include "fspr_hash.h"
  20. #if APR_HAVE_STDLIB_H
  21. #include <stdlib.h>
  22. #endif
  23. #if APR_HAVE_STRING_H
  24. #include <string.h>
  25. #endif
  26. #include <assert.h> /* assert() */
  27. #if APR_POOL_DEBUG && APR_HAVE_STDIO_H
  28. #include <stdio.h>
  29. #endif
  30. /*
  31. * The internal form of a hash table.
  32. *
  33. * The table is an array indexed by the hash of the key; collisions
  34. * are resolved by hanging a linked list of hash entries off each
  35. * element of the array. Although this is a really simple design it
  36. * isn't too bad given that pools have a low allocation overhead.
  37. */
  38. typedef struct fspr_hash_entry_t fspr_hash_entry_t;
  39. struct fspr_hash_entry_t {
  40. fspr_hash_entry_t *next;
  41. unsigned int hash;
  42. const void *key;
  43. fspr_ssize_t klen;
  44. const void *val;
  45. };
  46. /*
  47. * Data structure for iterating through a hash table.
  48. *
  49. * We keep a pointer to the next hash entry here to allow the current
  50. * hash entry to be freed or otherwise mangled between calls to
  51. * fspr_hash_next().
  52. */
  53. struct fspr_hash_index_t {
  54. fspr_hash_t *ht;
  55. fspr_hash_entry_t *this, *next;
  56. unsigned int index;
  57. };
  58. /*
  59. * The size of the array is always a power of two. We use the maximum
  60. * index rather than the size so that we can use bitwise-AND for
  61. * modular arithmetic.
  62. * The count of hash entries may be greater depending on the chosen
  63. * collision rate.
  64. */
  65. struct fspr_hash_t {
  66. fspr_pool_t *pool;
  67. fspr_hash_entry_t **array;
  68. fspr_hash_index_t iterator; /* For fspr_hash_first(NULL, ...) */
  69. unsigned int count, max;
  70. fspr_hashfunc_t hash_func;
  71. fspr_hash_entry_t *free; /* List of recycled entries */
  72. };
  73. #define INITIAL_MAX 15 /* tunable == 2^n - 1 */
  74. /*
  75. * Hash creation functions.
  76. */
  77. static fspr_hash_entry_t **alloc_array(fspr_hash_t *ht, unsigned int max)
  78. {
  79. return fspr_pcalloc(ht->pool, sizeof(*ht->array) * (max + 1));
  80. }
  81. APR_DECLARE(fspr_hash_t *) fspr_hash_make(fspr_pool_t *pool)
  82. {
  83. fspr_hash_t *ht;
  84. ht = fspr_palloc(pool, sizeof(fspr_hash_t));
  85. ht->pool = pool;
  86. ht->free = NULL;
  87. ht->count = 0;
  88. ht->max = INITIAL_MAX;
  89. ht->array = alloc_array(ht, ht->max);
  90. ht->hash_func = fspr_hashfunc_default;
  91. return ht;
  92. }
  93. APR_DECLARE(fspr_hash_t *) fspr_hash_make_custom(fspr_pool_t *pool,
  94. fspr_hashfunc_t hash_func)
  95. {
  96. fspr_hash_t *ht = fspr_hash_make(pool);
  97. ht->hash_func = hash_func;
  98. return ht;
  99. }
  100. /*
  101. * Hash iteration functions.
  102. */
  103. APR_DECLARE(fspr_hash_index_t *) fspr_hash_next(fspr_hash_index_t *hi)
  104. {
  105. hi->this = hi->next;
  106. while (!hi->this) {
  107. if (hi->index > hi->ht->max)
  108. return NULL;
  109. hi->this = hi->ht->array[hi->index++];
  110. }
  111. hi->next = hi->this->next;
  112. return hi;
  113. }
  114. APR_DECLARE(fspr_hash_index_t *) fspr_hash_first(fspr_pool_t *p, fspr_hash_t *ht)
  115. {
  116. fspr_hash_index_t *hi;
  117. if (p)
  118. hi = fspr_palloc(p, sizeof(*hi));
  119. else
  120. hi = &ht->iterator;
  121. hi->ht = ht;
  122. hi->index = 0;
  123. hi->this = NULL;
  124. hi->next = NULL;
  125. return fspr_hash_next(hi);
  126. }
  127. APR_DECLARE(void) fspr_hash_this(fspr_hash_index_t *hi,
  128. const void **key,
  129. fspr_ssize_t *klen,
  130. void **val)
  131. {
  132. if (key) *key = hi->this->key;
  133. if (klen) *klen = hi->this->klen;
  134. if (val) *val = (void *)hi->this->val;
  135. }
  136. /*
  137. * Expanding a hash table
  138. */
  139. static void expand_array(fspr_hash_t *ht)
  140. {
  141. fspr_hash_index_t *hi;
  142. fspr_hash_entry_t **new_array;
  143. unsigned int new_max;
  144. new_max = ht->max * 2 + 1;
  145. new_array = alloc_array(ht, new_max);
  146. for (hi = fspr_hash_first(NULL, ht); hi; hi = fspr_hash_next(hi)) {
  147. unsigned int i = hi->this->hash & new_max;
  148. hi->this->next = new_array[i];
  149. new_array[i] = hi->this;
  150. }
  151. ht->array = new_array;
  152. ht->max = new_max;
  153. }
  154. APR_DECLARE_NONSTD(unsigned int) fspr_hashfunc_default(const char *char_key,
  155. fspr_ssize_t *klen)
  156. {
  157. unsigned int hash = 0;
  158. const unsigned char *key = (const unsigned char *)char_key;
  159. const unsigned char *p;
  160. fspr_ssize_t i;
  161. /*
  162. * This is the popular `times 33' hash algorithm which is used by
  163. * perl and also appears in Berkeley DB. This is one of the best
  164. * known hash functions for strings because it is both computed
  165. * very fast and distributes very well.
  166. *
  167. * The originator may be Dan Bernstein but the code in Berkeley DB
  168. * cites Chris Torek as the source. The best citation I have found
  169. * is "Chris Torek, Hash function for text in C, Usenet message
  170. * <27038@mimsy.umd.edu> in comp.lang.c , October, 1990." in Rich
  171. * Salz's USENIX 1992 paper about INN which can be found at
  172. * <http://citeseer.nj.nec.com/salz92internetnews.html>.
  173. *
  174. * The magic of number 33, i.e. why it works better than many other
  175. * constants, prime or not, has never been adequately explained by
  176. * anyone. So I try an explanation: if one experimentally tests all
  177. * multipliers between 1 and 256 (as I did while writing a low-level
  178. * data structure library some time ago) one detects that even
  179. * numbers are not useable at all. The remaining 128 odd numbers
  180. * (except for the number 1) work more or less all equally well.
  181. * They all distribute in an acceptable way and this way fill a hash
  182. * table with an average percent of approx. 86%.
  183. *
  184. * If one compares the chi^2 values of the variants (see
  185. * Bob Jenkins ``Hashing Frequently Asked Questions'' at
  186. * http://burtleburtle.net/bob/hash/hashfaq.html for a description
  187. * of chi^2), the number 33 not even has the best value. But the
  188. * number 33 and a few other equally good numbers like 17, 31, 63,
  189. * 127 and 129 have nevertheless a great advantage to the remaining
  190. * numbers in the large set of possible multipliers: their multiply
  191. * operation can be replaced by a faster operation based on just one
  192. * shift plus either a single addition or subtraction operation. And
  193. * because a hash function has to both distribute good _and_ has to
  194. * be very fast to compute, those few numbers should be preferred.
  195. *
  196. * -- Ralf S. Engelschall <rse@engelschall.com>
  197. */
  198. if (*klen == APR_HASH_KEY_STRING) {
  199. for (p = key; *p; p++) {
  200. hash = hash * 33 + *p;
  201. }
  202. *klen = p - key;
  203. }
  204. else {
  205. for (p = key, i = *klen; i; i--, p++) {
  206. hash = hash * 33 + *p;
  207. }
  208. }
  209. return hash;
  210. }
  211. /*
  212. * This is where we keep the details of the hash function and control
  213. * the maximum collision rate.
  214. *
  215. * If val is non-NULL it creates and initializes a new hash entry if
  216. * there isn't already one there; it returns an updatable pointer so
  217. * that hash entries can be removed.
  218. */
  219. static fspr_hash_entry_t **find_entry(fspr_hash_t *ht,
  220. const void *key,
  221. fspr_ssize_t klen,
  222. const void *val)
  223. {
  224. fspr_hash_entry_t **hep, *he;
  225. unsigned int hash;
  226. hash = ht->hash_func(key, &klen);
  227. /* scan linked list */
  228. for (hep = &ht->array[hash & ht->max], he = *hep;
  229. he; hep = &he->next, he = *hep) {
  230. if (he->hash == hash
  231. && he->klen == klen
  232. && memcmp(he->key, key, klen) == 0)
  233. break;
  234. }
  235. if (he || !val)
  236. return hep;
  237. /* add a new entry for non-NULL values */
  238. if ((he = ht->free) != NULL)
  239. ht->free = he->next;
  240. else
  241. he = fspr_palloc(ht->pool, sizeof(*he));
  242. he->next = NULL;
  243. he->hash = hash;
  244. he->key = key;
  245. he->klen = klen;
  246. he->val = val;
  247. *hep = he;
  248. ht->count++;
  249. return hep;
  250. }
  251. APR_DECLARE(fspr_hash_t *) fspr_hash_copy(fspr_pool_t *pool,
  252. const fspr_hash_t *orig)
  253. {
  254. fspr_hash_t *ht;
  255. fspr_hash_entry_t *new_vals;
  256. unsigned int i, j;
  257. ht = fspr_palloc(pool, sizeof(fspr_hash_t) +
  258. sizeof(*ht->array) * (orig->max + 1) +
  259. sizeof(fspr_hash_entry_t) * orig->count);
  260. ht->pool = pool;
  261. ht->free = NULL;
  262. ht->count = orig->count;
  263. ht->max = orig->max;
  264. ht->hash_func = orig->hash_func;
  265. ht->array = (fspr_hash_entry_t **)((char *)ht + sizeof(fspr_hash_t));
  266. new_vals = (fspr_hash_entry_t *)((char *)(ht) + sizeof(fspr_hash_t) +
  267. sizeof(*ht->array) * (orig->max + 1));
  268. j = 0;
  269. for (i = 0; i <= ht->max; i++) {
  270. fspr_hash_entry_t **new_entry = &(ht->array[i]);
  271. fspr_hash_entry_t *orig_entry = orig->array[i];
  272. while (orig_entry) {
  273. *new_entry = &new_vals[j++];
  274. (*new_entry)->hash = orig_entry->hash;
  275. (*new_entry)->key = orig_entry->key;
  276. (*new_entry)->klen = orig_entry->klen;
  277. (*new_entry)->val = orig_entry->val;
  278. new_entry = &((*new_entry)->next);
  279. orig_entry = orig_entry->next;
  280. }
  281. *new_entry = NULL;
  282. }
  283. return ht;
  284. }
  285. APR_DECLARE(void *) fspr_hash_get(fspr_hash_t *ht,
  286. const void *key,
  287. fspr_ssize_t klen)
  288. {
  289. fspr_hash_entry_t *he;
  290. he = *find_entry(ht, key, klen, NULL);
  291. if (he)
  292. return (void *)he->val;
  293. else
  294. return NULL;
  295. }
  296. APR_DECLARE(void) fspr_hash_set(fspr_hash_t *ht,
  297. const void *key,
  298. fspr_ssize_t klen,
  299. const void *val)
  300. {
  301. fspr_hash_entry_t **hep;
  302. hep = find_entry(ht, key, klen, val);
  303. if (*hep) {
  304. if (!val) {
  305. /* delete entry */
  306. fspr_hash_entry_t *old = *hep;
  307. *hep = (*hep)->next;
  308. old->next = ht->free;
  309. ht->free = old;
  310. --ht->count;
  311. }
  312. else {
  313. /* replace entry */
  314. (*hep)->val = val;
  315. /* check that the collision rate isn't too high */
  316. if (ht->count > ht->max) {
  317. expand_array(ht);
  318. }
  319. }
  320. }
  321. /* else key not present and val==NULL */
  322. }
  323. APR_DECLARE(unsigned int) fspr_hash_count(fspr_hash_t *ht)
  324. {
  325. return ht->count;
  326. }
  327. APR_DECLARE(void) fspr_hash_clear(fspr_hash_t *ht)
  328. {
  329. fspr_hash_index_t *hi;
  330. for (hi = fspr_hash_first(NULL, ht); hi; hi = fspr_hash_next(hi))
  331. fspr_hash_set(ht, hi->this->key, hi->this->klen, NULL);
  332. }
  333. APR_DECLARE(fspr_hash_t*) fspr_hash_overlay(fspr_pool_t *p,
  334. const fspr_hash_t *overlay,
  335. const fspr_hash_t *base)
  336. {
  337. return fspr_hash_merge(p, overlay, base, NULL, NULL);
  338. }
  339. APR_DECLARE(fspr_hash_t *) fspr_hash_merge(fspr_pool_t *p,
  340. const fspr_hash_t *overlay,
  341. const fspr_hash_t *base,
  342. void * (*merger)(fspr_pool_t *p,
  343. const void *key,
  344. fspr_ssize_t klen,
  345. const void *h1_val,
  346. const void *h2_val,
  347. const void *data),
  348. const void *data)
  349. {
  350. fspr_hash_t *res;
  351. fspr_hash_entry_t *new_vals = NULL;
  352. fspr_hash_entry_t *iter;
  353. fspr_hash_entry_t *ent;
  354. unsigned int i,j,k;
  355. #if APR_POOL_DEBUG
  356. /* we don't copy keys and values, so it's necessary that
  357. * overlay->a.pool and base->a.pool have a life span at least
  358. * as long as p
  359. */
  360. if (!fspr_pool_is_ancestor(overlay->pool, p)) {
  361. fprintf(stderr,
  362. "fspr_hash_merge: overlay's pool is not an ancestor of p\n");
  363. abort();
  364. }
  365. if (!fspr_pool_is_ancestor(base->pool, p)) {
  366. fprintf(stderr,
  367. "fspr_hash_merge: base's pool is not an ancestor of p\n");
  368. abort();
  369. }
  370. #endif
  371. res = fspr_palloc(p, sizeof(fspr_hash_t));
  372. res->pool = p;
  373. res->free = NULL;
  374. res->hash_func = base->hash_func;
  375. res->count = base->count;
  376. res->max = (overlay->max > base->max) ? overlay->max : base->max;
  377. if (base->count + overlay->count > res->max) {
  378. res->max = res->max * 2 + 1;
  379. }
  380. res->array = alloc_array(res, res->max);
  381. if (base->count + overlay->count) {
  382. new_vals = fspr_palloc(p, sizeof(fspr_hash_entry_t) *
  383. (base->count + overlay->count));
  384. }
  385. j = 0;
  386. for (k = 0; k <= base->max; k++) {
  387. for (iter = base->array[k]; iter; iter = iter->next) {
  388. i = iter->hash & res->max;
  389. assert(new_vals);
  390. new_vals[j].klen = iter->klen;
  391. new_vals[j].key = iter->key;
  392. new_vals[j].val = iter->val;
  393. new_vals[j].hash = iter->hash;
  394. new_vals[j].next = res->array[i];
  395. res->array[i] = &new_vals[j];
  396. j++;
  397. }
  398. }
  399. for (k = 0; k <= overlay->max; k++) {
  400. for (iter = overlay->array[k]; iter; iter = iter->next) {
  401. i = iter->hash & res->max;
  402. for (ent = res->array[i]; ent; ent = ent->next) {
  403. if ((ent->klen == iter->klen) &&
  404. (memcmp(ent->key, iter->key, iter->klen) == 0)) {
  405. if (merger) {
  406. ent->val = (*merger)(p, iter->key, iter->klen,
  407. iter->val, ent->val, data);
  408. }
  409. else {
  410. ent->val = iter->val;
  411. }
  412. break;
  413. }
  414. }
  415. if (new_vals && !ent) {
  416. new_vals[j].klen = iter->klen;
  417. new_vals[j].key = iter->key;
  418. new_vals[j].val = iter->val;
  419. new_vals[j].hash = iter->hash;
  420. new_vals[j].next = res->array[i];
  421. res->array[i] = &new_vals[j];
  422. res->count++;
  423. j++;
  424. }
  425. }
  426. }
  427. return res;
  428. }
  429. APR_POOL_IMPLEMENT_ACCESSOR(hash)