[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/include/common/time.h b/include/common/time.h
index 3d81759..770974c 100644
--- a/include/common/time.h
+++ b/include/common/time.h
@@ -2,7 +2,7 @@
   include/common/time.h
   Time calculation functions and macros.
 
-  Copyright (C) 2000-2006 Willy Tarreau - w@1wt.eu
+  Copyright (C) 2000-2007 Willy Tarreau - w@1wt.eu
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -25,6 +25,7 @@
 #include <stdlib.h>
 #include <sys/time.h>
 #include <common/config.h>
+#include <common/standard.h>
 
 /* eternity when exprimed in timeval */
 #ifndef TV_ETERNITY
@@ -47,50 +48,74 @@
 extern struct timeval start_date;       /* the process's start date */
 
 
+/**** exported functions *************************************************/
+
+
 /*
  * 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);
 
 /*
  * 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);
 
 /*
  * 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);
 
-/*
- * 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).
- */
-REGPRM2 int tv_cmp2_le(const struct timeval *tv1, const struct timeval *tv2);
+/**** general purpose functions and macros *******************************/
+
+
+/* tv_now: sets <tv> to the current time */
+REGPRM1 static inline struct timeval *tv_now(struct timeval *tv)
+{
+	gettimeofday(tv, NULL);
+	return tv;
+}
 
 /*
- * returns the remaining time between tv1=now and event=tv2
- * if tv2 is passed, 0 is returned.
- * Returns TIME_ETERNITY if tv2 is eternity.
+ * sets a struct timeval to its highest value so that it can never happen
+ * note that only tv_usec is necessary to detect it since a tv_usec > 999999
+ * is normally not possible.
+ *
  */
-REGPRM2 unsigned long tv_remain2(const struct timeval *tv1, const struct timeval *tv2);
 
-/* sets <tv> to the current time */
-REGPRM1 static inline struct timeval *tv_now(struct timeval *tv)
+REGPRM1 static inline struct timeval *tv_eternity(struct timeval *tv)
 {
-	if (tv)
-		gettimeofday(tv, NULL);
+	tv->tv_sec = tv->tv_usec = TV_ETERNITY;
 	return tv;
 }
 
 /*
- * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2
+ * sets a struct timeval to 0
+ *
  */
-REGPRM2 static inline int tv_cmp(const struct timeval *tv1, const struct timeval *tv2)
+REGPRM1 static inline struct timeval *tv_zero(struct timeval *tv) {
+	tv->tv_sec = tv->tv_usec = 0;
+	return tv;
+}
+
+/*
+ * returns non null if tv is [eternity], otherwise 0.
+ */
+#define tv_iseternity(tv)       ((tv)->tv_usec == TV_ETERNITY)
+
+/*
+ * returns non null if tv is [0], otherwise 0.
+ */
+#define tv_iszero(tv)           (((tv)->tv_sec | (tv)->tv_usec) == 0)
+
+
+/**** comparision functions and macros ***********************************/
+
+
+/* tv_cmp: compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2. */
+REGPRM2 static inline int __tv_cmp(const struct timeval *tv1, const struct timeval *tv2)
 {
 	if ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec)
 		return -1;
@@ -104,34 +129,142 @@
 		return 0;
 }
 
+/* tv_iseq: compares <tv1> and <tv2> : returns 1 if tv1 == tv2, otherwise 0 */
+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);
+}
+
+/* tv_isgt: compares <tv1> and <tv2> : returns 1 if tv1 > tv2, otherwise 0 */
+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);
+}
+
+/* tv_isge: compares <tv1> and <tv2> : returns 1 if tv1 >= tv2, otherwise 0 */
+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);
+}
+
+/* tv_islt: compares <tv1> and <tv2> : returns 1 if tv1 < tv2, otherwise 0 */
+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);
+}
+
+/* tv_isle: compares <tv1> and <tv2> : returns 1 if tv1 <= tv2, otherwise 0 */
+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);
+}
+
 /*
- * compares <tv1> and <tv2> : returns 0 if tv1 < tv2, 1 if tv1 >= tv2
+ * 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_ms_cmp2() for that.
  */
