timeval.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. /**
  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. #ifndef timeradd
  19. /** Replacement for timeradd on platforms that do not have it: sets tvout to
  20. * the sum of tv1 and tv2. */
  21. #define timeradd(tv1,tv2,tvout) \
  22. do { \
  23. (tvout)->tv_sec = (tv1)->tv_sec + (tv2)->tv_sec; \
  24. (tvout)->tv_usec = (tv1)->tv_usec + (tv2)->tv_usec; \
  25. if ((tvout)->tv_usec >= 1000000) { \
  26. (tvout)->tv_usec -= 1000000; \
  27. (tvout)->tv_sec++; \
  28. } \
  29. } while (0)
  30. #endif /* !defined(timeradd) */
  31. #ifndef timersub
  32. /** Replacement for timersub on platforms that do not have it: sets tvout to
  33. * tv1 minus tv2. */
  34. #define timersub(tv1,tv2,tvout) \
  35. do { \
  36. (tvout)->tv_sec = (tv1)->tv_sec - (tv2)->tv_sec; \
  37. (tvout)->tv_usec = (tv1)->tv_usec - (tv2)->tv_usec; \
  38. if ((tvout)->tv_usec < 0) { \
  39. (tvout)->tv_usec += 1000000; \
  40. (tvout)->tv_sec--; \
  41. } \
  42. } while (0)
  43. #endif /* !defined(timersub) */
  44. #ifndef timercmp
  45. /** Replacement for timercmp on platforms that do not have it: returns true
  46. * iff the relational operator "op" makes the expression tv1 op tv2 true.
  47. *
  48. * Note that while this definition should work for all boolean operators, some
  49. * platforms' native timercmp definitions do not support >=, <=, or ==. So
  50. * don't use those.
  51. */
  52. #define timercmp(tv1,tv2,op) \
  53. (((tv1)->tv_sec == (tv2)->tv_sec) ? \
  54. ((tv1)->tv_usec op (tv2)->tv_usec) : \
  55. ((tv1)->tv_sec op (tv2)->tv_sec))
  56. #endif /* !defined(timercmp) */
  57. #endif