From: Nick Mathewson Date: Fri, 13 Apr 2018 15:42:54 +0000 (-0400) Subject: Move token_bucket_raw_* functions to the start of the module. X-Git-Tag: tor-0.3.4.1-alpha~155^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2307bef7a2403a48d57e3e352d8befcd8c9f482e;p=thirdparty%2Ftor.git Move token_bucket_raw_* functions to the start of the module. (These functions were previously helper functions for token_bucket_rw_t). --- diff --git a/src/common/token_bucket.c b/src/common/token_bucket.c index abfcc680c4..8be377599b 100644 --- a/src/common/token_bucket.c +++ b/src/common/token_bucket.c @@ -66,6 +66,50 @@ token_bucket_raw_adjust(token_bucket_raw_t *bucket, bucket->bucket = MIN(bucket->bucket, cfg->burst); } +/** + * Given an amount of elapsed time units, and a bucket configuration + * cfg, refill the level of bucket accordingly. Note that the + * units of time in elapsed must correspond to those used to set the + * rate in cfg, or the result will be illogical. + */ +int +token_bucket_raw_refill_steps(token_bucket_raw_t *bucket, + const token_bucket_cfg_t *cfg, + const uint32_t elapsed) +{ + const int was_empty = (bucket->bucket <= 0); + /* The casts here prevent an underflow. + * + * Note that even if the bucket value is negative, subtracting it from + * "burst" will still produce a correct result. If this result is + * ridiculously high, then the "elapsed > gap / rate" check below + * should catch it. */ + const size_t gap = ((size_t)cfg->burst) - ((size_t)bucket->bucket); + + if (elapsed > gap / cfg->rate) { + bucket->bucket = cfg->burst; + } else { + bucket->bucket += cfg->rate * elapsed; + } + + return was_empty && bucket->bucket > 0; +} + +/** + * Decrement a provided bucket by n units. Note that n + * must be nonnegative. + */ +int +token_bucket_raw_dec(token_bucket_raw_t *bucket, + ssize_t n) +{ + if (BUG(n < 0)) + return 0; + const int becomes_empty = bucket->bucket > 0 && n >= bucket->bucket; + bucket->bucket -= n; + return becomes_empty; +} + /** Convert a rate in bytes per second to a rate in bytes per step */ static uint32_t rate_per_sec_to_rate_per_step(uint32_t rate) @@ -127,35 +171,6 @@ token_bucket_rw_reset(token_bucket_rw_t *bucket, &bucket->cfg, now_ts); } -/** - * Given an amount of elapsed time units, and a bucket configuration - * cfg, refill the level of bucket accordingly. Note that the - * units of time in elapsed must correspond to those used to set the - * rate in cfg, or the result will be illogical. - */ -int -token_bucket_raw_refill_steps(token_bucket_raw_t *bucket, - const token_bucket_cfg_t *cfg, - const uint32_t elapsed) -{ - const int was_empty = (bucket->bucket <= 0); - /* The casts here prevent an underflow. - * - * Note that even if the bucket value is negative, subtracting it from - * "burst" will still produce a correct result. If this result is - * ridiculously high, then the "elapsed > gap / rate" check below - * should catch it. */ - const size_t gap = ((size_t)cfg->burst) - ((size_t)bucket->bucket); - - if (elapsed > gap / cfg->rate) { - bucket->bucket = cfg->burst; - } else { - bucket->bucket += cfg->rate * elapsed; - } - - return was_empty && bucket->bucket > 0; -} - /** * Refill bucket as appropriate, given that the current timestamp * is now_ts. @@ -197,21 +212,6 @@ token_bucket_rw_refill(token_bucket_rw_t *bucket, return flags; } -/** - * Decrement a provided bucket by n units. Note that n - * must be nonnegative. - */ -int -token_bucket_raw_dec(token_bucket_raw_t *bucket, - ssize_t n) -{ - if (BUG(n < 0)) - return 0; - const int becomes_empty = bucket->bucket > 0 && n >= bucket->bucket; - bucket->bucket -= n; - return becomes_empty; -} - /** * Decrement the read token bucket in bucket by n bytes. *