timeval.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. /**
  6. * \file timeval.h
  7. *
  8. * \brief Declarations for timeval-related macros that some platforms
  9. * are missing.
  10. **/
  11. #ifndef TOR_TIMEVAL_H
  12. #define TOR_TIMEVAL_H
  13. #include "orconfig.h"
  14. #include "lib/cc/torint.h"
  15. #ifdef HAVE_SYS_TIME_H
  16. #include <sys/time.h>
  17. #endif
  18. #ifdef TOR_COVERAGE
  19. /* For coverage builds, we use a slower definition of these macros without
  20. * branches, to make coverage consistent. */
  21. #undef timeradd
  22. #undef timersub
  23. #define timeradd(tv1,tv2,tvout) \
  24. do { \
  25. (tvout)->tv_sec = (tv1)->tv_sec + (tv2)->tv_sec; \
  26. (tvout)->tv_usec = (tv1)->tv_usec + (tv2)->tv_usec; \
  27. (tvout)->tv_sec += (tvout)->tv_usec / 1000000; \
  28. (tvout)->tv_usec %= 1000000; \
  29. } while (0)
  30. #define timersub(tv1,tv2,tvout) \
  31. do { \
  32. (tvout)->tv_sec = (tv1)->tv_sec - (tv2)->tv_sec - 1; \
  33. (tvout)->tv_usec = (tv1)->tv_usec - (tv2)->tv_usec + 1000000; \
  34. (tvout)->tv_sec += (tvout)->tv_usec / 1000000; \
  35. (tvout)->tv_usec %= 1000000; \
  36. } while (0)
  37. #endif /* defined(TOR_COVERAGE) */
  38. #ifndef timeradd
  39. /** Replacement for timeradd on platforms that do not have it: sets tvout to
  40. * the sum of tv1 and tv2. */
  41. #define timeradd(tv1,tv2,tvout) \
  42. do { \
  43. (tvout)->tv_sec = (tv1)->tv_sec + (tv2)->tv_sec; \
  44. (tvout)->tv_usec = (tv1)->tv_usec + (tv2)->tv_usec; \
  45. if ((tvout)->tv_usec >= 1000000) { \
  46. (tvout)->tv_usec -= 1000000; \
  47. (tvout)->tv_sec++; \
  48. } \
  49. } while (0)
  50. #endif /* !defined(timeradd) */
  51. #ifndef timersub
  52. /** Replacement for timersub on platforms that do not have it: sets tvout to
  53. * tv1 minus tv2. */
  54. #define timersub(tv1,tv2,tvout) \
  55. do { \
  56. (tvout)->tv_sec = (tv1)->tv_sec - (tv2)->tv_sec; \
  57. (tvout)->tv_usec = (tv1)->tv_usec - (tv2)->tv_usec; \
  58. if ((tvout)->tv_usec < 0) { \
  59. (tvout)->tv_usec += 1000000; \
  60. (tvout)->tv_sec--; \
  61. } \
  62. } while (0)
  63. #endif /* !defined(timersub) */
  64. #ifndef timercmp
  65. /** Replacement for timercmp on platforms that do not have it: returns true
  66. * iff the relational operator "op" makes the expression tv1 op tv2 true.
  67. *
  68. * Note that while this definition should work for all boolean operators, some
  69. * platforms' native timercmp definitions do not support >=, <=, or ==. So
  70. * don't use those.
  71. */
  72. #define timercmp(tv1,tv2,op) \
  73. (((tv1)->tv_sec == (tv2)->tv_sec) ? \
  74. ((tv1)->tv_usec op (tv2)->tv_usec) : \
  75. ((tv1)->tv_sec op (tv2)->tv_sec))
  76. #endif /* !defined(timercmp) */
  77. #endif /* !defined(TOR_TIMEVAL_H) */