chiark / gitweb /
buf: Fix two embarassing bugs found while writing Lisp bindings.
[mLib] / tv.h
CommitLineData
247239be 1/* -*-c-*-
2 *
8656dc50 3 * $Id: tv.h,v 1.7 2004/04/08 01:36:13 mdw Exp $
247239be 4 *
5 * Manipulation of timeval structures
6 *
7 * (c) 1998 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the mLib utilities library.
13 *
14 * mLib is free software; you can redistribute it and/or modify
c846879c 15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
247239be 18 *
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c846879c 22 * GNU Library General Public License for more details.
247239be 23 *
c846879c 24 * You should have received a copy of the GNU Library General Public
0bd98442 25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
247239be 28 */
29
c6e0eaf0 30#ifndef MLIB_TV_H
31#define MLIB_TV_H
247239be 32
33#ifdef __cplusplus
34 extern "C" {
35#endif
36
37/*----- Header files ------------------------------------------------------*/
38
39#include <sys/time.h>
40
9239a724 41/*----- A macro to make reading easier ------------------------------------*/
42
43#define MILLION 1000000
44
247239be 45/*----- Functions provided ------------------------------------------------*/
46
47/* --- @tv_add@ --- *
48 *
49 * Arguments: @struct timeval *dst@ = destination block
50 * @const struct timeval *a, *b@ = source blocks
51 *
52 * Returns: ---
53 *
54 * Use: Adds two timevals.
55 */
56
57extern void tv_add(struct timeval */*dst*/,
58 const struct timeval */*a*/,
59 const struct timeval */*b*/);
60
9239a724 61#define TV_ADD(dst, a, b) TV_ADDL(dst, a, (b)->tv_sec, (b)->tv_usec)
62
76a6457a 63/* --- @tv_addl@ --- *
64 *
65 * Arguments: @struct timeval *dst@ = destination block
66 * @const struct timeval *a@ = source blocks
67 * @time_t sec@, @unsigned long usec@ = time to add
68 *
69 * Returns: ---
70 *
71 * Use: Adds a literal time in seconds and microseconds.
72 */
73
74extern void tv_addl(struct timeval */*dst*/,
75 const struct timeval */*a*/,
76 time_t /*sec*/, unsigned long /*usec*/);
77
9239a724 78#define TV_ADDL(dst, a, sec, usec) do { \
79 (dst)->tv_sec = (a)->tv_sec + (sec); \
80 (dst)->tv_usec = (a)->tv_usec + (usec); \
81 if ((dst)->tv_usec >= MILLION) { \
82 (dst)->tv_usec -= MILLION; \
83 (dst)->tv_sec++; \
84 } \
85} while (0)
86
247239be 87/* --- @tv_sub@ --- *
88 *
89 * Arguments: @struct timeval *dst@ = destination block
90 * @const struct timeval *a, *b@ = source blocks
91 *
92 * Returns: ---
93 *
94 * Use: Subtracts two timevals.
95 */
96
97extern void tv_sub(struct timeval */*dst*/,
98 const struct timeval */*a*/,
99 const struct timeval */*b*/);
100
9239a724 101#define TV_SUB(dst, a, b) TV_SUBL(dst, a, (b)->tv_sec, (b)->tv_usec)
102
103/* --- @tv_subl@ --- *
104 *
105 * Arguments: @struct timeval *dst@ = destination block
106 * @const struct timeval *a@ = source blocks
107 * @time_t sec@, @unsigned long usec@ = time to subtract
108 *
109 * Returns: ---
110 *
111 * Use: Subtracts a literal time in seconds and microseconds.
112 */
113
114extern void tv_subl(struct timeval */*dst*/,
115 const struct timeval */*a*/,
116 time_t /*sec*/, unsigned long /*usec*/);
117
118#define TV_SUBL(dst, a, sec, usec) do { \
119 (dst)->tv_sec = (a)->tv_sec - (sec); \
120 if ((a)->tv_usec >= (usec)) \
121 (dst)->tv_usec = (a)->tv_usec - (usec); \
122 else { \
123 (dst)->tv_usec = (a)->tv_usec + MILLION - (usec); \
124 (dst)->tv_sec--; \
125 } \
126} while (0)
127
247239be 128/* --- @tv_cmp@ --- *
129 *
130 * Arguments: @const struct timeval *a, *b@ = source blocks
131 *
132 * Returns: Less than, equal to, or greater than zero.
133 *
134 * Use: Compares two timevals.
135 */
136
137extern int tv_cmp(const struct timeval */*a*/,
138 const struct timeval */*b*/);
139
9239a724 140#define TV_CMP(a, op, b) ((a)->tv_sec == (b)->tv_sec ? \
141 (a)->tv_usec op (b)->tv_usec : \
142 (a)->tv_sec op (b)->tv_sec)
143
247239be 144/*----- That's all, folks -------------------------------------------------*/
145
146#ifdef __cplusplus
147 }
148#endif
149
150#endif