From: Ondřej Surý Date: Sun, 13 Mar 2022 11:13:11 +0000 (+0100) Subject: Implement isc_interval_t on top of isc_time_t X-Git-Tag: v9.19.0~64^2~1 X-Git-Url: http://git.ipfire.org/gitweb/?a=commitdiff_plain;h=79b5ccbf34cffcc7d89b9c978b383f5000f0e4ad;p=thirdparty%2Fbind9.git Implement isc_interval_t on top of isc_time_t Change the isc_interval_t implementation from separate data type and separate implementation to be shim implementation on top of isc_time_t. The distinction between isc_interval_t and isc_time_t has been kept because they are semantically different - isc_interval_t is relative and isc_time_t is absolute, but this allows isc_time_t and isc_interval_t to be freely interchangeable, f.e. this: isc_time_t *t1; isc_interval_t *interval; isc_time_t *t2; isc_interval_set(interval, isc_time_seconds(t2), isc_time_nanoseconds(t2);; isc_time_subtract(t1, interval, t2); isc_interval_set(interval, isc_time_seconds(t2), isc_time_nanoseconds(t2)); to just: isc_time_t *t1; isc_interval_t *interval; isc_time_t *t2; isc_time_subtract(t1, t2, interval); without introducing a whole set of new functions. --- diff --git a/lib/dns/resolver.c b/lib/dns/resolver.c index 689d6d953ff..96e8a0cf992 100644 --- a/lib/dns/resolver.c +++ b/lib/dns/resolver.c @@ -1265,12 +1265,7 @@ fctx_starttimer(fetchctx_t *fctx) { if (isc_time_compare(&expires, &now) <= 0) { isc_interval_set(&interval, 0, 1); } else { - isc_time_t tmp; - isc_interval_set(&interval, isc_time_seconds(&now), - isc_time_nanoseconds(&now)); - isc_time_subtract(&expires, &interval, &tmp); - isc_interval_set(&interval, isc_time_seconds(&tmp), - isc_time_nanoseconds(&tmp)); + isc_time_subtract(&expires, &now, &interval); } return (isc_timer_reset(fctx->timer, isc_timertype_once, &interval, diff --git a/lib/dns/zone.c b/lib/dns/zone.c index 92f66ff5f31..3cf03209703 100644 --- a/lib/dns/zone.c +++ b/lib/dns/zone.c @@ -15283,17 +15283,7 @@ zone_settimer(dns_zone_t *zone, isc_time_t *now) { if (isc_time_compare(&next, now) <= 0) { isc_interval_set(&interval, 0, 1); } else { - /* - * In theory, we could just type isc_interval_t to - * isc_time_t and back, but there's no such guarantee, - * so a safer method is being used here. - */ - isc_time_t tmp; - isc_interval_set(&interval, isc_time_seconds(now), - isc_time_nanoseconds(now)); - isc_time_subtract(&next, &interval, &tmp); - isc_interval_set(&interval, isc_time_seconds(&tmp), - isc_time_nanoseconds(&tmp)); + isc_time_subtract(&next, now, &interval); } result = isc_timer_reset(zone->timer, isc_timertype_once, diff --git a/lib/isc/include/isc/ratelimiter.h b/lib/isc/include/isc/ratelimiter.h index 45ec4470f15..828665418c2 100644 --- a/lib/isc/include/isc/ratelimiter.h +++ b/lib/isc/include/isc/ratelimiter.h @@ -31,6 +31,7 @@ #include #include +#include #include ISC_LANG_BEGINDECLS diff --git a/lib/isc/include/isc/time.h b/lib/isc/include/isc/time.h index 8416927f020..5dff7f8b60f 100644 --- a/lib/isc/include/isc/time.h +++ b/lib/isc/include/isc/time.h @@ -21,24 +21,6 @@ #include #include -/*** - *** Intervals - ***/ - -/*! - * \brief - * The contents of this structure are private, and MUST NOT be accessed - * directly by callers. - * - * The contents are exposed only to allow callers to avoid dynamic allocation. - */ -struct isc_interval { - unsigned int seconds; - unsigned int nanoseconds; -}; - -extern const isc_interval_t *const isc_interval_zero; - /* * ISC_FORMATHTTPTIMESTAMP_SIZE needs to be 30 in C locale and potentially * more for other locales to handle longer national abbreviations when @@ -46,11 +28,16 @@ extern const isc_interval_t *const isc_interval_zero; */ #define ISC_FORMATHTTPTIMESTAMP_SIZE 50 +/* + * Semantic shims to distinguish between relative and absolute time + */ +#define isc_interval_zero isc_time_epoch +#define isc_interval_t isc_time_t + ISC_LANG_BEGINDECLS -void -isc_interval_set(isc_interval_t *i, unsigned int seconds, - unsigned int nanoseconds); +#define isc_interval_set(i, seconds, nanoseconds) \ + isc_time_set((isc_time_t *)i, seconds, nanoseconds) /*%< * Set 'i' to a value representing an interval of 'seconds' seconds and * 'nanoseconds' nanoseconds, suitable for use in isc_time_add() and @@ -62,8 +49,7 @@ isc_interval_set(isc_interval_t *i, unsigned int seconds, *\li nanoseconds < 1000000000. */ -bool -isc_interval_iszero(const isc_interval_t *i); +#define isc_interval_iszero(i) isc_time_isepoch((const isc_time_t *)i) /*%< * Returns true iff. 'i' is the zero interval. * @@ -72,8 +58,7 @@ isc_interval_iszero(const isc_interval_t *i); *\li 'i' is a valid pointer. */ -unsigned int -isc_interval_ms(const isc_interval_t *i); +#define isc_interval_ms(i) isc_time_miliseconds((const isc_time_t *)i) /*%< * Returns interval 'i' expressed as a number of milliseconds. * @@ -320,6 +305,16 @@ isc_time_nanoseconds(const isc_time_t *t); *\li The returned value is less than 1*10^9. */ +uint32_t +isc_time_miliseconds(const isc_time_t *t); +/*%< + * Returns time 't' expressed as a number of milliseconds. + * + * Requires: + * + *\li 't' is a valid pointer. + */ + void isc_time_formattimestamp(const isc_time_t *t, char *buf, unsigned int len); /*%< diff --git a/lib/isc/include/isc/types.h b/lib/isc/include/isc/types.h index c18b4f170e8..78d5ce4a737 100644 --- a/lib/isc/include/isc/types.h +++ b/lib/isc/include/isc/types.h @@ -55,7 +55,6 @@ typedef struct isc_httpdurl isc_httpdurl_t; /*%< HTTP URL */ typedef void(isc_httpdondestroy_t)(void *); /*%< Callback on destroying httpd */ typedef struct isc_interface isc_interface_t; /*%< Interface */ typedef struct isc_interfaceiter isc_interfaceiter_t; /*%< Interface Iterator */ -typedef struct isc_interval isc_interval_t; /*%< Interval */ typedef struct isc_lex isc_lex_t; /*%< Lex */ typedef struct isc_log isc_log_t; /*%< Log */ typedef struct isc_logcategory isc_logcategory_t; /*%< Log Category */ diff --git a/lib/isc/time.c b/lib/isc/time.c index 3e3cfd70b84..fa5f8bc3452 100644 --- a/lib/isc/time.c +++ b/lib/isc/time.c @@ -51,49 +51,6 @@ #define CLOCKSOURCE_HIRES CLOCKSOURCE #endif /* #ifndef CLOCKSOURCE_HIRES */ -/*% - *** Intervals - ***/ - -#if !defined(UNIT_TESTING) -static const isc_interval_t zero_interval = { 0, 0 }; -const isc_interval_t *const isc_interval_zero = &zero_interval; -#endif - -void -isc_interval_set(isc_interval_t *i, unsigned int seconds, - unsigned int nanoseconds) { - REQUIRE(i != NULL); - REQUIRE(nanoseconds < NS_PER_S); - - i->seconds = seconds; - i->nanoseconds = nanoseconds; -} - -bool -isc_interval_iszero(const isc_interval_t *i) { - REQUIRE(i != NULL); - INSIST(i->nanoseconds < NS_PER_S); - - if (i->seconds == 0 && i->nanoseconds == 0) { - return (true); - } - - return (false); -} - -unsigned int -isc_interval_ms(const isc_interval_t *i) { - REQUIRE(i != NULL); - INSIST(i->nanoseconds < NS_PER_S); - - return ((i->seconds * MS_PER_S) + (i->nanoseconds / NS_PER_MS)); -} - -/*** - *** Absolute Times - ***/ - #if !defined(UNIT_TESTING) static const isc_time_t epoch = { 0, 0 }; const isc_time_t *const isc_time_epoch = &epoch; @@ -131,11 +88,11 @@ isc_time_isepoch(const isc_time_t *t) { static inline isc_result_t time_now(isc_time_t *t, clockid_t clock) { struct timespec ts; - char strbuf[ISC_STRERRORSIZE]; REQUIRE(t != NULL); if (clock_gettime(clock, &ts) == -1) { + char strbuf[ISC_STRERRORSIZE]; strerror_r(errno, strbuf, sizeof(strbuf)); UNEXPECTED_ERROR(__FILE__, __LINE__, "%s", strbuf); return (ISC_R_UNEXPECTED); @@ -173,13 +130,13 @@ isc_time_now(isc_time_t *t) { isc_result_t isc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i) { struct timespec ts; - char strbuf[ISC_STRERRORSIZE]; REQUIRE(t != NULL); REQUIRE(i != NULL); INSIST(i->nanoseconds < NS_PER_S); if (clock_gettime(CLOCKSOURCE, &ts) == -1) { + char strbuf[ISC_STRERRORSIZE]; strerror_r(errno, strbuf, sizeof(strbuf)); UNEXPECTED_ERROR(__FILE__, __LINE__, "%s", strbuf); return (ISC_R_UNEXPECTED); @@ -373,6 +330,14 @@ isc_time_nanoseconds(const isc_time_t *t) { return ((uint32_t)t->nanoseconds); } +uint32_t +isc_time_miliseconds(const isc_time_t *t) { + REQUIRE(t != NULL); + INSIST(t->nanoseconds < NS_PER_S); + + return ((t->seconds * MS_PER_S) + (t->nanoseconds / NS_PER_MS)); +} + void isc_time_formattimestamp(const isc_time_t *t, char *buf, unsigned int len) { time_t now;