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 /src/util/container_multishortmap.c | |
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
Diffstat (limited to 'src/util/container_multishortmap.c')
-rw-r--r-- | src/util/container_multishortmap.c | 36 |
1 files changed, 23 insertions, 13 deletions
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++) |