diff options
author | Nick Mathewson <nickm@torproject.org> | 2011-03-30 14:29:57 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2011-03-30 14:41:41 -0400 |
commit | 5eaba5ac2128eebf095441e23b6b7516ce35dd5d (patch) | |
tree | 9cffc73c49ca17508f85a06ced54853386ecc93f /src/common/compat.h | |
parent | 22f7042b9184d4463429072d788d52f4fc75e866 (diff) | |
download | tor-5eaba5ac2128eebf095441e23b6b7516ce35dd5d.tar.gz tor-5eaba5ac2128eebf095441e23b6b7516ce35dd5d.zip |
Implement replacements for timer(add,cmp,sub) on platforms lacking them.
Diffstat (limited to 'src/common/compat.h')
-rw-r--r-- | src/common/compat.h | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/common/compat.h b/src/common/compat.h index 35829e18b4..550c08b533 100644 --- a/src/common/compat.h +++ b/src/common/compat.h @@ -333,6 +333,48 @@ struct tm *tor_localtime_r(const time_t *timep, struct tm *result); struct tm *tor_gmtime_r(const time_t *timep, struct tm *result); #endif +#ifndef timeradd +/** Replacement for timeradd on platforms that do not have it: sets tvout to + * the sum of tv1 and tv2. */ +#define timeradd(tv1,tv2,tvout) \ + do { \ + (tvout)->tv_sec = (tv1)->tv_sec + (tv2)->tv_sec; \ + (tvout)->tv_usec = (tv2)->tv_usec + (tv2)->tv_usec; \ + if ((tvout)->tv_usec >= 1000000) { \ + (tvout)->tv_usec -= 1000000; \ + (tvout)->tv_sec++; \ + } \ + } while (0) +#endif + +#ifndef timersub +/** Replacement for timersub on platforms that do not have it: sets tvout to + * tv1 minus tv2. */ +#define timersub(tv1,tv2,tvout) \ + do { \ + (tvout)->tv_sec = (tv1)->tv_sec - (tv2)->tv_sec; \ + (tvout)->tv_usec = (tv2)->tv_usec - (tv2)->tv_usec; \ + if ((tvout)->tv_usec < 0) { \ + (tvout)->tv_usec += 1000000; \ + (tvout)->tv_sec--; \ + } \ + } while (0) +#endif + +#ifndef timercmp +/** Replacement for timersub on platforms that do not have it: returns true + * iff the relational operator "op" makes the expression tv1 op tv2 true. + * + * Note that while this definition should work for all boolean opeators, some + * platforms' native timercmp definitions do not support >=, <=, or ==. So + * don't use those. + */ +#define timercmp(tv1,tv2,op) \ + (((tv1)->tv_sec == (tv2)->tv_sec) ? \ + ((tv1)->tv_usec op (tv2)->tv_usec) : \ + ((tv1)->tv_sec op (tv2)->tv_sec)) +#endif + /* ===== File compatibility */ int replace_file(const char *from, const char *to); int touch_file(const char *fname); |