Annotation of embedaddon/curl/src/tool_util.c, revision 1.1.1.1

1.1       misho       1: /***************************************************************************
                      2:  *                                  _   _ ____  _
                      3:  *  Project                     ___| | | |  _ \| |
                      4:  *                             / __| | | | |_) | |
                      5:  *                            | (__| |_| |  _ <| |___
                      6:  *                             \___|\___/|_| \_\_____|
                      7:  *
                      8:  * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, 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: #include "tool_setup.h"
                     23: 
                     24: #include "tool_util.h"
                     25: 
                     26: #include "memdebug.h" /* keep this as LAST include */
                     27: 
                     28: #if defined(WIN32) && !defined(MSDOS)
                     29: 
                     30: /* set in win32_init() */
                     31: extern LARGE_INTEGER tool_freq;
                     32: extern bool tool_isVistaOrGreater;
                     33: 
                     34: /* In case of bug fix this function has a counterpart in timeval.c */
                     35: struct timeval tvnow(void)
                     36: {
                     37:   struct timeval now;
                     38:   if(tool_isVistaOrGreater) { /* QPC timer might have issues pre-Vista */
                     39:     LARGE_INTEGER count;
                     40:     QueryPerformanceCounter(&count);
                     41:     now.tv_sec = (long)(count.QuadPart / tool_freq.QuadPart);
                     42:     now.tv_usec = (long)((count.QuadPart % tool_freq.QuadPart) * 1000000 /
                     43:                          tool_freq.QuadPart);
                     44:   }
                     45:   else {
                     46:     /* Disable /analyze warning that GetTickCount64 is preferred  */
                     47: #if defined(_MSC_VER)
                     48: #pragma warning(push)
                     49: #pragma warning(disable:28159)
                     50: #endif
                     51:     DWORD milliseconds = GetTickCount();
                     52: #if defined(_MSC_VER)
                     53: #pragma warning(pop)
                     54: #endif
                     55: 
                     56:     now.tv_sec = (long)(milliseconds / 1000);
                     57:     now.tv_usec = (long)((milliseconds % 1000) * 1000);
                     58:   }
                     59:   return now;
                     60: }
                     61: 
                     62: #elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
                     63: 
                     64: struct timeval tvnow(void)
                     65: {
                     66:   /*
                     67:   ** clock_gettime() is granted to be increased monotonically when the
                     68:   ** monotonic clock is queried. Time starting point is unspecified, it
                     69:   ** could be the system start-up time, the Epoch, or something else,
                     70:   ** in any case the time starting point does not change once that the
                     71:   ** system has started up.
                     72:   */
                     73:   struct timeval now;
                     74:   struct timespec tsnow;
                     75:   if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
                     76:     now.tv_sec = tsnow.tv_sec;
                     77:     now.tv_usec = tsnow.tv_nsec / 1000;
                     78:   }
                     79:   /*
                     80:   ** Even when the configure process has truly detected monotonic clock
                     81:   ** availability, it might happen that it is not actually available at
                     82:   ** run-time. When this occurs simply fallback to other time source.
                     83:   */
                     84: #ifdef HAVE_GETTIMEOFDAY
                     85:   else
                     86:     (void)gettimeofday(&now, NULL);
                     87: #else
                     88:   else {
                     89:     now.tv_sec = (long)time(NULL);
                     90:     now.tv_usec = 0;
                     91:   }
                     92: #endif
                     93:   return now;
                     94: }
                     95: 
                     96: #elif defined(HAVE_GETTIMEOFDAY)
                     97: 
                     98: struct timeval tvnow(void)
                     99: {
                    100:   /*
                    101:   ** gettimeofday() is not granted to be increased monotonically, due to
                    102:   ** clock drifting and external source time synchronization it can jump
                    103:   ** forward or backward in time.
                    104:   */
                    105:   struct timeval now;
                    106:   (void)gettimeofday(&now, NULL);
                    107:   return now;
                    108: }
                    109: 
                    110: #else
                    111: 
                    112: struct timeval tvnow(void)
                    113: {
                    114:   /*
                    115:   ** time() returns the value of time in seconds since the Epoch.
                    116:   */
                    117:   struct timeval now;
                    118:   now.tv_sec = (long)time(NULL);
                    119:   now.tv_usec = 0;
                    120:   return now;
                    121: }
                    122: 
                    123: #endif
                    124: 
                    125: /*
                    126:  * Make sure that the first argument is the more recent time, as otherwise
                    127:  * we'll get a weird negative time-diff back...
                    128:  *
                    129:  * Returns: the time difference in number of milliseconds.
                    130:  */
                    131: long tvdiff(struct timeval newer, struct timeval older)
                    132: {
                    133:   return (long)(newer.tv_sec-older.tv_sec)*1000+
                    134:     (long)(newer.tv_usec-older.tv_usec)/1000;
                    135: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>