]> granicus.if.org Git - graphviz/commitdiff
use a local allocation instead of global hack in gvrender_polygon
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sun, 4 Oct 2020 20:24:07 +0000 (13:24 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 10 Oct 2020 21:45:05 +0000 (14:45 -0700)
There is a static global, AF, that is reused for a number of gvrender functions,
but none need to retain previous data stored in this array. This hack presumably
was from a time when allocators were much slower. Refactoring this into a local
allocation makes this function thread safe and removes the need to unnecessarily
prolong the lifetime of this allocation, thus decreasing Graphviz memory usage.
This commit introduces a -Wshadow warning about AF, but that will be removed
when we soon remove the static global.

lib/gvc/gvrender.c

index 9a47173c40af2df0b74c19e682d2badb2149f128..0cc0a4b6a6c5dba61acab8769a6bbc27caa7e025 100644 (file)
@@ -19,6 +19,7 @@
 
 #include "config.h"
 
+#include <assert.h>
 #include <string.h>
 #include <common/memory.h>
 #include <common/const.h>
@@ -31,6 +32,7 @@
 #include <common/geomprocs.h>
 #include <gvc/gvcproc.h>
 #include <cgraph/strcasecmp.h>
+#include <stdlib.h>
 
 extern int emit_once(char *str);
 extern shape_desc *find_user_shape(char *name);
@@ -583,12 +585,12 @@ void gvrender_polygon(GVJ_t * job, pointf * af, int n, int filled)
            if (job->flags & GVRENDER_DOES_TRANSFORM)
                gvre->polygon(job, af, n, filled);
            else {
-               if (sizeAF < n) {
-                   sizeAF = n + 10;
-                   AF = grealloc(AF, sizeAF * sizeof(pointf));
-               }
+               pointf *AF;
+               assert(n >= 0);
+               AF = gcalloc((size_t)n, sizeof(pointf));
                gvrender_ptf_A(job, af, AF, n);
                gvre->polygon(job, AF, n, filled);
+               free(AF);
            }
            if (noPoly)
                job->obj->pencolor = save_pencolor;