timespec: Add timespec_add_nsec helper
Add a (timespec) = (timespec) + (nsec) helper, to save intermediate
conversions to nanoseconds in its users.
Signed-off-by: Daniel Stone <daniels@collabora.com>
Reviewed-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>
diff --git a/shared/timespec-util.h b/shared/timespec-util.h
index edd4ec1..a1d6881 100644
--- a/shared/timespec-util.h
+++ b/shared/timespec-util.h
@@ -49,6 +49,27 @@
}
}
+/* Add a nanosecond value to a timespec
+ *
+ * \param r[out] result: a + b
+ * \param a[in] base operand as timespec
+ * \param b[in] operand in nanoseconds
+ */
+static inline void
+timespec_add_nsec(struct timespec *r, const struct timespec *a, int64_t b)
+{
+ r->tv_sec = a->tv_sec + (b / NSEC_PER_SEC);
+ r->tv_nsec = a->tv_nsec + (b % NSEC_PER_SEC);
+
+ if (r->tv_nsec >= NSEC_PER_SEC) {
+ r->tv_sec++;
+ r->tv_nsec -= NSEC_PER_SEC;
+ } else if (r->tv_nsec < 0) {
+ r->tv_sec--;
+ r->tv_nsec += NSEC_PER_SEC;
+ }
+}
+
/* Convert timespec to nanoseconds
*
* \param a timespec