From b53e10a1739b31cb8900ea792572d15919edbcea Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Fri, 25 Mar 2016 11:26:20 -0400 Subject: [PATCH] lib: Size the pollfds array once The pollfds was being resized if the # of fds grew to be more than the original array size. Just size it once. Ticket: CM-10077 Signed-off-by: Donald Sharp Reviewed-by: Don Slice --- lib/thread.c | 29 +++-------------------------- 1 file changed, 3 insertions(+), 26 deletions(-) diff --git a/lib/thread.c b/lib/thread.c index bb524f0ad0..b1d2d91903 100644 --- a/lib/thread.c +++ b/lib/thread.c @@ -560,7 +560,7 @@ thread_master_create (void) rv->timer->update = rv->background->update = thread_timer_update; #if defined(HAVE_POLL) - rv->handler.pfdsize = 64; + rv->handler.pfdsize = rv->fd_limit; rv->handler.pfdcount = 0; rv->handler.pfds = (struct pollfd *) malloc (sizeof (struct pollfd) * rv->handler.pfdsize); memset (rv->handler.pfds, 0, sizeof (struct pollfd) * rv->handler.pfdsize); @@ -795,25 +795,6 @@ thread_get (struct thread_master *m, u_char type, #define fd_copy_fd_set(X) (X) -static short -realloc_pfds (struct thread_master *m, int fd) -{ - size_t oldpfdlen = m->handler.pfdsize * sizeof(struct pollfd); - void *newpfd = NULL; - - m->handler.pfdsize *= 2; - newpfd = XREALLOC (MTYPE_THREAD, m->handler.pfds, m->handler.pfdsize * sizeof(struct pollfd)); - if (newpfd == NULL) - { - close(fd); - zlog (NULL, LOG_ERR, "failed to allocate space for pollfds"); - return 0; - } - memset((struct pollfd*)newpfd + (m->handler.pfdsize / 2), 0, oldpfdlen); - m->handler.pfds = (struct pollfd*)newpfd; - return 1; -} - /* generic add thread function */ static struct thread * generic_thread_add(struct thread_master *m, int (*func) (struct thread *), @@ -845,9 +826,7 @@ generic_thread_add(struct thread_master *m, int (*func) (struct thread *), } /* is there enough space for a new fd? */ - if (queuepos >= m->handler.pfdsize) - if (realloc_pfds(m, fd) == 0) - return NULL; + assert (queuepos < m->handler.pfdsize); thread = thread_get (m, type, func, arg, funcname); m->handler.pfds[queuepos].fd = fd; @@ -1244,9 +1223,7 @@ add_snmp_pollfds(struct thread_master *m, fd_set *snmpfds, int fdsetsize) { if (FD_ISSET(i, snmpfds)) { - if (m->handler.pfdcountsnmp > m->handler.pfdsize) - if (realloc_pfds(m, i) < 0) - return; + assert (m->handler.pfdcountsnmp <= m->handler.pfdsize); m->handler.pfds[m->handler.pfdcountsnmp].fd = i; m->handler.pfds[m->handler.pfdcountsnmp].events = POLLIN; -- 2.39.5