aboutsummaryrefslogtreecommitdiff
path: root/src/common/compat_time.h
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2018-04-26 11:17:48 -0400
committerNick Mathewson <nickm@torproject.org>2018-04-26 12:01:48 -0400
commit9abf541f7f70068b6f7567481739ca6374f1fd57 (patch)
tree2d3feeaa192761116fb7eee48d1b8cde46b86a49 /src/common/compat_time.h
parent7cbc44eeb19831bc467f8e1b87062ed1c87934d5 (diff)
downloadtor-9abf541f7f70068b6f7567481739ca6374f1fd57.tar.gz
tor-9abf541f7f70068b6f7567481739ca6374f1fd57.zip
Add a function to compute millisecond time difference quickly.
Our main function, though accurate on all platforms, can be very slow on 32-bit hosts. This one is faster on all 32-bit hosts, and accurate everywhere except apple, where it will typically be off by 1%. But since 32-bit apple is a relic anyway, I think we should be fine.
Diffstat (limited to 'src/common/compat_time.h')
-rw-r--r--src/common/compat_time.h27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/common/compat_time.h b/src/common/compat_time.h
index 09dd6add3d..57ab20ab11 100644
--- a/src/common/compat_time.h
+++ b/src/common/compat_time.h
@@ -173,6 +173,33 @@ void monotime_coarse_add_msec(monotime_coarse_t *out,
#define monotime_coarse_add_msec monotime_add_msec
#endif /* defined(MONOTIME_COARSE_TYPE_IS_DIFFERENT) */
+/**
+ * As monotime_coarse_diff_msec, but avoid 64-bit division.
+ *
+ * Requires that the difference fit into an int32_t; not for use with
+ * large time differences.
+ */
+int32_t monotime_coarse_diff_msec32_(const monotime_coarse_t *start,
+ const monotime_coarse_t *end);
+
+/**
+ * As monotime_coarse_diff_msec, but avoid 64-bit division if it is expensive.
+ *
+ * Requires that the difference fit into an int32_t; not for use with
+ * large time differences.
+ */
+static inline int32_t
+monotime_coarse_diff_msec32(const monotime_coarse_t *start,
+ const monotime_coarse_t *end)
+{
+#if SIZEOF_VOID_P == 8
+ // on a 64-bit platform, let's assume 64/64 division is cheap.
+ return (int32_t) monotime_coarse_diff_msec(start, end);
+#else
+ return monotime_coarse_diff_msec32_(start, end);
+#endif
+}
+
MOCK_DECL(void, tor_gettimeofday, (struct timeval *timeval));
#ifdef TOR_UNIT_TESTS