container_multihashmap32.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2008 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file util/container_multihashmap32.c
  18. * @brief a version of hash map implemented in container_multihashmap.c but with
  19. * uint32_t as keys
  20. * @author Christian Grothoff
  21. * @author Sree Harsha Totakura
  22. */
  23. #include "platform.h"
  24. #include "gnunet_container_lib.h"
  25. #define LOG(kind, ...) \
  26. GNUNET_log_from (kind, "util-container-multihashmap32", __VA_ARGS__)
  27. /**
  28. * Maximum recursion depth for callbacks of
  29. * #GNUNET_CONTAINER_multihashmap_get_multiple() themselves
  30. * again calling #GNUNET_CONTAINER_multihashmap_get_multiple().
  31. * Should be totally excessive, but if violated we die.
  32. */
  33. #define NEXT_CACHE_SIZE 16
  34. /**
  35. * An entry in the hash map.
  36. */
  37. struct MapEntry
  38. {
  39. /**
  40. * Key for the entry.
  41. */
  42. uint32_t key;
  43. /**
  44. * Value of the entry.
  45. */
  46. void *value;
  47. /**
  48. * If there is a hash collision, we create a linked list.
  49. */
  50. struct MapEntry *next;
  51. };
  52. /**
  53. * Internal representation of the hash map.
  54. */
  55. struct GNUNET_CONTAINER_MultiHashMap32
  56. {
  57. /**
  58. * All of our buckets.
  59. */
  60. struct MapEntry **map;
  61. /**
  62. * Number of entries in the map.
  63. */
  64. unsigned int size;
  65. /**
  66. * Length of the @e map array.
  67. */
  68. unsigned int map_length;
  69. /**
  70. * Counts the destructive modifications (grow, remove)
  71. * to the map, so that iterators can check if they are still valid.
  72. */
  73. unsigned int modification_counter;
  74. /**
  75. * Map entries indicating iteration positions currently
  76. * in use by #GNUNET_CONTAINER_multihashmap_get_multiple().
  77. * Only used up to @e next_cache_off.
  78. */
  79. struct MapEntry *next_cache[NEXT_CACHE_SIZE];
  80. /**
  81. * Offset of @e next_cache entries in use, must be smaller
  82. * than #NEXT_CACHE_SIZE.
  83. */
  84. unsigned int next_cache_off;
  85. };
  86. /**
  87. * Cursor into a multihashmap.
  88. * Allows to enumerate elements asynchronously.
  89. */
  90. struct GNUNET_CONTAINER_MultiHashMap32Iterator
  91. {
  92. /**
  93. * Position in the bucket @e idx
  94. */
  95. struct MapEntry *me;
  96. /**
  97. * Current bucket index.
  98. */
  99. unsigned int idx;
  100. /**
  101. * Modification counter as observed on the map when the iterator
  102. * was created.
  103. */
  104. unsigned int modification_counter;
  105. /**
  106. * Map that we are iterating over.
  107. */
  108. const struct GNUNET_CONTAINER_MultiHashMap32 *map;
  109. };
  110. /**
  111. * Create a multi hash map.
  112. *
  113. * @param len initial size (map will grow as needed)
  114. * @return NULL on error
  115. */
  116. struct GNUNET_CONTAINER_MultiHashMap32 *
  117. GNUNET_CONTAINER_multihashmap32_create (unsigned int len)
  118. {
  119. struct GNUNET_CONTAINER_MultiHashMap32 *ret;
  120. GNUNET_assert (len > 0);
  121. ret = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMap32);
  122. ret->map = GNUNET_malloc_large (len * sizeof(struct MapEntry *));
  123. if (NULL == ret->map)
  124. {
  125. GNUNET_free (ret);
  126. return NULL;
  127. }
  128. ret->map_length = len;
  129. return ret;
  130. }
  131. /**
  132. * Destroy a hash map. Will not free any values
  133. * stored in the hash map!
  134. *
  135. * @param map the map
  136. */
  137. void
  138. GNUNET_CONTAINER_multihashmap32_destroy (
  139. struct GNUNET_CONTAINER_MultiHashMap32 *map)
  140. {
  141. struct MapEntry *e;
  142. for (unsigned int i = 0; i < map->map_length; i++)
  143. {
  144. while (NULL != (e = map->map[i]))
  145. {
  146. map->map[i] = e->next;
  147. GNUNET_free (e);
  148. }
  149. }
  150. GNUNET_free (map->map);
  151. GNUNET_free (map);
  152. }
  153. /**
  154. * Compute the index of the bucket for the given key.
  155. *
  156. * @param m hash map for which to compute the index
  157. * @param key what key should the index be computed for
  158. * @return offset into the "map" array of "m"
  159. */
  160. static unsigned int
  161. idx_of (const struct GNUNET_CONTAINER_MultiHashMap32 *m, const uint32_t key)
  162. {
  163. GNUNET_assert (NULL != m);
  164. return ((unsigned int) key) % m->map_length;
  165. }
  166. /**
  167. * Get the number of key-value pairs in the map.
  168. *
  169. * @param map the map
  170. * @return the number of key value pairs
  171. */
  172. unsigned int
  173. GNUNET_CONTAINER_multihashmap32_size (
  174. const struct GNUNET_CONTAINER_MultiHashMap32 *map)
  175. {
  176. return map->size;
  177. }
  178. /**
  179. * Given a key find a value in the map matching the key.
  180. *
  181. * @param map the map
  182. * @param key what to look for
  183. * @return NULL if no value was found; note that
  184. * this is indistinguishable from values that just
  185. * happen to be NULL; use "contains" to test for
  186. * key-value pairs with value NULL
  187. */
  188. void *
  189. GNUNET_CONTAINER_multihashmap32_get (
  190. const struct GNUNET_CONTAINER_MultiHashMap32 *map,
  191. uint32_t key)
  192. {
  193. struct MapEntry *e;
  194. e = map->map[idx_of (map, key)];
  195. while (NULL != e)
  196. {
  197. if (key == e->key)
  198. return e->value;
  199. e = e->next;
  200. }
  201. return NULL;
  202. }
  203. /**
  204. * Iterate over all entries in the map.
  205. *
  206. * @param map the map
  207. * @param it function to call on each entry
  208. * @param it_cls extra argument to @a it
  209. * @return the number of key value pairs processed,
  210. * #GNUNET_SYSERR if it aborted iteration
  211. */
  212. int
  213. GNUNET_CONTAINER_multihashmap32_iterate (
  214. struct GNUNET_CONTAINER_MultiHashMap32 *map,
  215. GNUNET_CONTAINER_MulitHashMapIterator32Callback it,
  216. void *it_cls)
  217. {
  218. int count;
  219. struct MapEntry **ce;
  220. count = 0;
  221. GNUNET_assert (NULL != map);
  222. ce = &map->next_cache[map->next_cache_off];
  223. GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
  224. for (unsigned int i = 0; i < map->map_length; i++)
  225. {
  226. struct MapEntry *e;
  227. *ce = map->map[i];
  228. while (NULL != (e = *ce))
  229. {
  230. *ce = e->next;
  231. if (NULL != it)
  232. {
  233. if (GNUNET_OK != it (it_cls, e->key, e->value))
  234. {
  235. GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
  236. return GNUNET_SYSERR;
  237. }
  238. }
  239. count++;
  240. }
  241. }
  242. GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
  243. return count;
  244. }
  245. /**
  246. * We are about to free() the @a bme, make sure it is not in
  247. * the list of next values for any iterator in the @a map's next_cache.
  248. *
  249. * @param map the map to check
  250. * @param bme the entry that is about to be free'd
  251. */
  252. static void
  253. update_next_cache (struct GNUNET_CONTAINER_MultiHashMap32 *map,
  254. const struct MapEntry *me)
  255. {
  256. for (unsigned int i = 0; i < map->next_cache_off; i++)
  257. if (map->next_cache[i] == me)
  258. map->next_cache[i] = me->next;
  259. }
  260. /**
  261. * Remove the given key-value pair from the map. Note that if the
  262. * key-value pair is in the map multiple times, only one of the pairs
  263. * will be removed.
  264. *
  265. * @param map the map
  266. * @param key key of the key-value pair
  267. * @param value value of the key-value pair
  268. * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
  269. * is not in the map
  270. */
  271. int
  272. GNUNET_CONTAINER_multihashmap32_remove (
  273. struct GNUNET_CONTAINER_MultiHashMap32 *map,
  274. uint32_t key,
  275. const void *value)
  276. {
  277. struct MapEntry *e;
  278. struct MapEntry *p;
  279. unsigned int i;
  280. map->modification_counter++;
  281. i = idx_of (map, key);
  282. p = NULL;
  283. e = map->map[i];
  284. while (e != NULL)
  285. {
  286. if ((key == e->key) && (value == e->value))
  287. {
  288. if (p == NULL)
  289. map->map[i] = e->next;
  290. else
  291. p->next = e->next;
  292. update_next_cache (map, e);
  293. GNUNET_free (e);
  294. map->size--;
  295. return GNUNET_YES;
  296. }
  297. p = e;
  298. e = e->next;
  299. }
  300. return GNUNET_NO;
  301. }
  302. /**
  303. * Remove all entries for the given key from the map.
  304. * Note that the values would not be "freed".
  305. *
  306. * @param map the map
  307. * @param key identifies values to be removed
  308. * @return number of values removed
  309. */
  310. int
  311. GNUNET_CONTAINER_multihashmap32_remove_all (
  312. struct GNUNET_CONTAINER_MultiHashMap32 *map,
  313. uint32_t key)
  314. {
  315. struct MapEntry *e;
  316. struct MapEntry *p;
  317. unsigned int i;
  318. int ret;
  319. map->modification_counter++;
  320. ret = 0;
  321. i = idx_of (map, key);
  322. p = NULL;
  323. e = map->map[i];
  324. while (e != NULL)
  325. {
  326. if (key == e->key)
  327. {
  328. if (p == NULL)
  329. map->map[i] = e->next;
  330. else
  331. p->next = e->next;
  332. update_next_cache (map, e);
  333. GNUNET_free (e);
  334. map->size--;
  335. if (p == NULL)
  336. e = map->map[i];
  337. else
  338. e = p->next;
  339. ret++;
  340. }
  341. else
  342. {
  343. p = e;
  344. e = e->next;
  345. }
  346. }
  347. return ret;
  348. }
  349. /**
  350. * Check if the map contains any value under the given
  351. * key (including values that are NULL).
  352. *
  353. * @param map the map
  354. * @param key the key to test if a value exists for it
  355. * @return #GNUNET_YES if such a value exists,
  356. * #GNUNET_NO if not
  357. */
  358. int
  359. GNUNET_CONTAINER_multihashmap32_contains (
  360. const struct GNUNET_CONTAINER_MultiHashMap32 *map,
  361. uint32_t key)
  362. {
  363. struct MapEntry *e;
  364. e = map->map[idx_of (map, key)];
  365. while (e != NULL)
  366. {
  367. if (key == e->key)
  368. return GNUNET_YES;
  369. e = e->next;
  370. }
  371. return GNUNET_NO;
  372. }
  373. /**
  374. * Check if the map contains the given value under the given
  375. * key.
  376. *
  377. * @param map the map
  378. * @param key the key to test if a value exists for it
  379. * @param value value to test for
  380. * @return #GNUNET_YES if such a value exists,
  381. * #GNUNET_NO if not
  382. */
  383. int
  384. GNUNET_CONTAINER_multihashmap32_contains_value (
  385. const struct GNUNET_CONTAINER_MultiHashMap32 *map,
  386. uint32_t key,
  387. const void *value)
  388. {
  389. struct MapEntry *e;
  390. e = map->map[idx_of (map, key)];
  391. while (e != NULL)
  392. {
  393. if ((key == e->key) && (e->value == value))
  394. return GNUNET_YES;
  395. e = e->next;
  396. }
  397. return GNUNET_NO;
  398. }
  399. /**
  400. * Grow the given map to a more appropriate size.
  401. *
  402. * @param map the hash map to grow
  403. */
  404. static void
  405. grow (struct GNUNET_CONTAINER_MultiHashMap32 *map)
  406. {
  407. struct MapEntry **old_map;
  408. struct MapEntry **new_map;
  409. struct MapEntry *e;
  410. unsigned int old_len;
  411. unsigned int new_len;
  412. unsigned int idx;
  413. old_map = map->map;
  414. old_len = map->map_length;
  415. new_len = old_len * 2;
  416. if (0 == new_len) /* 2^31 * 2 == 0 */
  417. new_len = old_len; /* never use 0 */
  418. if (new_len == old_len)
  419. return; /* nothing changed */
  420. new_map = GNUNET_malloc_large (new_len * sizeof(struct MapEntry *));
  421. if (NULL == new_map)
  422. return; /* grow not possible */
  423. map->modification_counter++;
  424. map->map_length = new_len;
  425. map->map = new_map;
  426. for (unsigned int i = 0; i < old_len; i++)
  427. {
  428. while (NULL != (e = old_map[i]))
  429. {
  430. old_map[i] = e->next;
  431. idx = idx_of (map, e->key);
  432. e->next = new_map[idx];
  433. new_map[idx] = e;
  434. }
  435. }
  436. GNUNET_free (old_map);
  437. }
  438. /**
  439. * Store a key-value pair in the map.
  440. *
  441. * @param map the map
  442. * @param key key to use
  443. * @param value value to use
  444. * @param opt options for put
  445. * @return #GNUNET_OK on success,
  446. * #GNUNET_NO if a value was replaced (with REPLACE)
  447. * #GNUNET_SYSERR if UNIQUE_ONLY was the option and the
  448. * value already exists
  449. */
  450. int
  451. GNUNET_CONTAINER_multihashmap32_put (
  452. struct GNUNET_CONTAINER_MultiHashMap32 *map,
  453. uint32_t key,
  454. void *value,
  455. enum GNUNET_CONTAINER_MultiHashMapOption opt)
  456. {
  457. struct MapEntry *e;
  458. unsigned int i;
  459. i = idx_of (map, key);
  460. if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
  461. (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
  462. {
  463. e = map->map[i];
  464. while (e != NULL)
  465. {
  466. if (key == e->key)
  467. {
  468. if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
  469. return GNUNET_SYSERR;
  470. e->value = value;
  471. return GNUNET_NO;
  472. }
  473. e = e->next;
  474. }
  475. }
  476. if (map->size / 3 >= map->map_length / 4)
  477. {
  478. grow (map);
  479. i = idx_of (map, key);
  480. }
  481. e = GNUNET_new (struct MapEntry);
  482. e->key = key;
  483. e->value = value;
  484. e->next = map->map[i];
  485. map->map[i] = e;
  486. map->size++;
  487. return GNUNET_OK;
  488. }
  489. /**
  490. * Iterate over all entries in the map that match a particular key.
  491. *
  492. * @param map the map
  493. * @param key key that the entries must correspond to
  494. * @param it function to call on each entry
  495. * @param it_cls extra argument to @a it
  496. * @return the number of key value pairs processed,
  497. * GNUNET_SYSERR if it aborted iteration
  498. */
  499. int
  500. GNUNET_CONTAINER_multihashmap32_get_multiple (
  501. struct GNUNET_CONTAINER_MultiHashMap32 *map,
  502. uint32_t key,
  503. GNUNET_CONTAINER_MulitHashMapIterator32Callback it,
  504. void *it_cls)
  505. {
  506. int count;
  507. struct MapEntry *e;
  508. struct MapEntry **ce;
  509. count = 0;
  510. ce = &map->next_cache[map->next_cache_off];
  511. GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
  512. *ce = map->map[idx_of (map, key)];
  513. while (NULL != (e = *ce))
  514. {
  515. *ce = e->next;
  516. if (key != e->key)
  517. continue;
  518. if ((NULL != it) && (GNUNET_OK != it (it_cls, key, e->value)))
  519. {
  520. GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
  521. return GNUNET_SYSERR;
  522. }
  523. count++;
  524. }
  525. GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
  526. return count;
  527. }
  528. /**
  529. * Create an iterator for a multihashmap.
  530. * The iterator can be used to retrieve all the elements in the multihashmap
  531. * one by one, without having to handle all elements at once (in contrast to
  532. * GNUNET_CONTAINER_multihashmap_iterate()). Note that the iterator can not be
  533. * used anymore if elements have been removed from 'map' after the creation of
  534. * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
  535. * result in skipped or repeated elements.
  536. *
  537. * @param map the map to create an iterator for
  538. * @return an iterator over the given multihashmap @a map
  539. */
  540. struct GNUNET_CONTAINER_MultiHashMap32Iterator *
  541. GNUNET_CONTAINER_multihashmap32_iterator_create (
  542. const struct GNUNET_CONTAINER_MultiHashMap32 *map)
  543. {
  544. struct GNUNET_CONTAINER_MultiHashMap32Iterator *iter;
  545. iter = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMap32Iterator);
  546. iter->map = map;
  547. iter->modification_counter = map->modification_counter;
  548. iter->me = map->map[0];
  549. return iter;
  550. }
  551. /**
  552. * Retrieve the next element from the hash map at the iterator's position.
  553. * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
  554. * are not modified.
  555. * This operation is only allowed if no elements have been removed from the
  556. * multihashmap since the creation of 'iter', and the map has not been destroyed.
  557. * Adding elements may result in repeating or skipping elements.
  558. *
  559. * @param iter the iterator to get the next element from
  560. * @param key pointer to store the key in, can be NULL
  561. * @param value pointer to store the value in, can be NULL
  562. * @return #GNUNET_YES we returned an element,
  563. * #GNUNET_NO if we are out of elements
  564. */
  565. int
  566. GNUNET_CONTAINER_multihashmap32_iterator_next (
  567. struct GNUNET_CONTAINER_MultiHashMap32Iterator *iter,
  568. uint32_t *key,
  569. const void **value)
  570. {
  571. /* make sure the map has not been modified */
  572. GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
  573. /* look for the next entry, skipping empty buckets */
  574. while (1)
  575. {
  576. if (iter->idx >= iter->map->map_length)
  577. return GNUNET_NO;
  578. if (NULL != iter->me)
  579. {
  580. if (NULL != key)
  581. *key = iter->me->key;
  582. if (NULL != value)
  583. *value = iter->me->value;
  584. iter->me = iter->me->next;
  585. return GNUNET_YES;
  586. }
  587. iter->idx += 1;
  588. if (iter->idx < iter->map->map_length)
  589. iter->me = iter->map->map[iter->idx];
  590. }
  591. }
  592. /**
  593. * Destroy a multihashmap iterator.
  594. *
  595. * @param iter the iterator to destroy
  596. */
  597. void
  598. GNUNET_CONTAINER_multihashmap32_iterator_destroy (
  599. struct GNUNET_CONTAINER_MultiHashMapIterator *iter)
  600. {
  601. GNUNET_free (iter);
  602. }
  603. /* end of container_multihashmap.c */