[MEDIUM] pollers: store the events in arrays

Instead of managing StaticReadEvent/StaticWriteEvent, use evts[dir]
diff --git a/src/ev_epoll.c b/src/ev_epoll.c
index ff49505..65b3d74 100644
--- a/src/ev_epoll.c
+++ b/src/ev_epoll.c
@@ -34,8 +34,8 @@
 #endif
 
 
-static fd_set *StaticReadEvent, *StaticWriteEvent;
-static fd_set *PrevReadEvent, *PrevWriteEvent;
+static fd_set *fd_evts[2];
+static fd_set *old_evts[2];
 
 /* private data */
 static struct epoll_event *epoll_events;
@@ -49,79 +49,49 @@
  */
 REGPRM2 static int __fd_isset(const int fd, const int dir)
 {
-	fd_set *ev;
-	if (dir == DIR_RD)
-		ev = StaticReadEvent;
-	else
-		ev = StaticWriteEvent;
-
-	return FD_ISSET(fd, ev);
+	return FD_ISSET(fd, fd_evts[dir]);
 }
 
 REGPRM2 static void __fd_set(const int fd, const int dir)
 {
-	fd_set *ev;
-	if (dir == DIR_RD)
-		ev = StaticReadEvent;
-	else
-		ev = StaticWriteEvent;
-
-	FD_SET(fd, ev);
+	FD_SET(fd, fd_evts[dir]);
 }
 
 REGPRM2 static void __fd_clr(const int fd, const int dir)
 {
-	fd_set *ev;
-	if (dir == DIR_RD)
-		ev = StaticReadEvent;
-	else
-		ev = StaticWriteEvent;
-
-	FD_CLR(fd, ev);
+	FD_CLR(fd, fd_evts[dir]);
 }
 
 REGPRM2 static int __fd_cond_s(const int fd, const int dir)
 {
 	int ret;
-	fd_set *ev;
-	if (dir == DIR_RD)
-		ev = StaticReadEvent;
-	else
-		ev = StaticWriteEvent;
-
-	ret = !FD_ISSET(fd, ev);
+	ret = !FD_ISSET(fd, fd_evts[dir]);
 	if (ret)
-		FD_SET(fd, ev);
+		FD_SET(fd, fd_evts[dir]);
 	return ret;
 }
 
 REGPRM2 static int __fd_cond_c(const int fd, const int dir)
 {
 	int ret;
-	fd_set *ev;
-	if (dir == DIR_RD)
-		ev = StaticReadEvent;
-	else
-		ev = StaticWriteEvent;
-
-	ret = FD_ISSET(fd, ev);
+	ret = FD_ISSET(fd, fd_evts[dir]);
 	if (ret)
-		FD_CLR(fd, ev);
+		FD_CLR(fd, fd_evts[dir]);
 	return ret;
 }
 
 REGPRM1 static void __fd_rem(const int fd)
 {
-	FD_CLR(fd, StaticReadEvent);
-	FD_CLR(fd, StaticWriteEvent);
+	FD_CLR(fd, fd_evts[DIR_RD]);
+	FD_CLR(fd, fd_evts[DIR_WR]);
 }
 
 REGPRM1 static void __fd_clo(const int fd)
 {
-	FD_CLR(fd, StaticReadEvent);
-	FD_CLR(fd, StaticWriteEvent);
-	FD_CLR(fd, PrevReadEvent);
-	FD_CLR(fd, PrevWriteEvent);
+	FD_CLR(fd, fd_evts[DIR_RD]);
+	FD_CLR(fd, fd_evts[DIR_WR]);
+	FD_CLR(fd, old_evts[DIR_RD]);
+	FD_CLR(fd, old_evts[DIR_WR]);
 }
 
 
@@ -149,26 +119,26 @@
 	if (epoll_events == NULL)
 		goto fail_ee;
 
