sort: Expand linebuf.lines by more than one pointer-size at a time

This commit is contained in:
Robert Ransom 2012-05-21 20:27:03 +00:00
parent e9d6735a9d
commit 6a86755fee

5
sort.c
View File

@ -15,8 +15,9 @@ static bool uflag = false;
struct linebuf { struct linebuf {
char **lines; char **lines;
long nlines; long nlines;
long capacity;
}; };
#define EMPTY_LINEBUF {NULL, 0,} #define EMPTY_LINEBUF {NULL, 0, 0,}
static struct linebuf linebuf = EMPTY_LINEBUF; static struct linebuf linebuf = EMPTY_LINEBUF;
static void getlines(FILE *, struct linebuf *); static void getlines(FILE *, struct linebuf *);
@ -62,7 +63,7 @@ getlines(FILE *fp, struct linebuf *b)
size_t size = 0; size_t size = 0;
while(afgets(&line, &size, fp)) { while(afgets(&line, &size, fp)) {
if(!(b->lines = realloc(b->lines, ++b->nlines * sizeof *b->lines))) if(++b->nlines > b->capacity && !(b->lines = realloc(b->lines, (b->capacity+=512) * sizeof *b->lines)))
eprintf("realloc:"); eprintf("realloc:");
if(!(b->lines[b->nlines-1] = malloc(strlen(line)+1))) if(!(b->lines[b->nlines-1] = malloc(strlen(line)+1)))
eprintf("malloc:"); eprintf("malloc:");