easy: add 'populate_fds' func to reduce size of 'wait_or_timeout'

Closes #16820
This commit is contained in:
Gabriel Marin 2025-03-24 18:18:49 +02:00 committed by Daniel Stenberg
parent 088f0e6a5b
commit c329321bf9
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2

View File

@ -542,12 +542,34 @@ static void events_setup(struct Curl_multi *multi, struct events *ev)
curl_multi_setopt(multi, CURLMOPT_SOCKETDATA, ev);
}
/* populate_fds()
*
* populate the fds[] array
*/
static unsigned int populate_fds(struct pollfd *fds, struct events *ev)
{
unsigned int numfds = 0;
struct pollfd *f;
struct socketmonitor *m;
f = &fds[0];
for(m = ev->list; m; m = m->next) {
f->fd = m->socket.fd;
f->events = m->socket.events;
f->revents = 0;
#if DEBUG_EV_POLL
fprintf(stderr, "poll() %d check socket %d\n", numfds, f->fd);
#endif
f++;
numfds++;
}
return numfds;
}
/* wait_or_timeout()
*
* waits for activity on any of the given sockets, or the timeout to trigger.
*/
static CURLcode wait_or_timeout(struct Curl_multi *multi, struct events *ev)
{
bool done = FALSE;
@ -556,26 +578,10 @@ static CURLcode wait_or_timeout(struct Curl_multi *multi, struct events *ev)
while(!done) {
CURLMsg *msg;
struct socketmonitor *m;
struct pollfd *f;
struct pollfd fds[4];
int numfds = 0;
int pollrc;
int i;
struct curltime before;
/* populate the fds[] array */
f = &fds[0];
for(m = ev->list; m; m = m->next) {
f->fd = m->socket.fd;
f->events = m->socket.events;
f->revents = 0;
#if DEBUG_EV_POLL
fprintf(stderr, "poll() %d check socket %d\n", numfds, f->fd);
#endif
f++;
numfds++;
}
const unsigned int numfds = populate_fds(fds, ev);
/* get the time stamp to use to figure out how long poll takes */
before = Curl_now();
@ -583,11 +589,11 @@ static CURLcode wait_or_timeout(struct Curl_multi *multi, struct events *ev)
if(numfds) {
/* wait for activity or timeout */
#if DEBUG_EV_POLL
fprintf(stderr, "poll(numfds=%d, timeout=%ldms)\n", numfds, ev->ms);
fprintf(stderr, "poll(numfds=%u, timeout=%ldms)\n", numfds, ev->ms);
#endif
pollrc = Curl_poll(fds, (unsigned int)numfds, ev->ms);
pollrc = Curl_poll(fds, numfds, ev->ms);
#if DEBUG_EV_POLL
fprintf(stderr, "poll(numfds=%d, timeout=%ldms) -> %d\n",
fprintf(stderr, "poll(numfds=%u, timeout=%ldms) -> %d\n",
numfds, ev->ms, pollrc);
#endif
if(pollrc < 0)
@ -615,6 +621,7 @@ static CURLcode wait_or_timeout(struct Curl_multi *multi, struct events *ev)
/* here pollrc is > 0 */
struct Curl_llist_node *e = Curl_llist_head(&multi->process);
struct Curl_easy *data;
unsigned int i;
DEBUGASSERT(e);
data = Curl_node_elem(e);
DEBUGASSERT(data);