Browse Source

MSVC6 is apparently terrified of unnatural cross-breeding between uint64_t and double, and needs more persuasion than usual to cast one to the other. Issue identified by Frediano Ziglio; patch revised for minimal impact on non-MSVC6 compilers.

svn:r6768
Nick Mathewson 18 years ago
parent
commit
e572d5990c
4 changed files with 18 additions and 7 deletions
  1. 13 0
      src/common/compat.h
  2. 1 1
      src/or/hibernate.c
  3. 2 2
      src/or/main.c
  4. 2 4
      src/or/rephist.c

+ 13 - 0
src/common/compat.h

@@ -76,6 +76,19 @@
 #endif /* ifndef MAVE_MACRO__func__ */
 #endif /* if not windows */
 
+#if defined(_MSC_VER) && (_MSC_VER < 1300)
+/* MSVC versions before 7 apparently don't believe that you can cast uint64_t
+ * to double and really mean it. */
+extern inline double U64_TO_DBL(uint64_t x) {
+  int64_t i = (int64_t) x;
+  return (i < 0) ? ((double) INT64_MAX) : (double) i;
+}
+#define DBL_TO_U64(x) ((uint64_t)(int64_t) (x))
+#else
+#define U64_TO_DBL(x) ((double) (x))
+#define DBL_TO_U64(x) ((uint64_t) (x))
+#endif
+
 /* ===== String compatibility */
 #ifdef MS_WINDOWS
 /* Windows names string functions differently from most other platforms. */

+ 1 - 1
src/or/hibernate.c

@@ -683,7 +683,7 @@ hibernate_hard_limit_reached(void)
 static int
 hibernate_soft_limit_reached(void)
 {
-  uint64_t soft_limit = (uint64_t) ((get_options()->AccountingMax) * .95);
+  uint64_t soft_limit = DBL_TO_U64((get_options()->AccountingMax) * .95);
   if (!soft_limit)
     return 0;
   return n_bytes_read_in_interval >= soft_limit

+ 2 - 2
src/or/main.c

@@ -1359,11 +1359,11 @@ dumpstats(int severity)
       U64_PRINTF_ARG(stats_n_destroy_cells_processed));
   if (stats_n_data_cells_packaged)
     log(severity,LD_NET,"Average packaged cell fullness: %2.3f%%",
-        100*(((double)stats_n_data_bytes_packaged) /
+        100*(U64_TO_DBL(stats_n_data_bytes_packaged) /
              (stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE)) );
   if (stats_n_data_cells_received)
     log(severity,LD_NET,"Average delivered cell fullness: %2.3f%%",
-        100*(((double)stats_n_data_bytes_received) /
+        100*(U64_TO_DBL(stats_n_data_bytes_received) /
              (stats_n_data_cells_received*RELAY_PAYLOAD_SIZE)) );
 
   if (now - time_of_process_start >= 0)

+ 2 - 4
src/or/rephist.c

@@ -577,11 +577,9 @@ rep_hist_bandwidth_assess(void)
   r = find_largest_max(read_array);
   w = find_largest_max(write_array);
   if (r>w)
-    return (int)(w/(double)NUM_SECS_ROLLING_MEASURE);
+    return (int)(U64_TO_DBL(w)/NUM_SECS_ROLLING_MEASURE);
   else
-    return (int)(r/(double)NUM_SECS_ROLLING_MEASURE);
-
-  return 0;
+    return (int)(U64_TO_DBL(r)/NUM_SECS_ROLLING_MEASURE);
 }
 
 /**