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