1 /* Copyright (c) 2003-2004, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2021, 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 /* For coverage builds, we use a slower definition of these macros without
25 * branches, to make coverage consistent. */
28 #define timeradd(tv1,tv2,tvout) \
30 (tvout)->tv_sec = (tv1)->tv_sec + (tv2)->tv_sec; \
31 (tvout)->tv_usec = (tv1)->tv_usec + (tv2)->tv_usec; \
32 (tvout)->tv_sec += (tvout)->tv_usec / 1000000; \
33 (tvout)->tv_usec %= 1000000; \
35 #define timersub(tv1,tv2,tvout) \
37 (tvout)->tv_sec = (tv1)->tv_sec - (tv2)->tv_sec - 1; \
38 (tvout)->tv_usec = (tv1)->tv_usec - (tv2)->tv_usec + 1000000; \
39 (tvout)->tv_sec += (tvout)->tv_usec / 1000000; \
40 (tvout)->tv_usec %= 1000000; \
42 #endif /* defined(TOR_COVERAGE) */
45 /** Replacement for timeradd on platforms that do not have it: sets tvout to
46 * the sum of tv1 and tv2. */
47 #define timeradd(tv1,tv2,tvout) \
49 (tvout)->tv_sec = (tv1)->tv_sec + (tv2)->tv_sec; \
50 (tvout)->tv_usec = (tv1)->tv_usec + (tv2)->tv_usec; \
51 if ((tvout)->tv_usec >= 1000000) { \
52 (tvout)->tv_usec -= 1000000; \
56 #endif /* !defined(timeradd) */
59 /** Replacement for timersub on platforms that do not have it: sets tvout to
61 #define timersub(tv1,tv2,tvout) \
63 (tvout)->tv_sec = (tv1)->tv_sec - (tv2)->tv_sec; \
64 (tvout)->tv_usec = (tv1)->tv_usec - (tv2)->tv_usec; \
65 if ((tvout)->tv_usec < 0) { \
66 (tvout)->tv_usec += 1000000; \
70 #endif /* !defined(timersub) */
74 /** Replacement for timercmp on platforms that do not have it: returns true
75 * iff the relational operator "op" makes the expression tv1 op tv2 true.
77 * Note that while this definition should work for all boolean operators, some
78 * platforms' native timercmp definitions do not support >=, <=, or ==. So
81 #define timercmp(tv1,tv2,op) \
82 (((tv1)->tv_sec == (tv2)->tv_sec) ? \
83 ((tv1)->tv_usec op (tv2)->tv_usec) : \
84 ((tv1)->tv_sec op (tv2)->tv_sec))
85 #endif /* !defined(timercmp) */
86 #endif /* !defined(COCCI) */
88 #endif /* !defined(TOR_TIMEVAL_H) */