[PATCH weston 2/7] timespec: Add timespec_add_msec helper

Pekka Paalanen ppaalanen at gmail.com
Tue Feb 14 14:24:43 UTC 2017


On Tue, 14 Feb 2017 13:18:02 +0000
Daniel Stone <daniels at collabora.com> wrote:

> Add a (timespec) = (timespec) + (msec) helper, to save intermediate
> conversions in its users.
> 
> Signed-off-by: Daniel Stone <daniels at collabora.com>
> ---
>  shared/timespec-util.h | 21 +++++++++++++++++++++
>  tests/timespec-test.c  | 11 +++++++++++
>  2 files changed, 32 insertions(+)
> 
> diff --git a/shared/timespec-util.h b/shared/timespec-util.h
> index 80b557859..c2bc8635c 100644
> --- a/shared/timespec-util.h
> +++ b/shared/timespec-util.h
> @@ -70,6 +70,27 @@ timespec_add_nsec(struct timespec *r, const struct timespec *a, int64_t b)
>  	}
>  }
>  
> +/* Add a millisecond value to a timespec
> + *
> + * \param r[out] result: a + b
> + * \param a[in] base operand as timespec
> + * \param b[in] operand in milliseconds
> + */
> +static inline void
> +timespec_add_msec(struct timespec *r, const struct timespec *a, int64_t b)
> +{
> +	r->tv_sec = a->tv_sec + (b / 1000);
> +	r->tv_nsec = a->tv_nsec + ((b % 1000) * 1000000);
> +
> +	if (r->tv_nsec >= NSEC_PER_SEC) {
> +		r->tv_sec++;
> +		r->tv_nsec -= NSEC_PER_SEC;
> +	} else if (r->tv_nsec <= -NSEC_PER_SEC) {

The same problem as with the previous patch, tv_nsec cannot be allowed
negative.

> +		r->tv_sec--;
> +		r->tv_nsec += NSEC_PER_SEC;
> +	}
> +}

Any reason to not write this as a call to timespec_add_nsec()?
The testing added below is very thin otherwise.

> +
>  /* Convert timespec to nanoseconds
>   *
>   * \param a timespec
> diff --git a/tests/timespec-test.c b/tests/timespec-test.c
> index f1193507a..bfb9e0362 100644
> --- a/tests/timespec-test.c
> +++ b/tests/timespec-test.c
> @@ -124,3 +124,14 @@ ZUC_TEST(timespec_test, timespec_add_nsec)
>  	ZUC_ASSERT_EQ(16, r.tv_sec);
>  	ZUC_ASSERT_EQ(0, r.tv_nsec);
>  }
> +
> +ZUC_TEST(timespec_test, timespec_add_msec)
> +{
> +	struct timespec a, r;
> +
> +	a.tv_sec = 1000;
> +	a.tv_nsec = 1;
> +	timespec_add_msec(&r, &a, 2002);
> +	ZUC_ASSERT_EQ(1002, r.tv_sec);
> +	ZUC_ASSERT_EQ(2000001, r.tv_nsec);
> +}

Correct.


Thanks,
pq
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 801 bytes
Desc: OpenPGP digital signature
URL: <https://lists.freedesktop.org/archives/wayland-devel/attachments/20170214/6b93ecf8/attachment.sig>


More information about the wayland-devel mailing list