This is the mail archive of the libc-hacker@sourceware.cygnus.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: dumb question


>>>>> Roland McGrath writes:

>> The advantage of using poll instead of select is that glibc is not
>> dependend anymore on FD_SETSIZE and doesn't need to be recompiled if
>> the user changes NR_OPEN.

 > The select interface was designed specifically to avoid this problem,
 > if you use it right.  You should never need to use FD_SETSIZE.  You can
 > dynamically allocate as many bits as you need, and then you pass the max fd
 > number to select.  If the kernel is not a piece of shit, this works fine.

But this is not done everywhere.  For example the poll emulation works
so far only with FD_SETSIZE fds and therefore I propose the appended patch.
Some of the others changes I made so far were also optimizations.


1998-06-30  Andreas Jaeger  <aj@arthur.rhein-neckar.de>

	* sysdeps/unix/bsd/poll.c (__poll): Allocate fd_set dynamically so 
	that fd can be bigger than FD_SETSIZE.

Index: sysdeps/unix/bsd/poll.c
===================================================================
RCS file: /egcs/carton/cvsfiles/libc/sysdeps/unix/bsd/poll.c,v
retrieving revision 1.5
diff -u -r1.5 poll.c
--- sysdeps/unix/bsd/poll.c	1998/06/27 13:01:50	1.5
+++ sysdeps/unix/bsd/poll.c	1998/06/30 19:40:45
@@ -16,11 +16,14 @@
    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.  */
 
+#include <alloca.h>
 #include <sys/poll.h>
 #include <sys/types.h>
 #include <errno.h>
 #include <string.h>
 #include <sys/time.h>
+#include <sys/param.h>
+#include <unistd.h>
 
 /* Poll the file descriptors described by the NFDS structures starting at
    FDS.  If TIMEOUT is nonzero and not -1, allow TIMEOUT milliseconds for
@@ -35,24 +38,37 @@
      int timeout;
 {
   struct timeval tv;
-  fd_set rset, wset, xset;
+  fd_set *rset, *wset, *xset;
   struct pollfd *f;
   int ready;
   int maxfd = 0;
+  int bytes;
+  static int max_fd_size;
 
-  FD_ZERO (&rset);
-  FD_ZERO (&wset);
-  FD_ZERO (&xset);
+  if (!max_fd_size)
+    max_fd_size = __getdtablesize ();
 
+  bytes = howmany (max_fd_size, __NFDBITS);
+  rset = alloca (bytes);
+  wset = alloca (bytes);
+  xset = alloca (bytes);
+
+  /* We can't call FD_ZERO, since FD_ZERO only works with sets
+     of exactly __FD_SETSIZE size.  */
+  __bzero (rset, bytes);
+  __bzero (wset, bytes);
+  __bzero (xset, bytes);
+  
+
   for (f = fds; f < &fds[nfds]; ++f)
     if (f->fd >= 0)
       {
 	if (f->events & POLLIN)
-	  FD_SET (f->fd, &rset);
+	  FD_SET (f->fd, rset);
 	if (f->events & POLLOUT)
-	  FD_SET (f->fd, &wset);
+	  FD_SET (f->fd, wset);
 	if (f->events & POLLPRI)
-	  FD_SET (f->fd, &xset);
+	  FD_SET (f->fd, xset);
 	if (f->fd > maxfd && (f->events & (POLLIN|POLLOUT|POLLPRI)))
 	  maxfd = f->fd;
       }
@@ -60,7 +76,7 @@
   tv.tv_sec = timeout / 1000;
   tv.tv_usec = (timeout % 1000) * 1000;
 
-  ready = __select (maxfd + 1, &rset, &wset, &xset,
+  ready = __select (maxfd + 1, rset, wset, xset,
 		    timeout == -1 ? NULL : &tv);
   if (ready > 0)
     for (f = fds; f < &fds[nfds]; ++f)
@@ -68,11 +84,11 @@
 	f->revents = 0;
 	if (f->fd >= 0)
 	  {
-	    if (FD_ISSET (f->fd, &rset))
+	    if (FD_ISSET (f->fd, rset))
 	      f->revents |= POLLIN;
-	    if (FD_ISSET (f->fd, &wset))
+	    if (FD_ISSET (f->fd, wset))
 	      f->revents |= POLLOUT;
-	    if (FD_ISSET (f->fd, &xset))
+	    if (FD_ISSET (f->fd, xset))
 	      f->revents |= POLLPRI;
 	  }
       }


-- 
 Andreas Jaeger   aj@arthur.rhein-neckar.de    jaeger@informatik.uni-kl.de
  for pgp-key finger ajaeger@alma.student.uni-kl.de


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]