From ac082e8eb624bc5ae0c19e91f412c8c5f12bfddf Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 19 Jul 2005 18:36:14 +0000 Subject: * Change timeval arithmetic functions to use 64bit integers. This fixes some problems with long lived queries git-svn-id: file:///home/lennart/svn/public/avahi/trunk@170 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- avahi-common/Makefile.am | 9 +++++- avahi-common/timeval-test.c | 15 ++++++++++ avahi-common/util.c | 70 ++++++++++++++++++++++++++++----------------- avahi-common/util.h | 12 ++++---- 4 files changed, 73 insertions(+), 33 deletions(-) create mode 100644 avahi-common/timeval-test.c (limited to 'avahi-common') diff --git a/avahi-common/Makefile.am b/avahi-common/Makefile.am index b1188cd..208aa94 100644 --- a/avahi-common/Makefile.am +++ b/avahi-common/Makefile.am @@ -41,7 +41,8 @@ noinst_HEADERS = \ noinst_PROGRAMS = \ strlst-test \ domain-test \ - alternative-test + alternative-test \ + timeval-test lib_LTLIBRARIES = \ libavahi-common.la @@ -80,3 +81,9 @@ domain_test_SOURCES = \ domain_test_CFLAGS = $(AM_CFLAGS) domain_test_LDADD = $(AM_LDADD) +timeval_test_SOURCES = \ + util.c util.h \ + timeval-test.c +timeval_test_CFLAGS = $(AM_CFLAGS) +timeval_test_LDADD = $(AM_LDADD) + diff --git a/avahi-common/timeval-test.c b/avahi-common/timeval-test.c new file mode 100644 index 0000000..a0392eb --- /dev/null +++ b/avahi-common/timeval-test.c @@ -0,0 +1,15 @@ +#include "util.h" + +int main(int argc, char *argv[]) { + + GTimeVal a = { 5, 5 }, b; + + b = a; + + g_message("%li.%li", a.tv_sec, a.tv_usec); + avahi_timeval_add(&a, -50); + + g_message("%li.%li", a.tv_sec, a.tv_usec); + + g_message("%lli", avahi_timeval_diff(&a, &b)); +} diff --git a/avahi-common/util.c b/avahi-common/util.c index baf5527..c9a3143 100644 --- a/avahi-common/util.c +++ b/avahi-common/util.c @@ -112,16 +112,56 @@ gint avahi_timeval_compare(const GTimeVal *a, const GTimeVal *b) { return 0; } -glong avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b) { +AvahiUsec avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b) { g_assert(a); g_assert(b); if (avahi_timeval_compare(a, b) < 0) - return avahi_timeval_diff(b, a); + return - avahi_timeval_diff(b, a); - return ((glong) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec; + return ((AvahiUsec) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec; } +GTimeVal* avahi_timeval_add(GTimeVal *a, AvahiUsec usec) { + AvahiUsec u; + g_assert(a); + + u = usec + a->tv_usec; + + if (u < 0) { + a->tv_usec = (glong) (1000000 + (u % 1000000)); + a->tv_sec += (glong) (-1 + (u / 1000000)); + } else { + a->tv_usec = (glong) (u % 1000000); + a->tv_sec += (glong) (u / 1000000); + } + + return a; +} + +AvahiUsec avahi_age(const GTimeVal *a) { + GTimeVal now; + + g_assert(a); + + g_get_current_time(&now); + + return avahi_timeval_diff(&now, a); +} + +GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter) { + g_assert(tv); + + g_get_current_time(tv); + + if (msec) + avahi_timeval_add(tv, (AvahiUsec) msec*1000); + + if (jitter) + avahi_timeval_add(tv, (AvahiUsec) g_random_int_range(0, jitter) * 1000); + + return tv; +} gint avahi_set_cloexec(gint fd) { gint n; @@ -169,30 +209,6 @@ gint avahi_wait_for_write(gint fd) { return 0; } -GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter) { - g_assert(tv); - - g_get_current_time(tv); - - if (msec) - g_time_val_add(tv, msec*1000); - - if (jitter) - g_time_val_add(tv, g_random_int_range(0, jitter) * 1000); - - return tv; -} - -glong avahi_age(const GTimeVal *a) { - GTimeVal now; - - g_assert(a); - - g_get_current_time(&now); - - return avahi_timeval_diff(&now, a); -} - /* Read the first label from string *name, unescape "\" and write it to dest */ gchar *avahi_unescape_label(const gchar **name, gchar *dest, guint size) { guint i = 0; diff --git a/avahi-common/util.h b/avahi-common/util.h index d590423..731a5a1 100644 --- a/avahi-common/util.h +++ b/avahi-common/util.h @@ -28,20 +28,22 @@ AVAHI_C_DECL_BEGIN +typedef gint64 AvahiUsec; + gchar *avahi_normalize_name(const gchar *s); /* g_free() the result! */ gchar *avahi_get_host_name(void); /* g_free() the result! */ gint avahi_timeval_compare(const GTimeVal *a, const GTimeVal *b); -glong avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b); +AvahiUsec avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b); +GTimeVal* avahi_timeval_add(GTimeVal *a, AvahiUsec usec); + +AvahiUsec avahi_age(const GTimeVal *a); +GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter); gint avahi_set_cloexec(gint fd); gint avahi_set_nonblock(gint fd); gint avahi_wait_for_write(gint fd); -GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter); - -glong avahi_age(const GTimeVal *a); - gboolean avahi_domain_equal(const gchar *a, const gchar *b); gint avahi_binary_domain_cmp(const gchar *a, const gchar *b); -- cgit