[MINOR] add new tv_* functions
The most useful, tv_add_ifset only adds the increment if it is set. It
is designed for use in expiration computation.
diff --git a/include/common/time.h b/include/common/time.h
index 33a62f8..001d63e 100644
--- a/include/common/time.h
+++ b/include/common/time.h
@@ -157,46 +157,53 @@
}
/* tv_iseq: compares <tv1> and <tv2> : returns 1 if tv1 == tv2, otherwise 0 */
+#define tv_iseq __tv_iseq
REGPRM2 static inline int __tv_iseq(const struct timeval *tv1, const struct timeval *tv2)
{
- return ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) &&
- ((unsigned)tv1->tv_usec == (unsigned)tv2->tv_usec);
+ return ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) &&
+ ((unsigned)tv1->tv_usec == (unsigned)tv2->tv_usec);
}
/* tv_isgt: compares <tv1> and <tv2> : returns 1 if tv1 > tv2, otherwise 0 */
+#define tv_isgt _tv_isgt
+REGPRM2 int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2);
REGPRM2 static inline int __tv_isgt(const struct timeval *tv1, const struct timeval *tv2)
{
- return
- ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
- ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec) :
- ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec);
+ return
+ ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
+ ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec) :
+ ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec);
}
/* tv_isge: compares <tv1> and <tv2> : returns 1 if tv1 >= tv2, otherwise 0 */
+#define tv_isge __tv_isge
REGPRM2 static inline int __tv_isge(const struct timeval *tv1, const struct timeval *tv2)
{
- return
- ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
- ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec) :
- ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec);
+ return
+ ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
+ ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec) :
+ ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec);
}
/* tv_islt: compares <tv1> and <tv2> : returns 1 if tv1 < tv2, otherwise 0 */
+#define tv_islt __tv_islt
REGPRM2 static inline int __tv_islt(const struct timeval *tv1, const struct timeval *tv2)
{
- return
- ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
- ((unsigned)tv1->tv_usec < (unsigned)tv2->tv_usec) :
- ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec);
+ return
+ ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
+ ((unsigned)tv1->tv_usec < (unsigned)tv2->tv_usec) :
+ ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec);
}
/* tv_isle: compares <tv1> and <tv2> : returns 1 if tv1 <= tv2, otherwise 0 */
+#define tv_isle _tv_isle
+REGPRM2 int _tv_isle(const struct timeval *tv1, const struct timeval *tv2);
REGPRM2 static inline int __tv_isle(const struct timeval *tv1, const struct timeval *tv2)
{
- return
- ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
- ((unsigned)tv1->tv_usec <= (unsigned)tv2->tv_usec) :
- ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec);
+ return
+ ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
+ ((unsigned)tv1->tv_usec <= (unsigned)tv2->tv_usec) :
+ ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec);
}
/*
@@ -328,7 +335,7 @@
/*
* adds <inc> to <from>, set the result to <tv> and returns a pointer <tv>
*/
-#define tv_add __tv_add
+#define tv_add _tv_add
REGPRM3 struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc);
REGPRM3 static inline struct timeval *__tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
{
@@ -343,6 +350,25 @@
/*
+ * If <inc> is set, then add it to <from> and set the result to <tv>, then
+ * return 1, otherwise return 0. It is meant to be used in if conditions.
+ */
+#define tv_add_ifset _tv_add_ifset
+REGPRM3 int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc);
+REGPRM3 static inline int __tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
+{
+ if (tv_iseternity(inc))
+ return 0;
+ tv->tv_usec = from->tv_usec + inc->tv_usec;
+ tv->tv_sec = from->tv_sec + inc->tv_sec;
+ if (tv->tv_usec >= 1000000) {
+ tv->tv_usec -= 1000000;
+ tv->tv_sec++;
+ }
+ return 1;
+}
+
+/*
* adds <inc> to <tv> and returns a pointer <tv>
*/
REGPRM2 static inline struct timeval *__tv_add2(struct timeval *tv, const struct timeval *inc)
diff --git a/src/time.c b/src/time.c
index cf3d5cf..b80dca9 100644
--- a/src/time.c
+++ b/src/time.c
@@ -103,6 +103,15 @@
}
/*
+ * If <inc> is set, then add it to <from> and set the result to <tv>, then
+ * return 1, otherwise return 0. It is meant to be used in if conditions.
+ */
+REGPRM3 int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
+{
+ return __tv_add_ifset(tv, from, inc);
+}
+
+/*
* Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
* 0 is returned. The result is stored into tv.
*/
@@ -121,6 +130,18 @@
return __tv_remain2(tv1, tv2, tv);
}
+/* tv_isle: compares <tv1> and <tv2> : returns 1 if tv1 <= tv2, otherwise 0 */
+REGPRM2 int _tv_isle(const struct timeval *tv1, const struct timeval *tv2)
+{
+ return __tv_isle(tv1, tv2);
+}
+
+/* tv_isgt: compares <tv1> and <tv2> : returns 1 if tv1 > tv2, otherwise 0 */
+REGPRM2 int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2)
+{
+ return __tv_isgt(tv1, tv2);
+}
+
/*
* Local variables:
* c-indent-level: 8