From: Willy Tarreau Date: Sun, 13 May 2007 14:03:27 +0000 (+0200) Subject: [MINOR] add new tv_* functions X-Git-Tag: v1.3.11~5^2~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0481c20e66011edb74625fad9b3c91cb8d8adfa6;p=thirdparty%2Fhaproxy.git [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 33a62f8412..001d63e954 100644 --- a/include/common/time.h +++ b/include/common/time.h @@ -157,46 +157,53 @@ REGPRM2 static inline int __tv_cmp(const struct timeval *tv1, const struct timev } /* tv_iseq: compares and : 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 and : 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 and : 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 and : 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 and : 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 @@ REGPRM2 static inline unsigned long __tv_ms_remain2(const struct timeval *tv1, c /* * adds to , set the result to and returns a pointer */ -#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) { @@ -342,6 +349,25 @@ REGPRM3 static inline struct timeval *__tv_add(struct timeval *tv, const struct } +/* + * If is set, then add it to and set the result to , 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 to and returns a pointer */ diff --git a/src/time.c b/src/time.c index cf3d5cff69..b80dca9f49 100644 --- a/src/time.c +++ b/src/time.c @@ -102,6 +102,15 @@ REGPRM3 struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, return __tv_add(tv, from, inc); } +/* + * If is set, then add it to and set the result to , 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 @@ REGPRM3 struct timeval *_tv_remain2(const struct timeval *tv1, const struct time return __tv_remain2(tv1, tv2, tv); } +/* tv_isle: compares and : 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 and : 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