• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2015 The TCPDUMP project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
17  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
18  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef netdissect_timeval_operations_h
29 #define netdissect_timeval_operations_h
30 
31 /* Operations on timevals. */
32 
33 #define ND_MICRO_PER_SEC 1000000
34 #define ND_NANO_PER_SEC 1000000000
35 
36 #define netdissect_timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
37 
38 #define netdissect_timevalisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
39 
40 #define netdissect_timevalcmp(tvp, uvp, cmp)   \
41 	(((tvp)->tv_sec == (uvp)->tv_sec) ?    \
42 	 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
43 	 ((tvp)->tv_sec cmp (uvp)->tv_sec))
44 
45 #define netdissect_timevaladd(tvp, uvp, vvp, nano_prec)           \
46 	do {                                                      \
47 		(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;    \
48 		(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
49 		if (nano_prec) {                                  \
50 			if ((vvp)->tv_usec >= ND_NANO_PER_SEC) {  \
51 				(vvp)->tv_sec++;                  \
52 				(vvp)->tv_usec -= ND_NANO_PER_SEC; \
53 			}                                         \
54 		} else {                                          \
55 			if ((vvp)->tv_usec >= ND_MICRO_PER_SEC) { \
56 				(vvp)->tv_sec++;                  \
57 				(vvp)->tv_usec -= ND_MICRO_PER_SEC; \
58 			}                                         \
59 		}                                                 \
60 	} while (0)
61 
62 #define netdissect_timevalsub(tvp, uvp, vvp, nano_prec)            \
63 	do {                                                       \
64 		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;     \
65 		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;  \
66 		if ((vvp)->tv_usec < 0) {                          \
67 		    (vvp)->tv_sec--;                               \
68 		    (vvp)->tv_usec += (nano_prec ? ND_NANO_PER_SEC : \
69 				       ND_MICRO_PER_SEC);          \
70 		}                                                  \
71 	} while (0)
72 
73 #endif /* netdissect_timeval_operations_h */
74