Tor  0.4.7.0-alpha-dev
timeval.h
Go to the documentation of this file.
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 */
5 
6 /**
7  * \file timeval.h
8  *
9  * \brief Declarations for timeval-related macros that some platforms
10  * are missing.
11  **/
12 
13 #ifndef TOR_TIMEVAL_H
14 #define TOR_TIMEVAL_H
15 
16 #include "orconfig.h"
17 #include "lib/cc/torint.h"
18 
19 #ifdef HAVE_SYS_TIME_H
20 #include <sys/time.h>
21 #endif
22 
23 #ifdef TOR_COVERAGE
24 /* For coverage builds, we use a slower definition of these macros without
25  * branches, to make coverage consistent. */
26 #undef timeradd
27 #undef timersub
28 #define timeradd(tv1,tv2,tvout) \
29  do { \
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; \
34  } while (0)
35 #define timersub(tv1,tv2,tvout) \
36  do { \
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; \
41  } while (0)
42 #endif /* defined(TOR_COVERAGE) */
43 
44 #ifndef timeradd
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) \
48  do { \
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; \
53  (tvout)->tv_sec++; \
54  } \
55  } while (0)
56 #endif /* !defined(timeradd) */
57 
58 #ifndef timersub
59 /** Replacement for timersub on platforms that do not have it: sets tvout to
60  * tv1 minus tv2. */
61 #define timersub(tv1,tv2,tvout) \
62  do { \
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; \
67  (tvout)->tv_sec--; \
68  } \
69  } while (0)
70 #endif /* !defined(timersub) */
71 
72 #ifndef COCCI
73 #ifndef timercmp
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.
76  *
77  * Note that while this definition should work for all boolean operators, some
78  * platforms' native timercmp definitions do not support >=, <=, or ==. So
79  * don't use those.
80  */
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) */
87 
88 #endif /* !defined(TOR_TIMEVAL_H) */
Integer definitions used throughout Tor.