]> granicus.if.org Git - graphviz/commitdiff
bcomps getName: remove long lived allocation
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 14 Jan 2023 23:38:40 +0000 (15:38 -0800)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sun, 15 Jan 2023 17:51:26 +0000 (09:51 -0800)
This code is not on a hot path, so we can afford to use clearer scoping and
ownership semantics.

Gitlab: #1950

cmd/tools/bcomps.c

index 8312121a192d299eb6c9f704b4e6ad6a20f223e7..b17c76ab81cc116483091b75073422143f3cf13c 100644 (file)
@@ -92,30 +92,24 @@ static char *blockName(agxbuf *xb, char *gname, int d) {
  */
 static char *getName(int ng, int nb)
 {
-    char *name;
-    static char *buf;
+    agxbuf name = {0};
 
     if (ng == 0 && nb == 0)
-       name = outfile;
+       agxbput(&name, outfile);
     else {
-       if (!buf) {
-           size_t sz = strlen(outfile) + 100; // enough to handle '_<g>_<b>'
-           buf = gv_alloc(sz);
-       }
        if (suffix) {
            if (nb < 0)
-               sprintf(buf, "%s_%d_T.%s", path, ng, suffix);
+               agxbprint(&name, "%s_%d_T.%s", path, ng, suffix);
            else
-               sprintf(buf, "%s_%d_%d.%s", path, ng, nb, suffix);
+               agxbprint(&name, "%s_%d_%d.%s", path, ng, nb, suffix);
        } else {
            if (nb < 0)
-               sprintf(buf, "%s_%d_T", path, ng);
+               agxbprint(&name, "%s_%d_T", path, ng);
            else
-               sprintf(buf, "%s_%d_%d", path, ng, nb);
+               agxbprint(&name, "%s_%d_%d", path, ng, nb);
        }
-       name = buf;
     }
-    return name;
+    return agxbdisown(&name);
 }
 
 static void gwrite(Agraph_t * g, int ng, int nb)
@@ -134,8 +128,10 @@ static void gwrite(Agraph_t * g, int ng, int nb)
        if (!outf) {
            fprintf(stderr, "Could not open %s for writing\n", name);
            perror("bcomps");
+           free(name);
            graphviz_exit(1);
        }
+       free(name);
        agwrite(g, outf);
        fclose(outf);
     }