From: Willy Tarreau Date: Thu, 5 Jul 2012 21:19:22 +0000 (+0200) Subject: REORG/MEDIUM: fd: remove FD_STCLOSE from struct fdtab X-Git-Tag: v1.5-dev12~125 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=db3b32610f061b128401a23e83eae5e6b70a4a20;p=thirdparty%2Fhaproxy.git REORG/MEDIUM: fd: remove FD_STCLOSE from struct fdtab In an attempt to get rid of fdtab[].state, and to move the relevant parts to the connection struct, we remove the FD_STCLOSE state which can easily be deduced from the pointer as there is a 1:1 match. --- diff --git a/include/types/fd.h b/include/types/fd.h index 0abd3ef10f..c75734512b 100644 --- a/include/types/fd.h +++ b/include/types/fd.h @@ -32,7 +32,6 @@ #include /* different possible states for the fd */ -#define FD_STCLOSE 0 #define FD_STLISTEN 1 #define FD_STCONN 2 #define FD_STREADY 3 @@ -70,7 +69,7 @@ struct fdtab { struct { int (*f)(int fd); /* read/write function */ } cb[DIR_SIZE]; - void *owner; /* the session (or proxy) associated with this fd */ + void *owner; /* the session (or proxy) associated with this fd, NULL if closed */ struct { /* used by pollers which support speculative polling */ unsigned char e; /* read and write events status. 4 bits, may be merged into flags' lower bits */ unsigned int s1; /* Position in spec list+1. 0=not in list. */ diff --git a/src/ev_epoll.c b/src/ev_epoll.c index 7026d56d30..482992ee62 100644 --- a/src/ev_epoll.c +++ b/src/ev_epoll.c @@ -257,14 +257,14 @@ REGPRM2 static void _do_poll(struct poller *p, int exp) ((e & EPOLLHUP) ? FD_POLL_HUP : 0); if ((fd_evts[FD2OFS(fd)] >> FD2BIT(fd)) & DIR2MSK(DIR_RD)) { - if (fdtab[fd].state == FD_STCLOSE) + if (!fdtab[fd].owner) continue; if (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP|FD_POLL_ERR)) fdtab[fd].cb[DIR_RD].f(fd); } if ((fd_evts[FD2OFS(fd)] >> FD2BIT(fd)) & DIR2MSK(DIR_WR)) { - if (fdtab[fd].state == FD_STCLOSE) + if (!fdtab[fd].owner) continue; if (fdtab[fd].ev & (FD_POLL_OUT|FD_POLL_ERR|FD_POLL_HUP)) fdtab[fd].cb[DIR_WR].f(fd); diff --git a/src/ev_kqueue.c b/src/ev_kqueue.c index df4f920b75..b27f37231b 100644 --- a/src/ev_kqueue.c +++ b/src/ev_kqueue.c @@ -140,14 +140,14 @@ REGPRM2 static void _do_poll(struct poller *p, int exp) fd = kev[count].ident; if (kev[count].filter == EVFILT_READ) { if (FD_ISSET(fd, fd_evts[DIR_RD])) { - if (fdtab[fd].state == FD_STCLOSE) + if (!fdtab[fd].owner) continue; fdtab[fd].ev |= FD_POLL_IN; fdtab[fd].cb[DIR_RD].f(fd); } } else if (kev[count].filter == EVFILT_WRITE) { if (FD_ISSET(fd, fd_evts[DIR_WR])) { - if (fdtab[fd].state == FD_STCLOSE) + if (!fdtab[fd].owner) continue; fdtab[fd].ev |= FD_POLL_OUT; fdtab[fd].cb[DIR_WR].f(fd); diff --git a/src/ev_poll.c b/src/ev_poll.c index 8f2e1d986e..6351a72f0d 100644 --- a/src/ev_poll.c +++ b/src/ev_poll.c @@ -160,14 +160,14 @@ REGPRM2 static void _do_poll(struct poller *p, int exp) status--; if (FD_ISSET(fd, fd_evts[DIR_RD])) { - if (fdtab[fd].state == FD_STCLOSE) + if (!fdtab[fd].owner) continue; if (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP|FD_POLL_ERR)) fdtab[fd].cb[DIR_RD].f(fd); } if (FD_ISSET(fd, fd_evts[DIR_WR])) { - if (fdtab[fd].state == FD_STCLOSE) + if (!fdtab[fd].owner) continue; if (fdtab[fd].ev & (FD_POLL_OUT|FD_POLL_ERR|FD_POLL_HUP)) fdtab[fd].cb[DIR_WR].f(fd); diff --git a/src/ev_select.c b/src/ev_select.c index 72eca0b3f4..64031cc3ee 100644 --- a/src/ev_select.c +++ b/src/ev_select.c @@ -147,14 +147,14 @@ REGPRM2 static void _do_poll(struct poller *p, int exp) * seen first. Moreover, system buffers will be flushed faster. */ if (FD_ISSET(fd, tmp_evts[DIR_RD])) { - if (fdtab[fd].state == FD_STCLOSE) + if (!fdtab[fd].owner) continue; fdtab[fd].ev |= FD_POLL_IN; fdtab[fd].cb[DIR_RD].f(fd); } if (FD_ISSET(fd, tmp_evts[DIR_WR])) { - if (fdtab[fd].state == FD_STCLOSE) + if (!fdtab[fd].owner) continue; fdtab[fd].ev |= FD_POLL_OUT; fdtab[fd].cb[DIR_WR].f(fd); diff --git a/src/ev_sepoll.c b/src/ev_sepoll.c index b5048157bd..4b1580995e 100644 --- a/src/ev_sepoll.c +++ b/src/ev_sepoll.c @@ -202,7 +202,7 @@ REGPRM2 static int __fd_is_set(const int fd, int dir) int ret; #if DEBUG_DEV - if (fdtab[fd].state == FD_STCLOSE) { + if (!fdtab[fd].owner) { fprintf(stderr, "sepoll.fd_isset called on closed fd #%d.\n", fd); ABORT_NOW(); } @@ -220,7 +220,7 @@ REGPRM2 static int __fd_set(const int fd, int dir) unsigned int i; #if DEBUG_DEV - if (fdtab[fd].state == FD_STCLOSE) { + if (!fdtab[fd].owner) { fprintf(stderr, "sepoll.fd_set called on closed fd #%d.\n", fd); ABORT_NOW(); } @@ -242,7 +242,7 @@ REGPRM2 static int __fd_clr(const int fd, int dir) unsigned int i; #if DEBUG_DEV - if (fdtab[fd].state == FD_STCLOSE) { + if (!fdtab[fd].owner) { fprintf(stderr, "sepoll.fd_clr called on closed fd #%d.\n", fd); ABORT_NOW(); } @@ -410,14 +410,14 @@ REGPRM2 static void _do_poll(struct poller *p, int exp) ((e & EPOLLHUP) ? FD_POLL_HUP : 0); if ((fdtab[fd].spec.e & FD_EV_MASK_R) == FD_EV_WAIT_R) { - if (fdtab[fd].state == FD_STCLOSE || fdtab[fd].state == FD_STERROR) + if (!fdtab[fd].owner || fdtab[fd].state == FD_STERROR) continue; if (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP|FD_POLL_ERR)) fdtab[fd].cb[DIR_RD].f(fd); } if ((fdtab[fd].spec.e & FD_EV_MASK_W) == FD_EV_WAIT_W) { - if (fdtab[fd].state == FD_STCLOSE || fdtab[fd].state == FD_STERROR) + if (!fdtab[fd].owner || fdtab[fd].state == FD_STERROR) continue; if (fdtab[fd].ev & (FD_POLL_OUT|FD_POLL_ERR)) fdtab[fd].cb[DIR_WR].f(fd); @@ -471,7 +471,7 @@ REGPRM2 static void _do_poll(struct poller *p, int exp) } /* one callback might already have closed the fd by itself */ - if (fdtab[fd].state == FD_STCLOSE) + if (!fdtab[fd].owner) continue; if (!(fdtab[fd].spec.e & (FD_EV_RW_SL|FD_EV_RW_PL))) { diff --git a/src/fd.c b/src/fd.c index 80bddd6a0f..7958087e47 100644 --- a/src/fd.c +++ b/src/fd.c @@ -41,9 +41,9 @@ void fd_delete(int fd) port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); fdinfo[fd].port_range = NULL; close(fd); - fdtab[fd].state = FD_STCLOSE; + fdtab[fd].owner = NULL; - while ((maxfd-1 >= 0) && (fdtab[maxfd-1].state == FD_STCLOSE)) + while ((maxfd-1 >= 0) && !fdtab[maxfd-1].owner) maxfd--; } diff --git a/src/haproxy.c b/src/haproxy.c index cede41ca65..499d3985de 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -351,7 +351,6 @@ void dump(struct sig_handler *sh) */ void init(int argc, char **argv) { - int i; int arg_mode = 0; /* MODE_DEBUG, ... */ char *tmp; char *cfg_pidfile = NULL; @@ -691,10 +690,6 @@ void init(int argc, char **argv) sizeof(struct fdinfo) * (global.maxsock)); fdtab = (struct fdtab *)calloc(1, sizeof(struct fdtab) * (global.maxsock)); - for (i = 0; i < global.maxsock; i++) { - fdtab[i].state = FD_STCLOSE; - } - /* * Note: we could register external pollers here. * Built-in pollers have been registered before main(). diff --git a/src/session.c b/src/session.c index 43252b8637..e839922e39 100644 --- a/src/session.c +++ b/src/session.c @@ -320,7 +320,7 @@ int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr) send(cfd, err_msg->str, err_msg->len, MSG_DONTWAIT|MSG_NOSIGNAL); } - if (fdtab[cfd].state != FD_STCLOSE) + if (fdtab[cfd].owner) fd_delete(cfd); else close(cfd);