container_multihashmap.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2008, 2012 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_multihashmap.c
  18. * @brief hash map where the same key may be present multiple times
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_container_lib.h"
  23. #define LOG(kind, ...) \
  24. GNUNET_log_from (kind, "util-container-multihashmap", __VA_ARGS__)
  25. /**
  26. * Maximum recursion depth for callbacks of
  27. * #GNUNET_CONTAINER_multihashmap_get_multiple() themselves s
  28. * again calling #GNUNET_CONTAINER_multihashmap_get_multiple().
  29. * Should be totally excessive, but if violated we die.
  30. */
  31. #define NEXT_CACHE_SIZE 16
  32. /**
  33. * An entry in the hash map with the full key.
  34. */
  35. struct BigMapEntry
  36. {
  37. /**
  38. * Value of the entry.
  39. */
  40. void *value;
  41. /**
  42. * If there is a hash collision, we create a linked list.
  43. */
  44. struct BigMapEntry *next;
  45. /**
  46. * Key for the entry.
  47. */
  48. struct GNUNET_HashCode key;
  49. };
  50. /**
  51. * An entry in the hash map with just a pointer to the key.
  52. */
  53. struct SmallMapEntry
  54. {
  55. /**
  56. * Value of the entry.
  57. */
  58. void *value;
  59. /**
  60. * If there is a hash collision, we create a linked list.
  61. */
  62. struct SmallMapEntry *next;
  63. /**
  64. * Key for the entry.
  65. */
  66. const struct GNUNET_HashCode *key;
  67. };
  68. /**
  69. * Entry in the map.
  70. */
  71. union MapEntry
  72. {
  73. /**
  74. * Variant used if map entries only contain a pointer to the key.
  75. */
  76. struct SmallMapEntry *sme;
  77. /**
  78. * Variant used if map entries contain the full key.
  79. */
  80. struct BigMapEntry *bme;
  81. };
  82. /**
  83. * Internal representation of the hash map.
  84. */
  85. struct GNUNET_CONTAINER_MultiHashMap
  86. {
  87. /**
  88. * All of our buckets.
  89. */
  90. union MapEntry *map;
  91. /**
  92. * Number of entries in the map.
  93. */
  94. unsigned int size;
  95. /**
  96. * Length of the "map" array.
  97. */
  98. unsigned int map_length;
  99. /**
  100. * #GNUNET_NO if the map entries are of type 'struct BigMapEntry',
  101. * #GNUNET_YES if the map entries are of type 'struct SmallMapEntry'.
  102. */
  103. int use_small_entries;
  104. /**
  105. * Counts the destructive modifications (grow, remove)
  106. * to the map, so that iterators can check if they are still valid.
  107. */
  108. unsigned int modification_counter;
  109. /**
  110. * Map entries indicating iteration positions currently
  111. * in use by #GNUNET_CONTAINER_multihashmap_get_multiple().
  112. * Only used up to @e next_cache_off.
  113. */
  114. union MapEntry next_cache[NEXT_CACHE_SIZE];
  115. /**
  116. * Offset of @e next_cache entries in use, must be smaller
  117. * than #NEXT_CACHE_SIZE.
  118. */
  119. unsigned int next_cache_off;
  120. };
  121. /**
  122. * Cursor into a multihashmap.
  123. * Allows to enumerate elements asynchronously.
  124. */
  125. struct GNUNET_CONTAINER_MultiHashMapIterator
  126. {
  127. /**
  128. * Position in the bucket @e idx
  129. */
  130. union MapEntry me;
  131. /**
  132. * Current bucket index.
  133. */
  134. unsigned int idx;
  135. /**
  136. * Modification counter as observed on the map when the iterator
  137. * was created.
  138. */
  139. unsigned int modification_counter;
  140. /**
  141. * Map that we are iterating over.
  142. */
  143. const struct GNUNET_CONTAINER_MultiHashMap *map;
  144. };
  145. /**
  146. * Create a multi hash map.
  147. *
  148. * @param len initial size (map will grow as needed)
  149. * @param do_not_copy_keys #GNUNET_NO is always safe and should be used by default;
  150. * #GNUNET_YES means that on 'put', the 'key' does not have
  151. * to be copied as the destination of the pointer is
  152. * guaranteed to be life as long as the value is stored in
  153. * the hashmap. This can significantly reduce memory
  154. * consumption, but of course is also a recipe for
  155. * heap corruption if the assumption is not true. Only
  156. * use this if (1) memory use is important in this case and
  157. * (2) you have triple-checked that the invariant holds
  158. * @return NULL on error
  159. */
  160. struct GNUNET_CONTAINER_MultiHashMap *
  161. GNUNET_CONTAINER_multihashmap_create (unsigned int len, int do_not_copy_keys)
  162. {
  163. struct GNUNET_CONTAINER_MultiHashMap *hm;
  164. GNUNET_assert (len > 0);
  165. hm = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMap);
  166. if (len * sizeof(union MapEntry) > GNUNET_MAX_MALLOC_CHECKED)
  167. {
  168. size_t s;
  169. /* application *explicitly* requested very large map, hopefully
  170. it checks the return value... */
  171. s = len * sizeof(union MapEntry);
  172. if ((s / sizeof(union MapEntry)) != len)
  173. return NULL; /* integer overflow on multiplication */
  174. if (NULL == (hm->map = GNUNET_malloc_large (s)))
  175. {
  176. /* out of memory */
  177. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  178. "Out of memory allocating large hash map (%u entries)\n",
  179. len);
  180. GNUNET_free (hm);
  181. return NULL;
  182. }
  183. }
  184. else
  185. {
  186. hm->map = GNUNET_new_array (len, union MapEntry);
  187. }
  188. hm->map_length = len;
  189. hm->use_small_entries = do_not_copy_keys;
  190. return hm;
  191. }
  192. /**
  193. * Destroy a hash map. Will not free any values stored in the hash
  194. * map!
  195. *
  196. * @param map the map
  197. */
  198. void
  199. GNUNET_CONTAINER_multihashmap_destroy (
  200. struct GNUNET_CONTAINER_MultiHashMap *map)
  201. {
  202. GNUNET_assert (0 == map->next_cache_off);
  203. for (unsigned int i = 0; i < map->map_length; i++)
  204. {
  205. union MapEntry me;
  206. me = map->map[i];
  207. if (map->use_small_entries)
  208. {
  209. struct SmallMapEntry *sme;
  210. struct SmallMapEntry *nxt;
  211. nxt = me.sme;
  212. while (NULL != (sme = nxt))
  213. {
  214. nxt = sme->next;
  215. GNUNET_free (sme);
  216. }
  217. me.sme = NULL;
  218. }
  219. else
  220. {
  221. struct BigMapEntry *bme;
  222. struct BigMapEntry *nxt;
  223. nxt = me.bme;
  224. while (NULL != (bme = nxt))
  225. {
  226. nxt = bme->next;
  227. GNUNET_free (bme);
  228. }
  229. me.bme = NULL;
  230. }
  231. }
  232. GNUNET_free (map->map);
  233. GNUNET_free (map);
  234. }
  235. /**
  236. * Compute the index of the bucket for the given key.
  237. *
  238. * @param map hash map for which to compute the index
  239. * @param key what key should the index be computed for
  240. * @return offset into the "map" array of "map"
  241. */
  242. static unsigned int
  243. idx_of (const struct GNUNET_CONTAINER_MultiHashMap *map,
  244. const struct GNUNET_HashCode *key)
  245. {
  246. GNUNET_assert (map != NULL);
  247. return (*(unsigned int *) key) % map->map_length;
  248. }
  249. /**
  250. * Get the number of key-value pairs in the map.
  251. *
  252. * @param map the map
  253. * @return the number of key value pairs
  254. */
  255. unsigned int
  256. GNUNET_CONTAINER_multihashmap_size (
  257. const struct GNUNET_CONTAINER_MultiHashMap *map)
  258. {
  259. return map->size;
  260. }
  261. /**
  262. * Given a key find a value in the map matching the key.
  263. *
  264. * @param map the map
  265. * @param key what to look for
  266. * @return NULL if no value was found; note that
  267. * this is indistinguishable from values that just
  268. * happen to be NULL; use "contains" to test for
  269. * key-value pairs with value NULL
  270. */
  271. void *
  272. GNUNET_CONTAINER_multihashmap_get (
  273. const struct GNUNET_CONTAINER_MultiHashMap *map,
  274. const struct GNUNET_HashCode *key)
  275. {
  276. union MapEntry me;
  277. me = map->map[idx_of (map, key)];
  278. if (map->use_small_entries)
  279. {
  280. struct SmallMapEntry *sme;
  281. for (sme = me.sme; NULL != sme; sme = sme->next)
  282. if (0 == GNUNET_memcmp (key, sme->key))
  283. return sme->value;
  284. }
  285. else
  286. {
  287. struct BigMapEntry *bme;
  288. for (bme = me.bme; NULL != bme; bme = bme->next)
  289. if (0 == GNUNET_memcmp (key, &bme->key))
  290. return bme->value;
  291. }
  292. return NULL;
  293. }
  294. /**
  295. * Iterate over all entries in the map.
  296. *
  297. * @param map the map
  298. * @param it function to call on each entry
  299. * @param it_cls extra argument to @a it
  300. * @return the number of key value pairs processed,
  301. * #GNUNET_SYSERR if it aborted iteration
  302. */
  303. int
  304. GNUNET_CONTAINER_multihashmap_iterate (
  305. struct GNUNET_CONTAINER_MultiHashMap *map,
  306. GNUNET_CONTAINER_MulitHashMapIteratorCallback it,
  307. void *it_cls)
  308. {
  309. int count;
  310. union MapEntry me;
  311. union MapEntry *ce;
  312. struct GNUNET_HashCode kc;
  313. GNUNET_assert (NULL != map);
  314. ce = &map->next_cache[map->next_cache_off];
  315. GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
  316. count = 0;
  317. for (unsigned i = 0; i < map->map_length; i++)
  318. {
  319. me = map->map[i];
  320. if (map->use_small_entries)
  321. {
  322. struct SmallMapEntry *sme;
  323. ce->sme = me.sme;
  324. while (NULL != (sme = ce->sme))
  325. {
  326. ce->sme = sme->next;
  327. if (NULL != it)
  328. {
  329. if (GNUNET_OK != it (it_cls, sme->key, sme->value))
  330. {
  331. GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
  332. return GNUNET_SYSERR;
  333. }
  334. }
  335. count++;
  336. }
  337. }
  338. else
  339. {
  340. struct BigMapEntry *bme;
  341. ce->bme = me.bme;
  342. while (NULL != (bme = ce->bme))
  343. {
  344. ce->bme = bme->next;
  345. if (NULL != it)
  346. {
  347. kc = bme->key;
  348. if (GNUNET_OK != it (it_cls, &kc, bme->value))
  349. {
  350. GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
  351. return GNUNET_SYSERR;
  352. }
  353. }
  354. count++;
  355. }
  356. }
  357. }
  358. GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
  359. return count;
  360. }
  361. /**
  362. * We are about to free() the @a bme, make sure it is not in
  363. * the list of next values for any iterator in the @a map's next_cache.
  364. *
  365. * @param map the map to check
  366. * @param bme the entry that is about to be free'd
  367. */
  368. static void
  369. update_next_cache_bme (struct GNUNET_CONTAINER_MultiHashMap *map,
  370. const struct BigMapEntry *bme)
  371. {
  372. for (unsigned int i = 0; i < map->next_cache_off; i++)
  373. if (map->next_cache[i].bme == bme)
  374. map->next_cache[i].bme = bme->next;
  375. }
  376. /**
  377. * We are about to free() the @a sme, make sure it is not in
  378. * the list of next values for any iterator in the @a map's next_cache.
  379. *
  380. * @param map the map to check
  381. * @param sme the entry that is about to be free'd
  382. */
  383. static void
  384. update_next_cache_sme (struct GNUNET_CONTAINER_MultiHashMap *map,
  385. const struct SmallMapEntry *sme)
  386. {
  387. for (unsigned int i = 0; i < map->next_cache_off; i++)
  388. if (map->next_cache[i].sme == sme)
  389. map->next_cache[i].sme = sme->next;
  390. }
  391. /**
  392. * Remove the given key-value pair from the map. Note that if the
  393. * key-value pair is in the map multiple times, only one of the pairs
  394. * will be removed.
  395. *
  396. * @param map the map
  397. * @param key key of the key-value pair
  398. * @param value value of the key-value pair
  399. * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
  400. * is not in the map
  401. */
  402. int
  403. GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap *map,
  404. const struct GNUNET_HashCode *key,
  405. const void *value)
  406. {
  407. union MapEntry me;
  408. unsigned int i;
  409. map->modification_counter++;
  410. i = idx_of (map, key);
  411. me = map->map[i];
  412. if (map->use_small_entries)
  413. {
  414. struct SmallMapEntry *p;
  415. p = NULL;
  416. for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
  417. {
  418. if ((0 == GNUNET_memcmp (key, sme->key)) && (value == sme->value))
  419. {
  420. if (NULL == p)
  421. map->map[i].sme = sme->next;
  422. else
  423. p->next = sme->next;
  424. update_next_cache_sme (map, sme);
  425. GNUNET_free (sme);
  426. map->size--;
  427. return GNUNET_YES;
  428. }
  429. p = sme;
  430. }
  431. }
  432. else
  433. {
  434. struct BigMapEntry *p;
  435. p = NULL;
  436. for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
  437. {
  438. if ((0 == GNUNET_memcmp (key, &bme->key)) && (value == bme->value))
  439. {
  440. if (NULL == p)
  441. map->map[i].bme = bme->next;
  442. else
  443. p->next = bme->next;
  444. update_next_cache_bme (map, bme);
  445. GNUNET_free (bme);
  446. map->size--;
  447. return GNUNET_YES;
  448. }
  449. p = bme;
  450. }
  451. }
  452. return GNUNET_NO;
  453. }
  454. /**
  455. * Remove all entries for the given key from the map.
  456. * Note that the values would not be "freed".
  457. *
  458. * @param map the map
  459. * @param key identifies values to be removed
  460. * @return number of values removed
  461. */
  462. int
  463. GNUNET_CONTAINER_multihashmap_remove_all (
  464. struct GNUNET_CONTAINER_MultiHashMap *map,
  465. const struct GNUNET_HashCode *key)
  466. {
  467. union MapEntry me;
  468. unsigned int i;
  469. int ret;
  470. map->modification_counter++;
  471. ret = 0;
  472. i = idx_of (map, key);
  473. me = map->map[i];
  474. if (map->use_small_entries)
  475. {
  476. struct SmallMapEntry *sme;
  477. struct SmallMapEntry *p;
  478. p = NULL;
  479. sme = me.sme;
  480. while (NULL != sme)
  481. {
  482. if (0 == GNUNET_memcmp (key, sme->key))
  483. {
  484. if (NULL == p)
  485. map->map[i].sme = sme->next;
  486. else
  487. p->next = sme->next;
  488. update_next_cache_sme (map, sme);
  489. GNUNET_free (sme);
  490. map->size--;
  491. if (NULL == p)
  492. sme = map->map[i].sme;
  493. else
  494. sme = p->next;
  495. ret++;
  496. }
  497. else
  498. {
  499. p = sme;
  500. sme = sme->next;
  501. }
  502. }
  503. }
  504. else
  505. {
  506. struct BigMapEntry *bme;
  507. struct BigMapEntry *p;
  508. p = NULL;
  509. bme = me.bme;
  510. while (NULL != bme)
  511. {
  512. if (0 == GNUNET_memcmp (key, &bme->key))
  513. {
  514. if (NULL == p)
  515. map->map[i].bme = bme->next;
  516. else
  517. p->next = bme->next;
  518. update_next_cache_bme (map, bme);
  519. GNUNET_free (bme);
  520. map->size--;
  521. if (NULL == p)
  522. bme = map->map[i].bme;
  523. else
  524. bme = p->next;
  525. ret++;
  526. }
  527. else
  528. {
  529. p = bme;
  530. bme = bme->next;
  531. }
  532. }
  533. }
  534. return ret;
  535. }
  536. /**
  537. * Callback used to remove all entries from the map.
  538. *
  539. * @param cls the `struct GNUNET_CONTAINER_MultiHashMap`
  540. * @param key the key
  541. * @param value the value
  542. * @return #GNUNET_OK (continue to iterate)
  543. */
  544. static int
  545. remove_all (void *cls, const struct GNUNET_HashCode *key, void *value)
  546. {
  547. struct GNUNET_CONTAINER_MultiHashMap *map = cls;
  548. GNUNET_assert (GNUNET_YES ==
  549. GNUNET_CONTAINER_multihashmap_remove (map, key, value));
  550. return GNUNET_OK;
  551. }
  552. /**
  553. * @ingroup hashmap
  554. * Remove all entries from the map.
  555. * Note that the values would not be "freed".
  556. *
  557. * @param map the map
  558. * @return number of values removed
  559. */
  560. unsigned int
  561. GNUNET_CONTAINER_multihashmap_clear (struct GNUNET_CONTAINER_MultiHashMap *map)
  562. {
  563. unsigned int ret;
  564. ret = map->size;
  565. GNUNET_CONTAINER_multihashmap_iterate (map, &remove_all, map);
  566. return ret;
  567. }
  568. /**
  569. * Check if the map contains any value under the given
  570. * key (including values that are NULL).
  571. *
  572. * @param map the map
  573. * @param key the key to test if a value exists for it
  574. * @return #GNUNET_YES if such a value exists,
  575. * #GNUNET_NO if not
  576. */
  577. int
  578. GNUNET_CONTAINER_multihashmap_contains (
  579. const struct GNUNET_CONTAINER_MultiHashMap *map,
  580. const struct GNUNET_HashCode *key)
  581. {
  582. union MapEntry me;
  583. me = map->map[idx_of (map, key)];
  584. if (map->use_small_entries)
  585. {
  586. struct SmallMapEntry *sme;
  587. for (sme = me.sme; NULL != sme; sme = sme->next)
  588. if (0 == GNUNET_memcmp (key, sme->key))
  589. return GNUNET_YES;
  590. }
  591. else
  592. {
  593. struct BigMapEntry *bme;
  594. for (bme = me.bme; NULL != bme; bme = bme->next)
  595. if (0 == GNUNET_memcmp (key, &bme->key))
  596. return GNUNET_YES;
  597. }
  598. return GNUNET_NO;
  599. }
  600. /**
  601. * Check if the map contains the given value under the given
  602. * key.
  603. *
  604. * @param map the map
  605. * @param key the key to test if a value exists for it
  606. * @param value value to test for
  607. * @return #GNUNET_YES if such a value exists,
  608. * #GNUNET_NO if not
  609. */
  610. int
  611. GNUNET_CONTAINER_multihashmap_contains_value (
  612. const struct GNUNET_CONTAINER_MultiHashMap *map,
  613. const struct GNUNET_HashCode *key,
  614. const void *value)
  615. {
  616. union MapEntry me;
  617. me = map->map[idx_of (map, key)];
  618. if (map->use_small_entries)
  619. {
  620. struct SmallMapEntry *sme;
  621. for (sme = me.sme; NULL != sme; sme = sme->next)
  622. if ((0 == GNUNET_memcmp (key, sme->key)) && (sme->value == value))
  623. return GNUNET_YES;
  624. }
  625. else
  626. {
  627. struct BigMapEntry *bme;
  628. for (bme = me.bme; NULL != bme; bme = bme->next)
  629. if ((0 == GNUNET_memcmp (key, &bme->key)) && (bme->value == value))
  630. return GNUNET_YES;
  631. }
  632. return GNUNET_NO;
  633. }
  634. /**
  635. * Grow the given map to a more appropriate size.
  636. *
  637. * @param map the hash map to grow
  638. */
  639. static void
  640. grow (struct GNUNET_CONTAINER_MultiHashMap *map)
  641. {
  642. union MapEntry *old_map;
  643. union MapEntry *new_map;
  644. unsigned int old_len;
  645. unsigned int new_len;
  646. unsigned int idx;
  647. old_map = map->map;
  648. old_len = map->map_length;
  649. GNUNET_assert (0 != old_len);
  650. new_len = old_len * 2;
  651. if (0 == new_len) /* 2^31 * 2 == 0 */
  652. new_len = old_len; /* never use 0 */
  653. if (new_len == old_len)
  654. return; /* nothing changed */
  655. new_map = GNUNET_malloc_large (new_len * sizeof(union MapEntry));
  656. if (NULL == new_map)
  657. return; /* grow not possible */
  658. map->modification_counter++;
  659. map->map_length = new_len;
  660. map->map = new_map;
  661. for (unsigned int i = 0; i < old_len; i++)
  662. {
  663. if (map->use_small_entries)
  664. {
  665. struct SmallMapEntry *sme;
  666. while (NULL != (sme = old_map[i].sme))
  667. {
  668. old_map[i].sme = sme->next;
  669. idx = idx_of (map, sme->key);
  670. sme->next = new_map[idx].sme;
  671. new_map[idx].sme = sme;
  672. }
  673. }
  674. else
  675. {
  676. struct BigMapEntry *bme;
  677. while (NULL != (bme = old_map[i].bme))
  678. {
  679. old_map[i].bme = bme->next;
  680. idx = idx_of (map, &bme->key);
  681. bme->next = new_map[idx].bme;
  682. new_map[idx].bme = bme;
  683. }
  684. }
  685. }
  686. GNUNET_free (old_map);
  687. }
  688. /**
  689. * Store a key-value pair in the map.
  690. *
  691. * @param map the map
  692. * @param key key to use
  693. * @param value value to use
  694. * @param opt options for put
  695. * @return #GNUNET_OK on success,
  696. * #GNUNET_NO if a value was replaced (with REPLACE)
  697. * #GNUNET_SYSERR if UNIQUE_ONLY was the option and the
  698. * value already exists
  699. */
  700. int
  701. GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map,
  702. const struct GNUNET_HashCode *key,
  703. void *value,
  704. enum GNUNET_CONTAINER_MultiHashMapOption opt)
  705. {
  706. union MapEntry me;
  707. unsigned int i;
  708. i = idx_of (map, key);
  709. if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
  710. (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
  711. {
  712. me = map->map[i];
  713. if (map->use_small_entries)
  714. {
  715. struct SmallMapEntry *sme;
  716. for (sme = me.sme; NULL != sme; sme = sme->next)
  717. if (0 == GNUNET_memcmp (key, sme->key))
  718. {
  719. if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
  720. return GNUNET_SYSERR;
  721. sme->value = value;
  722. return GNUNET_NO;
  723. }
  724. }
  725. else
  726. {
  727. struct BigMapEntry *bme;
  728. for (bme = me.bme; NULL != bme; bme = bme->next)
  729. if (0 == GNUNET_memcmp (key, &bme->key))
  730. {
  731. if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
  732. return GNUNET_SYSERR;
  733. bme->value = value;
  734. return GNUNET_NO;
  735. }
  736. }
  737. }
  738. if (map->size / 3 >= map->map_length / 4)
  739. {
  740. grow (map);
  741. i = idx_of (map, key);
  742. }
  743. if (map->use_small_entries)
  744. {
  745. struct SmallMapEntry *sme;
  746. sme = GNUNET_new (struct SmallMapEntry);
  747. sme->key = key;
  748. sme->value = value;
  749. sme->next = map->map[i].sme;
  750. map->map[i].sme = sme;
  751. }
  752. else
  753. {
  754. struct BigMapEntry *bme;
  755. bme = GNUNET_new (struct BigMapEntry);
  756. bme->key = *key;
  757. bme->value = value;
  758. bme->next = map->map[i].bme;
  759. map->map[i].bme = bme;
  760. }
  761. map->size++;
  762. return GNUNET_OK;
  763. }
  764. /**
  765. * Iterate over all entries in the map that match a particular key.
  766. *
  767. * @param map the map
  768. * @param key key that the entries must correspond to
  769. * @param it function to call on each entry
  770. * @param it_cls extra argument to it
  771. * @return the number of key value pairs processed,
  772. * #GNUNET_SYSERR if it aborted iteration
  773. */
  774. int
  775. GNUNET_CONTAINER_multihashmap_get_multiple (
  776. struct GNUNET_CONTAINER_MultiHashMap *map,
  777. const struct GNUNET_HashCode *key,
  778. GNUNET_CONTAINER_MulitHashMapIteratorCallback it,
  779. void *it_cls)
  780. {
  781. int count;
  782. union MapEntry *me;
  783. union MapEntry *ce;
  784. ce = &map->next_cache[map->next_cache_off];
  785. GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
  786. count = 0;
  787. me = &map->map[idx_of (map, key)];
  788. if (map->use_small_entries)
  789. {
  790. struct SmallMapEntry *sme;
  791. ce->sme = me->sme;
  792. while (NULL != (sme = ce->sme))
  793. {
  794. ce->sme = sme->next;
  795. if (0 != GNUNET_memcmp (key, sme->key))
  796. continue;
  797. if ((NULL != it) && (GNUNET_OK != it (it_cls, key, sme->value)))
  798. {
  799. GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
  800. return GNUNET_SYSERR;
  801. }
  802. count++;
  803. }
  804. }
  805. else
  806. {
  807. struct BigMapEntry *bme;
  808. ce->bme = me->bme;
  809. while (NULL != (bme = ce->bme))
  810. {
  811. ce->bme = bme->next;
  812. if (0 != GNUNET_memcmp (key, &bme->key))
  813. continue;
  814. if ((NULL != it) && (GNUNET_OK != it (it_cls, key, bme->value)))
  815. {
  816. GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
  817. return GNUNET_SYSERR;
  818. }
  819. count++;
  820. }
  821. }
  822. GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
  823. return count;
  824. }
  825. /**
  826. * @ingroup hashmap
  827. * Call @a it on a random value from the map, or not at all
  828. * if the map is empty. Note that this function has linear
  829. * complexity (in the size of the map).
  830. *
  831. * @param map the map
  832. * @param it function to call on a random entry
  833. * @param it_cls extra argument to @a it
  834. * @return the number of key value pairs processed, zero or one.
  835. */
  836. unsigned int
  837. GNUNET_CONTAINER_multihashmap_get_random (
  838. const struct GNUNET_CONTAINER_MultiHashMap *map,
  839. GNUNET_CONTAINER_MulitHashMapIteratorCallback it,
  840. void *it_cls)
  841. {
  842. unsigned int off;
  843. unsigned int idx;
  844. union MapEntry me;
  845. if (0 == map->size)
  846. return 0;
  847. if (NULL == it)
  848. return 1;
  849. off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, map->size);
  850. for (idx = 0; idx < map->map_length; idx++)
  851. {
  852. me = map->map[idx];
  853. if (map->use_small_entries)
  854. {
  855. struct SmallMapEntry *sme;
  856. struct SmallMapEntry *nxt;
  857. nxt = me.sme;
  858. while (NULL != (sme = nxt))
  859. {
  860. nxt = sme->next;
  861. if (0 == off)
  862. {
  863. if (GNUNET_OK != it (it_cls, sme->key, sme->value))
  864. return GNUNET_SYSERR;
  865. return 1;
  866. }
  867. off--;
  868. }
  869. }
  870. else
  871. {
  872. struct BigMapEntry *bme;
  873. struct BigMapEntry *nxt;
  874. nxt = me.bme;
  875. while (NULL != (bme = nxt))
  876. {
  877. nxt = bme->next;
  878. if (0 == off)
  879. {
  880. if (GNUNET_OK != it (it_cls, &bme->key, bme->value))
  881. return GNUNET_SYSERR;
  882. return 1;
  883. }
  884. off--;
  885. }
  886. }
  887. }
  888. GNUNET_break (0);
  889. return GNUNET_SYSERR;
  890. }
  891. /**
  892. * Create an iterator for a multihashmap.
  893. * The iterator can be used to retrieve all the elements in the multihashmap
  894. * one by one, without having to handle all elements at once (in contrast to
  895. * GNUNET_CONTAINER_multihashmap_iterate()). Note that the iterator can not be
  896. * used anymore if elements have been removed from 'map' after the creation of
  897. * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
  898. * result in skipped or repeated elements.
  899. *
  900. * @param map the map to create an iterator for
  901. * @return an iterator over the given multihashmap 'map'
  902. */
  903. struct GNUNET_CONTAINER_MultiHashMapIterator *
  904. GNUNET_CONTAINER_multihashmap_iterator_create (
  905. const struct GNUNET_CONTAINER_MultiHashMap *map)
  906. {
  907. struct GNUNET_CONTAINER_MultiHashMapIterator *iter;
  908. iter = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMapIterator);
  909. iter->map = map;
  910. iter->modification_counter = map->modification_counter;
  911. iter->me = map->map[0];
  912. return iter;
  913. }
  914. /**
  915. * Retrieve the next element from the hash map at the iterator's position.
  916. * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
  917. * are not modified.
  918. * This operation is only allowed if no elements have been removed from the
  919. * multihashmap since the creation of 'iter', and the map has not been destroyed.
  920. * Adding elements may result in repeating or skipping elements.
  921. *
  922. * @param iter the iterator to get the next element from
  923. * @param key pointer to store the key in, can be NULL
  924. * @param value pointer to store the value in, can be NULL
  925. * @return #GNUNET_YES we returned an element,
  926. * #GNUNET_NO if we are out of elements
  927. */
  928. int
  929. GNUNET_CONTAINER_multihashmap_iterator_next (
  930. struct GNUNET_CONTAINER_MultiHashMapIterator *iter,
  931. struct GNUNET_HashCode *key,
  932. const void **value)
  933. {
  934. /* make sure the map has not been modified */
  935. GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
  936. /* look for the next entry, skipping empty buckets */
  937. while (1)
  938. {
  939. if (iter->idx >= iter->map->map_length)
  940. return GNUNET_NO;
  941. if (GNUNET_YES == iter->map->use_small_entries)
  942. {
  943. if (NULL != iter->me.sme)
  944. {
  945. if (NULL != key)
  946. *key = *iter->me.sme->key;
  947. if (NULL != value)
  948. *value = iter->me.sme->value;
  949. iter->me.sme = iter->me.sme->next;
  950. return GNUNET_YES;
  951. }
  952. }
  953. else
  954. {
  955. if (NULL != iter->me.bme)
  956. {
  957. if (NULL != key)
  958. *key = iter->me.bme->key;
  959. if (NULL != value)
  960. *value = iter->me.bme->value;
  961. iter->me.bme = iter->me.bme->next;
  962. return GNUNET_YES;
  963. }
  964. }
  965. iter->idx += 1;
  966. if (iter->idx < iter->map->map_length)
  967. iter->me = iter->map->map[iter->idx];
  968. }
  969. }
  970. /**
  971. * Destroy a multihashmap iterator.
  972. *
  973. * @param iter the iterator to destroy
  974. */
  975. void
  976. GNUNET_CONTAINER_multihashmap_iterator_destroy (
  977. struct GNUNET_CONTAINER_MultiHashMapIterator *iter)
  978. {
  979. GNUNET_free (iter);
  980. }
  981. /* end of container_multihashmap.c */