bcopy(fdp->fd_himap, newhimap, (i = NDHISLOTS(fdp->fd_nfiles) * sizeof(u_int))); bzero((char *)newhimap + i, NDHISLOTS(nfiles) * sizeof(u_int) - i);  bcopy(fdp->fd_lomap, newlomap, (i = NDLOSLOTS(fdp->fd_nfiles) * sizeof(u_int))); bzero((char *)newlomap + i, NDLOSLOTS(nfiles) * sizeof(u_int) - i);
fdalloc(p, want, result)	struct proc *p;	int want;	int *result;{	register struct filedesc *fdp = p->p_fd;	register int i;	int lim, last, nfiles;	struct file **newofile;	char *newofileflags;	u_int *newhimap, *newlomap, new, off;	/*	 * Search for a free descriptor starting at the higher	 * of want or fd_freefile.  If that fails, consider	 * expanding the ofile array.	 */	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);	for (;;) {		last = min(fdp->fd_nfiles, lim);		if ((i = want) < fdp->fd_freefile)			i = fdp->fd_freefile;		off = i >> NDENTRYSHIFT;		new = find_next_zero(fdp->fd_himap, off,				     (last + NDENTRIES - 1) >> NDENTRYSHIFT);		if (new != -1) {			i = find_next_zero(&fdp->fd_lomap[new], 					   new > off ? 0 : i & NDENTRYMASK,					   NDENTRIES);			if (i == -1) {				/* free file descriptor in this block was				 * below want, try again with higher want.				 */				want = (new + 1) << NDENTRYSHIFT;				continue;			}			i += (new << NDENTRYSHIFT);			if (i < last) {				fd_used(fdp, i);				if (want <= fdp->fd_freefile)					fdp->fd_freefile = i;				*result = i;				return (0);			}		}		/*		 * No space in current array.  Expand?		 */		if (fdp->fd_nfiles >= lim)			return (EMFILE);		if (fdp->fd_nfiles < NDEXTENT)			nfiles = NDEXTENT;		else			nfiles = 2 * fdp->fd_nfiles;		nfiles = min(lim, nfiles);		MALLOC(newofile, struct file **, nfiles * OFILESIZE,		    M_FILEDESC, M_WAITOK);		newofileflags = (char *) &newofile[nfiles];		MALLOC(newhimap, u_int *, NDHISLOTS(nfiles) * sizeof(u_int),		       M_FILEDESC, M_WAITOK);		MALLOC(newlomap, u_int *, NDLOSLOTS(nfiles) * sizeof(u_int),		       M_FILEDESC, M_WAITOK);		/*		 * Copy the existing ofile and ofileflags arrays		 * and zero the new portion of each array.		 */		bcopy(fdp->fd_ofiles, newofile,			(i = sizeof(struct file *) * fdp->fd_nfiles));		bzero((char *)newofile + i, nfiles * sizeof(struct file *) - i);		bcopy(fdp->fd_ofileflags, newofileflags,			(i = sizeof(char) * fdp->fd_nfiles));		bzero(newofileflags + i, nfiles * sizeof(char) - i);		bcopy(fdp->fd_himap, newhimap,		      (i = NDHISLOTS(fdp->fd_nfiles) * sizeof(u_int)));		bzero((char *)newhimap + i,		      NDHISLOTS(nfiles) * sizeof(u_int) - i);		bcopy(fdp->fd_lomap, newlomap,		      (i = NDLOSLOTS(fdp->fd_nfiles) * sizeof(u_int)));		bzero((char *)newlomap + i,		      NDLOSLOTS(nfiles) * sizeof(u_int) - i);		if (fdp->fd_nfiles > NDFILE)			FREE(fdp->fd_ofiles, M_FILEDESC);		if (NDHISLOTS(fdp->fd_nfiles) > NDHISLOTS(NDFILE)) {			FREE(fdp->fd_himap, M_FILEDESC);			FREE(fdp->fd_lomap, M_FILEDESC);		}		fdp->fd_ofiles = newofile;		fdp->fd_ofileflags = newofileflags;		fdp->fd_nfiles = nfiles;		fdp->fd_himap = newhimap;		fdp->fd_lomap = newlomap;		fdexpand++;	}}