[tor-commits] [tor/master] Move token_bucket_raw_* functions to the start of the module.

nickm at torproject.org nickm at torproject.org
Fri Apr 13 20:44:04 UTC 2018


commit 2307bef7a2403a48d57e3e352d8befcd8c9f482e
Author: Nick Mathewson <nickm at torproject.org>
Date:   Fri Apr 13 11:42:54 2018 -0400

    Move token_bucket_raw_* functions to the start of the module.
    
    (These functions were previously helper functions for
    token_bucket_rw_t).
---
 src/common/token_bucket.c | 88 +++++++++++++++++++++++------------------------
 1 file changed, 44 insertions(+), 44 deletions(-)

diff --git a/src/common/token_bucket.c b/src/common/token_bucket.c
index abfcc680c..8be377599 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





More information about the tor-commits mailing list