- Timestamp:
- Jan 12, 2007, 12:30:29 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
zzuf/trunk/src/fd.c
r1652 r1653 27 27 #include <stdlib.h> 28 28 #include <regex.h> 29 #include <string.h> 29 30 30 31 #include "libzzuf.h" … … 35 36 static int has_include = 0, has_exclude = 0; 36 37 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 40 44 static struct files 41 45 { … … 46 50 struct fuzz fuzz; 47 51 } 48 *files ;49 static int *fds ;52 *files, static_files[STATIC_FILES]; 53 static int *fds, static_fds[STATIC_FILES]; 50 54 static int maxfd, nfiles; 51 55 … … 67 71 * calls to malloc() that we do, so we get better chances that memory 68 72 * corruption errors are reproducible */ 69 files = malloc(32 * sizeof(*files));73 files = static_files; 70 74 for(nfiles = 0; nfiles < 32; nfiles++) 71 75 files[nfiles].managed = 0; 72 76 73 fds = malloc(32 * sizeof(int));77 fds = static_fds; 74 78 for(maxfd = 0; maxfd < 32; maxfd++) 75 79 fds[maxfd] = -1; … … 89 93 } 90 94 91 free(files); 92 free(fds); 95 if(files != static_files) 96 free(files); 97 if(fds != static_fds) 98 free(fds); 93 99 } 94 100 … … 119 125 return; 120 126 127 /* If filedescriptor is outside our bounds */ 121 128 while(fd >= maxfd) 122 129 { 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)); 124 137 for(i = maxfd; i < maxfd * 2; i++) 125 138 fds[i] = -1; … … 136 149 { 137 150 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)); 139 158 } 140 159
Note: See TracChangeset
for help on using the changeset viewer.