- if (fdp->fd_maxfiles == NDFILE) {
- fdp->fd_moreofiles = (struct file **)
- malloc(NDEXTENT * OFILESIZE, M_FILE, M_WAITOK);
- fdp->fd_moreofileflags =
- (char *)&fdp->fd_moreofiles[NDEXTENT];
- bzero((char *)fdp->fd_moreofiles, NDEXTENT * OFILESIZE);
- fdp->fd_maxfiles = NDFILE + NDEXTENT;
- fdexpand++;
- continue;
- }
- ofiles = fdp->fd_maxfiles - NDFILE;
- osize = ofiles * OFILESIZE;
- nfiles = (2 * osize) / OFILESIZE;
- newofile = (struct file **) malloc(2 * osize, M_FILE, M_WAITOK);
- newofileflags = (char *)&newofile[nfiles];
- bzero((char *)newofile, 2 * osize);
- bcopy((char *)fdp->fd_moreofiles, (char *)newofile,
- sizeof(struct file *) * ofiles);
- bcopy((char *)fdp->fd_moreofileflags, (char *)newofileflags,
- sizeof(char) * ofiles);
- free(fdp->fd_moreofiles, M_FILE);
- fdp->fd_moreofiles = newofile;
- fdp->fd_moreofileflags = newofileflags;
- fdp->fd_maxfiles = NDFILE + nfiles;
- fdreexpand++;
+ if (fdp->fd_nfiles < NDEXTENT)
+ nfiles = NDEXTENT;
+ else
+ nfiles = 2 * fdp->fd_nfiles;
+ MALLOC(newofile, struct file **, nfiles * OFILESIZE,
+ M_FILEDESC, M_WAITOK);
+ newofileflags = (char *) &newofile[nfiles];
+ /*
+ * 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);
+ if (fdp->fd_nfiles > NDFILE)
+ FREE(fdp->fd_ofiles, M_FILEDESC);
+ fdp->fd_ofiles = newofile;
+ fdp->fd_ofileflags = newofileflags;
+ fdp->fd_nfiles = nfiles;
+ fdexpand++;