From 200abb1d752bd9edc51cf50cc3d80750438bfaa8 Mon Sep 17 00:00:00 2001 From: Zeex Date: Sat, 25 Jan 2014 18:14:00 +0700 Subject: [PATCH] Fix grow_stgbuffer() not updating buffer size The grow_stgbuffer() function, which grows the staging buffer, was not updating the buffer size after reallocating the buffer itself. This caused lots of reallocations because stgwrite() calls this function for every character (!). This also decreases compile times by ~20% (from 10s down to 8s in my setup). --- source/compiler/sc7.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/source/compiler/sc7.c b/source/compiler/sc7.c index 0b402ecf..e460b458 100644 --- a/source/compiler/sc7.c +++ b/source/compiler/sc7.c @@ -82,25 +82,25 @@ static char *stgpipe=NULL; static int pipemax=0; /* current size of the stage pipe, a second staging buffer */ static int pipeidx=0; -#define CHECK_STGBUFFER(index) if ((int)(index)>=stgmax) grow_stgbuffer(&stgbuf, stgmax, (index)+1) -#define CHECK_STGPIPE(index) if ((int)(index)>=pipemax) grow_stgbuffer(&stgpipe, pipemax, (index)+1) +#define CHECK_STGBUFFER(index) if ((int)(index)>=stgmax) grow_stgbuffer(&stgbuf, &stgmax, (index)+1) +#define CHECK_STGPIPE(index) if ((int)(index)>=pipemax) grow_stgbuffer(&stgpipe, &pipemax, (index)+1) -static void grow_stgbuffer(char **buffer, int curmax, int requiredsize) +static void grow_stgbuffer(char **buffer, int *curmax, int requiredsize) { char *p; int clear= (*buffer==NULL); /* if previously none, empty buffer explicitly */ - assert(curmaxsSTG_MAX) error(102,"staging buffer"); /* staging buffer overflow (fatal error) */ - curmax=requiredsize+sSTG_GROW; + *curmax=requiredsize+sSTG_GROW; if (*buffer!=NULL) - p=(char *)realloc(*buffer,curmax*sizeof(char)); + p=(char *)realloc(*buffer,*curmax*sizeof(char)); else - p=(char *)malloc(curmax*sizeof(char)); + p=(char *)malloc(*curmax*sizeof(char)); if (p==NULL) error(102,"staging buffer"); /* staging buffer overflow (fatal error) */ *buffer=p;