From 95569e4aa4da405c0df152956a58a119e248143c Mon Sep 17 00:00:00 2001 From: David Gibson Date: Tue, 6 Aug 2024 16:18:36 +1000 Subject: [PATCH] util: Some corrections for timespec_diff_us MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The comment for timespec_diff_us() claims it will wrap after 2^64µs. This is incorrect for two reasons: * It returns a long long, which is probably 64-bits, but might not be * It returns a signed value, so even if it is 64 bits it will wrap after 2^63µs Correct the comment and use an explicitly 64-bit type to avoid that imprecision. Signed-off-by: David Gibson Signed-off-by: Stefano Brivio --- util.c | 4 ++-- util.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/util.c b/util.c index 54a9f58..07fb21c 100644 --- a/util.c +++ b/util.c @@ -244,9 +244,9 @@ void sock_probe_mem(struct ctx *c) * @a: Minuend timestamp * @b: Subtrahend timestamp * - * Return: difference in microseconds (wraps after 2^64 / 10^6s ~= 585k years) + * Return: difference in microseconds (wraps after 2^63 / 10^6s ~= 292k years) */ -long long timespec_diff_us(const struct timespec *a, const struct timespec *b) +int64_t timespec_diff_us(const struct timespec *a, const struct timespec *b) { if (a->tv_nsec < b->tv_nsec) { return (b->tv_nsec - a->tv_nsec) / 1000 + diff --git a/util.h b/util.h index e8bf957..83d2b53 100644 --- a/util.h +++ b/util.h @@ -168,7 +168,7 @@ int sock_l4(const struct ctx *c, sa_family_t af, enum epoll_type type, uint32_t data); void sock_probe_mem(struct ctx *c); long timespec_diff_ms(const struct timespec *a, const struct timespec *b); -long long timespec_diff_us(const struct timespec *a, const struct timespec *b); +int64_t timespec_diff_us(const struct timespec *a, const struct timespec *b); void bitmap_set(uint8_t *map, unsigned bit); void bitmap_clear(uint8_t *map, unsigned bit); bool bitmap_isset(const uint8_t *map, unsigned bit);