-REGPRM2 static inline int tv_cmp_ge(const struct timeval *tv1, const struct timeval *tv2)
+#define tv_ms_cmp _tv_ms_cmp
+REGPRM2 int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2);
+REGPRM2 static inline int __tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2)
 {
-	if ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec)
+	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;
-	if ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec)
+	else
 		return 0;
-	if ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec)
+}
+
+/*
+ * 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.
+ */
+#define tv_ms_cmp2 _tv_ms_cmp2
+REGPRM2 int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2);
+REGPRM2 static inline 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 */
+	return tv_ms_cmp(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_ms_cmp2(tv,now) <= 0).
+ */
+#define tv_ms_le2 _tv_ms_le2
+REGPRM2 int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2);
+REGPRM2 static inline 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;
+	}
+
+	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;
-	return 0;
 }
 
+
+/**** operators **********************************************************/
+
+
 /*
- * returns the 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 static inline unsigned long tv_diff(const struct timeval *tv1, const struct timeval *tv2)
+#define tv_ms_elapsed __tv_ms_elapsed
+REGPRM2 unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2);
+REGPRM2 static inline unsigned long __tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2)
 {
 	unsigned long ret;
-  
-	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;
+
+	ret  = ((signed long)(tv2->tv_sec  - tv1->tv_sec))  * 1000;
+	ret += ((signed long)(tv2->tv_usec - tv1->tv_usec)) / 1000;
+	return ret;
 }
 
 /*
@@ -139,53 +272,48 @@
  * if tv2 is passed, 0 is returned.
  * Must not be used when either argument is eternity.
  */
-REGPRM2 static inline unsigned long tv_remain(const struct timeval *tv1, const struct timeval *tv2)
+
+#define tv_ms_remain __tv_ms_remain
+REGPRM2 unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2);
+REGPRM2 static inline unsigned long __tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2)
 {
-	unsigned long ret;
-  
-	if (tv_cmp_ms(tv1, tv2) >= 0)
+	if (tv_ms_cmp(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_elapsed(tv1, tv2);
 }
 
-
 /*
- * sets a struct timeval to its highest value so that it can never happen
- * note that only tv_usec is necessary to detect it since a tv_usec > 999999
- * is normally not possible.
- *
+ * returns the remaining time between tv1=now and event=tv2
+ * if tv2 is passed, 0 is returned.
+ * Returns TIME_ETERNITY if tv2 is eternity.
  */
-
-REGPRM1 static inline struct timeval *tv_eternity(struct timeval *tv)
+#define tv_ms_remain2 _tv_ms_remain2
+REGPRM2 unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2);
+REGPRM2 static inline unsigned long __tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2)
 {
-	tv->tv_sec = tv->tv_usec = TV_ETERNITY;
-	return tv;
+	if (tv_iseternity(tv2))
+		return TIME_ETERNITY;
+
+	return tv_ms_remain(tv1, tv2);
 }
 
 /*
- * sets a struct timeval to 0
- *
+ * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
  */
-REGPRM1 static inline struct timeval *tv_zero(struct timeval *tv) {
-	tv->tv_sec = tv->tv_usec = 0;
+#define tv_ms_add _tv_ms_add
+REGPRM3 struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms);
+REGPRM3 static inline struct timeval *__tv_ms_add(struct timeval *tv, const struct timeval *from, int ms)
+{
+	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++;
+	}
 	return tv;
 }
 
-/*
- * returns non null if tv is [eternity], otherwise 0.
- */
-#define tv_iseternity(tv)       ((tv)->tv_usec == TV_ETERNITY)
-
-/*
- * returns non null if tv is [0], otherwise 0.
- */
-#define tv_iszero(tv)           (((tv)->tv_sec | (tv)->tv_usec) == 0)
 
 /*
  * compares <tv1> and <tv2> : returns 1 if <tv1> is before <tv2>, otherwise 0.