[hackers] [sbase] sort: Expand linebuf.lines by more than one pointer-size at a time || Robert Ransom

From: <hg_AT_suckless.org>
Date: Tue, 22 May 2012 13:08:46 +0200 (CEST)

changeset: 126:9c2ed93ea5ed
user: Robert Ransom <rransom.8774_AT_gmail.com>
date: Mon May 21 20:27:03 2012 +0000
files: sort.c
description:
sort: Expand linebuf.lines by more than one pointer-size at a time


diff -r 6ce18e295316 -r 9c2ed93ea5ed sort.c
--- a/sort.c Mon May 21 20:09:44 2012 +0000
+++ b/sort.c Mon May 21 20:27:03 2012 +0000
_AT_@ -15,8 +15,9 @@
 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 *);
_AT_@ -62,7 +63,7 @@
         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:");
Received on Tue May 22 2012 - 13:08:46 CEST

This archive was generated by hypermail 2.3.0 : Tue May 22 2012 - 13:12:10 CEST