VirtualBox

source: vbox/trunk/src/libs/curl-7.64.0/lib/timeval.c@ 92350

Last change on this file since 92350 was 85671, checked in by vboxsync, 4 years ago

Export out internal curl copy to make it a lot simpler to build VBox (OSE) on Windows. bugref:9814

  • Property svn:eol-style set to native
File size: 6.0 KB
Line 
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2019, Daniel Stenberg, <[email protected]>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22
23#include "timeval.h"
24#include "system_win32.h"
25
26#if defined(WIN32) && !defined(MSDOS)
27
28struct curltime Curl_now(void)
29{
30 struct curltime now;
31 static LARGE_INTEGER freq;
32 static int isVistaOrGreater = -1;
33 if(isVistaOrGreater == -1) {
34 if(Curl_verify_windows_version(6, 0, PLATFORM_WINNT,
35 VERSION_GREATER_THAN_EQUAL)) {
36 isVistaOrGreater = 1;
37 QueryPerformanceFrequency(&freq);
38 }
39 else
40 isVistaOrGreater = 0;
41 }
42 if(isVistaOrGreater == 1) { /* QPC timer might have issues pre-Vista */
43 LARGE_INTEGER count;
44 QueryPerformanceCounter(&count);
45 now.tv_sec = (time_t)(count.QuadPart / freq.QuadPart);
46 now.tv_usec =
47 (int)((count.QuadPart % freq.QuadPart) * 1000000 / freq.QuadPart);
48 }
49 else {
50 /* Disable /analyze warning that GetTickCount64 is preferred */
51#if defined(_MSC_VER)
52#pragma warning(push)
53#pragma warning(disable:28159)
54#endif
55 DWORD milliseconds = GetTickCount();
56#if defined(_MSC_VER)
57#pragma warning(pop)
58#endif
59
60 now.tv_sec = milliseconds / 1000;
61 now.tv_usec = (milliseconds % 1000) * 1000;
62 }
63 return now;
64}
65
66#elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
67
68struct curltime Curl_now(void)
69{
70 /*
71 ** clock_gettime() is granted to be increased monotonically when the
72 ** monotonic clock is queried. Time starting point is unspecified, it
73 ** could be the system start-up time, the Epoch, or something else,
74 ** in any case the time starting point does not change once that the
75 ** system has started up.
76 */
77 struct timeval now;
78 struct curltime cnow;
79 struct timespec tsnow;
80
81 /*
82 ** clock_gettime() may be defined by Apple's SDK as weak symbol thus
83 ** code compiles but fails during run-time if clock_gettime() is
84 ** called on unsupported OS version.
85 */
86#if defined(__APPLE__) && (HAVE_BUILTIN_AVAILABLE == 1)
87 bool have_clock_gettime = FALSE;
88 if(__builtin_available(macOS 10.12, iOS 10, tvOS 10, watchOS 3, *))
89 have_clock_gettime = TRUE;
90#endif
91
92 if(
93#if defined(__APPLE__) && (HAVE_BUILTIN_AVAILABLE == 1)
94 have_clock_gettime &&
95#endif
96 (0 == clock_gettime(CLOCK_MONOTONIC, &tsnow))) {
97 cnow.tv_sec = tsnow.tv_sec;
98 cnow.tv_usec = (unsigned int)(tsnow.tv_nsec / 1000);
99 }
100 /*
101 ** Even when the configure process has truly detected monotonic clock
102 ** availability, it might happen that it is not actually available at
103 ** run-time. When this occurs simply fallback to other time source.
104 */
105#ifdef HAVE_GETTIMEOFDAY
106 else {
107 (void)gettimeofday(&now, NULL);
108 cnow.tv_sec = now.tv_sec;
109 cnow.tv_usec = (unsigned int)now.tv_usec;
110 }
111#else
112 else {
113 cnow.tv_sec = time(NULL);
114 cnow.tv_usec = 0;
115 }
116#endif
117 return cnow;
118}
119
120#elif defined(HAVE_MACH_ABSOLUTE_TIME)
121
122#include <stdint.h>
123#include <mach/mach_time.h>
124
125struct curltime Curl_now(void)
126{
127 /*
128 ** Monotonic timer on Mac OS is provided by mach_absolute_time(), which
129 ** returns time in Mach "absolute time units," which are platform-dependent.
130 ** To convert to nanoseconds, one must use conversion factors specified by
131 ** mach_timebase_info().
132 */
133 static mach_timebase_info_data_t timebase;
134 struct curltime cnow;
135 uint64_t usecs;
136
137 if(0 == timebase.denom)
138 (void) mach_timebase_info(&timebase);
139
140 usecs = mach_absolute_time();
141 usecs *= timebase.numer;
142 usecs /= timebase.denom;
143 usecs /= 1000;
144
145 cnow.tv_sec = usecs / 1000000;
146 cnow.tv_usec = (int)(usecs % 1000000);
147
148 return cnow;
149}
150
151#elif defined(HAVE_GETTIMEOFDAY)
152
153struct curltime Curl_now(void)
154{
155 /*
156 ** gettimeofday() is not granted to be increased monotonically, due to
157 ** clock drifting and external source time synchronization it can jump
158 ** forward or backward in time.
159 */
160 struct timeval now;
161 struct curltime ret;
162 (void)gettimeofday(&now, NULL);
163 ret.tv_sec = now.tv_sec;
164 ret.tv_usec = (int)now.tv_usec;
165 return ret;
166}
167
168#else
169
170struct curltime Curl_now(void)
171{
172 /*
173 ** time() returns the value of time in seconds since the Epoch.
174 */
175 struct curltime now;
176 now.tv_sec = time(NULL);
177 now.tv_usec = 0;
178 return now;
179}
180
181#endif
182
183#if SIZEOF_TIME_T < 8
184#define TIME_MAX INT_MAX
185#define TIME_MIN INT_MIN
186#else
187#define TIME_MAX 9223372036854775807LL
188#define TIME_MIN -9223372036854775807LL
189#endif
190
191/*
192 * Returns: time difference in number of milliseconds. For too large diffs it
193 * returns max value.
194 *
195 * @unittest: 1323
196 */
197timediff_t Curl_timediff(struct curltime newer, struct curltime older)
198{
199 timediff_t diff = (timediff_t)newer.tv_sec-older.tv_sec;
200 if(diff >= (TIME_MAX/1000))
201 return TIME_MAX;
202 else if(diff <= (TIME_MIN/1000))
203 return TIME_MIN;
204 return diff * 1000 + (newer.tv_usec-older.tv_usec)/1000;
205}
206
207/*
208 * Returns: time difference in number of microseconds. For too large diffs it
209 * returns max value.
210 */
211timediff_t Curl_timediff_us(struct curltime newer, struct curltime older)
212{
213 timediff_t diff = (timediff_t)newer.tv_sec-older.tv_sec;
214 if(diff >= (TIME_MAX/1000000))
215 return TIME_MAX;
216 else if(diff <= (TIME_MIN/1000000))
217 return TIME_MIN;
218 return diff * 1000000 + newer.tv_usec-older.tv_usec;
219}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette