summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorJustus Winter <4winter@informatik.uni-hamburg.de>2015-07-25 20:04:27 +0200
committerJustus Winter <4winter@informatik.uni-hamburg.de>2015-07-25 20:12:14 +0200
commitbb23879791cf015f17cb50b6443bac44b1cfbc72 (patch)
treef9a5be6535ffe938a5b51a967388f273d0d0456f /include
parenta7fb5a40ab0700477b39bb89d70172b97280d4d1 (diff)
include: provide time-value substraction
* include/mach/time_value.h (time_value_assert): New macro to assert that the given value is well-formed. (time_value_add_usec): Use the new macro. (time_value_sub_usec): New macro. (time_value_add): Use `time_value_add_usec'. (time_value_sub_usec): New macro.
Diffstat (limited to 'include')
-rw-r--r--include/mach/time_value.h31
1 files changed, 24 insertions, 7 deletions
diff --git a/include/mach/time_value.h b/include/mach/time_value.h
index 2a2f091..61be581 100644
--- a/include/mach/time_value.h
+++ b/include/mach/time_value.h
@@ -45,23 +45,40 @@ typedef struct time_value time_value_t;
*/
#define TIME_MICROS_MAX (1000000)
+#define time_value_assert(val) \
+ assert(0 <= (val)->microseconds && (val)->microseconds < TIME_MICROS_MAX);
+
#define time_value_add_usec(val, micros) { \
+ time_value_assert(val); \
if (((val)->microseconds += (micros)) \
>= TIME_MICROS_MAX) { \
(val)->microseconds -= TIME_MICROS_MAX; \
(val)->seconds++; \
} \
+ time_value_assert(val); \
}
-#define time_value_add(result, addend) { \
- (result)->microseconds += (addend)->microseconds; \
- (result)->seconds += (addend)->seconds; \
- if ((result)->microseconds >= TIME_MICROS_MAX) { \
- (result)->microseconds -= TIME_MICROS_MAX; \
- (result)->seconds++; \
- } \
+#define time_value_sub_usec(val, micros) { \
+ time_value_assert(val); \
+ if (((val)->microseconds -= (micros)) < 0) { \
+ (val)->microseconds += TIME_MICROS_MAX; \
+ (val)->seconds--; \
+ } \
+ time_value_assert(val); \
}
+#define time_value_add(result, addend) { \
+ time_value_assert(addend); \
+ (result)->seconds += (addend)->seconds; \
+ time_value_add_usec(result, (addend)->microseconds); \
+ }
+
+#define time_value_sub(result, subtrahend) { \
+ time_value_assert(subtrahend); \
+ (result)->seconds -= (subtrahend)->seconds; \
+ time_value_sub_usec(result, (subtrahend)->microseconds); \
+ }
+
/*
* Time value available through the mapped-time interface.
* Read this mapped value with