timediff.[ch]: add curlx helper functions for timeval conversions

Also move timediff_t definitions from timeval.h to timediff.h and
then make timeval.h include the new standalone-capable timediff.h.

Reviewed-by: Jay Satiro
Reviewed-by: Daniel Stenberg

Supersedes #5888
Closes #8595
This commit is contained in:
Marc Hoersken 2020-08-29 12:54:30 +02:00
parent 80eb71a3f5
commit 68035af29d
No known key found for this signature in database
GPG key ID: 61E03CBED7BC859E
16 changed files with 176 additions and 85 deletions

View file

@ -207,6 +207,7 @@ LIB_CFILES = \
system_win32.c \
telnet.c \
tftp.c \
timediff.c \
timeval.c \
transfer.c \
url.c \
@ -327,6 +328,7 @@ LIB_HFILES = \
system_win32.h \
telnet.h \
tftp.h \
timediff.h \
timeval.h \
transfer.h \
url.h \

View file

@ -65,6 +65,7 @@
#include "connect.h"
#include "select.h"
#include "progress.h"
#include "timediff.h"
# if defined(CURL_STATICLIB) && !defined(CARES_STATICLIB) && \
defined(WIN32)
@ -290,7 +291,7 @@ int Curl_resolver_getsock(struct Curl_easy *data,
timeout = ares_timeout((ares_channel)data->state.async.resolver, &maxtime,
&timebuf);
milli = (timeout->tv_sec * 1000) + (timeout->tv_usec/1000);
milli = (long)curlx_tvtoms(timeout);
if(milli == 0)
milli += 10;
Curl_expire(data, milli, EXPIRE_ASYNC_NAME);

View file

@ -43,7 +43,7 @@
#include "urldata.h"
#include "connect.h"
#include "select.h"
#include "timeval.h"
#include "timediff.h"
#include "warnless.h"
/*
@ -93,26 +93,7 @@ int Curl_wait_ms(timediff_t timeout_ms)
#else
{
struct timeval pending_tv;
timediff_t tv_sec = timeout_ms / 1000;
timediff_t tv_usec = (timeout_ms % 1000) * 1000; /* max=999999 */
#ifdef HAVE_SUSECONDS_T
#if TIMEDIFF_T_MAX > TIME_T_MAX
/* tv_sec overflow check in case time_t is signed */
if(tv_sec > TIME_T_MAX)
tv_sec = TIME_T_MAX;
#endif
pending_tv.tv_sec = (time_t)tv_sec;
pending_tv.tv_usec = (suseconds_t)tv_usec;
#else
#if TIMEDIFF_T_MAX > INT_MAX
/* tv_sec overflow check in case time_t is signed */
if(tv_sec > INT_MAX)
tv_sec = INT_MAX;
#endif
pending_tv.tv_sec = (int)tv_sec;
pending_tv.tv_usec = (int)tv_usec;
#endif
r = select(0, NULL, NULL, NULL, &pending_tv);
r = select(0, NULL, NULL, NULL, curlx_mstotv(&pending_tv, timeout_ms));
}
#endif /* HAVE_POLL_FINE */
#endif /* USE_WINSOCK */
@ -152,43 +133,7 @@ static int our_select(curl_socket_t maxfd, /* highest socket number */
}
#endif
ptimeout = &pending_tv;
if(timeout_ms < 0) {
ptimeout = NULL;
}
else if(timeout_ms > 0) {
timediff_t tv_sec = timeout_ms / 1000;
timediff_t tv_usec = (timeout_ms % 1000) * 1000; /* max=999999 */
#ifdef HAVE_SUSECONDS_T
#if TIMEDIFF_T_MAX > TIME_T_MAX
/* tv_sec overflow check in case time_t is signed */
if(tv_sec > TIME_T_MAX)
tv_sec = TIME_T_MAX;
#endif
pending_tv.tv_sec = (time_t)tv_sec;
pending_tv.tv_usec = (suseconds_t)tv_usec;
#elif defined(WIN32) /* maybe also others in the future */
#if TIMEDIFF_T_MAX > LONG_MAX
/* tv_sec overflow check on Windows there we know it is long */
if(tv_sec > LONG_MAX)
tv_sec = LONG_MAX;
#endif
pending_tv.tv_sec = (long)tv_sec;
pending_tv.tv_usec = (long)tv_usec;
#else
#if TIMEDIFF_T_MAX > INT_MAX
/* tv_sec overflow check in case time_t is signed */
if(tv_sec > INT_MAX)
tv_sec = INT_MAX;
#endif
pending_tv.tv_sec = (int)tv_sec;
pending_tv.tv_usec = (int)tv_usec;
#endif
}
else {
pending_tv.tv_sec = 0;
pending_tv.tv_usec = 0;
}
ptimeout = curlx_mstotv(&pending_tv, timeout_ms);
#ifdef USE_WINSOCK
/* WinSock select() must not be called with an fd_set that contains zero

84
lib/timediff.c Normal file
View file

@ -0,0 +1,84 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************/
#include "timediff.h"
/*
* Converts number of milliseconds into a timeval structure.
*
* Return values:
* NULL IF tv is NULL or ms < 0 (eg. no timeout -> blocking select)
* tv with 0 in both fields IF ms == 0 (eg. 0ms timeout -> polling select)
* tv with converted fields IF ms > 0 (eg. >0ms timeout -> waiting select)
*/
struct timeval *curlx_mstotv(struct timeval *tv, timediff_t ms)
{
if(!tv)
return NULL;
if(ms < 0)
return NULL;
if(ms > 0) {
timediff_t tv_sec = ms / 1000;
timediff_t tv_usec = (ms % 1000) * 1000; /* max=999999 */
#ifdef HAVE_SUSECONDS_T
#if TIMEDIFF_T_MAX > TIME_T_MAX
/* tv_sec overflow check in case time_t is signed */
if(tv_sec > TIME_T_MAX)
tv_sec = TIME_T_MAX;
#endif
tv->tv_sec = (time_t)tv_sec;
tv->tv_usec = (suseconds_t)tv_usec;
#elif defined(WIN32) /* maybe also others in the future */
#if TIMEDIFF_T_MAX > LONG_MAX
/* tv_sec overflow check on Windows there we know it is long */
if(tv_sec > LONG_MAX)
tv_sec = LONG_MAX;
#endif
tv->tv_sec = (long)tv_sec;
tv->tv_usec = (long)tv_usec;
#else
#if TIMEDIFF_T_MAX > INT_MAX
/* tv_sec overflow check in case time_t is signed */
if(tv_sec > INT_MAX)
tv_sec = INT_MAX;
#endif
tv->tv_sec = (int)tv_sec;
tv->tv_usec = (int)tv_usec;
#endif
}
else {
tv->tv_sec = 0;
tv->tv_usec = 0;
}
return tv;
}
/*
* Converts a timeval structure into number of milliseconds.
*/
timediff_t curlx_tvtoms(struct timeval *tv)
{
return (tv->tv_sec*1000) + (timediff_t)(((double)tv->tv_usec)/1000.0);
}

