commit 6a86755fee8b7879f10aeb43b29d35c839a624b2
parent e9d6735a9d15f42507f03533b5c00869fdc3aba7
Author: Robert Ransom <rransom.8774@gmail.com>
Date: Mon, 21 May 2012 20:27:03 +0000
sort: Expand linebuf.lines by more than one pointer-size at a time
Diffstat:
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/sort.c b/sort.c
@@ -15,8 +15,9 @@ static bool uflag = false;
struct linebuf {
char **lines;
long nlines;
+ long capacity;
};
-#define EMPTY_LINEBUF {NULL, 0,}
+#define EMPTY_LINEBUF {NULL, 0, 0,}
static struct linebuf linebuf = EMPTY_LINEBUF;
static void getlines(FILE *, struct linebuf *);
@@ -62,7 +63,7 @@ getlines(FILE *fp, struct linebuf *b)
size_t size = 0;
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:");
if(!(b->lines[b->nlines-1] = malloc(strlen(line)+1)))
eprintf("malloc:");