diff options
author | Alexandros Frantzis <alexandros.frantzis@collabora.com> | 2017-12-13 13:27:54 +0200 |
---|---|---|
committer | Pekka Paalanen <pekka.paalanen@collabora.co.uk> | 2017-12-18 11:27:43 +0200 |
commit | 10d708d2688f26c17b3f009fcc681174d9877166 (patch) | |
tree | 53f4a6d0798d904dd9fcb1973645eb9df6fd704d /shared | |
parent | 787fa611de1b7c4144b7491260c513a5ec0a4728 (diff) |
shared: Add timespec_to_proto helper function
Add helper function to convert from struct timespec values to tv_sec_hi,
tv_sec_lo, tv_nsec triplets used for sending high-resolution timestamp
data over the wayland protocol. Replace existing conversion code with
the helper function.
Signed-off-by: Alexandros Frantzis <alexandros.frantzis@collabora.com>
Reviewed-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>
Diffstat (limited to 'shared')
-rw-r--r-- | shared/timespec-util.h | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/shared/timespec-util.h b/shared/timespec-util.h index 5184d281..5f4b2b9e 100644 --- a/shared/timespec-util.h +++ b/shared/timespec-util.h @@ -147,6 +147,30 @@ timespec_to_usec(const struct timespec *a) return (int64_t)a->tv_sec * 1000000 + a->tv_nsec / 1000; } +/* Convert timespec to protocol data + * + * \param a timespec + * \param tv_sec_hi[out] the high bytes of the seconds part + * \param tv_sec_lo[out] the low bytes of the seconds part + * \param tv_nsec[out] the nanoseconds part + * + * The input timespec must be normalized (the nanoseconds part should + * be less than 1 second) and non-negative. + */ +static inline void +timespec_to_proto(const struct timespec *a, uint32_t *tv_sec_hi, + uint32_t *tv_sec_lo, uint32_t *tv_nsec) +{ + assert(a->tv_sec >= 0); + assert(a->tv_nsec >= 0 && a->tv_nsec < NSEC_PER_SEC); + + uint64_t sec64 = a->tv_sec; + + *tv_sec_hi = sec64 >> 32; + *tv_sec_lo = sec64 & 0xffffffff; + *tv_nsec = a->tv_nsec; +} + /* Convert nanoseconds to timespec * * \param a timespec |