timeval.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #ifndef TOR_TIMEVAL_H
  6. #define TOR_TIMEVAL_H
  7. #include "orconfig.h"
  8. #include "lib/cc/torint.h"
  9. #ifdef HAVE_SYS_TIME_H
  10. #include <sys/time.h>
  11. #endif
  12. #ifndef timeradd
  13. /** Replacement for timeradd on platforms that do not have it: sets tvout to
  14. * the sum of tv1 and tv2. */
  15. #define timeradd(tv1,tv2,tvout) \
  16. do { \
  17. (tvout)->tv_sec = (tv1)->tv_sec + (tv2)->tv_sec; \
  18. (tvout)->tv_usec = (tv1)->tv_usec + (tv2)->tv_usec; \
  19. if ((tvout)->tv_usec >= 1000000) { \
  20. (tvout)->tv_usec -= 1000000; \
  21. (tvout)->tv_sec++; \
  22. } \
  23. } while (0)
  24. #endif /* !defined(timeradd) */
  25. #ifndef timersub
  26. /** Replacement for timersub on platforms that do not have it: sets tvout to
  27. * tv1 minus tv2. */
  28. #define timersub(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. if ((tvout)->tv_usec < 0) { \
  33. (tvout)->tv_usec += 1000000; \
  34. (tvout)->tv_sec--; \
  35. } \
  36. } while (0)
  37. #endif /* !defined(timersub) */
  38. #ifndef timercmp
  39. /** Replacement for timercmp on platforms that do not have it: returns true
  40. * iff the relational operator "op" makes the expression tv1 op tv2 true.
  41. *
  42. * Note that while this definition should work for all boolean operators, some
  43. * platforms' native timercmp definitions do not support >=, <=, or ==. So
  44. * don't use those.
  45. */
  46. #define timercmp(tv1,tv2,op) \
  47. (((tv1)->tv_sec == (tv2)->tv_sec) ? \
  48. ((tv1)->tv_usec op (tv2)->tv_usec) : \
  49. ((tv1)->tv_sec op (tv2)->tv_sec))
  50. #endif /* !defined(timercmp) */
  51. #endif