Browse Source

Add a simple integer-ceiling-division macro before we get it wrong

Nick Mathewson 13 years ago
parent
commit
6d8fc4eb38
4 changed files with 9 additions and 6 deletions
  1. 5 0
      src/common/util.h
  2. 1 2
      src/or/connection_or.c
  3. 1 2
      src/or/relay.c
  4. 2 2
      src/or/routerlist.c

+ 5 - 0
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. */

+ 1 - 2
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;

+ 1 - 2
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;

+ 2 - 2
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) {