diff options
author | David Barksdale <amatus@amatus.name> | 2016-11-17 23:24:43 -0600 |
---|---|---|
committer | David Barksdale <amatus@amatus.name> | 2016-11-17 23:24:43 -0600 |
commit | 12597da2a33fe196a785a656136cba7675a06e21 (patch) | |
tree | c189c7fb7f2835f8219622400d83c453a91dbb69 /src/util/time.c | |
parent | cdeb1253bfda209def1ef48b436ff09cf7ab8be6 (diff) | |
download | gnunet-12597da2a33fe196a785a656136cba7675a06e21.tar.gz gnunet-12597da2a33fe196a785a656136cba7675a06e21.zip |
Sometimes it's OK if multiplication overflows
Diffstat (limited to 'src/util/time.c')
-rw-r--r-- | src/util/time.c | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/util/time.c b/src/util/time.c index eb168d531..89b0c2d44 100644 --- a/src/util/time.c +++ b/src/util/time.c | |||
@@ -446,6 +446,32 @@ GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel, | |||
446 | 446 | ||
447 | 447 | ||
448 | /** | 448 | /** |
449 | * Saturating multiply relative time by a given factor. | ||
450 | * | ||
451 | * @param rel some duration | ||
452 | * @param factor integer to multiply with | ||
453 | * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor | ||
454 | */ | ||
455 | struct GNUNET_TIME_Relative | ||
456 | GNUNET_TIME_relative_saturating_multiply (struct GNUNET_TIME_Relative rel, | ||
457 | unsigned long long factor) | ||
458 | { | ||
459 | struct GNUNET_TIME_Relative ret; | ||
460 | |||
461 | if (0 == factor) | ||
462 | return GNUNET_TIME_UNIT_ZERO; | ||
463 | if (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) | ||
464 | return GNUNET_TIME_UNIT_FOREVER_REL; | ||
465 | ret.rel_value_us = rel.rel_value_us * factor; | ||
466 | if (ret.rel_value_us / factor != rel.rel_value_us) | ||
467 | { | ||
468 | return GNUNET_TIME_UNIT_FOREVER_REL; | ||
469 | } | ||
470 | return ret; | ||
471 | } | ||
472 | |||
473 | |||
474 | /** | ||
449 | * Divide relative time by a given factor. | 475 | * Divide relative time by a given factor. |
450 | * | 476 | * |
451 | * @param rel some duration | 477 | * @param rel some duration |