From 13f4c15f214dd807899c10ebdff74ab5148d650f Mon Sep 17 00:00:00 2001 From: "/C=EU/ST=EU/CN=Pablo Neira Ayuso/emailAddress=pablo@netfilter.org" Date: Tue, 19 Feb 2008 23:04:49 +0000 Subject: compose the file descriptor set at initialization stage to save some cycles --- src/fds.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/fds.c (limited to 'src/fds.c') diff --git a/src/fds.c b/src/fds.c new file mode 100644 index 0000000..908f048 --- /dev/null +++ b/src/fds.c @@ -0,0 +1,74 @@ +/* + * (C) 2006-2008 by Pablo Neira Ayuso + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +#include +#include +#include "fds.h" + +/* we don't handle that many descriptors so eight is just fine */ +#define FDS_ARRAY_LEN 8 +#define FDS_ARRAY_SIZE (sizeof(int) * FDS_ARRAY_LEN) + +struct fds *create_fds(void) +{ + struct fds *fds; + + fds = (struct fds *) malloc(sizeof(struct fds)); + if (fds == NULL) + return NULL; + + memset(fds, 0, sizeof(struct fds)); + + fds->fd_array = (int *) malloc(FDS_ARRAY_SIZE); + if (fds->fd_array == NULL) { + free(fds); + return NULL; + } + + memset(fds->fd_array, 0, FDS_ARRAY_SIZE); + fds->fd_array_len = FDS_ARRAY_LEN; + + return fds; +} + +void destroy_fds(struct fds *fds) +{ + free(fds->fd_array); + free(fds); +} + +int register_fd(int fd, struct fds *fds) +{ + FD_SET(fd, &fds->readfds); + + if (fd > fds->maxfd) + fds->maxfd = fd; + + if (fds->fd_array_cur >= fds->fd_array_len) { + fds->fd_array_len += FDS_ARRAY_LEN; + fds->fd_array = realloc(fds->fd_array, + fds->fd_array_len * sizeof(int)); + if (fds->fd_array == NULL) { + fds->fd_array_len -= FDS_ARRAY_LEN; + return -1; + } + } + + fds->fd_array[fds->fd_array_cur++] = fd; + + return 0; +} -- cgit v1.2.3