From 53f555b6f6b0a9fb58a781e40e3db67924a2a9c8 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 1 Feb 2016 16:13:51 +0100 Subject: util: add usec_add() which adds two usec_t values with overflow handling --- src/basic/time-util.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/basic/time-util.h') diff --git a/src/basic/time-util.h b/src/basic/time-util.h index 7321e3c670..b37d5ad5dc 100644 --- a/src/basic/time-util.h +++ b/src/basic/time-util.h @@ -127,3 +127,16 @@ time_t mktime_or_timegm(struct tm *tm, bool utc); struct tm *localtime_or_gmtime_r(const time_t *t, struct tm *tm, bool utc); unsigned long usec_to_jiffies(usec_t usec); + +static inline usec_t usec_add(usec_t a, usec_t b) { + usec_t c; + + /* Adds two time values, and makes sure USEC_INFINITY as input results as USEC_INFINITY in output, and doesn't + * overflow. */ + + c = a + b; + if (c < a || c < b) /* overflow check */ + return USEC_INFINITY; + + return c; +} -- cgit v1.2.3-54-g00ecf