summaryrefslogtreecommitdiff
path: root/nuttx/netutils/thttpd/fdwatch.c
diff options
context:
space:
mode:
Diffstat (limited to 'nuttx/netutils/thttpd/fdwatch.c')
-rw-r--r--nuttx/netutils/thttpd/fdwatch.c213
1 files changed, 116 insertions, 97 deletions
diff --git a/nuttx/netutils/thttpd/fdwatch.c b/nuttx/netutils/thttpd/fdwatch.c
index 2f82fc19c..724631918 100644
--- a/nuttx/netutils/thttpd/fdwatch.c
+++ b/nuttx/netutils/thttpd/fdwatch.c
@@ -71,18 +71,9 @@
****************************************************************************/
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
-static long nwatches;
+static long nwatches = 0;
#endif
-static int *fd_rw;
-static void **fd_data;
-static struct pollfd *pollfds;
-static int *poll_pollndx;
-static int *poll_rfdidx;
-static int npoll_fds;
-static int nreturned;
-static int next_rfndx;
-
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
@@ -97,87 +88,123 @@ static int next_rfndx;
/* Initialize the fdwatch data structures. Returns -1 on failure. */
-int fdwatch_initialize(void)
+struct fdwatch_s *fdwatch_initialize(int nfds)
{
+ struct fdwatch_s *fw;
int i;
+ /* Allocate the fdwatch data structure */
+
+ fw = (struct fdwatch_s*)zalloc(sizeof(struct fdwatch_s));
+ if (!fw)
+ {
+ ndbg("Failed to allocate fdwatch\n");
+ return NULL;
+ }
+
/* Initialize the fdwatch data structures. */
-#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
- nwatches = 0;
-#endif
+ fw->nfds = nfds;
- fd_rw = (int *)malloc(sizeof(int) * CONFIG_NSOCKET_DESCRIPTORS);
- if (!fd_rw)
+ fw->fd_rw = (int*)malloc(sizeof(int) * nfds);
+ if (!fw->fd_rw)
{
- goto errout;
+ goto errout_with_allocations;
}
- fd_data = (void **)malloc(sizeof(void*) * CONFIG_NSOCKET_DESCRIPTORS);
- if (!fd_data)
+ fw->fd_data = (void**)malloc(sizeof(void*) * nfds);
+ if (!fw->fd_data)
{
- goto errout_with_fd_rw;
+ goto errout_with_allocations;
}
- for (i = 0; i < CONFIG_NSOCKET_DESCRIPTORS; ++i)
+ for (i = 0; i < nfds; ++i)
{
- fd_rw[i] = -1;
+ fw->fd_rw[i] = -1;
}
- pollfds = (struct pollfd *)malloc(sizeof(struct pollfd) * CONFIG_NSOCKET_DESCRIPTORS);
- if (!pollfds)
+ fw->pollfds = (struct pollfd*)malloc(sizeof(struct pollfd) * nfds);
+ if (!fw->pollfds)
{
- goto errout_with_fd_data;
+ goto errout_with_allocations;
}
- poll_pollndx = (int *)malloc(sizeof(int) * CONFIG_NSOCKET_DESCRIPTORS);
- if (!poll_pollndx)
+ fw->poll_pollndx = (int*)malloc(sizeof(int) * nfds);
+ if (!fw->poll_pollndx)
{
- goto errout_with_pollfds;
+ goto errout_with_allocations;
}
- poll_rfdidx = (int *)malloc(sizeof(int) * CONFIG_NSOCKET_DESCRIPTORS);
- if (!poll_rfdidx)
+ fw->poll_rfdidx = (int*)malloc(sizeof(int) * nfds);
+ if (!fw->poll_rfdidx)
{
- goto errout_with_poll_pollndx;
+ goto errout_with_allocations;
}
- for (i = 0; i < CONFIG_NSOCKET_DESCRIPTORS; i++)
+ for (i = 0; i < nfds; i++)
{
- pollfds[i].fd = poll_pollndx[i] = -1;
+ fw->pollfds[i].fd = fw->poll_pollndx[i] = -1;
}
- return OK;
-
-errout_with_poll_pollndx:
- free(poll_pollndx);
-errout_with_pollfds:
- free(pollfds);
-errout_with_fd_data:
- free(fd_data);
-errout_with_fd_rw:
- free(fd_rw);
-errout:
- return ERROR;
+ return fw;
+
+errout_with_allocations:
+ fdwatch_uninitialize(fw);
+ return NULL;
+}
+
+/* Uninitialize the fwdatch data structure */
+
+void fdwatch_uninitialize(struct fdwatch_s *fw)
+{
+ if (fw)
+ {
+ if (fw->fd_rw)
+ {
+ free(fw->fd_rw);
+ }
+
+ if (fw->fd_data)
+ {
+ free(fw->fd_data);
+ }
+
+ if (fw->pollfds)
+ {
+ free(fw->pollfds);
+ }
+
+ if (fw->poll_pollndx)
+ {
+ free(fw->poll_pollndx);
+ }
+
+ if (fw->poll_rfdidx)
+ {
+ free(fw->poll_rfdidx);
+ }
+
+ free(fw);
+ }
}
/* Add a descriptor to the watch list. rw is either FDW_READ or FDW_WRITE. */
-void fdwatch_add_fd(int fd, void *client_data, int rw)
+void fdwatch_add_fd(struct fdwatch_s *fw, int fd, void *client_data, int rw)
{
int fdndx;
#ifdef CONFIG_DEBUG
if (fd < CONFIG_NFILE_DESCRIPTORS ||
- fd >= CONFIG_NFILE_DESCRIPTORS+CONFIG_NSOCKET_DESCRIPTORS ||
- fd_rw[fd-CONFIG_NFILE_DESCRIPTORS] != -1)
+ fd >= CONFIG_NFILE_DESCRIPTORS+fw->nfds ||
+ fw->fd_rw[fd-CONFIG_NFILE_DESCRIPTORS] != -1)
{
ndbg("Received bad fd (%d)\n", fd);
return;
}
#endif
- if (npoll_fds >= CONFIG_NSOCKET_DESCRIPTORS)
+ if (fw->npoll_fds >= fw->nfds)
{
ndbg("too many fds\n");
return;
@@ -189,31 +216,31 @@ void fdwatch_add_fd(int fd, void *client_data, int rw)
/* Save the new fd at the end of the list */
- pollfds[npoll_fds].fd = fd;
+ fw->pollfds[fw->npoll_fds].fd = fd;
switch (rw)
{
default:
case FDW_READ:
- pollfds[npoll_fds].events = POLLIN;
+ fw->pollfds[fw->npoll_fds].events = POLLIN;
break;
case FDW_WRITE:
- pollfds[npoll_fds].events = POLLOUT;
+ fw->pollfds[fw->npoll_fds].events = POLLOUT;
break;
}
/* Save the new index and increment the cound of watched descriptors */
- poll_pollndx[fdndx] = npoll_fds;
- npoll_fds++;
+ fw->poll_pollndx[fdndx] = fw->npoll_fds;
+ fw->npoll_fds++;
- fd_rw[fdndx] = rw;
- fd_data[fdndx] = client_data;
+ fw->fd_rw[fdndx] = rw;
+ fw->fd_data[fdndx] = client_data;
}
/* Remove a descriptor from the watch list. */
-void fdwatch_del_fd(int fd)
+void fdwatch_del_fd(struct fdwatch_s *fw, int fd)
{
int fdndx;
int pollndx;
@@ -221,8 +248,8 @@ void fdwatch_del_fd(int fd)
#ifdef CONFIG_DEBUG
if (fd < CONFIG_NFILE_DESCRIPTORS ||
- fd >= CONFIG_NFILE_DESCRIPTORS+CONFIG_NSOCKET_DESCRIPTORS ||
- fd_rw[fd-CONFIG_NFILE_DESCRIPTORS] == -1)
+ fd >= CONFIG_NFILE_DESCRIPTORS+fw->nfds ||
+ fw->fd_rw[fd-CONFIG_NFILE_DESCRIPTORS] == -1)
{
ndbg("Received bad fd: %d\n", fd);
return;
@@ -234,10 +261,10 @@ void fdwatch_del_fd(int fd)
*/
fdndx = fd-CONFIG_NFILE_DESCRIPTORS;
- pollndx = poll_pollndx[fdndx];
+ pollndx = fw->poll_pollndx[fdndx];
#ifdef CONFIG_DEBUG
- if (pollndx < 0 || pollndx >= CONFIG_NSOCKET_DESCRIPTORS)
+ if (pollndx < 0 || pollndx >= fw->nfds)
{
ndbg("Bad poll index: %d\n", pollndx);
return;
@@ -246,20 +273,20 @@ void fdwatch_del_fd(int fd)
/* Decrement the number of fds in the poll table */
- npoll_fds--;
+ fw->npoll_fds--;
/* Replace the deleted one with the one at the the end
* of the list.
*/
- tmpndx = pollfds[pollndx].fd - CONFIG_NFILE_DESCRIPTORS;
- pollfds[pollndx] = pollfds[npoll_fds];
- poll_pollndx[tmpndx] = poll_pollndx[fdndx];;
- pollfds[npoll_fds].fd = -1;
- poll_pollndx[fdndx] = -1;
+ tmpndx = fw->pollfds[pollndx].fd - CONFIG_NFILE_DESCRIPTORS;
+ fw->pollfds[pollndx] = fw->pollfds[fw->npoll_fds];
+ fw->poll_pollndx[tmpndx] = fw->poll_pollndx[fdndx];;
+ fw->pollfds[fw->npoll_fds].fd = -1;
+ fw->poll_pollndx[fdndx] = -1;
- fd_rw[fdndx] = -1;
- fd_data[fdndx] = NULL;
+ fw->fd_rw[fdndx] = -1;
+ fw->fd_data[fdndx] = NULL;
}
/* Do the watch. Return value is the number of descriptors that are ready,
@@ -267,7 +294,7 @@ void fdwatch_del_fd(int fd)
* wait indefinitely.
*/
-int fdwatch(long timeout_msecs)
+int fdwatch(struct fdwatch_s *fw, long timeout_msecs)
{
int rfndx;
int ret;
@@ -277,18 +304,18 @@ int fdwatch(long timeout_msecs)
nwatches++;
#endif
- ret = poll(pollfds, npoll_fds, (int)timeout_msecs);
+ ret = poll(fw->pollfds, fw->npoll_fds, (int)timeout_msecs);
if (ret <= 0)
{
return ret;
}
rfndx = 0;
- for (i = 0; i < npoll_fds; i++)
+ for (i = 0; i < fw->npoll_fds; i++)
{
- if (pollfds[i].revents & (POLLIN | POLLOUT | POLLERR | POLLHUP | POLLNVAL))
+ if (fw->pollfds[i].revents & (POLLIN | POLLOUT | POLLERR | POLLHUP | POLLNVAL))
{
- poll_rfdidx[rfndx++] = pollfds[i].fd;
+ fw->poll_rfdidx[rfndx++] = fw->pollfds[i].fd;
if (rfndx == ret)
{
break;
@@ -296,21 +323,20 @@ int fdwatch(long timeout_msecs)
}
}
- next_rfndx = 0;
return rfndx;
}
/* Check if a descriptor was ready. */
-int fdwatch_check_fd(int fd)
+int fdwatch_check_fd(struct fdwatch_s *fw, int fd)
{
int fdndx;
int pollndx;
#ifdef CONFIG_DEBUG
if (fd < CONFIG_NFILE_DESCRIPTORS ||
- fd >= CONFIG_NFILE_DESCRIPTORS+CONFIG_NSOCKET_DESCRIPTORS ||
- fd_rw[fd-CONFIG_NFILE_DESCRIPTORS] == -1)
+ fd >= CONFIG_NFILE_DESCRIPTORS+fw->nfds ||
+ fw->fd_rw[fd-CONFIG_NFILE_DESCRIPTORS] == -1)
{
ndbg("Bad fd: %d\n", fd);
return 0;
@@ -322,28 +348,28 @@ int fdwatch_check_fd(int fd)
*/
fdndx = fd-CONFIG_NFILE_DESCRIPTORS;
- pollndx = poll_pollndx[fdndx];
+ pollndx = fw->poll_pollndx[fdndx];
#ifdef CONFIG_DEBUG
- if (pollndx < 0 || pollndx >= CONFIG_NSOCKET_DESCRIPTORS)
+ if (pollndx < 0 || pollndx >= fw->nfds)
{
ndbg("Bad poll index: %d\n", pollndx);
return 0;
}
#endif
- if (pollfds[pollndx].revents & POLLERR)
+ if (fw->pollfds[pollndx].revents & POLLERR)
{
return 0;
}
- switch (fd_rw[fdndx])
+ switch (fw->fd_rw[fdndx])
{
case FDW_READ:
- return pollfds[pollndx].revents & (POLLIN | POLLHUP | POLLNVAL);
+ return fw->pollfds[pollndx].revents & (POLLIN | POLLHUP | POLLNVAL);
case FDW_WRITE:
- return pollfds[pollndx].revents & (POLLOUT | POLLHUP | POLLNVAL);
+ return fw->pollfds[pollndx].revents & (POLLOUT | POLLHUP | POLLNVAL);
default:
break;
@@ -351,40 +377,33 @@ int fdwatch_check_fd(int fd)
return 0;
}
-void *fdwatch_get_next_client_data(void)
+void *fdwatch_get_next_client_data(struct fdwatch_s *fw)
{
int rfndx;
int fdndx;
int fd;
- if (next_rfndx >= nreturned)
- {
- return (void*)-1;
- }
-
- rfndx = next_rfndx++;
-
#ifdef CONFIG_DEBUG
- if (rfndx < 0 || rfndx >= CONFIG_NSOCKET_DESCRIPTORS)
+ if (rfndx < 0 || rfndx >= fw->nfds)
{
ndbg("Bad rfndx: %d\n", rfndx);
return NULL;
}
#endif
- fd = poll_rfdidx[rfndx];
+ fd = fw->poll_rfdidx[rfndx];
fdndx = fd-CONFIG_NFILE_DESCRIPTORS;
- if (fdndx < 0 || fdndx >= CONFIG_NSOCKET_DESCRIPTORS)
+ if (fdndx < 0 || fdndx >= fw->nfds)
{
return NULL;
}
- return fd_data[fdndx];
+ return fw->fd_data[fdndx];
}
/* Generate debugging statistics ndbg message. */
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
-void fdwatch_logstats(long secs)
+void fdwatch_logstats(struct fdwatch_s *fw, long secs)
{
if (secs > 0)
ndbg("fdwatch - %ld polls (%g/sec)\n", nwatches, (float)nwatches / secs);