From 8460717945ec20f5c9720d58193aab178150eb03 Mon Sep 17 00:00:00 2001 From: Matthew Fernandez Date: Sat, 14 Jan 2023 15:27:43 -0800 Subject: [PATCH] bcomps blockName: remove long lived allocation It looks like a mistake in the initial revision of Graphviz that this function never wrote to `bufsz`. The effect of this was that this function would reallocate on every single call. So empirically it seems there was no need to avoid allocating new memory on each call, which is what this commit does. Gitlab: #1950 --- cmd/tools/bcomps.c | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/cmd/tools/bcomps.c b/cmd/tools/bcomps.c index 3faeff8c0..19d8962e6 100644 --- a/cmd/tools/bcomps.c +++ b/cmd/tools/bcomps.c @@ -28,6 +28,7 @@ #include #include +#include #include #include #include @@ -74,22 +75,12 @@ typedef struct { Agraph_t *blks; } bcstate; -static char *blockName(char *gname, int d) -{ - static char *buf; - static size_t bufsz; - - size_t sz = strlen(gname) + 128; - if (sz > bufsz) { - free(buf); - buf = gv_alloc(sz); - } - +static char *blockName(agxbuf *xb, char *gname, int d) { if (*gname == '%') /* anonymous graph */ - sprintf(buf, "_%s_bcc_%d", gname, d); + agxbprint(xb, "_%s_bcc_%d", gname, d); else - sprintf(buf, "%s_bcc_%d", gname, d); - return buf; + agxbprint(xb, "%s_bcc_%d", gname, d); + return agxbuse(xb); } /* getName: @@ -154,7 +145,9 @@ static Agraph_t *mkBlock(Agraph_t * g, bcstate * stp) Agraph_t *sg; stp->nComp++; - sg = agsubg(g, blockName(agnameof(g), stp->nComp), 1); + agxbuf xb = {0}; + sg = agsubg(g, blockName(&xb, agnameof(g), stp->nComp), 1); + agxbfree(&xb); agbindrec(sg, "info", sizeof(Agraphinfo_t), true); NEXTBLK(sg) = stp->blks; stp->blks = sg; -- 2.40.0