1 /* Copyright (c) 2003-2004, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2019, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
9 * \brief Declarations for timeval-related macros that some platforms
17 #include "lib/cc/torint.h"
19 #ifdef HAVE_SYS_TIME_H
24 /** Replacement for timeradd on platforms that do not have it: sets tvout to
25 * the sum of tv1 and tv2. */
26 #define timeradd(tv1,tv2,tvout) \
28 (tvout)->tv_sec = (tv1)->tv_sec + (tv2)->tv_sec; \
29 (tvout)->tv_usec = (tv1)->tv_usec + (tv2)->tv_usec; \
30 if ((tvout)->tv_usec >= 1000000) { \
31 (tvout)->tv_usec -= 1000000; \
35 #endif /* !defined(timeradd) */
38 /** Replacement for timersub on platforms that do not have it: sets tvout to
40 #define timersub(tv1,tv2,tvout) \
42 (tvout)->tv_sec = (tv1)->tv_sec - (tv2)->tv_sec; \
43 (tvout)->tv_usec = (tv1)->tv_usec - (tv2)->tv_usec; \
44 if ((tvout)->tv_usec < 0) { \
45 (tvout)->tv_usec += 1000000; \
49 #endif /* !defined(timersub) */
52 /** Replacement for timercmp on platforms that do not have it: returns true
53 * iff the relational operator "op" makes the expression tv1 op tv2 true.
55 * Note that while this definition should work for all boolean operators, some
56 * platforms' native timercmp definitions do not support >=, <=, or ==. So
59 #define timercmp(tv1,tv2,op) \
60 (((tv1)->tv_sec == (tv2)->tv_sec) ? \
61 ((tv1)->tv_usec op (tv2)->tv_usec) : \
62 ((tv1)->tv_sec op (tv2)->tv_sec))
63 #endif /* !defined(timercmp) */