Changeset 1653 for zzuf/trunk/src


Ignore:
Timestamp:
Jan 12, 2007, 12:30:29 AM (13 years ago)
Author:
Sam Hocevar
Message:
  • Got rid of malloc() for each file opened. Now we only start calling malloc() after the 32th file descriptor.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • zzuf/trunk/src/fd.c

    r1652 r1653  
    2727#include <stdlib.h>
    2828#include <regex.h>
     29#include <string.h>
    2930
    3031#include "libzzuf.h"
     
    3536static int has_include = 0, has_exclude = 0;
    3637
    37 /* File descriptor stuff */
    38 /* TODO: split this into a static table of around 32 file descriptors, for
    39  * most common uses, and a dynamic table for optional additional fds. */
     38/* File descriptor stuff. When program is launched, we use the static array of
     39 * 32 structures, which ought to be enough for most programs. If it happens
     40 * not to be the case, ie. if the process opens more than 32 file descriptors
     41 * at the same time, a bigger array is malloc()ed and replaces the static one.
     42 */
     43#define STATIC_FILES 32
    4044static struct files
    4145{
     
    4650    struct fuzz fuzz;
    4751}
    48 *files;
    49 static int *fds;
     52*files, static_files[STATIC_FILES];
     53static int *fds, static_fds[STATIC_FILES];
    5054static int maxfd, nfiles;
    5155
     
    6771     * calls to malloc() that we do, so we get better chances that memory
    6872     * corruption errors are reproducible */
    69     files = malloc(32 * sizeof(*files));
     73    files = static_files;
    7074    for(nfiles = 0; nfiles < 32; nfiles++)
    7175        files[nfiles].managed = 0;
    7276
    73     fds = malloc(32 * sizeof(int));
     77    fds = static_fds;
    7478    for(maxfd = 0; maxfd < 32; maxfd++)
    7579        fds[maxfd] = -1;
     
    8993    }
    9094
    91     free(files);
    92     free(fds);
     95    if(files != static_files)
     96       free(files);
     97    if(fds != static_fds)
     98        free(fds);
    9399}
    94100
     
    119125        return;
    120126
     127    /* If filedescriptor is outside our bounds */
    121128    while(fd >= maxfd)
    122129    {
    123         fds = realloc(fds, 2 * maxfd * sizeof(int));
     130        if(fds == static_fds)
     131        {
     132            fds = malloc(2 * maxfd * sizeof(*fds));
     133            memcpy(fds, static_fds, maxfd * sizeof(*fds));
     134        }
     135        else
     136            fds = realloc(fds, 2 * maxfd * sizeof(*fds));
    124137        for(i = maxfd; i < maxfd * 2; i++)
    125138            fds[i] = -1;
     
    136149    {
    137150        nfiles++;
    138         files = realloc(files, nfiles * sizeof(struct files));
     151        if(files == static_files)
     152        {
     153            files = malloc(nfiles * sizeof(*files));
     154            memcpy(files, static_files, nfiles * sizeof(*files));
     155        }
     156        else
     157            files = realloc(files, nfiles * sizeof(*files));
    139158    }
    140159
Note: See TracChangeset for help on using the changeset viewer.