From 11e03ae94bc611cdc217fa72784f8245e13b0b9f Mon Sep 17 00:00:00 2001 From: Matthew Fernandez Date: Wed, 28 Oct 2020 19:50:49 -0700 Subject: [PATCH] remove some unnecessary branching for malloc/realloc When realloc() is passed NULL as its first parameter, it acts the same as malloc(). --- lib/cgraph/write.c | 5 +-- lib/gvc/gvconfig.c | 3 +- lib/pathplan/route.c | 22 +++---------- lib/pathplan/shortest.c | 65 +++++++++---------------------------- lib/pathplan/util.c | 2 +- plugin/gd/gvtextlayout_gd.c | 5 +-- 6 files changed, 25 insertions(+), 77 deletions(-) diff --git a/lib/cgraph/write.c b/lib/cgraph/write.c index 5b2f2e2ae..dbfb57d4b 100644 --- a/lib/cgraph/write.c +++ b/lib/cgraph/write.c @@ -175,10 +175,7 @@ static char *getoutputbuffer(char *str) req = MAX(2 * strlen(str) + 2, BUFSIZ); if (req > len) { - if (rv) - rv = realloc(rv, req); - else - rv = malloc(req); + rv = realloc(rv, req); len = req; } return rv; diff --git a/lib/gvc/gvconfig.c b/lib/gvc/gvconfig.c index 9b0f1ac4e..b1542cc8d 100644 --- a/lib/gvc/gvconfig.c +++ b/lib/gvc/gvconfig.c @@ -590,8 +590,7 @@ glob (GVC_t* gvc, char* pattern, int flags, int (*errfunc)(const char *, int), g do { if (cnt >= arrsize-1) { arrsize += 512; - if (str) str = (char**)realloc (str, arrsize*sizeof(char*)); - else str = (char**)malloc (arrsize*sizeof(char*)); + str = realloc (str, arrsize*sizeof(char*)); if (!str) return GLOB_NOSPACE; } str[cnt] = (char*)malloc (strlen(libdir)+1+strlen(wfd.cFileName)+1); diff --git a/lib/pathplan/route.c b/lib/pathplan/route.c index 12a32ad64..452fd0c72 100644 --- a/lib/pathplan/route.c +++ b/lib/pathplan/route.c @@ -200,13 +200,8 @@ static int reallyroutespline(Pedge_t * edges, int edgen, static int tnan; if (tnan < inpn) { - if (!tnas) { - if (!(tnas = malloc(sizeof(tna_t) * inpn))) - return -1; - } else { - if (!(tnas = realloc(tnas, sizeof(tna_t) * inpn))) - return -1; - } + if (!(tnas = realloc(tnas, sizeof(tna_t) * inpn))) + return -1; tnan = inpn; } tnas[0].t = 0; @@ -518,16 +513,9 @@ static void growops(int newopn) { if (newopn <= opn) return; - if (!ops) { - if (!(ops = malloc(POINTSIZE * newopn))) { - prerror("cannot malloc ops"); - longjmp(jbuf,1); - } - } else { - if (!(ops = realloc(ops, POINTSIZE * newopn))) { - prerror("cannot realloc ops"); - longjmp(jbuf,1); - } + if (!(ops = realloc(ops, POINTSIZE * newopn))) { + prerror("cannot realloc ops"); + longjmp(jbuf,1); } opn = newopn; } diff --git a/lib/pathplan/shortest.c b/lib/pathplan/shortest.c index a51d99ef3..c6d4efaae 100644 --- a/lib/pathplan/shortest.c +++ b/lib/pathplan/shortest.c @@ -522,24 +522,13 @@ static int growpnls(int newpnln) { if (newpnln <= pnln) return 0; - if (!pnls) { - if (!(pnls = malloc(POINTNLINKSIZE * newpnln))) { - prerror("cannot malloc pnls"); - return -1; - } - if (!(pnlps = malloc(POINTNLINKPSIZE * newpnln))) { - prerror("cannot malloc pnlps"); - return -1; - } - } else { - if (!(pnls = realloc(pnls, POINTNLINKSIZE * newpnln))) { - prerror("cannot realloc pnls"); - return -1; - } - if (!(pnlps = realloc(pnlps, POINTNLINKPSIZE * newpnln))) { - prerror("cannot realloc pnlps"); - return -1; - } + if (!(pnls = realloc(pnls, POINTNLINKSIZE * newpnln))) { + prerror("cannot realloc pnls"); + return -1; + } + if (!(pnlps = realloc(pnlps, POINTNLINKPSIZE * newpnln))) { + prerror("cannot realloc pnlps"); + return -1; } pnln = newpnln; return 0; @@ -549,16 +538,9 @@ static int growtris(int newtrin) { if (newtrin <= trin) return 0; - if (!tris) { - if (!(tris = malloc(TRIANGLESIZE * newtrin))) { - prerror("cannot malloc tris"); - return -1; - } - } else { - if (!(tris = realloc(tris, TRIANGLESIZE * newtrin))) { - prerror("cannot realloc tris"); - return -1; - } + if (!(tris = realloc(tris, TRIANGLESIZE * newtrin))) { + prerror("cannot realloc tris"); + return -1; } trin = newtrin; @@ -569,17 +551,9 @@ static int growdq(int newdqn) { if (newdqn <= dq.pnlpn) return 0; - if (!dq.pnlps) { - if (! - (dq.pnlps = malloc(POINTNLINKPSIZE * newdqn))) { - prerror("cannot malloc dq.pnls"); - return -1; - } - } else { - if (!(dq.pnlps = realloc(dq.pnlps, POINTNLINKPSIZE * newdqn))) { - prerror("cannot realloc dq.pnls"); - return -1; - } + if (!(dq.pnlps = realloc(dq.pnlps, POINTNLINKPSIZE * newdqn))) { + prerror("cannot realloc dq.pnls"); + return -1; } dq.pnlpn = newdqn; return 0; @@ -589,16 +563,9 @@ static int growops(int newopn) { if (newopn <= opn) return 0; - if (!ops) { - if (!(ops = malloc(POINTSIZE * newopn))) { - prerror("cannot malloc ops"); - return -1; - } - } else { - if (!(ops = realloc((void *) ops, POINTSIZE * newopn))) { - prerror("cannot realloc ops"); - return -1; - } + if (!(ops = realloc((void *) ops, POINTSIZE * newopn))) { + prerror("cannot realloc ops"); + return -1; } opn = newopn; diff --git a/lib/pathplan/util.c b/lib/pathplan/util.c index 589399f39..a6f2eab22 100644 --- a/lib/pathplan/util.c +++ b/lib/pathplan/util.c @@ -16,7 +16,7 @@ #include #include -#define ALLOC(size,ptr,type) (ptr? realloc(ptr,(size)*sizeof(type)):malloc((size)*sizeof(type))) +#define ALLOC(size,ptr,type) realloc(ptr,(size)*sizeof(type)) Ppoly_t copypoly(Ppoly_t argpoly) { diff --git a/plugin/gd/gvtextlayout_gd.c b/plugin/gd/gvtextlayout_gd.c index 7d8780375..3537fb063 100644 --- a/plugin/gd/gvtextlayout_gd.c +++ b/plugin/gd/gvtextlayout_gd.c @@ -43,10 +43,7 @@ char *gd_alternate_fontlist(char *font) len = strlen(font) + 1; if (len > fontbufsz) { fontbufsz = 2 * len; - if (fontbuf == NULL) - fontbuf = malloc(fontbufsz); - else - fontbuf = realloc(fontbuf, fontbufsz); + fontbuf = realloc(fontbuf, fontbufsz); } /* fontbuf to contain font without style descriptions like -Roman or -Italic */ -- 2.40.0