[MEDIUM] many cleanups in the time functions
Now, functions whose name begins with '__tv_' are inlined. Also,
'tv_ms' is used as a prefix for functions using milliseconds.
diff --git a/src/time.c b/src/time.c
index c146504..262bc44 100644
--- a/src/time.c
+++ b/src/time.c
@@ -1,7 +1,7 @@
/*
* Time calculation functions.
*
- * Copyright 2000-2006 Willy Tarreau <w@1wt.eu>
+ * Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@@ -22,12 +22,10 @@
/*
* adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
*/
-REGPRM3 struct timeval *tv_delayfrom(struct timeval *tv, const struct timeval *from, int ms)
+REGPRM3 struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms)
{
- if (!tv || !from)
- return NULL;
- tv->tv_usec = from->tv_usec + (ms%1000)*1000;
- tv->tv_sec = from->tv_sec + (ms/1000);
+ tv->tv_usec = from->tv_usec + (ms % 1000) * 1000;
+ tv->tv_sec = from->tv_sec + (ms / 1000);
while (tv->tv_usec >= 1000000) {
tv->tv_usec -= 1000000;
tv->tv_sec++;
@@ -37,90 +35,41 @@
/*
* compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2
- * Must not be used when either argument is eternity. Use tv_cmp2_ms() for that.
+ * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that.
*/
-REGPRM2 int tv_cmp_ms(const struct timeval *tv1, const struct timeval *tv2)
+REGPRM2 int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2)
{
- if ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) {
- if ((unsigned)tv2->tv_usec >= (unsigned)tv1->tv_usec + 1000)
- return -1;
- else if ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec + 1000)
- return 1;
- else
- return 0;
- }
- else if (((unsigned)tv2->tv_sec > (unsigned)tv1->tv_sec + 1) ||
- (((unsigned)tv2->tv_sec == (unsigned)tv1->tv_sec + 1) &&
- ((unsigned)tv2->tv_usec + 1000000 >= (unsigned)tv1->tv_usec + 1000)))
- return -1;
- else if (((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec + 1) ||
- (((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec + 1) &&
- ((unsigned)tv1->tv_usec + 1000000 >= (unsigned)tv2->tv_usec + 1000)))
- return 1;
- else
- return 0;
+ return __tv_ms_cmp(tv1, tv2);
}
/*
* compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
* assuming that TV_ETERNITY is greater than everything.
*/
-REGPRM2 int tv_cmp2_ms(const struct timeval *tv1, const struct timeval *tv2)
+REGPRM2 int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2)
{
- if (tv_iseternity(tv1))
- if (tv_iseternity(tv2))
- return 0; /* same */
- else
- return 1; /* tv1 later than tv2 */
- else if (tv_iseternity(tv2))
- return -1; /* tv2 later than tv1 */
-
- if ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) {
- if ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec + 1000)
- return 1;
- else if ((unsigned)tv2->tv_usec >= (unsigned)tv1->tv_usec + 1000)
- return -1;
- else
- return 0;
- }
- else if (((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec + 1) ||
- (((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec + 1) &&
- ((unsigned)tv1->tv_usec + 1000000 >= (unsigned)tv2->tv_usec + 1000)))
- return 1;
- else if (((unsigned)tv2->tv_sec > (unsigned)tv1->tv_sec + 1) ||
- (((unsigned)tv2->tv_sec == (unsigned)tv1->tv_sec + 1) &&
- ((unsigned)tv2->tv_usec + 1000000 >= (unsigned)tv1->tv_usec + 1000)))
- return -1;
- else
- return 0;
+ return __tv_ms_cmp2(tv1, tv2);
}
/*
* compares <tv1> and <tv2> modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2,
* assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is
* TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace
- * occurrences of (tv_cmp2_ms(tv,now) <= 0).
+ * occurrences of (tv_ms_cmp2(tv,now) <= 0).
*/
-REGPRM2 int tv_cmp2_le(const struct timeval *tv1, const struct timeval *tv2)
+REGPRM2 int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2)
{
- if (likely((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec + 1))
- return 0;
-
- if (likely((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec))
- return 1;
-
- if (likely((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec)) {
- if ((unsigned)tv2->tv_usec >= (unsigned)tv1->tv_usec + 1000)
- return 1;
- else
- return 0;
- }
+ return __tv_ms_le2(tv1, tv2);
+}
- if (unlikely(((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec + 1) &&
- ((unsigned)tv1->tv_usec + 1000000 >= (unsigned)tv2->tv_usec + 1000)))
- return 0;
- else
- return 1;
+/*
+ * returns the remaining time between tv1=now and event=tv2
+ * if tv2 is passed, 0 is returned.
+ * Must not be used when either argument is eternity.
+ */
+REGPRM2 unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2)
+{
+ return __tv_ms_remain(tv1, tv2);
}
/*
@@ -128,49 +77,21 @@
* if tv2 is passed, 0 is returned.
* Returns TIME_ETERNITY if tv2 is eternity.
*/
-REGPRM2 unsigned long tv_remain2(const struct timeval *tv1, const struct timeval *tv2)
+REGPRM2 unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2)
{
- unsigned long ret;
-
if (tv_iseternity(tv2))
return TIME_ETERNITY;
- if (tv_cmp_ms(tv1, tv2) >= 0)
- return 0; /* event elapsed */
-
- ret = (tv2->tv_sec - tv1->tv_sec) * 1000;
- if ((unsigned)tv2->tv_usec > (unsigned)tv1->tv_usec)
- ret += (tv2->tv_usec - tv1->tv_usec) / 1000;
- else
- ret -= (tv1->tv_usec - tv2->tv_usec) / 1000;
- return (unsigned long) ret;
+ return __tv_ms_remain(tv1, tv2);
}
-
/*
- * returns the absolute difference, in ms, between tv1 and tv2
+ * Returns the time in ms elapsed between tv1 and tv2, assuming that tv1<=tv2.
* Must not be used when either argument is eternity.
*/
-REGPRM2 unsigned long tv_delta(const struct timeval *tv1, const struct timeval *tv2)
+REGPRM2 unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2)
{
- int cmp;
- unsigned long ret;
-
-
- cmp = tv_cmp(tv1, tv2);
- if (!cmp)
- return 0; /* same dates, null diff */
- else if (cmp < 0) {
- const struct timeval *tmp = tv1;
- tv1 = tv2;
- tv2 = tmp;
- }
- ret = (tv1->tv_sec - tv2->tv_sec) * 1000;
- if ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec)
- ret += (tv1->tv_usec - tv2->tv_usec) / 1000;
- else
- ret -= (tv2->tv_usec - tv1->tv_usec) / 1000;
- return (unsigned long) ret;
+ return __tv_ms_elapsed(tv1, tv2);
}
/*