libosmocore  0.9.6-16.20170220git32ee5af8.fc35
Osmocom core library
timer_compat.h
Go to the documentation of this file.
1 /*
2  * (C) 2011 Sylvain Munaut <tnt@246tNt.com>
3  * All Rights Reserved
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  */
20 
29 #pragma once
30 
31 
32 /* Convenience macros for operations on timevals.
33  NOTE: `timercmp' does not work for >= or <=. */
34 
35 #ifndef timerisset
36 # define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
37 #endif
38 
39 #ifndef timerclear
40 # define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
41 #endif
42 
43 #ifndef timercmp
44 # define timercmp(a, b, CMP) \
45  (((a)->tv_sec == (b)->tv_sec) ? \
46  ((a)->tv_usec CMP (b)->tv_usec) : \
47  ((a)->tv_sec CMP (b)->tv_sec))
48 #endif
49 
50 #ifndef timeradd
51 # define timeradd(a, b, result) \
52  do { \
53  (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
54  (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
55  if ((result)->tv_usec >= 1000000) \
56  { \
57  ++(result)->tv_sec; \
58  (result)->tv_usec -= 1000000; \
59  } \
60  } while (0)
61 #endif
62 
63 #ifndef timersub
64 # define timersub(a, b, result) \
65  do { \
66  (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
67  (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
68  if ((result)->tv_usec < 0) { \
69  --(result)->tv_sec; \
70  (result)->tv_usec += 1000000; \
71  } \
72  } while (0)
73 #endif
74 
75