To: djgpp AT sun DOT soe DOT clarkson DOT edu Subject: debug32 fixes for large source files Date: Tue, 22 Jun 1993 00:23:11 -0700 From: Darryl Okahata About a week and a half ago, I posted some patches for debug32 that allowed it to work with large source files (say, > 1000-2000 lines); the current 1.10 debug32 will die if you have a source file with a "large" number of lines. Unfortunately, those patches worked only with Borland C++ 3.1, and had a limit of 10000 lines. The following patches fix these problems. They will work with Turbo C 2.0 (does this actually date back to 1988?) and newer compilers, and have no number-of-lines limitation. These patches are relative to the virginal 1.10 go32 distribution, and are not relative to my previous patches. -- Darryl Okahata Internet: darrylo AT sr DOT hp DOT com DISCLAIMER: this message is the author's personal opinion and does not constitute the support, opinion or policy of Hewlett-Packard or of the little green men that have been following him all day. =============================================================================== rcsdiff -c -r1.1 unassmbl.c *** f:/T0AA.AAA Mon Jun 21 23:04:10 1993 --- unassmbl.c Mon Jun 21 23:02:50 1993 *************** *** 796,806 **** ** Courtesy of Kent Williams williams AT herky DOT cs DOT uiowa DOT edu ** ** KNOWN BUGS: - ** The program will summarily terminate if you run out - ** of memory while you're looking for all the line offsets. Since - ** a two thousand line source file only creats an 8K array, and the - ** symbol table goes into virtual memory, this shouldn't happen too - ** often. ** ** One file is left open for reading indefinitely. */ --- 796,801 ---- *************** *** 833,853 **** xmalloc(unsigned size) { void *rval = malloc(size); if(!rval) { ! fprintf(stderr,"Out of memory\n"); exit(1); } return rval; } static void * xrealloc(void *rval,unsigned size) { rval = realloc(rval,size); if(!rval) { ! fprintf(stderr,"Out of memory\n"); exit(1); } return rval; } static char * xstrdup(char *s) { --- 828,853 ---- xmalloc(unsigned size) { void *rval = malloc(size); if(!rval) { ! fprintf(stderr,"\nOut of memory trying to allocate %u bytes (coreleft = %u)\n", ! size, coreleft()); exit(1); } return rval; } + #if 0 /* no longer used */ static void * xrealloc(void *rval,unsigned size) { + void *orig = rval; rval = realloc(rval,size); if(!rval) { ! fprintf(stderr,"\nOut of memory trying to reallocate %u bytes (0x%08lx)\n", ! size, (unsigned long) orig); exit(1); } return rval; } + #endif static char * xstrdup(char *s) { *************** *** 859,897 **** /* ** add_file -- add a file to the source line database */ static int add_file(char *name) { ! FILE *f = fopen(name,"r"); char c; ! long *lines,curpos; ! unsigned curline = 0; if(!f) return -1; files[last_file].filename = xstrdup(name); /* ! ** build an array of line offsets in real memory. */ - lines = xmalloc(sizeof(long)); - lines[curline++] = curpos = 0L; - while((c = getc(f)) != EOF) { curpos++; if(c == '\n') { ! lines = xrealloc(lines,sizeof(long)*(curline+1)); ! lines[curline++] = curpos; } } /* ! ** now move the whole array into virtual memory ! */ ! files[last_file].offsets = salloc(curline*sizeof(long)); ! symsput(files[last_file].offsets,lines,(curline*sizeof(long))); ! /* ! ** don't need the real memory version any more */ ! free(lines); fclose(f); --- 859,932 ---- /* ** add_file -- add a file to the source line database */ + + #define LINES_BUF_SIZE 1000 /* number of lines in buffer */ + long line_offsets[LINES_BUF_SIZE]; + static int add_file(char *name) { ! FILE *f; char c; ! long curpos; ! unsigned int curline, line_count, block; + f = fopen(name,"r"); if(!f) return -1; files[last_file].filename = xstrdup(name); + + /* + * Pass 1: Count number of lines in file. + */ + curline = 0; + while((c = getc(f)) != EOF) { + if(c == '\n') { + ++curline; + } + } + line_count = curline; + fclose(f); + + f = fopen(name,"r"); + if(!f) + return -1; + + /* + * Allocate virtual memory and prepare for pass 2. + */ + files[last_file].offsets = salloc(line_count*sizeof(long)); + curline = 0; + block = 0; + line_offsets[curline++] = curpos = 0L; + /* ! ** Pass 2: Build an array of line offsets in real memory. ! ** When the buffer fills up, it is flushed to virtual memory. ! ** In theory, we should be able to handle files of any length ! ** using this method. */ while((c = getc(f)) != EOF) { curpos++; if(c == '\n') { ! if (curline >= LINES_BUF_SIZE) { ! /* ! * Flush buffer to virtual memory ! */ ! symsput(files[last_file].offsets ! + block * sizeof(line_offsets), ! line_offsets,sizeof(line_offsets)); ! ++block; ! curline = 0; ! } ! line_offsets[curline++] = curpos; } } /* ! ** now flush (move) the remainder into virtual memory */ ! symsput(files[last_file].offsets + block * sizeof(line_offsets), ! line_offsets,(curline*sizeof(long))); fclose(f);