Skip to content

Commit

Permalink
curl_multi_wait: avoid second loop if nothing to do
Browse files Browse the repository at this point in the history
... hopefully this will also make clang-analyzer stop warning on
potentional NULL dereferences (which were false positives anyway).
  • Loading branch information
bagder committed Mar 9, 2013
1 parent 64b2d2d commit 136a3a0
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions lib/multi.c
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,8 @@ CURLMcode curl_multi_wait(CURLM *multi_handle,
curl_socket_t sockbunch[MAX_SOCKSPEREASYHANDLE];
int bitmap;
unsigned int i;
unsigned int nfds = extra_nfds;
unsigned int nfds = 0;
unsigned int curlfds;
struct pollfd *ufds = NULL;

if(!GOOD_MULTI_HANDLE(multi))
Expand Down Expand Up @@ -832,39 +833,47 @@ CURLMcode curl_multi_wait(CURLM *multi_handle,
easy = easy->next; /* check next handle */
}

curlfds = nfds; /* number of internal file descriptors */
nfds += extra_nfds; /* add the externally provided ones */

if(nfds) {
ufds = malloc(nfds * sizeof(struct pollfd));
if(!ufds)
return CURLM_OUT_OF_MEMORY;
}
nfds = 0;

/* Add the curl handles to our pollfds first */
easy=multi->easy.next;
while(easy != &multi->easy) {
bitmap = multi_getsock(easy, sockbunch, MAX_SOCKSPEREASYHANDLE);
/* only do the second loop if we found descriptors in the first stage run
above */

for(i=0; i< MAX_SOCKSPEREASYHANDLE; i++) {
curl_socket_t s = CURL_SOCKET_BAD;
if(curlfds) {
/* Add the curl handles to our pollfds first */
easy=multi->easy.next;
while(easy != &multi->easy) {
bitmap = multi_getsock(easy, sockbunch, MAX_SOCKSPEREASYHANDLE);

if(bitmap & GETSOCK_READSOCK(i)) {
ufds[nfds].fd = sockbunch[i];
ufds[nfds].events = POLLIN;
++nfds;
s = sockbunch[i];
}
if(bitmap & GETSOCK_WRITESOCK(i)) {
ufds[nfds].fd = sockbunch[i];
ufds[nfds].events = POLLOUT;
++nfds;
s = sockbunch[i];
}
if(s == CURL_SOCKET_BAD) {
break;
for(i=0; i< MAX_SOCKSPEREASYHANDLE; i++) {
curl_socket_t s = CURL_SOCKET_BAD;

if(bitmap & GETSOCK_READSOCK(i)) {
ufds[nfds].fd = sockbunch[i];
ufds[nfds].events = POLLIN;
++nfds;
s = sockbunch[i];
}
if(bitmap & GETSOCK_WRITESOCK(i)) {
ufds[nfds].fd = sockbunch[i];
ufds[nfds].events = POLLOUT;
++nfds;
s = sockbunch[i];
}
if(s == CURL_SOCKET_BAD) {
break;
}
}
}

easy = easy->next; /* check next handle */
easy = easy->next; /* check next handle */
}
}

/* Add external file descriptions from poll-like struct curl_waitfd */
Expand Down

0 comments on commit 136a3a0

Please sign in to comment.