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 #ifndef _MICRO_PER_SEC 34 #define _MICRO_PER_SEC 1000000 35 #endif 36 37 #ifndef _NANO_PER_SEC 38 #define _NANO_PER_SEC 1000000000 39 #endif 40 41 #define netdissect_timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0) 42 43 #define netdissect_timevalisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) 44 45 #define netdissect_timevalcmp(tvp, uvp, cmp) \ 46 (((tvp)->tv_sec == (uvp)->tv_sec) ? \ 47 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \ 48 ((tvp)->tv_sec cmp (uvp)->tv_sec)) 49 50 #define netdissect_timevaladd(tvp, uvp, vvp, nano_prec) \ 51 do { \ 52 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \ 53 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \ 54 if (nano_prec) { \ 55 if ((vvp)->tv_usec >= _NANO_PER_SEC) { \ 56 (vvp)->tv_sec++; \ 57 (vvp)->tv_usec -= _NANO_PER_SEC; \ 58 } \ 59 } else { \ 60 if ((vvp)->tv_usec >= _MICRO_PER_SEC) { \ 61 (vvp)->tv_sec++; \ 62 (vvp)->tv_usec -= _MICRO_PER_SEC; \ 63 } \ 64 } \ 65 } while (0) 66 67 #define netdissect_timevalsub(tvp, uvp, vvp, nano_prec) \ 68 do { \ 69 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ 70 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ 71 if ((vvp)->tv_usec < 0) { \ 72 (vvp)->tv_sec--; \ 73 (vvp)->tv_usec += (nano_prec ? _NANO_PER_SEC : \ 74 _MICRO_PER_SEC); \ 75 } \ 76 } while (0) 77 78 #endif /* netdissect_timeval_operations_h */ 79