testutil.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include <curl/curl.h>
  26. #include "testutil.h"
  27. #include "memdebug.h"
  28. #if defined(WIN32) && !defined(MSDOS)
  29. struct timeval tutil_tvnow(void)
  30. {
  31. /*
  32. ** GetTickCount() is available on _all_ Windows versions from W95 up
  33. ** to nowadays. Returns milliseconds elapsed since last system boot,
  34. ** increases monotonically and wraps once 49.7 days have elapsed.
  35. */
  36. struct timeval now;
  37. DWORD milliseconds = GetTickCount();
  38. now.tv_sec = milliseconds / 1000;
  39. now.tv_usec = (milliseconds % 1000) * 1000;
  40. return now;
  41. }
  42. #elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
  43. struct timeval tutil_tvnow(void)
  44. {
  45. /*
  46. ** clock_gettime() is granted to be increased monotonically when the
  47. ** monotonic clock is queried. Time starting point is unspecified, it
  48. ** could be the system start-up time, the Epoch, or something else,
  49. ** in any case the time starting point does not change once that the
  50. ** system has started up.
  51. */
  52. struct timeval now;
  53. struct timespec tsnow;
  54. if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
  55. now.tv_sec = tsnow.tv_sec;
  56. now.tv_usec = (int)(tsnow.tv_nsec / 1000);
  57. }
  58. /*
  59. ** Even when the configure process has truly detected monotonic clock
  60. ** availability, it might happen that it is not actually available at
  61. ** run-time. When this occurs simply fallback to other time source.
  62. */
  63. #ifdef HAVE_GETTIMEOFDAY
  64. else
  65. (void)gettimeofday(&now, NULL);
  66. #else
  67. else {
  68. now.tv_sec = time(NULL);
  69. now.tv_usec = 0;
  70. }
  71. #endif
  72. return now;
  73. }
  74. #elif defined(HAVE_GETTIMEOFDAY)
  75. struct timeval tutil_tvnow(void)
  76. {
  77. /*
  78. ** gettimeofday() is not granted to be increased monotonically, due to
  79. ** clock drifting and external source time synchronization it can jump
  80. ** forward or backward in time.
  81. */
  82. struct timeval now;
  83. (void)gettimeofday(&now, NULL);
  84. return now;
  85. }
  86. #else
  87. struct timeval tutil_tvnow(void)
  88. {
  89. /*
  90. ** time() returns the value of time in seconds since the Epoch.
  91. */
  92. struct timeval now;
  93. now.tv_sec = time(NULL);
  94. now.tv_usec = 0;
  95. return now;
  96. }
  97. #endif
  98. /*
  99. * Make sure that the first argument is the more recent time, as otherwise
  100. * we'll get a weird negative time-diff back...
  101. *
  102. * Returns: the time difference in number of milliseconds.
  103. */
  104. long tutil_tvdiff(struct timeval newer, struct timeval older)
  105. {
  106. return (long)(newer.tv_sec-older.tv_sec)*1000+
  107. (long)(newer.tv_usec-older.tv_usec)/1000;
  108. }
  109. /*
  110. * Same as tutil_tvdiff but with full usec resolution.
  111. *
  112. * Returns: the time difference in seconds with subsecond resolution.
  113. */
  114. double tutil_tvdiff_secs(struct timeval newer, struct timeval older)
  115. {
  116. if(newer.tv_sec != older.tv_sec)
  117. return (double)(newer.tv_sec-older.tv_sec)+
  118. (double)(newer.tv_usec-older.tv_usec)/1000000.0;
  119. return (double)(newer.tv_usec-older.tv_usec)/1000000.0;
  120. }
  121. #ifdef WIN32
  122. HMODULE win32_load_system_library(const TCHAR *filename)
  123. {
  124. size_t filenamelen = _tcslen(filename);
  125. size_t systemdirlen = GetSystemDirectory(NULL, 0);
  126. size_t written;
  127. TCHAR *path;
  128. if(!filenamelen || filenamelen > 32768 ||
  129. !systemdirlen || systemdirlen > 32768)
  130. return NULL;
  131. /* systemdirlen includes null character */
  132. path = malloc(sizeof(TCHAR) * (systemdirlen + 1 + filenamelen));
  133. if(!path)
  134. return NULL;
  135. /* if written >= systemdirlen then nothing was written */
  136. written = GetSystemDirectory(path, (unsigned int)systemdirlen);
  137. if(!written || written >= systemdirlen)
  138. return NULL;
  139. if(path[written - 1] != _T('\\'))
  140. path[written++] = _T('\\');
  141. _tcscpy(path + written, filename);
  142. return LoadLibrary(path);
  143. }
  144. #endif