Skip to content

Commit

Permalink
Fix grow_stgbuffer() not updating buffer size
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
Zeex committed Jan 25, 2014
1 parent fba30db commit 200abb1
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions source/compiler/sc7.c
Expand Up @@ -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(curmax<requiredsize);
assert(*curmax<requiredsize);
/* if the staging buffer (holding intermediate code for one line) grows
* over a few kBytes, there is probably a run-away expression
*/
if (requiredsize>sSTG_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;
Expand Down

0 comments on commit 200abb1

Please sign in to comment.