diff options
author | rodri <rgl@antares-labs.eu> | 2023-12-18 22:03:02 +0000 |
---|---|---|
committer | rodri <rgl@antares-labs.eu> | 2023-12-18 22:03:02 +0000 |
commit | c6b9f1912fd3821f9806f74fa9b81ff9ba4d7b4e (patch) | |
tree | 279886b20dc3a4b32d4ddd21968f469ced256833 | |
parent | 2621771b920cecec2c0232a174a5aea4e1bd027f (diff) | |
download | tinyrend-c6b9f1912fd3821f9806f74fa9b81ff9ba4d7b4e.tar.gz tinyrend-c6b9f1912fd3821f9806f74fa9b81ff9ba4d7b4e.tar.bz2 tinyrend-c6b9f1912fd3821f9806f74fa9b81ff9ba4d7b4e.zip |
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.
-rw-r--r-- | main.c | 43 |
1 files changed, 23 insertions, 20 deletions
@@ -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(); |