timer.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* $Id: timer.h 1294 2005-05-17 00:12:43Z aturner $ */
  2. /*
  3. * Copyright (c) 2001-2004 Aaron Turner.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright owners nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #ifndef _TIMER_H_
  32. #define _TIMER_H_
  33. #include "config.h"
  34. #include "defines.h"
  35. #include "tcpreplay.h"
  36. #include "common.h"
  37. #include <time.h>
  38. #include <sys/time.h>
  39. #include <math.h>
  40. inline void timerdiv(struct timeval *tvp, float div);
  41. inline void float2timer(float time, struct timeval *tvp);
  42. #ifndef TIMEVAL_TO_TIMESPEC
  43. #define TIMEVAL_TO_TIMESPEC(tv, ts) { \
  44. (ts)->tv_sec = (tv)->tv_sec; \
  45. (ts)->tv_nsec = (tv)->tv_usec * 1000; }
  46. #endif
  47. /* zero out a timer */
  48. #ifndef timerclear
  49. #define timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0
  50. #endif
  51. /* is timer non-zero? */
  52. #ifndef timerisset
  53. #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
  54. #endif
  55. /* add tvp and uvp and store in vvp */
  56. #ifndef timeradd
  57. #define timeradd(tvp, uvp, vvp) \
  58. do { \
  59. (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
  60. (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
  61. if ((vvp)->tv_usec >= 1000000) { \
  62. (vvp)->tv_sec++; \
  63. (vvp)->tv_usec -= 1000000; \
  64. } \
  65. } while (0)
  66. #endif
  67. /* subtract uvp from tvp and store in vvp */
  68. #ifndef timersub
  69. #define timersub(tvp, uvp, vvp) \
  70. do { \
  71. (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
  72. (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
  73. if ((vvp)->tv_usec < 0) { \
  74. (vvp)->tv_sec--; \
  75. (vvp)->tv_usec += 1000000; \
  76. } \
  77. } while (0)
  78. #endif
  79. /* compare tvp and uvp using cmp */
  80. #ifndef timercmp
  81. #define timercmp(tvp, uvp, cmp) \
  82. (((tvp)->tv_sec == (uvp)->tv_sec) ? \
  83. ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
  84. ((tvp)->tv_sec cmp (uvp)->tv_sec))
  85. #endif
  86. /* multiply tvp by x and store in uvp */
  87. #define timermul(tvp, uvp, x) \
  88. do { \
  89. (uvp)->tv_sec = (tvp)->tv_sec * x; \
  90. (uvp)->tv_usec = (tvp)->tv_usec * x; \
  91. while((uvp)->tv_usec > 1000000) { \
  92. (uvp)->tv_sec++; \
  93. (uvp)->tv_usec -= 1000000; \
  94. } \
  95. } while(0)
  96. /* device tvp by x. store in tvp */
  97. #define timerdiv2(tvp, x) \
  98. do { \
  99. (tvp)->tv_sec = (tvp)->tv_sec / x; \
  100. (tvp)->tv_usec = (tvp)->tv_usec / x; \
  101. } while(0)
  102. #endif
  103. /*
  104. Local Variables:
  105. mode:c
  106. indent-tabs-mode:nil
  107. c-basic-offset:4
  108. End:
  109. */