tor  0.4.2.1-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-2019, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
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 
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 
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 timercmp
73 
80 #define timercmp(tv1,tv2,op) \
81  (((tv1)->tv_sec == (tv2)->tv_sec) ? \
82  ((tv1)->tv_usec op (tv2)->tv_usec) : \
83  ((tv1)->tv_sec op (tv2)->tv_sec))
84 #endif /* !defined(timercmp) */
85 
86 #endif /* !defined(TOR_TIMEVAL_H) */
Integer definitions used throughout Tor.