summaryrefslogtreecommitdiffstats
path: root/src/pulse/timeval.c
blob: b405a70a671ea86142d0ac0f8dbb4aba95aa91cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/***
  This file is part of PulseAudio.

  Copyright 2004-2006 Lennart Poettering
  Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB

  PulseAudio is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as
  published by the Free Software Foundation; either version 2.1 of the
  License, or (at your option) any later version.

  PulseAudio is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with PulseAudio; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stddef.h>
#include <sys/time.h>

#ifdef HAVE_WINDOWS_H
#include <windows.h>
#endif

#include <pulsecore/macro.h>
#include <pulsecore/core-util.h>

#include "timeval.h"

struct timeval *pa_gettimeofday(struct timeval *tv) {
    pa_assert(tv);

#if 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 t;

    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  = (time_t) (t / PA_USEC_PER_SEC);
    tv->tv_usec = (suseconds_t) (t % PA_USEC_PER_SEC);
}
#elif defined(HAVE_GETTIMEOFDAY)
    pa_assert_se(gettimeofday(tv, NULL) == 0);
#else
#error "Platform lacks gettimeofday() or equivalent function."
#endif

    return tv;
}

pa_usec_t pa_timeval_diff(const struct timeval *a, const struct timeval *b) {
    pa_usec_t r;

    pa_assert(a);
    pa_assert(b);

    /* Check which whan is the earlier time and swap the two arguments if required. */
    if (PA_UNLIKELY(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 - (pa_usec_t) b->tv_sec) * PA_USEC_PER_SEC;

    /* Calculate the microsecond difference */
    if (a->tv_usec > b->tv_usec)
        r += (pa_usec_t) a->tv_usec - (pa_usec_t) b->tv_usec;
    else if (a->tv_usec < b->tv_usec)
        r -= (pa_usec_t) b->tv_usec - (pa_usec_t) a->tv_usec;

    return r;
}

int pa_timeval_cmp(const struct timeval *a, const struct timeval *b) {
    pa_assert(a);
    pa_assert(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;
    pa_assert(tv);

    return pa_timeval_diff(pa_gettimeofday(&now), tv);
}

struct timeval* pa_timeval_add(struct timeval *tv, pa_usec_t v) {
    time_t secs;
    pa_assert(tv);

    secs = (time_t) (v/PA_USEC_PER_SEC);

    if (PA_UNLIKELY(tv->tv_sec > PA_INT_TYPE_MAX(time_t) - secs))
        goto overflow;

    tv->tv_sec += secs;
    v -= (pa_usec_t) secs * PA_USEC_PER_SEC;
    tv->tv_usec += (suseconds_t) v;

    /* Normalize */
    while ((pa_usec_t) tv->tv_usec >= PA_USEC_PER_SEC) {

        if (PA_UNLIKELY(tv->tv_sec >= PA_INT_TYPE_MAX(time_t)))
            goto overflow;

        tv->tv_sec++;
        tv->tv_usec -= (suseconds_t) PA_USEC_PER_SEC;
    }

    return tv;

overflow:
    tv->tv_sec = PA_INT_TYPE_MAX(time_t);
    tv->tv_usec = (suseconds_t) (PA_USEC_PER_SEC-1);
    return tv;
}

struct timeval* pa_timeval_sub(struct timeval *tv, pa_usec_t v) {
    time_t secs;
    pa_assert(tv);

    secs = (time_t) (v/PA_USEC_PER_SEC);

    if (PA_UNLIKELY(tv->tv_sec < secs))
        goto underflow;

    tv->tv_sec -= secs;
    v -= (pa_usec_t) secs * PA_USEC_PER_SEC;

    if (tv->tv_usec >= (suseconds_t) v)
        tv->tv_usec -= (suseconds_t) v;
    else {

        if (PA_UNLIKELY(tv->tv_sec <= 0))
            goto underflow;

        tv->tv_sec --;
        tv->tv_usec += (suseconds_t) (PA_USEC_PER_SEC - v);
    }

    return tv;

underflow:
    tv->tv_sec = 0;
    tv->tv_usec = 0;
    return tv;
}

struct timeval* pa_timeval_store(struct timeval *tv, pa_usec_t v) {
    pa_assert(tv);

    if (PA_UNLIKELY(v == PA_USEC_INVALID)) {
        tv->tv_sec = PA_INT_TYPE_MAX(time_t);
        tv->tv_usec = (suseconds_t) (PA_USEC_PER_SEC-1);

        return tv;
    }

    tv->tv_sec = (time_t) (v / PA_USEC_PER_SEC);
    tv->tv_usec = (suseconds_t) (v % PA_USEC_PER_SEC);

    return tv;
}

pa_usec_t pa_timeval_load(const struct timeval *tv) {

    if (PA_UNLIKELY(!tv))
        return PA_USEC_INVALID;

    return
        (pa_usec_t) tv->tv_sec * PA_USEC_PER_SEC +
        (pa_usec_t) tv->tv_usec;
}