]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
time-util: introduce usec_sub() 2542/head
authorAlexander Kuleshov <kuleshovmail@gmail.com>
Thu, 4 Feb 2016 18:02:39 +0000 (00:02 +0600)
committerAlexander Kuleshov <kuleshovmail@gmail.com>
Mon, 8 Feb 2016 19:15:17 +0000 (01:15 +0600)
The dual_timestamp_from_realtime(), dual_timestamp_from_monotonic()
and dual_timestamp_from_boottime_or_monotonic() shares the same
code for comparison given ts with delta. Let's move it to the
separate inline function to prevent code duplication.

src/basic/time-util.c
src/basic/time-util.h

index 293442cf0e747523a9d9610555a48f372126b16d..17352a7a9616f7eb9274d8399b0eec5f5f9cfaba 100644 (file)
@@ -79,12 +79,7 @@ dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u) {
         ts->realtime = u;
 
         delta = (int64_t) now(CLOCK_REALTIME) - (int64_t) u;
-        ts->monotonic = now(CLOCK_MONOTONIC);
-
-        if ((int64_t) ts->monotonic > delta)
-                ts->monotonic -= delta;
-        else
-                ts->monotonic = 0;
+        ts->monotonic = usec_sub(now(CLOCK_MONOTONIC), delta);
 
         return ts;
 }
@@ -100,12 +95,7 @@ dual_timestamp* dual_timestamp_from_monotonic(dual_timestamp *ts, usec_t u) {
 
         ts->monotonic = u;
         delta = (int64_t) now(CLOCK_MONOTONIC) - (int64_t) u;
-
-        ts->realtime = now(CLOCK_REALTIME);
-        if ((int64_t) ts->realtime > delta)
-                ts->realtime -= delta;
-        else
-                ts->realtime = 0;
+        ts->realtime = usec_sub(now(CLOCK_REALTIME), delta);
 
         return ts;
 }
@@ -120,16 +110,8 @@ dual_timestamp* dual_timestamp_from_boottime_or_monotonic(dual_timestamp *ts, us
 
         dual_timestamp_get(ts);
         delta = (int64_t) now(clock_boottime_or_monotonic()) - (int64_t) u;
-
-        if ((int64_t) ts->realtime > delta)
-                ts->realtime -= delta;
-        else
-                ts->realtime = 0;
-
-        if ((int64_t) ts->monotonic > delta)
-                ts->monotonic -= delta;
-        else
-                ts->monotonic = 0;
+        ts->realtime = usec_sub(ts->realtime, delta);
+        ts->monotonic = usec_sub(ts->monotonic, delta);
 
         return ts;
 }
index 9c7758a9596802a423994618b0644d7e056481f7..87440faeeeec645b2625d6b2c1e6869632f966ff 100644 (file)
@@ -140,3 +140,14 @@ static inline usec_t usec_add(usec_t a, usec_t b) {
 
         return c;
 }
+
+static inline usec_t usec_sub(usec_t timestamp, int64_t delta) {
+        if (delta < 0)
+                timestamp = usec_add(timestamp, (usec_t) (-delta));
+        else if (timestamp > (usec_t) delta)
+                timestamp -= delta;
+        else
+                timestamp = 0;
+
+        return timestamp;
+}