replace the OpenBSD fd_set sizing code with something more efficient.

Only call malloc() if the fd is too big for the compiled in fd_set size,
and don't use calloc either.  This should reduce the impact of conflicts
with private malloc implementations etc.  When using the fd_set on the
stack, only zero what is needed rather than all 1024 bits like FD_ZERO did.
This commit is contained in:
peter 1997-06-28 04:19:52 +00:00
parent a5bae08625
commit 78a457bdd0

View File

@ -56,7 +56,7 @@
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)res_send.c 8.1 (Berkeley) 6/4/93";
static char orig_rcsid[] = "From: Id: res_send.c,v 8.13 1997/06/01 20:34:37 vixie Exp";
static char rcsid[] = "$Id: res_send.c,v 1.16 1997/06/27 08:22:03 peter Exp $";
static char rcsid[] = "$Id: res_send.c,v 1.17 1997/06/27 13:00:51 peter Exp $";
#endif /* LIBC_SCCS and not lint */
/*
@ -469,7 +469,8 @@ res_send(buf, buflen, ans, anssiz)
* Use datagrams.
*/
struct timeval timeout;
fd_set *dsmaskp;
fd_set dsmask, *dsmaskp;
int dsmasklen;
struct sockaddr_in from;
int fromlen;
@ -575,16 +576,22 @@ res_send(buf, buflen, ans, anssiz)
timeout.tv_sec = 1;
timeout.tv_usec = 0;
wait:
dsmaskp = (fd_set *)calloc(howmany(s+1, NFDBITS),
sizeof(fd_mask));
if (dsmaskp == NULL) {
res_close();
goto next_ns;
}
dsmasklen = howmany(s+1, NFDBITS) * sizeof(fd_mask);
if (dsmasklen > sizeof(fd_set)) {
dsmaskp = (fd_set *)malloc(dsmasklen);
if (dsmaskp == NULL) {
res_close();
goto next_ns;
}
} else
dsmaskp = &dsmask;
/* only zero what we need */
bzero((char *)dsmaskp, dsmasklen);
FD_SET(s, dsmaskp);
n = select(s+1, dsmaskp, (fd_set *)NULL,
(fd_set *)NULL, &timeout);
free(dsmaskp);
if (dsmaskp != &dsmask)
free(dsmaskp);
if (n < 0) {
if (errno == EINTR)
goto wait;