From: Tomas Mraz Date: Thu, 10 Oct 2024 13:49:29 +0000 (+0200) Subject: poll_builder.c: Minor fixes and optimizations X-Git-Tag: openssl-3.5.0-alpha1~333 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b7e1d375f1c14804882dbbc247a967cc32e1f61b;p=thirdparty%2Fopenssl.git poll_builder.c: Minor fixes and optimizations Reviewed-by: Matt Caswell Reviewed-by: Saša Nedvědický (Merged from https://github.com/openssl/openssl/pull/25416) --- diff --git a/ssl/rio/poll_builder.c b/ssl/rio/poll_builder.c index 166905d4a88..f0e39ccc82a 100644 --- a/ssl/rio/poll_builder.c +++ b/ssl/rio/poll_builder.c @@ -57,6 +57,10 @@ static int rpb_ensure_alloc(RIO_POLL_BUILDER *rpb, size_t alloc) if (pfd_heap_new == NULL) return 0; + if (rpb->pfd_heap == NULL) { + /* Copy the contents of the stacked array. */ + memcpy(pfd_heap_new, rpb->pfds, sizeof(rpb->pfds)); + } rpb->pfd_heap = pfd_heap_new; rpb->pfd_alloc = alloc; return 1; @@ -67,7 +71,9 @@ int ossl_rio_poll_builder_add_fd(RIO_POLL_BUILDER *rpb, int fd, int want_read, int want_write) { #if RIO_POLL_METHOD == RIO_POLL_METHOD_POLL - size_t num_loop; + size_t i; + struct pollfd *pfds = (rpb->pfd_heap != NULL ? rpb->pfd_heap : rpb->pfds); + struct pollfd *pfd; #endif if (fd < 0) @@ -97,44 +103,31 @@ int ossl_rio_poll_builder_add_fd(RIO_POLL_BUILDER *rpb, int fd, return 1; #elif RIO_POLL_METHOD == RIO_POLL_METHOD_POLL - for (num_loop = 0;; ++num_loop) { - size_t i; - struct pollfd *pfds = (rpb->pfd_heap != NULL ? rpb->pfd_heap : rpb->pfds); - struct pollfd *pfd; - - for (i = 0; i < rpb->pfd_num; ++i) { - pfd = &pfds[i]; - - if (pfd->fd == -1 || pfd->fd == fd) - break; - } + for (i = 0; i < rpb->pfd_num; ++i) { + pfd = &pfds[i]; - if (i >= rpb->pfd_alloc) { - /* Was not able to find room - enlarge and try again. */ - if (num_loop > 0) - /* Should not happen twice. */ - return 0; - - if (!rpb_ensure_alloc(rpb, rpb->pfd_alloc * 2)) - return 0; + if (pfd->fd == -1 || pfd->fd == fd) + break; + } - continue; - } + if (i >= rpb->pfd_alloc) { + if (!rpb_ensure_alloc(rpb, rpb->pfd_alloc * 2)) + return 0; + } - assert(i <= rpb->pfd_num && rpb->pfd_num <= rpb->pfd_alloc); - pfds[i].fd = fd; - pfds[i].events = 0; + assert(i <= rpb->pfd_num && rpb->pfd_num <= rpb->pfd_alloc); + pfds[i].fd = fd; + pfds[i].events = 0; - if (want_read) - pfds[i].events |= POLLIN; - if (want_write) - pfds[i].events |= POLLOUT; + if (want_read) + pfds[i].events |= POLLIN; + if (want_write) + pfds[i].events |= POLLOUT; - if (i == rpb->pfd_num) - ++rpb->pfd_num; + if (i == rpb->pfd_num) + ++rpb->pfd_num; - return 1; - } + return 1; #endif }