]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Implement isc_interval_t on top of isc_time_t
authorOndřej Surý <ondrej@isc.org>
Sun, 13 Mar 2022 11:13:11 +0000 (12:13 +0100)
committerEvan Hunt <each@isc.org>
Mon, 14 Mar 2022 20:00:05 +0000 (13:00 -0700)
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.

lib/dns/resolver.c
lib/dns/zone.c
lib/isc/include/isc/ratelimiter.h
lib/isc/include/isc/time.h
lib/isc/include/isc/types.h
lib/isc/time.c

index 689d6d953ff89b022c326e336536317d75dcf5ad..96e8a0cf992f9b99a46536f917085bd7e4415811 100644 (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,
index 92f66ff5f3143ee6be6a558526b6fcc4dc18b373..3cf032097031c86349341db58694abd5e74281b3 100644 (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,
index 45ec4470f15bb317cf9f27efa24710d7bc669a46..828665418c2821cf5ec06c00cfe95bd0cafb43dc 100644 (file)
@@ -31,6 +31,7 @@
 #include <stdbool.h>
 
 #include <isc/lang.h>
+#include <isc/time.h>
 #include <isc/types.h>
 
 ISC_LANG_BEGINDECLS
index 8416927f020a8dc1060bbe1088aa2a9288bb9b9a..5dff7f8b60f55ec2c6c82eb0154273a12df3d172 100644 (file)
 #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);
 /*%<
index c18b4f170e8f859e55c3a2c44c8725aa28066256..78d5ce4a7377cbf1f76bf2156e4fe229eb648160 100644 (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 */
index 3e3cfd70b84518af1a82f5d6d28533a9953b3323..fa5f8bc3452df2824b4f3f422246033e3f15f378 100644 (file)
 #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;