diff options
author | Pierre Ossman <ossman@cendio.se> | 2006-05-18 06:45:43 +0000 |
---|---|---|
committer | Pierre Ossman <ossman@cendio.se> | 2006-05-18 06:45:43 +0000 |
commit | 0796ead0db250f6a46a7531c9db471592ed6e129 (patch) | |
tree | dd812f396d9c933bf2dbcba9deb790c91a1fe1d1 /src/polyp/util.c | |
parent | 5f458db84f38c52170af26be93156c24ac7831fc (diff) |
Move timeval calculation functions into their own file.
git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@926 fefdeb5f-60dc-0310-8127-8f9354f1896f
Diffstat (limited to 'src/polyp/util.c')
-rw-r--r-- | src/polyp/util.c | 136 |
1 files changed, 12 insertions, 124 deletions
diff --git a/src/polyp/util.c b/src/polyp/util.c index 76b5c9b9..dcebff59 100644 --- a/src/polyp/util.c +++ b/src/polyp/util.c @@ -23,39 +23,33 @@ #include <config.h> #endif -#include <stdarg.h> -#include <stdlib.h> -#include <signal.h> -#include <errno.h> #include <assert.h> -#include <string.h> +#include <errno.h> #include <stdio.h> -#include <fcntl.h> -#include <unistd.h> -#include <limits.h> +#include <stdlib.h> +#include <string.h> #include <time.h> -#include <ctype.h> +#include <unistd.h> #include <sys/types.h> -#include <sys/stat.h> -#include <sys/time.h> - -#ifdef HAVE_NETDB_H -#include <netdb.h> -#endif #ifdef HAVE_PWD_H #include <pwd.h> #endif -#ifdef HAVE_GRP_H -#include <grp.h> +#ifdef HAVE_SYS_SOCKET_H +#include <sys/socket.h> +#endif + +#ifdef HAVE_NETDB_H +#include <netdb.h> #endif #ifdef HAVE_WINDOWS_H #include <windows.h> #endif -#include <polyp/xmalloc.h> +#include "../polypcore/winsock.h" + #include <polypcore/log.h> #include <polypcore/core-util.h> @@ -154,112 +148,6 @@ char *pa_get_home_dir(char *s, size_t l) { #endif } -struct timeval *pa_gettimeofday(struct timeval *tv) { -#ifdef HAVE_GETTIMEOFDAY - assert(tv); - - return gettimeofday(tv, NULL) < 0 ? NULL : tv; -#elif defined(OS_IS_WIN32) - /* - * Copied from implementation by Steven Edwards (LGPL). - * Found on wine mailing list. - */ - -#if defined(_MSC_VER) || defined(__BORLANDC__) -#define EPOCHFILETIME (116444736000000000i64) -#else -#define EPOCHFILETIME (116444736000000000LL) -#endif - - FILETIME ft; - LARGE_INTEGER li; - __int64 t; - - assert(tv); - - GetSystemTimeAsFileTime(&ft); - li.LowPart = ft.dwLowDateTime; - li.HighPart = ft.dwHighDateTime; - t = li.QuadPart; /* In 100-nanosecond intervals */ - t -= EPOCHFILETIME; /* Offset to the Epoch time */ - t /= 10; /* In microseconds */ - tv->tv_sec = (long)(t / 1000000); - tv->tv_usec = (long)(t % 1000000); - - return tv; -#else -#error "Platform lacks gettimeofday() or equivalent function." -#endif -} - -pa_usec_t pa_timeval_diff(const struct timeval *a, const struct timeval *b) { - pa_usec_t r; - assert(a && b); - - /* Check which whan is the earlier time and swap the two arguments if reuqired. */ - if (pa_timeval_cmp(a, b) < 0) { - const struct timeval *c; - c = a; - a = b; - b = c; - } - - /* Calculate the second difference*/ - r = ((pa_usec_t) a->tv_sec - b->tv_sec)* 1000000; - - /* Calculate the microsecond difference */ - if (a->tv_usec > b->tv_usec) - r += ((pa_usec_t) a->tv_usec - b->tv_usec); - else if (a->tv_usec < b->tv_usec) - r -= ((pa_usec_t) b->tv_usec - a->tv_usec); - - return r; -} - -int pa_timeval_cmp(const struct timeval *a, const struct timeval *b) { - assert(a && b); - - if (a->tv_sec < b->tv_sec) - return -1; - - if (a->tv_sec > b->tv_sec) - return 1; - - if (a->tv_usec < b->tv_usec) - return -1; - - if (a->tv_usec > b->tv_usec) - return 1; - - return 0; -} - -pa_usec_t pa_timeval_age(const struct timeval *tv) { - struct timeval now; - assert(tv); - - return pa_timeval_diff(pa_gettimeofday(&now), tv); -} - -struct timeval* pa_timeval_add(struct timeval *tv, pa_usec_t v) { - unsigned long secs; - assert(tv); - - secs = (v/1000000); - tv->tv_sec += (unsigned long) secs; - v -= secs*1000000; - - tv->tv_usec += v; - - /* Normalize */ - while (tv->tv_usec >= 1000000) { - tv->tv_sec++; - tv->tv_usec -= 1000000; - } - - return tv; -} - char *pa_get_binary_name(char *s, size_t l) { #ifdef HAVE_READLINK |