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.
This commit is contained in:
Ondřej Surý
2022-03-14 13:00:05 -07:00
committed by Evan Hunt
parent e6ca2a651f
commit 79b5ccbf34
6 changed files with 33 additions and 88 deletions
+1 -6
View File
@@ -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,
+1 -11
View File
@@ -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,
+1
View File
@@ -31,6 +31,7 @@
#include <stdbool.h>
#include <isc/lang.h>
#include <isc/time.h>
#include <isc/types.h>
ISC_LANG_BEGINDECLS
+20 -25
View File
@@ -21,24 +21,6 @@
#include <isc/lang.h>
#include <isc/types.h>
/***
*** 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);
/*%<
-1
View File
@@ -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 */
+10 -45
View File
@@ -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;