From c6b9f1912fd3821f9806f74fa9b81ff9ba4d7b4e Mon Sep 17 00:00:00 2001 From: rodri Date: Mon, 18 Dec 2023 22:03:02 +0000 Subject: fix memory leak when counting faces during shading. since the model won't change, instead of freeing the elems on every run i decided to make the relevant counts statically allocated, and do the counting once. --- main.c | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/main.c b/main.c index 5165ce2..67aae9b 100644 --- a/main.c +++ b/main.c @@ -712,32 +712,35 @@ shaderunit(void *arg) void shade(Framebuf *fb, Shader *s) { - int i, nelems, nparts, nworkers; + static int nparts, nworkers; + static OBJElem **elems = nil; + int i, nelems; uvlong time; OBJObject *o; - OBJElem **elems, *e; + OBJElem *e; OBJIndexArray *idxtab; SUparams *params; Channel *donec; - elems = nil; - nelems = 0; - for(i = 0; i < nelem(model->objtab); i++) - for(o = model->objtab[i]; o != nil; o = o->next) - for(e = o->child; e != nil; e = e->next){ - idxtab = &e->indextab[OBJVGeometric]; - /* discard non-triangles */ - if(e->type != OBJEFace || idxtab->nindex != 3) - continue; - elems = erealloc(elems, ++nelems*sizeof(*elems)); - elems[nelems-1] = e; - } - if(nelems < nprocs){ - nworkers = nelems; - nparts = 1; - }else{ - nworkers = nprocs; - nparts = nelems/nprocs; + if(elems == nil){ + nelems = 0; + for(i = 0; i < nelem(model->objtab); i++) + for(o = model->objtab[i]; o != nil; o = o->next) + for(e = o->child; e != nil; e = e->next){ + idxtab = &e->indextab[OBJVGeometric]; + /* discard non-triangles */ + if(e->type != OBJEFace || idxtab->nindex != 3) + continue; + elems = erealloc(elems, ++nelems*sizeof(*elems)); + elems[nelems-1] = e; + } + if(nelems < nprocs){ + nworkers = nelems; + nparts = 1; + }else{ + nworkers = nprocs; + nparts = nelems/nprocs; + } } time = nanosec(); -- cgit v1.2.3