Handle sender commands in vss_post_select().
[paraslash.git] / time.c
diff --git a/time.c b/time.c
index d6e070809e527fba5437f59e0fe7976893432b9e..1fd7b0981584fc4557079ee62a87fbd713554f3a 100644 (file)
--- a/time.c
+++ b/time.c
@@ -1,12 +1,18 @@
+/*
+ * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
+ *
+ * Licensed under the GPL v2. For licencing details see COPYING.
+ */
+
 #include "para.h"
-/** \file time.c helper functions for dealing with timeouts */
+/** \file time.c Helper functions for dealing with time values. */
 
 /**
- * convert struct timeval to milliseconds
+ * Convert struct timeval to milliseconds.
  *
- * \param tv the value to convert
+ * \param tv The time value value to convert.
  *
- * \return The number off milliseconds in \a tv
+ * \return The number off milliseconds in \a tv.
  */
 long unsigned tv2ms(const struct timeval *tv)
 {
@@ -14,10 +20,10 @@ long unsigned tv2ms(const struct timeval *tv)
 }
 
 /**
- * convert milliseconds to struct timeval
+ * Convert milliseconds to a struct timeval.
  *
- * \param n the number of milliseconds
- * \param tv will be filled in by the function
+ * \param n The number of milliseconds.
+ * \param tv Result pointer.
  */
 void ms2tv(long unsigned n, struct timeval *tv)
 {
@@ -26,10 +32,10 @@ void ms2tv(long unsigned n, struct timeval *tv)
 }
 
 /**
- * convert double to struct timeval
+ * Convert a double to a struct timeval.
  *
- * \param x the value to convert
- * \param tv converted value is stored here
+ * \param x The value to convert.
+ * \param tv Result pointer.
  */
 void d2tv(double x, struct timeval *tv)
 {
@@ -38,15 +44,16 @@ void d2tv(double x, struct timeval *tv)
 }
 
 /**
- * compute the difference of two time values
+ * Compute the difference of two time values.
+ *
+ * \param b Minuend.
+ * \param a Subtrahend.
+ * \param diff Result pointer.
  *
- * \param b minuend
- * \param a subtrahend
- * \param diff difference is stored here
+ * If \a diff is not \p NULL, it contains the absolute value |\a b - \a a| on
+ * return.
  *
- * \return If b > a, compute b - a and return 1. if b < a
- * compute a - b and return -1. If \a diff is not  \p NULL, \a diff gets
- * filled in with the absolute value |b - a|.
+ * \return If \a b < \a a, this function returns -1, otherwise it returns 1.
  */
 int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *diff)
 {
@@ -71,11 +78,11 @@ int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *di
 }
 
 /**
- * add two structs of type timeval
+ * Add two time values.
  *
- * \param a first addend
- * \param b second addend
- * \param sum holds the sum upon return
+ * \param a First addend.
+ * \param b Second addend.
+ * \param sum Contains the sum \a + \a b on return.
  */
 void tv_add(const struct timeval *a, const struct timeval *b,
        struct timeval *sum)
@@ -89,11 +96,12 @@ void tv_add(const struct timeval *a, const struct timeval *b,
 }
 
 /**
- * compute integer multiple of given struct timeval
+ * Compute integer multiple of given struct timeval.
  *
- * \param mult the integer value to multiply with
- * \param tv the timevalue to multiply
- * \param result holds mult * tv upon return
+ * \param mult The integer value to multiply with.
+ * \param tv The timevalue to multiply.
+ *
+ * \param result Contains \a mult * \a tv on return.
  */
 void tv_scale(const unsigned long mult, const struct timeval *tv,
        struct timeval *result)
@@ -104,37 +112,34 @@ void tv_scale(const unsigned long mult, const struct timeval *tv,
 }
 
 /**
- * compute fraction of given struct timeval
+ * Compute a fraction of given struct timeval.
  *
- * \param divider the integer value to divide by
- * \param tv the timevalue to divide
- * \param result holds (1 / mult) * tv upon return
+ * \param divisor The integer value to divide by.
+ * \param tv The timevalue to divide.
+ * \param result Contains (1 / mult) * tv on return.
  */
 void tv_divide(const unsigned long divisor, const struct timeval *tv,
        struct timeval *result)
 {
-       long unsigned q = tv->tv_usec / divisor;
-       result->tv_sec = tv->tv_sec / divisor;
-       result->tv_usec = (tv->tv_sec - result->tv_sec * divisor)
-               * 1000 * 1000 / divisor;
-       if (result->tv_usec + q >= 1000 * 1000) {
-               result->tv_sec++;
-               result->tv_usec = 1000 * 1000 - result->tv_usec - q;
-       } else
-               result->tv_usec += q;
+       uint64_t x = ((uint64_t)tv->tv_sec * 1000 * 1000 + tv->tv_usec) / divisor;
+
+       result->tv_sec = x / 1000 / 1000;
+       result->tv_usec = x % (1000 * 1000);
 }
 
 /**
- * compute a convex combination of two time values
+ * Compute a convex combination of two time values.
  *
- * \param a the first coefiicent
- * \param tv1 the first time value
- * \param b the second coefiicent
- * \param tv2 the second time value
- * \param result holds the convex combination upon return
+ * \param a The first coefiicent.
+ * \param tv1 The first time value.
+ * \param b The second coefiicent.
+ * \param tv2 The second time value.
+ * \param result Contains the convex combination upon return.
  *
- * compute x := (a * tv1 + b * tv2) / (|a| + |b|). a, b may be negative.
- * returns sign(x) and stores |x| in the result pointer.
+ * compute x := (a * tv1 + b * tv2) / (|a| + |b|) and store |x| in \a result.
+ * Both \a a and \a b may be negative.
+ *
+ * \return One if \a x is positive, -1 otherwise.
  */
 int tv_convex_combination(const long a, const struct timeval *tv1,
                const long b, const struct timeval *tv2,
@@ -164,3 +169,13 @@ int tv_convex_combination(const long a, const struct timeval *tv1,
                        ret = -ret;
        return ret;
 }
+
+void compute_chunk_time(long unsigned chunk_num,
+               struct timeval *chunk_tv, struct timeval *stream_start,
+               struct timeval *result)
+{
+       struct timeval tmp;
+
+       tv_scale(chunk_num, chunk_tv, &tmp);
+       tv_add(&tmp, stream_start, result);
+}