-	if ((PrevReadEvent = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
+	if ((old_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
 		goto fail_prevt;
 
-	if ((PrevWriteEvent = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
+	if ((old_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
 		goto fail_pwevt;
 		
-	if ((StaticReadEvent = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
+	if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
 		goto fail_srevt;
 
-	if ((StaticWriteEvent = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
+	if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
 		goto fail_swevt;
 
 	return 1;
 
  fail_swevt:
-	free(StaticReadEvent);
+	free(fd_evts[DIR_RD]);
  fail_srevt:
-	free(PrevWriteEvent);
+	free(old_evts[DIR_WR]);
  fail_pwevt:
-	free(PrevReadEvent);
+	free(old_evts[DIR_RD]);
  fail_prevt:
 	free(epoll_events);
  fail_ee:
@@ -185,17 +155,17 @@
  */
 REGPRM1 static void epoll_term(struct poller *p)
 {
-	if (StaticWriteEvent)
-		free(StaticWriteEvent);
+	if (fd_evts[DIR_WR])
+		free(fd_evts[DIR_WR]);
 
-	if (StaticReadEvent)
-		free(StaticReadEvent);
+	if (fd_evts[DIR_RD])
+		free(fd_evts[DIR_RD]);
 
-	if (PrevWriteEvent)
-		free(PrevWriteEvent);
+	if (old_evts[DIR_WR])
+		free(old_evts[DIR_WR]);
 
-	if (PrevReadEvent)
-		free(PrevReadEvent);
+	if (old_evts[DIR_RD])
+		free(old_evts[DIR_RD]);
 
 	if (epoll_events)
 		free(epoll_events);
@@ -222,8 +192,8 @@
 
 	for (fds = 0; (fds << INTBITS) < maxfd; fds++) {
 	  
-		rn = ((int*)StaticReadEvent)[fds];  ro = ((int*)PrevReadEvent)[fds];
-		wn = ((int*)StaticWriteEvent)[fds]; wo = ((int*)PrevWriteEvent)[fds];
+		rn = ((int*)fd_evts[DIR_RD])[fds];  ro = ((int*)old_evts[DIR_RD])[fds];
+		wn = ((int*)fd_evts[DIR_WR])[fds]; wo = ((int*)old_evts[DIR_WR])[fds];
 	  
 		if ((ro^rn) | (wo^wn)) {
 			for (count = 0, fd = fds << INTBITS; count < (1<<INTBITS) && fd < maxfd; count++, fd++) {
@@ -243,10 +213,10 @@
 				sw = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&wn);
 #endif
 #else
-				pr = FD_ISSET(fd, PrevReadEvent);
-				pw = FD_ISSET(fd, PrevWriteEvent);
-				sr = FD_ISSET(fd, StaticReadEvent);
-				sw = FD_ISSET(fd, StaticWriteEvent);
+				pr = FD_ISSET(fd, old_evts[DIR_RD]);
+				pw = FD_ISSET(fd, old_evts[DIR_WR]);
+				sr = FD_ISSET(fd, fd_evts[DIR_RD]);
+				sw = FD_ISSET(fd, fd_evts[DIR_WR]);
 #endif
 				if (!((sr^pr) | (sw^pw)))
 					continue;
@@ -296,8 +266,8 @@
 				}
 #endif // EPOLL_CTL_MOD_WORKAROUND
 			}
-			((int*)PrevReadEvent)[fds] = rn;
-			((int*)PrevWriteEvent)[fds] = wn;
+			((int*)old_evts[DIR_RD])[fds] = rn;
+			((int*)old_evts[DIR_WR])[fds] = wn;
 		}		  
 	}
       
@@ -308,14 +278,14 @@
 	for (count = 0; count < status; count++) {
 		fd = epoll_events[count].data.fd;
 
-		if (FD_ISSET(fd, StaticReadEvent)) {
+		if (FD_ISSET(fd, fd_evts[DIR_RD])) {
 			if (fdtab[fd].state == FD_STCLOSE)
 				continue;
 			if (epoll_events[count].events & ( EPOLLIN | EPOLLERR | EPOLLHUP ))
 				fdtab[fd].cb[DIR_RD].f(fd);
 		}
 
-		if (FD_ISSET(fd, StaticWriteEvent)) {
+		if (FD_ISSET(fd, fd_evts[DIR_WR])) {
 			if (fdtab[fd].state == FD_STCLOSE)
 				continue;
 			if (epoll_events[count].events & ( EPOLLOUT | EPOLLERR | EPOLLHUP ))
diff --git a/src/ev_poll.c b/src/ev_poll.c
index 2d6d984..d43fa0c 100644
--- a/src/ev_poll.c
+++ b/src/ev_poll.c
@@ -26,7 +26,7 @@
 #include <proto/task.h>
 
 
-static fd_set *StaticReadEvent, *StaticWriteEvent;
+static fd_set *fd_evts[2];
 
 /* private data */
 static struct pollfd *poll_events = NULL;
@@ -39,71 +39,41 @@
  */
 REGPRM2 static int __fd_isset(const int fd, const int dir)
 {
-	fd_set *ev;
-	if (dir == DIR_RD)
-		ev = StaticReadEvent;
-	else
-		ev = StaticWriteEvent;
-
-	return FD_ISSET(fd, ev);
+	return FD_ISSET(fd, fd_evts[dir]);
 }
 
 REGPRM2 static void __fd_set(const int fd, const int dir)
 {
-	fd_set *ev;
-	if (dir == DIR_RD)
-		ev = StaticReadEvent;
-	else
-		ev = StaticWriteEvent;
-
-	FD_SET(fd, ev);
+	FD_SET(fd, fd_evts[dir]);
 }
 
 REGPRM2 static void __fd_clr(const int fd, const int dir)
 {
-	fd_set *ev;
-	if (dir == DIR_RD)
-		ev = StaticReadEvent;
-	else
-		ev = StaticWriteEvent;
-
-	FD_CLR(fd, ev);
+	FD_CLR(fd, fd_evts[dir]);
 }
 
 REGPRM2 static int __fd_cond_s(const int fd, const int dir)
 {
 	int ret;
-	fd_set *ev;
-	if (dir == DIR_RD)
-		ev = StaticReadEvent;
-	else
-		ev = StaticWriteEvent;
-
-	ret = !FD_ISSET(fd, ev);
+	ret = !FD_ISSET(fd, fd_evts[dir]);
 	if (ret)
-		FD_SET(fd, ev);
+		FD_SET(fd, fd_evts[dir]);
 	return ret;
 }
 
 REGPRM2 static int __fd_cond_c(const int fd, const int dir)
 {
 	int ret;
-	fd_set *ev;
-	if (dir == DIR_RD)
-		ev = StaticReadEvent;
-	else
-		ev = StaticWriteEvent;
-
-	ret = FD_ISSET(fd, ev);
+	ret = FD_ISSET(fd, fd_evts[dir]);
 	if (ret)
-		FD_CLR(fd, ev);
+		FD_CLR(fd, fd_evts[dir]);
 	return ret;
 }
 
 REGPRM1 static void __fd_rem(const int fd)
 {
-	FD_CLR(fd, StaticReadEvent);
-	FD_CLR(fd, StaticWriteEvent);
+	FD_CLR(fd, fd_evts[DIR_RD]);
+	FD_CLR(fd, fd_evts[DIR_WR]);
 }
 
 
@@ -127,16 +97,16 @@
 	if (poll_events == NULL)
 		goto fail_pe;
 		
-	if ((StaticReadEvent = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
+	if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
 		goto fail_srevt;
 
-	if ((StaticWriteEvent = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
+	if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
 		goto fail_swevt;
 
 	return 1;
 
  fail_swevt:
-	free(StaticReadEvent);
+	free(fd_evts[DIR_RD]);
  fail_srevt:
 	free(poll_events);
  fail_pe:
@@ -150,10 +120,10 @@
  */
 REGPRM1 static void poll_term(struct poller *p)
 {
-	if (StaticWriteEvent)
-		free(StaticWriteEvent);
-	if (StaticReadEvent)
-		free(StaticReadEvent);
+	if (fd_evts[DIR_WR])
+		free(fd_evts[DIR_WR]);
+	if (fd_evts[DIR_RD])
+		free(fd_evts[DIR_RD]);
 	if (poll_events)
 		free(poll_events);
 	p->private = NULL;
@@ -175,8 +145,8 @@
 	nbfd = 0;
 	for (fds = 0; (fds << INTBITS) < maxfd; fds++) {
 
-		rn = ((int*)StaticReadEvent)[fds];
-		wn = ((int*)StaticWriteEvent)[fds];
+		rn = ((int*)fd_evts[DIR_RD])[fds];
+		wn = ((int*)fd_evts[DIR_WR])[fds];
 	  
 		if ((rn|wn)) {
 			for (count = 0, fd = fds << INTBITS; count < (1<<INTBITS) && fd < maxfd; count++, fd++) {
@@ -192,8 +162,8 @@
 				sw = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&wn);
 #endif
 #else
-				sr = FD_ISSET(fd, StaticReadEvent);
-				sw = FD_ISSET(fd, StaticWriteEvent);
+				sr = FD_ISSET(fd, fd_evts[DIR_RD]);
+				sw = FD_ISSET(fd, fd_evts[DIR_WR]);
 #endif
 				if ((sr|sw)) {
 					poll_events[nbfd].fd = fd;
@@ -217,14 +187,14 @@
 		/* ok, we found one active fd */
 		status--;
 
-		if (FD_ISSET(fd, StaticReadEvent)) {
+		if (FD_ISSET(fd, fd_evts[DIR_RD])) {
 			if (fdtab[fd].state == FD_STCLOSE)
 				continue;
 			if (poll_events[count].revents & ( POLLIN | POLLERR | POLLHUP ))
 				fdtab[fd].cb[DIR_RD].f(fd);
 		}
 	  
-		if (FD_ISSET(fd, StaticWriteEvent)) {
+		if (FD_ISSET(fd, fd_evts[DIR_WR])) {
 			if (fdtab[fd].state == FD_STCLOSE)
 				continue;
 			if (poll_events[count].revents & ( POLLOUT | POLLERR | POLLHUP ))
diff --git a/src/ev_select.c b/src/ev_select.c
index b1cd44e..1ab7119 100644
--- a/src/ev_select.c
+++ b/src/ev_select.c
@@ -26,8 +26,8 @@
 #include <proto/task.h>
 
 
-static fd_set *ReadEvent, *WriteEvent;
-static fd_set *StaticReadEvent, *StaticWriteEvent;
+static fd_set *fd_evts[2];
+static fd_set *tmp_evts[2];
 
 
 /*
@@ -37,74 +37,45 @@
  */
 REGPRM2 static int __fd_isset(const int fd, const int dir)
 {
-	fd_set *ev;
-	if (dir == DIR_RD)
-		ev = StaticReadEvent;
-	else
-		ev = StaticWriteEvent;
-
-	return FD_ISSET(fd, ev);
+	return FD_ISSET(fd, fd_evts[dir]);
 }
 
 REGPRM2 static void __fd_set(const int fd, const int dir)
 {
-	fd_set *ev;
-	if (dir == DIR_RD)
-		ev = StaticReadEvent;
-	else
-		ev = StaticWriteEvent;
-
-	FD_SET(fd, ev);
+	FD_SET(fd, fd_evts[dir]);
 }
 
 REGPRM2 static void __fd_clr(const int fd, const int dir)
 {
-	fd_set *ev;
-	if (dir == DIR_RD)
-		ev = StaticReadEvent;
-	else
-		ev = StaticWriteEvent;
-
-	FD_CLR(fd, ev);
+	FD_CLR(fd, fd_evts[dir]);
 }
 
 REGPRM2 static int __fd_cond_s(const int fd, const int dir)
 {
 	int ret;
-	fd_set *ev;
-	if (dir == DIR_RD)
-		ev = StaticReadEvent;
-	else
-		ev = StaticWriteEvent;
-
-	ret = !FD_ISSET(fd, ev);
+	ret = !FD_ISSET(fd, fd_evts[dir]);
 	if (ret)
-		FD_SET(fd, ev);
+		FD_SET(fd, fd_evts[dir]);
 	return ret;
 }
 
 REGPRM2 static int __fd_cond_c(const int fd, const int dir)
 {
 	int ret;
-	fd_set *ev;
-	if (dir == DIR_RD)
-		ev = StaticReadEvent;
-	else
-		ev = StaticWriteEvent;
-
-	ret = FD_ISSET(fd, ev);
+	ret = FD_ISSET(fd, fd_evts[dir]);
 	if (ret)
-		FD_CLR(fd, ev);
+		FD_CLR(fd, fd_evts[dir]);
 	return ret;
 }
 
 REGPRM1 static void __fd_rem(const int fd)
 {
-	FD_CLR(fd, StaticReadEvent);
-	FD_CLR(fd, StaticWriteEvent);
+	FD_CLR(fd, fd_evts[DIR_RD]);
+	FD_CLR(fd, fd_evts[DIR_WR]);
 }
 
 
+
 /*
  * Initialization of the select() poller.
  * Returns 0 in case of failure, non-zero in case of success. If it fails, it
@@ -118,26 +89,26 @@
 	p->private = NULL;
 	fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE;
 
-	if ((ReadEvent = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
+	if ((tmp_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
 		goto fail_revt;
 		
-	if ((WriteEvent = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
+	if ((tmp_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
 		goto fail_wevt;
 
-	if ((StaticReadEvent = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
+	if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
 		goto fail_srevt;
 
-	if ((StaticWriteEvent = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
+	if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
 		goto fail_swevt;
 
 	return 1;
 
  fail_swevt:
-	free(StaticReadEvent);
+	free(fd_evts[DIR_RD]);
  fail_srevt:
-	free(WriteEvent);
+	free(tmp_evts[DIR_WR]);
  fail_wevt:
-	free(ReadEvent);
+	free(tmp_evts[DIR_RD]);
  fail_revt:
 	p->pref = 0;
 	return 0;
@@ -149,14 +120,14 @@
  */
 REGPRM1 static void select_term(struct poller *p)
 {
-	if (StaticWriteEvent)
-		free(StaticWriteEvent);
-	if (StaticReadEvent)
-		free(StaticReadEvent);
-	if (WriteEvent)
-		free(WriteEvent);
-	if (ReadEvent)
-		free(ReadEvent);
+	if (fd_evts[DIR_WR])
+		free(fd_evts[DIR_WR]);
+	if (fd_evts[DIR_RD])
+		free(fd_evts[DIR_RD]);
+	if (tmp_evts[DIR_WR])
+		free(tmp_evts[DIR_WR]);
+	if (tmp_evts[DIR_RD])
+		free(tmp_evts[DIR_RD]);
 	p->private = NULL;
 	p->pref = 0;
 }
@@ -187,22 +158,22 @@
 
 	readnotnull = 0; writenotnull = 0;
 	for (i = 0; i < (maxfd + FD_SETSIZE - 1)/(8*sizeof(int)); i++) {
-		readnotnull |= (*(((int*)ReadEvent)+i) = *(((int*)StaticReadEvent)+i)) != 0;
-		writenotnull |= (*(((int*)WriteEvent)+i) = *(((int*)StaticWriteEvent)+i)) != 0;
+		readnotnull |= (*(((int*)tmp_evts[DIR_RD])+i) = *(((int*)fd_evts[DIR_RD])+i)) != 0;
+		writenotnull |= (*(((int*)tmp_evts[DIR_WR])+i) = *(((int*)fd_evts[DIR_WR])+i)) != 0;
 	}
 
 	//	/* just a verification code, needs to be removed for performance */
 	//	for (i=0; i<maxfd; i++) {
-	//	    if (FD_ISSET(i, ReadEvent) != FD_ISSET(i, StaticReadEvent))
+	//	    if (FD_ISSET(i, tmp_evts[DIR_RD]) != FD_ISSET(i, fd_evts[DIR_RD]))
 	//		abort();
-	//	    if (FD_ISSET(i, WriteEvent) != FD_ISSET(i, StaticWriteEvent))
+	//	    if (FD_ISSET(i, tmp_evts[DIR_WR]) != FD_ISSET(i, fd_evts[DIR_WR]))
 	//		abort();
 	//	    
 	//	}
 
 	status = select(maxfd,
-			readnotnull ? ReadEvent : NULL,
-			writenotnull ? WriteEvent : NULL,
+			readnotnull ? tmp_evts[DIR_RD] : NULL,
+			writenotnull ? tmp_evts[DIR_WR] : NULL,
 			NULL,
 			(wait_time >= 0) ? &delta : NULL);
       
@@ -212,20 +183,20 @@
 		return;
 
 	for (fds = 0; (fds << INTBITS) < maxfd; fds++) {
-		if ((((int *)(ReadEvent))[fds] | ((int *)(WriteEvent))[fds]) == 0)
+		if ((((int *)(tmp_evts[DIR_RD]))[fds] | ((int *)(tmp_evts[DIR_WR]))[fds]) == 0)
 			continue;
 
 		for (count = 1<<INTBITS, fd = fds << INTBITS; count && fd < maxfd; count--, fd++) {
 			/* if we specify read first, the accepts and zero reads will be
 			 * seen first. Moreover, system buffers will be flushed faster.
 			 */
-			if (FD_ISSET(fd, ReadEvent)) {
+			if (FD_ISSET(fd, tmp_evts[DIR_RD])) {
 				if (fdtab[fd].state == FD_STCLOSE)
 					continue;
 				fdtab[fd].cb[DIR_RD].f(fd);
 			}
 
-			if (FD_ISSET(fd, WriteEvent)) {
+			if (FD_ISSET(fd, tmp_evts[DIR_WR])) {
 				if (fdtab[fd].state == FD_STCLOSE)
 					continue;
 				fdtab[fd].cb[DIR_WR].f(fd);