From e498ed8029b4488eb09436df0716bc12a076c161 Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Wed, 1 Apr 2015 05:32:43 -0400 Subject: Eliminate some duplication code for timespec arithmetic. --- gpsd.h-tail | 78 ------------------------------------------------ gpsd_json.c | 2 +- gpsmon.c | 2 +- ppsthread.c | 48 +----------------------------- timehint.c | 2 +- timespec.h | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ timespec_str.c | 2 +- timespec_str.h | 15 ---------- 8 files changed, 98 insertions(+), 144 deletions(-) create mode 100644 timespec.h delete mode 100644 timespec_str.h diff --git a/gpsd.h-tail b/gpsd.h-tail index a6685196..d01aba33 100644 --- a/gpsd.h-tail +++ b/gpsd.h-tail @@ -844,84 +844,6 @@ extern void ntpshm_link_activate(struct gps_device_t *); #endif /* NTPSHM_ENABLE */ #endif /* NTP_ENABLE */ -/* normalize a timespec - * - * three cases to note - * if tv_sec is positve, then tv_nsec must be positive - * if tv_sec is negative, then tv_nsec must be negative - * if tv_sec is zero, then tv_nsec may be positive or negative. - * - * this only handles the case where two normalized timespecs - * are added or subracted. (e.g. only a one needs to be borrowed/carried - */ -static inline void TS_NORM( struct timespec *ts) -{ - if ( ( 1 <= ts->tv_sec ) || - ( (0 == ts->tv_sec ) && (0 <= ts->tv_nsec ) ) ) { - /* result is positive */ - if ( 1000000000 <= ts->tv_nsec ) { - /* borrow from tv_sec */ - ts->tv_nsec -= 1000000000; - ts->tv_sec++; - } else if ( 0 > (ts)->tv_nsec ) { - /* carry to tv_sec */ - ts->tv_nsec += 1000000000; - ts->tv_sec--; - } - } else { - /* result is negative */ - if ( -1000000000 >= ts->tv_nsec ) { - /* carry to tv_sec */ - ts->tv_nsec += 1000000000; - ts->tv_sec--; - } else if ( 0 < ts->tv_nsec ) { - /* borrow from tv_sec */ - ts->tv_nsec -= 1000000000; - ts->tv_sec++; - } - } -} - -/* normalize a timeval */ -#define TV_NORM(tv) \ - do { \ - if ( 1000000 <= (tv)->tv_usec ) { \ - (tv)->tv_usec -= 1000000; \ - (tv)->tv_sec++; \ - } else if ( 0 > (tv)->tv_usec ) { \ - (tv)->tv_usec += 1000000; \ - (tv)->tv_sec--; \ - } \ - } while (0) - -/* convert timespec to timeval, with rounding */ -#define TSTOTV(tv, ts) \ - do { \ - (tv)->tv_sec = (ts)->tv_sec; \ - (tv)->tv_usec = ((ts)->tv_nsec + 500)/1000; \ - TV_NORM( tv ); \ - } while (0) - -/* convert timeval to timespec */ -#define TVTOTS(ts, tv) \ - do { \ - (ts)->tv_sec = (tv)->tv_sec; \ - (ts)->tv_nsec = (tv)->tv_usec*1000; \ - TS_NORM( ts ); \ - } while (0) - -/* subtract two timespec */ -#define TS_SUB(r, ts1, ts2) \ - do { \ - (r)->tv_sec = (ts1)->tv_sec - (ts2)->tv_sec; \ - (r)->tv_nsec = (ts1)->tv_nsec - (ts2)->tv_nsec; \ - TS_NORM( r ); \ - } while (0) - -/* convert a timespec to a double. - * is tv_sec > 2, then inevitable loss of precision in tv_nsec */ -#define TSTONS(ts) ((double)((ts)->tv_sec + ((ts)->tv_nsec / 1e9))) - extern void errout_reset(struct gpsd_errout_t *errout); extern void gpsd_acquire_reporting_lock(void); diff --git a/gpsd_json.c b/gpsd_json.c index 8bbcb45f..4ea21ad8 100644 --- a/gpsd_json.c +++ b/gpsd_json.c @@ -26,7 +26,7 @@ PERMISSIONS #ifdef SOCKET_EXPORT_ENABLE #include "gps_json.h" -#include "timespec_str.h" +#include "timespec.h" #include "revision.h" /* *INDENT-OFF* */ diff --git a/gpsmon.c b/gpsmon.c index 81d5c90b..506ef9f6 100644 --- a/gpsmon.c +++ b/gpsmon.c @@ -29,7 +29,7 @@ #include "gpsdclient.h" #include "revision.h" #include "strfuncs.h" -#include "timespec_str.h" +#include "timespec.h" #define BUFLEN 2048 diff --git a/ppsthread.c b/ppsthread.c index 16b468a8..3425dcda 100644 --- a/ppsthread.c +++ b/ppsthread.c @@ -75,7 +75,7 @@ #include #endif -#include "timespec_str.h" +#include "timespec.h" #include "ppsthread.h" /* @@ -130,52 +130,6 @@ static int get_edge_rfc2783(struct inner_context_t *, volatile struct timedelta_t *); #endif /* defined(HAVE_SYS_TIMEPPS_H) */ -/* normalize a timespec - * - * three cases to note - * if tv_sec is positve, then tv_nsec must be positive - * if tv_sec is negative, then tv_nsec must be negative - * if tv_sec is zero, then tv_nsec may be positive or negative. - * - * this only handles the case where two normalized timespecs - * are added or subracted. (e.g. only a one needs to be borrowed/carried - */ -static inline void TS_NORM( struct timespec *ts) -{ - if ( ( 1 <= ts->tv_sec ) || - ( (0 == ts->tv_sec ) && (0 <= ts->tv_nsec ) ) ) { - /* result is positive */ - if ( 1000000000 <= ts->tv_nsec ) { - /* borrow from tv_sec */ - ts->tv_nsec -= 1000000000; - ts->tv_sec++; - } else if ( 0 > (ts)->tv_nsec ) { - /* carry to tv_sec */ - ts->tv_nsec += 1000000000; - ts->tv_sec--; - } - } else { - /* result is negative */ - if ( -1000000000 >= ts->tv_nsec ) { - /* carry to tv_sec */ - ts->tv_nsec += 1000000000; - ts->tv_sec--; - } else if ( 0 < ts->tv_nsec ) { - /* borrow from tv_sec */ - ts->tv_nsec -= 1000000000; - ts->tv_sec++; - } - } -} - -/* subtract two timespec */ -#define TS_SUB(r, ts1, ts2) \ - do { \ - (r)->tv_sec = (ts1)->tv_sec - (ts2)->tv_sec; \ - (r)->tv_nsec = (ts1)->tv_nsec - (ts2)->tv_nsec; \ - TS_NORM( r ); \ - } while (0) - static pthread_mutex_t ppslast_mutex = PTHREAD_MUTEX_INITIALIZER; static void thread_lock(volatile struct pps_thread_t *pps_thread) diff --git a/timehint.c b/timehint.c index c3e2afef..c33e3fdf 100644 --- a/timehint.c +++ b/timehint.c @@ -19,7 +19,7 @@ #include #include -#include "timespec_str.h" +#include "timespec.h" #include "gpsd.h" #ifdef NTPSHM_ENABLE diff --git a/timespec.h b/timespec.h new file mode 100644 index 00000000..cb4801d8 --- /dev/null +++ b/timespec.h @@ -0,0 +1,93 @@ +/* + * This file is Copyright (c) 2015 by the GPSD project + * BSD terms apply: see the file COPYING in the distribution root for details. + */ + +#ifndef GPSD_TIMESPEC_H +#define GPSD_TIMESPEC_H + +/* normalize a timespec + * + * three cases to note + * if tv_sec is positve, then tv_nsec must be positive + * if tv_sec is negative, then tv_nsec must be negative + * if tv_sec is zero, then tv_nsec may be positive or negative. + * + * this only handles the case where two normalized timespecs + * are added or subracted. (e.g. only a one needs to be borrowed/carried + */ +static inline void TS_NORM( struct timespec *ts) +{ + if ( ( 1 <= ts->tv_sec ) || + ( (0 == ts->tv_sec ) && (0 <= ts->tv_nsec ) ) ) { + /* result is positive */ + if ( 1000000000 <= ts->tv_nsec ) { + /* borrow from tv_sec */ + ts->tv_nsec -= 1000000000; + ts->tv_sec++; + } else if ( 0 > (ts)->tv_nsec ) { + /* carry to tv_sec */ + ts->tv_nsec += 1000000000; + ts->tv_sec--; + } + } else { + /* result is negative */ + if ( -1000000000 >= ts->tv_nsec ) { + /* carry to tv_sec */ + ts->tv_nsec += 1000000000; + ts->tv_sec--; + } else if ( 0 < ts->tv_nsec ) { + /* borrow from tv_sec */ + ts->tv_nsec -= 1000000000; + ts->tv_sec++; + } + } +} + +/* normalize a timeval */ +#define TV_NORM(tv) \ + do { \ + if ( 1000000 <= (tv)->tv_usec ) { \ + (tv)->tv_usec -= 1000000; \ + (tv)->tv_sec++; \ + } else if ( 0 > (tv)->tv_usec ) { \ + (tv)->tv_usec += 1000000; \ + (tv)->tv_sec--; \ + } \ + } while (0) + +/* convert timespec to timeval, with rounding */ +#define TSTOTV(tv, ts) \ + do { \ + (tv)->tv_sec = (ts)->tv_sec; \ + (tv)->tv_usec = ((ts)->tv_nsec + 500)/1000; \ + TV_NORM( tv ); \ + } while (0) + +/* convert timeval to timespec */ +#define TVTOTS(ts, tv) \ + do { \ + (ts)->tv_sec = (tv)->tv_sec; \ + (ts)->tv_nsec = (tv)->tv_usec*1000; \ + TS_NORM( ts ); \ + } while (0) + +/* subtract two timespec */ +#define TS_SUB(r, ts1, ts2) \ + do { \ + (r)->tv_sec = (ts1)->tv_sec - (ts2)->tv_sec; \ + (r)->tv_nsec = (ts1)->tv_nsec - (ts2)->tv_nsec; \ + TS_NORM( r ); \ + } while (0) + +/* convert a timespec to a double. + * is tv_sec > 2, then inevitable loss of precision in tv_nsec */ +#define TSTONS(ts) ((double)((ts)->tv_sec + ((ts)->tv_nsec / 1e9))) + +#define TIMESPEC_LEN 22 /* required length of a timespec buffer */ + +extern void timespec_str(const struct timespec *, char *, size_t); + +#endif /* GPSD_TIMESPEC_H */ + +/* end */ diff --git a/timespec_str.c b/timespec_str.c index 5af8a825..4f2c3e98 100644 --- a/timespec_str.c +++ b/timespec_str.c @@ -19,7 +19,7 @@ #include #include -#include "timespec_str.h" +#include "timespec.h" /* Convert a normalized timespec to a nice string * put in it *buf, buf should be at least 22 bytes diff --git a/timespec_str.h b/timespec_str.h deleted file mode 100644 index 501c01f8..00000000 --- a/timespec_str.h +++ /dev/null @@ -1,15 +0,0 @@ -/* - * This file is Copyright (c) 2015 by the GPSD project - * BSD terms apply: see the file COPYING in the distribution root for details. - */ - -#ifndef GPSD_TIMESPEC_H -#define GPSD_TIMESPEC_H - -#define TIMESPEC_LEN 22 /* required length of a timespec buffer */ - -extern void timespec_str(const struct timespec *, char *, size_t); - -#endif /* GPSD_TIMESPEC_H */ - -/* end */ -- cgit v1.2.1