From 2baa34beb849e279175f9c654e1823ad8fd1abae Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 19 Dec 2019 16:43:51 +0530 Subject: [PATCH] Move a couple of functions to where they are actually needed --- glfw/backend_utils.c | 8 ++++ kitty/monotonic.h | 107 ++++++++++++++++++++++--------------------- 2 files changed, 63 insertions(+), 52 deletions(-) diff --git a/glfw/backend_utils.c b/glfw/backend_utils.c index 536dbdb1c..2a0f6fd7b 100644 --- a/glfw/backend_utils.c +++ b/glfw/backend_utils.c @@ -163,6 +163,14 @@ prepareForPoll(EventLoopData *eld, monotonic_t timeout) { return timeout; } +static inline struct timespec +calc_time(monotonic_t nsec) { + struct timespec result; + result.tv_sec = nsec / (1000LL * 1000LL * 1000LL); + result.tv_nsec = nsec % (1000LL * 1000LL * 1000LL); + return result; +} + int pollWithTimeout(struct pollfd *fds, nfds_t nfds, monotonic_t timeout) { struct timespec tv = calc_time(timeout); diff --git a/kitty/monotonic.h b/kitty/monotonic.h index 0d2d94ce1..a062cb476 100644 --- a/kitty/monotonic.h +++ b/kitty/monotonic.h @@ -16,7 +16,59 @@ typedef int64_t monotonic_t; -static inline monotonic_t calc_nano_time(struct timespec time) { +static inline monotonic_t +s_double_to_monotonic_t(double time) { + time *= 1000.0; + time *= 1000.0; + time *= 1000.0; + return (monotonic_t)time; +} + +static inline monotonic_t +ms_double_to_monotonic_t(double time) { + time *= 1000.0; + time *= 1000.0; + return (monotonic_t)time; +} + +static inline monotonic_t +s_to_monotonic_t(monotonic_t time) { + return time * 1000ll * 1000ll * 1000ll; +} + +static inline monotonic_t +ms_to_monotonic_t(monotonic_t time) { + return time * 1000ll * 1000ll; +} + +static inline int +monotonic_t_to_ms(monotonic_t time) { + return (int)(time / 1000ll / 1000ll); +} + +static inline double +monotonic_t_to_s_double(monotonic_t time) { + return (double)time / 1000.0 / 1000.0 / 1000.0; +} + +extern monotonic_t monotonic_start_time; +extern monotonic_t monotonic_(void); + +static inline monotonic_t +monotonic(void) { + return monotonic_() - monotonic_start_time; +} + +static inline void +init_monotonic(void) { + monotonic_start_time = monotonic_(); +} + +#ifdef MONOTONIC_IMPLEMENTATION +monotonic_t monotonic_start_time = 0; + +static inline monotonic_t +calc_nano_time(struct timespec time) { int64_t result = (monotonic_t)time.tv_sec; result *= 1000LL; result *= 1000LL; @@ -25,57 +77,8 @@ static inline monotonic_t calc_nano_time(struct timespec time) { return result; } -static inline struct timespec calc_time(monotonic_t nsec) { - struct timespec result; - result.tv_sec = nsec / (1000LL * 1000LL * 1000LL); - result.tv_nsec = nsec % (1000LL * 1000LL * 1000LL); - return result; -} - -static inline monotonic_t s_double_to_monotonic_t(double time) { - time *= 1000.0; - time *= 1000.0; - time *= 1000.0; - return (monotonic_t)time; -} - -static inline monotonic_t ms_double_to_monotonic_t(double time) { - time *= 1000.0; - time *= 1000.0; - return (monotonic_t)time; -} - -static inline monotonic_t s_to_monotonic_t(monotonic_t time) { - return time * 1000ll * 1000ll * 1000ll; -} - -static inline monotonic_t ms_to_monotonic_t(monotonic_t time) { - return time * 1000ll * 1000ll; -} - -static inline int monotonic_t_to_ms(monotonic_t time) { - return (int)(time / 1000ll / 1000ll); -} - -static inline double monotonic_t_to_s_double(monotonic_t time) { - return (double)time / 1000.0 / 1000.0 / 1000.0; -} - -extern monotonic_t monotonic_start_time; -extern monotonic_t monotonic_(void); - -static inline monotonic_t monotonic(void) { - return monotonic_() - monotonic_start_time; -} - -static inline void init_monotonic(void) { - monotonic_start_time = monotonic_(); -} - -#ifdef MONOTONIC_IMPLEMENTATION -monotonic_t monotonic_start_time = 0; - -monotonic_t monotonic_(void) { +monotonic_t +monotonic_(void) { struct timespec ts = {0}; #ifdef CLOCK_HIGHRES clock_gettime(CLOCK_HIGHRES, &ts);