50
lib/timediff.h Normal file
View file

@ -0,0 +1,50 @@
#ifndef HEADER_CURL_TIMEDIFF_H
#define HEADER_CURL_TIMEDIFF_H
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************/
#include "curl_setup.h"
/* Use a larger type even for 32 bit time_t systems so that we can keep
microsecond accuracy in it */
typedef curl_off_t timediff_t;
#define CURL_FORMAT_TIMEDIFF_T CURL_FORMAT_CURL_OFF_T
#define TIMEDIFF_T_MAX CURL_OFF_T_MAX
#define TIMEDIFF_T_MIN CURL_OFF_T_MIN
/*
* Converts number of milliseconds into a timeval structure.
*
* Return values:
* NULL IF tv is NULL or ms < 0 (eg. no timeout -> blocking select)
* tv with 0 in both fields IF ms == 0 (eg. 0ms timeout -> polling select)
* tv with converted fields IF ms > 0 (eg. >0ms timeout -> waiting select)
*/
struct timeval *curlx_mstotv(struct timeval *tv, timediff_t ms);
/*
* Converts a timeval structure into number of milliseconds.
*/
timediff_t curlx_tvtoms(struct timeval *tv);
#endif /* HEADER_CURL_TIMEDIFF_H */

View file

@ -24,13 +24,7 @@
#include "curl_setup.h"
/* Use a larger type even for 32 bit time_t systems so that we can keep
microsecond accuracy in it */
typedef curl_off_t timediff_t;
#define CURL_FORMAT_TIMEDIFF_T CURL_FORMAT_CURL_OFF_T
#define TIMEDIFF_T_MAX CURL_OFF_T_MAX
#define TIMEDIFF_T_MIN CURL_OFF_T_MIN
#include "timediff.h"
struct curltime {
time_t tv_sec; /* seconds */

View file

@ -74,6 +74,7 @@
#include "connect.h" /* for the connect timeout */
#include "select.h"
#include "strcase.h"
#include "timediff.h"
#include "x509asn1.h"
#include "curl_printf.h"
@ -975,11 +976,12 @@ static CURLcode gskit_connect_step2(struct Curl_easy *data,
for(;;) {
timediff_t timeout_ms = nonblocking? 0: Curl_timeleft(data, NULL, TRUE);
stmv.tv_sec = 0;
stmv.tv_usec = 0;
if(timeout_ms < 0)
timeout_ms = 0;
stmv.tv_sec = timeout_ms / 1000;
stmv.tv_usec = (timeout_ms - stmv.tv_sec * 1000) * 1000;
switch(QsoWaitForIOCompletion(BACKEND->iocport, &cstat, &stmv)) {
switch(QsoWaitForIOCompletion(BACKEND->iocport, &cstat,
curlx_mstotv(&stmv, timeout_ms))) {
case 1: /* Operation complete. */
break;
case -1: /* An error occurred: handshake still in progress. */