Skip to content

Commit

Permalink
Simplify malloc(3)/realloc(3) usage
Browse files Browse the repository at this point in the history
At a number of places in this code base, a memory block gets
allocated with malloc(3) first and then grown with realloc(3) if
necessary. But, if the pointer to the block is initialised to NULL,
then realloc(3) can always be used, because realloc(3) with a
pointer argument of NULL is equivalent to malloc(3). This allows
for a simplification at various points in the code.

We can also eliminate redundant typecasts while we're at it (these
are not needed as of the change to C90).
  • Loading branch information
McDutchie committed Nov 22, 2024
1 parent db20c30 commit da0a769
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 52 deletions.
16 changes: 2 additions & 14 deletions src/cmd/ksh93/sh/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -909,21 +909,9 @@ void sh_setmatch(const char *v, int vsize, int nmatch, int match[], int index)
index *= 2*mp->nmatch;
i = (index+2*mp->nmatch)*sizeof(match[0]);
if(i >= mp->msize)
{
if(mp->msize)
mp->match = (int*)sh_realloc(mp->match,2*i);
else
mp->match = (int*)sh_malloc(2*i);
mp->msize = 2*i;
}
mp->match = sh_realloc(mp->match, mp->msize = 2*i);
if(vsize >= mp->vsize)
{
if(mp->vsize)
mp->val = (char*)sh_realloc(mp->val,x=2*vsize);
else
mp->val = (char*)sh_malloc(x=vsize+1);
mp->vsize = x;
}
mp->val = sh_realloc(mp->val, mp->vsize = mp->vsize ? 2 * vsize : vsize + 1);
memcpy(mp->match+index,match,nmatch*2*sizeof(match[0]));
for(i=0; i < 2*nmatch; i++)
{
Expand Down
5 changes: 1 addition & 4 deletions src/cmd/ksh93/sh/lex.c
Original file line number Diff line number Diff line change
Expand Up @@ -2503,9 +2503,6 @@ static void setupalias(Lex_t *lp, const char *string,Namval_t *np)
static int stack_grow(void)
{
lex_max += STACK_ARRAY;
if(lex_match)
lex_match = (int*)sh_realloc((char*)lex_match,sizeof(int)*lex_max);
else
lex_match = (int*)sh_malloc(sizeof(int)*STACK_ARRAY);
lex_match = sh_realloc(lex_match,sizeof(int)*lex_max);
return 1;
}
16 changes: 2 additions & 14 deletions src/cmd/ksh93/sh/name.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,7 @@ static char *getbuf(size_t len)
static char *buf;
static size_t buflen;
if(buflen < len)
{
if(buflen==0)
buf = (char*)sh_malloc(len);
else
buf = (char*)sh_realloc(buf,len);
buflen = len;
}
buf = sh_realloc(buf, buflen = len);
return buf;
}

Expand Down Expand Up @@ -1474,13 +1468,7 @@ Namval_t *nv_open(const char *name, Dt_t *root, int flags)
xp->len = strlen(name);
c = roundof(xp->len+1,32);
if(c > xp->size)
{
if(xp->size==0)
xp->name = sh_malloc(c);
else
xp->name = sh_realloc(xp->name,c);
xp->size = c;
}
xp->name = sh_realloc(xp->name, xp->size = c);
memcpy(xp->name,name,xp->len);
xp->name[xp->len] = 0;
xp->root = root;
Expand Down
12 changes: 2 additions & 10 deletions src/lib/libast/disc/sfdcdos.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,8 @@ static ssize_t dos_read(Sfio_t *iop, void *buff, size_t size, Sfdisc_t* disc)
}
}
/* save original discipline inside buffer */
if(count>dp->bsize)
{
if(dp->bsize==0)
dp->buff = malloc(count);
else
dp->buff = realloc(dp->buff,count);
dp->bsize = count;
if(!dp->buff)
return -1;
}
if(count > dp->bsize && !(dp->buff = realloc(dp->buff, dp->bsize = count)))
return -1;
memcpy(dp->buff, cp, count);
count=1;
while(1)
Expand Down
9 changes: 3 additions & 6 deletions src/lib/libast/regex/reginit.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* *
* This software is part of the ast package *
* Copyright (c) 1985-2011 AT&T Intellectual Property *
* Copyright (c) 2020-2023 Contributors to ksh 93u+m *
* Copyright (c) 2020-2024 Contributors to ksh 93u+m *
* and is licensed under the *
* Eclipse Public License, Version 2.0 *
* *
Expand Down Expand Up @@ -385,14 +385,11 @@ alloc(regdisc_t* disc, void* p, size_t n)
return NULL;
return (*disc->re_resizef)(disc->re_resizehandle, p, n);
}
else if (!n)
if (!n)
{
if (!(disc->re_flags & REG_NOFREE))
free(p);
return NULL;
}
else if (p)
return realloc(p, n);
else
return malloc(n);
return realloc(p, n);
}
5 changes: 1 addition & 4 deletions src/lib/libast/sfio/sfexcept.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ int _sfexcept(Sfio_t* f, /* stream where the exception happened */
if((io -= size) <= 0)
io = SFIO_GRAIN;
size = ((size+io+SFIO_GRAIN-1)/SFIO_GRAIN)*SFIO_GRAIN;
if(f->size > 0)
data = (uchar*)realloc((char*)f->data,size);
else data = (uchar*)malloc(size);
if(!data)
if(!(data = realloc(f->data,size)))
goto chk_stack;
f->endb = data + size;
f->next = data + (f->next - f->data);
Expand Down

0 comments on commit da0a769

Please sign in to comment.