From: Guus Sliepen Date: Thu, 23 Apr 2020 22:31:26 +0000 (+0200) Subject: Move timespec functions to utils.h. X-Git-Url: http://git.meshlink.io/?p=meshlink;a=commitdiff_plain;h=a582852c2b7e5a0c0d438d625a0445b4a74f607a Move timespec functions to utils.h. --- diff --git a/src/event.c b/src/event.c index f8ebe8b8..857aaec2 100644 --- a/src/event.c +++ b/src/event.c @@ -36,36 +36,6 @@ #endif #endif -static void timespec_add(const struct timespec *a, const struct timespec *b, struct timespec *r) { - r->tv_sec = a->tv_sec + b->tv_sec; - r->tv_nsec = a->tv_nsec + b->tv_nsec; - - if(r->tv_nsec > 1000000000) { - r->tv_sec++, r->tv_nsec -= 1000000000; - } -} - -static void timespec_sub(const struct timespec *a, const struct timespec *b, struct timespec *r) { - r->tv_sec = a->tv_sec - b->tv_sec; - r->tv_nsec = a->tv_nsec - b->tv_nsec; - - if(r->tv_nsec < 0) { - r->tv_sec--, r->tv_nsec += 1000000000; - } -} - -static bool timespec_lt(const struct timespec *a, const struct timespec *b) { - if(a->tv_sec == b->tv_sec) { - return a->tv_nsec < b->tv_nsec; - } else { - return a->tv_sec < b->tv_sec; - } -} - -static void timespec_clear(struct timespec *a) { - a->tv_sec = 0; -} - static int io_compare(const io_t *a, const io_t *b) { return a->fd - b->fd; } diff --git a/src/meshlink.c b/src/meshlink.c index a5bf2c90..e70a6169 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -1021,14 +1021,6 @@ static bool ecdsa_keygen(meshlink_handle_t *mesh) { return true; } -static bool timespec_lt(const struct timespec *a, const struct timespec *b) { - if(a->tv_sec == b->tv_sec) { - return a->tv_nsec < b->tv_nsec; - } else { - return a->tv_sec < b->tv_sec; - } -} - static struct timespec idle(event_loop_t *loop, void *data) { (void)loop; meshlink_handle_t *mesh = data; diff --git a/src/utils.h b/src/utils.h index dfbb2055..e99ab9ff 100644 --- a/src/utils.h +++ b/src/utils.h @@ -47,4 +47,39 @@ const char *winerror(int); unsigned int bitfield_to_int(const void *bitfield, size_t size) __attribute__((__warn_unused_result__)); +static inline void timespec_add(const struct timespec *a, const struct timespec *b, struct timespec *r) { + r->tv_sec = a->tv_sec + b->tv_sec; + r->tv_nsec = a->tv_nsec + b->tv_nsec; + + if(r->tv_nsec > 1000000000) { + r->tv_sec++, r->tv_nsec -= 1000000000; + } +} + +static inline void timespec_sub(const struct timespec *a, const struct timespec *b, struct timespec *r) { + r->tv_sec = a->tv_sec - b->tv_sec; + r->tv_nsec = a->tv_nsec - b->tv_nsec; + + if(r->tv_nsec < 0) { + r->tv_sec--, r->tv_nsec += 1000000000; + } +} + +static inline bool timespec_lt(const struct timespec *a, const struct timespec *b) { + if(a->tv_sec == b->tv_sec) { + return a->tv_nsec < b->tv_nsec; + } else { + return a->tv_sec < b->tv_sec; + } +} + +static inline void timespec_clear(struct timespec *a) { + a->tv_sec = 0; + a->tv_nsec = 0; +} + +static inline bool timespec_isset(const struct timespec *a) { + return a->tv_sec || a->tv_nsec; +} + #endif