diff options
author | Christian Grothoff <christian@grothoff.org> | 2019-02-20 13:05:01 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2019-02-20 13:05:01 +0100 |
commit | 88a9a5beab50429901faf8f196e785f39f4da8d7 (patch) | |
tree | 96d601d1b573d3165ff5de96e96b919b879967f8 | |
parent | 2f685ab91bd689f835c97a265a978d2d45eab509 (diff) | |
download | gnunet-88a9a5beab50429901faf8f196e785f39f4da8d7.tar.gz gnunet-88a9a5beab50429901faf8f196e785f39f4da8d7.zip |
port hashmap fixes to other maps, investigated #4905, clearly no longer possible, closing that one
-rw-r--r-- | src/util/container_multihashmap32.c | 26 | ||||
-rw-r--r-- | src/util/container_multipeermap.c | 34 | ||||
-rw-r--r-- | src/util/container_multishortmap.c | 36 |
3 files changed, 63 insertions, 33 deletions
diff --git a/src/util/container_multihashmap32.c b/src/util/container_multihashmap32.c index 4b19c7c10..a614c04ae 100644 --- a/src/util/container_multihashmap32.c +++ b/src/util/container_multihashmap32.c | |||
@@ -11,7 +11,7 @@ | |||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Affero General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Affero General Public License | 15 | You should have received a copy of the GNU Affero General Public License |
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
@@ -146,8 +146,13 @@ GNUNET_CONTAINER_multihashmap32_create (unsigned int len) | |||
146 | 146 | ||
147 | GNUNET_assert (len > 0); | 147 | GNUNET_assert (len > 0); |
148 | ret = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMap32); | 148 | ret = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMap32); |
149 | ret->map = GNUNET_new_array (len, | 149 | ret->map = GNUNET_malloc_large (len * |
150 | struct MapEntry *); | 150 | sizeof (struct MapEntry *)); |
151 | if (NULL == ret->map) | ||
152 | { | ||
153 | GNUNET_free (ret); | ||
154 | return NULL; | ||
155 | } | ||
151 | ret->map_length = len; | 156 | ret->map_length = len; |
152 | return ret; | 157 | return ret; |
153 | } | 158 | } |
@@ -268,7 +273,7 @@ GNUNET_CONTAINER_multihashmap32_iterate (struct GNUNET_CONTAINER_MultiHashMap32 | |||
268 | e->key, | 273 | e->key, |
269 | e->value)) | 274 | e->value)) |
270 | { | 275 | { |
271 | GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); | 276 | GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); |
272 | return GNUNET_SYSERR; | 277 | return GNUNET_SYSERR; |
273 | } | 278 | } |
274 | } | 279 | } |
@@ -463,13 +468,18 @@ grow (struct GNUNET_CONTAINER_MultiHashMap32 *map) | |||
463 | unsigned int new_len; | 468 | unsigned int new_len; |
464 | unsigned int idx; | 469 | unsigned int idx; |
465 | 470 | ||
466 | map->modification_counter++; | ||
467 | |||
468 | old_map = map->map; | 471 | old_map = map->map; |
469 | old_len = map->map_length; | 472 | old_len = map->map_length; |
470 | new_len = old_len * 2; | 473 | new_len = old_len * 2; |
471 | new_map = GNUNET_new_array (new_len, | 474 | if (0 == new_len) /* 2^31 * 2 == 0 */ |
472 | struct MapEntry *); | 475 | new_len = old_len; /* never use 0 */ |
476 | if (new_len == old_len) | ||
477 | return; /* nothing changed */ | ||
478 | new_map = GNUNET_malloc_large (new_len * | ||
479 | sizeof (struct MapEntry *)); | ||
480 | if (NULL == new_map) | ||
481 | return; /* grow not possible */ | ||
482 | map->modification_counter++; | ||
473 | map->map_length = new_len; | 483 | map->map_length = new_len; |
474 | map->map = new_map; | 484 | map->map = new_map; |
475 | for (unsigned int i = 0; i < old_len; i++) | 485 | for (unsigned int i = 0; i < old_len; i++) |
diff --git a/src/util/container_multipeermap.c b/src/util/container_multipeermap.c index 8fa23df72..613efc0a9 100644 --- a/src/util/container_multipeermap.c +++ b/src/util/container_multipeermap.c | |||
@@ -11,7 +11,7 @@ | |||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Affero General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Affero General Public License | 15 | You should have received a copy of the GNU Affero General Public License |
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
@@ -200,8 +200,13 @@ GNUNET_CONTAINER_multipeermap_create (unsigned int len, | |||
200 | 200 | ||
201 | GNUNET_assert (len > 0); | 201 | GNUNET_assert (len > 0); |
202 | map = GNUNET_new (struct GNUNET_CONTAINER_MultiPeerMap); | 202 | map = GNUNET_new (struct GNUNET_CONTAINER_MultiPeerMap); |
203 | map->map = GNUNET_new_array (len, | 203 | map->map = GNUNET_malloc_large (len * |
204 | union MapEntry); | 204 | sizeof (union MapEntry)); |
205 | if (NULL == map->map) | ||
206 | { | ||
207 | GNUNET_free (map); | ||
208 | return NULL; | ||
209 | } | ||
205 | map->map_length = len; | 210 | map->map_length = len; |
206 | map->use_small_entries = do_not_copy_keys; | 211 | map->use_small_entries = do_not_copy_keys; |
207 | return map; | 212 | return map; |
@@ -493,7 +498,7 @@ GNUNET_CONTAINER_multipeermap_remove (struct GNUNET_CONTAINER_MultiPeerMap *map, | |||
493 | if (NULL == p) | 498 | if (NULL == p) |
494 | map->map[i].bme = bme->next; | 499 | map->map[i].bme = bme->next; |
495 | else | 500 | else |
496 | p->next = bme->next; | 501 | p->next = bme->next; |
497 | update_next_cache_bme (map, | 502 | update_next_cache_bme (map, |
498 | bme); | 503 | bme); |
499 | GNUNET_free (bme); | 504 | GNUNET_free (bme); |
@@ -685,18 +690,23 @@ grow (struct GNUNET_CONTAINER_MultiPeerMap *map) | |||
685 | unsigned int old_len; | 690 | unsigned int old_len; |
686 | unsigned int new_len; | 691 | unsigned int new_len; |
687 | unsigned int idx; | 692 | unsigned int idx; |
688 | unsigned int i; | ||
689 | |||
690 | map->modification_counter++; | ||
691 | 693 | ||
692 | old_map = map->map; | 694 | old_map = map->map; |
693 | old_len = map->map_length; | 695 | old_len = map->map_length; |
696 | GNUNET_assert (0 != old_len); | ||
694 | new_len = old_len * 2; | 697 | new_len = old_len * 2; |
695 | new_map = GNUNET_new_array (new_len, | 698 | if (0 == new_len) /* 2^31 * 2 == 0 */ |
696 | union MapEntry); | 699 | new_len = old_len; /* never use 0 */ |
700 | if (new_len == old_len) | ||
701 | return; /* nothing changed */ | ||
702 | new_map = GNUNET_malloc_large (new_len * | ||
703 | sizeof (union MapEntry)); | ||
704 | if (NULL == new_map) | ||
705 | return; /* grow not possible */ | ||
706 | map->modification_counter++; | ||
697 | map->map_length = new_len; | 707 | map->map_length = new_len; |
698 | map->map = new_map; | 708 | map->map = new_map; |
699 | for (i = 0; i < old_len; i++) | 709 | for (unsigned int i = 0; i < old_len; i++) |
700 | { | 710 | { |
701 | if (map->use_small_entries) | 711 | if (map->use_small_entries) |
702 | { | 712 | { |
@@ -829,7 +839,7 @@ GNUNET_CONTAINER_multipeermap_get_multiple (struct GNUNET_CONTAINER_MultiPeerMap | |||
829 | int count; | 839 | int count; |
830 | union MapEntry me; | 840 | union MapEntry me; |
831 | union MapEntry *ce; | 841 | union MapEntry *ce; |
832 | 842 | ||
833 | ce = &map->next_cache[map->next_cache_off]; | 843 | ce = &map->next_cache[map->next_cache_off]; |
834 | GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE); | 844 | GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE); |
835 | count = 0; | 845 | count = 0; |
@@ -903,7 +913,7 @@ GNUNET_CONTAINER_multipeermap_get_random (const struct GNUNET_CONTAINER_MultiPee | |||
903 | { | 913 | { |
904 | unsigned int off; | 914 | unsigned int off; |
905 | union MapEntry me; | 915 | union MapEntry me; |
906 | 916 | ||
907 | if (0 == map->size) | 917 | if (0 == map->size) |
908 | return 0; | 918 | return 0; |
909 | if (NULL == it) | 919 | if (NULL == it) |
diff --git a/src/util/container_multishortmap.c b/src/util/container_multishortmap.c index a48581b6a..966e23d35 100644 --- a/src/util/container_multishortmap.c +++ b/src/util/container_multishortmap.c | |||
@@ -11,7 +11,7 @@ | |||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Affero General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Affero General Public License | 15 | You should have received a copy of the GNU Affero General Public License |
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
@@ -183,8 +183,8 @@ struct GNUNET_CONTAINER_MultiShortmapIterator | |||
183 | * Create a multi hash map. | 183 | * Create a multi hash map. |
184 | * | 184 | * |
185 | * @param len initial size (map will grow as needed) | 185 | * @param len initial size (map will grow as needed) |
186 | * @param do_not_copy_keys GNUNET_NO is always safe and should be used by default; | 186 | * @param do_not_copy_keys #GNUNET_NO is always safe and should be used by default; |
187 | * GNUNET_YES means that on 'put', the 'key' does not have | 187 | * #GNUNET_YES means that on 'put', the 'key' does not have |
188 | * to be copied as the destination of the pointer is | 188 | * to be copied as the destination of the pointer is |
189 | * guaranteed to be life as long as the value is stored in | 189 | * guaranteed to be life as long as the value is stored in |
190 | * the hashmap. This can significantly reduce memory | 190 | * the hashmap. This can significantly reduce memory |
@@ -202,8 +202,13 @@ GNUNET_CONTAINER_multishortmap_create (unsigned int len, | |||
202 | 202 | ||
203 | GNUNET_assert (len > 0); | 203 | GNUNET_assert (len > 0); |
204 | map = GNUNET_new (struct GNUNET_CONTAINER_MultiShortmap); | 204 | map = GNUNET_new (struct GNUNET_CONTAINER_MultiShortmap); |
205 | map->map = GNUNET_new_array (len, | 205 | map->map = GNUNET_malloc_large (len * |
206 | union MapEntry); | 206 | sizeof (union MapEntry)); |
207 | if (NULL == map->map) | ||
208 | { | ||
209 | GNUNET_free (map); | ||
210 | return NULL; | ||
211 | } | ||
207 | map->map_length = len; | 212 | map->map_length = len; |
208 | map->use_small_entries = do_not_copy_keys; | 213 | map->use_small_entries = do_not_copy_keys; |
209 | return map; | 214 | return map; |
@@ -355,7 +360,7 @@ GNUNET_CONTAINER_multishortmap_iterate (struct GNUNET_CONTAINER_MultiShortmap *m | |||
355 | if (map->use_small_entries) | 360 | if (map->use_small_entries) |
356 | { | 361 | { |
357 | struct SmallMapEntry *sme; | 362 | struct SmallMapEntry *sme; |
358 | 363 | ||
359 | ce->sme = me.sme; | 364 | ce->sme = me.sme; |
360 | while (NULL != (sme = ce->sme)) | 365 | while (NULL != (sme = ce->sme)) |
361 | { | 366 | { |
@@ -366,7 +371,7 @@ GNUNET_CONTAINER_multishortmap_iterate (struct GNUNET_CONTAINER_MultiShortmap *m | |||
366 | sme->value)) ) | 371 | sme->value)) ) |
367 | { | 372 | { |
368 | GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); | 373 | GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); |
369 | return GNUNET_SYSERR; | 374 | return GNUNET_SYSERR; |
370 | } | 375 | } |
371 | count++; | 376 | count++; |
372 | } | 377 | } |
@@ -458,7 +463,7 @@ GNUNET_CONTAINER_multishortmap_remove (struct GNUNET_CONTAINER_MultiShortmap *ma | |||
458 | if (map->use_small_entries) | 463 | if (map->use_small_entries) |
459 | { | 464 | { |
460 | struct SmallMapEntry *p = NULL; | 465 | struct SmallMapEntry *p = NULL; |
461 | 466 | ||
462 | for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) | 467 | for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) |
463 | { | 468 | { |
464 | if ((0 == memcmp (key, | 469 | if ((0 == memcmp (key, |
@@ -482,7 +487,7 @@ GNUNET_CONTAINER_multishortmap_remove (struct GNUNET_CONTAINER_MultiShortmap *ma | |||
482 | else | 487 | else |
483 | { | 488 | { |
484 | struct BigMapEntry *p = NULL; | 489 | struct BigMapEntry *p = NULL; |
485 | 490 | ||
486 | for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) | 491 | for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) |
487 | { | 492 | { |
488 | if ((0 == memcmp (key, | 493 | if ((0 == memcmp (key, |
@@ -686,13 +691,18 @@ grow (struct GNUNET_CONTAINER_MultiShortmap *map) | |||
686 | unsigned int new_len; | 691 | unsigned int new_len; |
687 | unsigned int idx; | 692 | unsigned int idx; |
688 | 693 | ||
689 | map->modification_counter++; | ||
690 | |||
691 | old_map = map->map; | 694 | old_map = map->map; |
692 | old_len = map->map_length; | 695 | old_len = map->map_length; |
693 | new_len = old_len * 2; | 696 | new_len = old_len * 2; |
694 | new_map = GNUNET_new_array (new_len, | 697 | if (0 == new_len) /* 2^31 * 2 == 0 */ |
695 | union MapEntry); | 698 | new_len = old_len; /* never use 0 */ |
699 | if (new_len == old_len) | ||
700 | return; /* nothing changed */ | ||
701 | new_map = GNUNET_malloc_large (new_len * | ||
702 | sizeof (union MapEntry)); | ||
703 | if (NULL == new_map) | ||
704 | return; /* grow not possible */ | ||
705 | map->modification_counter++; | ||
696 | map->map_length = new_len; | 706 | map->map_length = new_len; |
697 | map->map = new_map; | 707 | map->map = new_map; |
698 | for (unsigned int i = 0; i < old_len; i++) | 708 | for (unsigned int i = 0; i < old_len; i++) |