[or-cvs] [tor/master] Add a simple integer-ceiling-division macro before we get it wrong

nickm at torproject.org nickm at torproject.org
Wed Sep 15 02:29:01 UTC 2010


Author: Nick Mathewson <nickm at torproject.org>
Date: Tue, 14 Sep 2010 22:32:36 -0400
Subject: Add a simple integer-ceiling-division macro before we get it wrong
Commit: 6d8fc4eb3866122ef42f209cc51a875a3e438607

---
 src/common/util.h      |    5 +++++
 src/or/connection_or.c |    3 +--
 src/or/relay.c         |    3 +--
 src/or/routerlist.c    |    4 ++--
 4 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/src/common/util.h b/src/common/util.h
index 3a3a873..4a31c27 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -160,6 +160,11 @@ unsigned round_to_next_multiple_of(unsigned number, unsigned divisor);
 uint32_t round_uint32_to_next_multiple_of(uint32_t number, uint32_t divisor);
 uint64_t round_uint64_to_next_multiple_of(uint64_t number, uint64_t divisor);
 
+/* Compute the CEIL of <b>a</b> divided by <b>b</b>, for nonnegative <b>a</b>
+ * and positive <b>b</b>.  Works on integer types only. Not defined if a+b can
+ * overflow. */
+#define CEIL_DIV(a,b) (((a)+(b)-1)/(b))
+
 /* String manipulation */
 
 /** Allowable characters in a hexadecimal string. */
diff --git a/src/or/connection_or.c b/src/or/connection_or.c
index 0ba1bca..686037f 100644
--- a/src/or/connection_or.c
+++ b/src/or/connection_or.c
@@ -252,8 +252,7 @@ connection_or_flushed_some(or_connection_t *conn)
   /* If we're under the low water mark, add cells until we're just over the
    * high water mark. */
   if (datalen < OR_CONN_LOWWATER) {
-    ssize_t n = (OR_CONN_HIGHWATER - datalen + CELL_NETWORK_SIZE-1)
-      / CELL_NETWORK_SIZE;
+    ssize_t n = CEIL_DIV(OR_CONN_HIGHWATER - datalen, CELL_NETWORK_SIZE);
     time_t now = approx_time();
     while (conn->active_circuits && n > 0) {
       int flushed;
diff --git a/src/or/relay.c b/src/or/relay.c
index 125b2f7..935fad9 100644
--- a/src/or/relay.c
+++ b/src/or/relay.c
@@ -1520,8 +1520,7 @@ circuit_resume_edge_reading_helper(edge_connection_t *first_conn,
 
  again:
 
-  /* ??? turn this into a ceildiv function? */
-  cells_per_conn = (max_to_package + n_streams - 1 ) / n_streams;
+  cells_per_conn = CEIL_DIV(max_to_package, n_streams);
 
   packaged_this_round = 0;
   n_streams_left = 0;
diff --git a/src/or/routerlist.c b/src/or/routerlist.c
index 480da44..b77107c 100644
--- a/src/or/routerlist.c
+++ b/src/or/routerlist.c
@@ -4220,7 +4220,7 @@ launch_router_descriptor_downloads(smartlist_t *downloadable,
       pds_flags |= PDS_NO_EXISTING_SERVERDESC_FETCH;
     }
 
-    n_per_request = (n_downloadable+MIN_REQUESTS-1) / MIN_REQUESTS;
+    n_per_request = CEIL_DIV(n_downloadable, MIN_REQUESTS);
     if (n_per_request > MAX_DL_PER_REQUEST)
       n_per_request = MAX_DL_PER_REQUEST;
     if (n_per_request < MIN_DL_PER_REQUEST)
@@ -4233,7 +4233,7 @@ launch_router_descriptor_downloads(smartlist_t *downloadable,
 
     log_info(LD_DIR,
              "Launching %d request%s for %d router%s, %d at a time",
-             (n_downloadable+n_per_request-1)/n_per_request,
+             CEIL_DIV(n_downloadable, n_per_request),
              req_plural, n_downloadable, rtr_plural, n_per_request);
     smartlist_sort_digests(downloadable);
     for (i=0; i < n_downloadable; i += n_per_request) {
-- 
1.7.1



More information about the tor-commits mailing list