]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
easy: add 'populate_fds' func to reduce size of 'wait_or_timeout'
authorGabriel Marin <gbrlmarn@gmail.com>
Mon, 24 Mar 2025 16:18:49 +0000 (18:18 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Mon, 24 Mar 2025 22:46:52 +0000 (23:46 +0100)
Closes #16820

lib/easy.c

index 1b03fddce7ce6dcf7dff6b60e2d3c527b0227b33..aa1bedec38d023f2c735dda9343550a303d54f4a 100644 (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);