From b18b6c5536d245ef4e710009d703601813755b56 Mon Sep 17 00:00:00 2001 From: dlg Date: Sun, 11 Aug 2024 00:49:34 +0000 Subject: [PATCH] make the timeout_add_foo wrappers more consistent. take uint64_t as the arg type for timeout_add_msec and timeout_add_usec like we do for timeout_add_nsec now. always calculate ticks using uint64_t, and factor out the range checks on the resulting ticks value so it's done consistently. ok claudio@ --- sys/kern/kern_timeout.c | 50 ++++++++++++++++++----------------------- sys/sys/timeout.h | 6 ++--- 2 files changed, 25 insertions(+), 31 deletions(-) diff --git a/sys/kern/kern_timeout.c b/sys/kern/kern_timeout.c index 04154542902..4c61f781b5a 100644 --- a/sys/kern/kern_timeout.c +++ b/sys/kern/kern_timeout.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_timeout.c,v 1.98 2024/08/05 23:51:11 dlg Exp $ */ +/* $OpenBSD: kern_timeout.c,v 1.99 2024/08/11 00:49:34 dlg Exp $ */ /* * Copyright (c) 2001 Thomas Nordin * Copyright (c) 2000-2001 Artur Grabowski @@ -332,18 +332,25 @@ timeout_add(struct timeout *new, int to_ticks) return ret; } +static inline int +timeout_add_ticks(struct timeout *to, uint64_t to_ticks, int notzero) +{ + if (to_ticks > INT_MAX) + to_ticks = INT_MAX; + else if (to_ticks == 0 && notzero) + to_ticks = 1; + + return timeout_add(to, (int)to_ticks); +} + int timeout_add_tv(struct timeout *to, const struct timeval *tv) { uint64_t to_ticks; to_ticks = (uint64_t)hz * tv->tv_sec + tv->tv_usec / tick; - if (to_ticks > INT_MAX) - to_ticks = INT_MAX; - if (to_ticks == 0 && tv->tv_usec > 0) - to_ticks = 1; - return timeout_add(to, (int)to_ticks); + return timeout_add_ticks(to, to_ticks, tv->tv_usec > 0); } int @@ -352,37 +359,28 @@ timeout_add_sec(struct timeout *to, int secs) uint64_t to_ticks; to_ticks = (uint64_t)hz * secs; - if (to_ticks > INT_MAX) - to_ticks = INT_MAX; - if (to_ticks == 0) - to_ticks = 1; - return timeout_add(to, (int)to_ticks); + return timeout_add_ticks(to, to_ticks, 1); } int -timeout_add_msec(struct timeout *to, int msecs) +timeout_add_msec(struct timeout *to, uint64_t msecs) { uint64_t to_ticks; - to_ticks = (uint64_t)msecs * 1000 / tick; - if (to_ticks > INT_MAX) - to_ticks = INT_MAX; - if (to_ticks == 0 && msecs > 0) - to_ticks = 1; + to_ticks = msecs * 1000 / tick; - return timeout_add(to, (int)to_ticks); + return timeout_add_ticks(to, to_ticks, msecs > 0); } int -timeout_add_usec(struct timeout *to, int usecs) +timeout_add_usec(struct timeout *to, uint64_t usecs) { - int to_ticks = usecs / tick; + uint64_t to_ticks; - if (to_ticks == 0 && usecs > 0) - to_ticks = 1; + to_ticks = usecs / tick; - return timeout_add(to, to_ticks); + return timeout_add_ticks(to, to_ticks, usecs > 0); } int @@ -391,12 +389,8 @@ timeout_add_nsec(struct timeout *to, uint64_t nsecs) uint64_t to_ticks; to_ticks = nsecs / (tick * 1000); - if (to_ticks > INT_MAX) - to_ticks = INT_MAX; - if (to_ticks == 0 && nsecs > 0) - to_ticks = 1; - return timeout_add(to, (int)to_ticks); + return timeout_add_ticks(to, to_ticks, nsecs > 0); } int diff --git a/sys/sys/timeout.h b/sys/sys/timeout.h index 0000705b710..54dd45db991 100644 --- a/sys/sys/timeout.h +++ b/sys/sys/timeout.h @@ -1,4 +1,4 @@ -/* $OpenBSD: timeout.h,v 1.49 2024/08/05 23:51:11 dlg Exp $ */ +/* $OpenBSD: timeout.h,v 1.50 2024/08/11 00:49:34 dlg Exp $ */ /* * Copyright (c) 2000-2001 Artur Grabowski * All rights reserved. @@ -108,8 +108,8 @@ void timeout_set_proc(struct timeout *, void (*)(void *), void *); int timeout_add(struct timeout *, int); int timeout_add_tv(struct timeout *, const struct timeval *); int timeout_add_sec(struct timeout *, int); -int timeout_add_msec(struct timeout *, int); -int timeout_add_usec(struct timeout *, int); +int timeout_add_msec(struct timeout *, uint64_t); +int timeout_add_usec(struct timeout *, uint64_t); int timeout_add_nsec(struct timeout *, uint64_t); int timeout_abs_ts(struct timeout *, const struct timespec *); -- 2.20.1