From: Matthew Fernandez Date: Sun, 4 Oct 2020 20:24:07 +0000 (-0700) Subject: use a local allocation instead of global hack in gvrender_polygon X-Git-Tag: 2.46.0~20^2^2~43^2~3 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ef1aba3b5b2a73040a92ef208b7dbe35b8aa8ac7;p=graphviz use a local allocation instead of global hack in gvrender_polygon 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. --- diff --git a/lib/gvc/gvrender.c b/lib/gvc/gvrender.c index 9a47173c4..0cc0a4b6a 100644 --- a/lib/gvc/gvrender.c +++ b/lib/gvc/gvrender.c @@ -19,6 +19,7 @@ #include "config.h" +#include #include #include #include @@ -31,6 +32,7 @@ #include #include #include +#include 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;