From 10b343057733ca75925de786f61cb7dfd58bacd5 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Sat, 17 Jan 2009 10:37:50 +0100 Subject: [PATCH] src/common.[ch]: Add `counter_diff', a function to calculate the difference of two counters. It's not just a simple (old - new), because wrap-around is handled, too. --- src/common.c | 18 ++++++++++++++++++ src/common.h | 2 ++ 2 files changed, 20 insertions(+) diff --git a/src/common.c b/src/common.c index ae89c44f..aeea28d3 100644 --- a/src/common.c +++ b/src/common.c @@ -925,4 +925,22 @@ int read_file_contents (const char *filename, char *buf, int bufsize) return n; } +counter_t counter_diff (counter_t old_value, counter_t new_value) +{ + counter_t diff; + + if (old_value > new_value) + { + if (old_value <= 4294967295U) + diff = (4294967295U - old_value) + new_value; + else + diff = (18446744073709551615ULL - old_value) + + new_value; + } + else + { + diff = new_value - old_value; + } + return (diff); +} /* counter_t counter_to_gauge */ diff --git a/src/common.h b/src/common.h index f463b77e..aefc2cc6 100644 --- a/src/common.h +++ b/src/common.h @@ -209,4 +209,6 @@ int walk_directory (const char *dir, dirwalk_callback_f callback, void *user_data); int read_file_contents (const char *filename, char *buf, int bufsize); +counter_t counter_diff (counter_t old_value, counter_t new_value); + #endif /* COMMON_H */ -- 2.30.2