diff options
author | Nick Mathewson <nickm@torproject.org> | 2018-04-13 11:42:54 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2018-04-13 16:31:47 -0400 |
commit | 2307bef7a2403a48d57e3e352d8befcd8c9f482e (patch) | |
tree | bb65e634c50630feeff5dfadd39d517c50eeee42 | |
parent | 9c405ba59528b931aefc9ed80fed3e5d9f231d27 (diff) | |
download | tor-2307bef7a2403a48d57e3e352d8befcd8c9f482e.tar.gz tor-2307bef7a2403a48d57e3e352d8befcd8c9f482e.zip |
Move token_bucket_raw_* functions to the start of the module.
(These functions were previously helper functions for
token_bucket_rw_t).
-rw-r--r-- | src/common/token_bucket.c | 88 |
1 files changed, 44 insertions, 44 deletions
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 <b>elapsed</b> time units, and a bucket configuration + * <b>cfg</b>, refill the level of <b>bucket</b> accordingly. Note that the + * units of time in <b>elapsed</b> must correspond to those used to set the + * rate in <b>cfg</b>, 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 <b>n</b> units. Note that <b>n</b> + * 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) @@ -128,35 +172,6 @@ token_bucket_rw_reset(token_bucket_rw_t *bucket, } /** - * Given an amount of <b>elapsed</b> time units, and a bucket configuration - * <b>cfg</b>, refill the level of <b>bucket</b> accordingly. Note that the - * units of time in <b>elapsed</b> must correspond to those used to set the - * rate in <b>cfg</b>, 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 <b>bucket</b> as appropriate, given that the current timestamp * is <b>now_ts</b>. * @@ -198,21 +213,6 @@ token_bucket_rw_refill(token_bucket_rw_t *bucket, } /** - * Decrement a provided bucket by <b>n</b> units. Note that <b>n</b> - * 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 <b>bucket</b> by <b>n</b> bytes. * * Return true if the bucket was nonempty and became empty; return false |