another optimization pass. biggest change is MDeformGroup->dw is now allocated via a somewhat simplistic, if effective allocator. This needs a little bit more work; I'd really prefer building this into guardedalloc, but the method requires mempools, which currently live in blenlib. and I'm not sure if we can have guardedalloc linking with blenlib? anyway, current allocator code is more of a temporary fix until I figure that out.

This commit is contained in:
Joseph Eagar 2009-09-10 01:46:52 +00:00
parent ae446d79e0
commit b0a1904d33
22 changed files with 302 additions and 35 deletions

@ -44,6 +44,7 @@
#include "BKE_bmesh.h"
#include "BLI_arithb.h"
#include "BLI_blenlib.h"
#include "BLI_cellalloc.h"
/*split this all into a seperate bevel.c file in src*/

@ -38,6 +38,7 @@
#include "BLI_arithb.h"
#include "BLI_blenlib.h"
#include "BLI_cellalloc.h"
#include "DNA_armature_types.h"
#include "DNA_action_types.h"

@ -48,6 +48,7 @@
#include "BKE_pointcache.h"
#include "BLI_kdopbvh.h"
#include "BLI_cellalloc.h"
#ifdef _WIN32
void tstart ( void )

@ -38,6 +38,7 @@
#include "BLI_blenlib.h"
#include "BLI_arithb.h"
#include "BLI_editVert.h"
#include "BLI_cellalloc.h"
#include "DNA_armature_types.h"
#include "DNA_constraint_types.h"

@ -38,6 +38,7 @@
#include "BLI_blenlib.h"
#include "BLI_linklist.h"
#include "BLI_mempool.h"
#include "BLI_cellalloc.h"
#include "DNA_customdata_types.h"
#include "DNA_listBase.h"
@ -112,7 +113,7 @@ static void layerCopy_mdeformvert(const void *source, void *dest,
MDeformVert *dvert = (MDeformVert *)((char *)dest + i * size);
if(dvert->totweight) {
MDeformWeight *dw = MEM_callocN(dvert->totweight * sizeof(*dw),
MDeformWeight *dw = BLI_cellalloc_calloc(dvert->totweight * sizeof(*dw),
"layerCopy_mdeformvert dw");
memcpy(dw, dvert->dw, dvert->totweight * sizeof(*dw));
@ -131,7 +132,7 @@ static void layerFree_mdeformvert(void *data, int count, int size)
MDeformVert *dvert = (MDeformVert *)((char *)data + i * size);
if(dvert->dw) {
MEM_freeN(dvert->dw);
BLI_cellalloc_free(dvert->dw);
dvert->dw = NULL;
dvert->totweight = 0;
}
@ -173,7 +174,7 @@ static void layerInterp_mdeformvert(void **sources, float *weights,
/* if this def_nr is not in the list, add it */
if(!node) {
MDeformWeight *tmp_dw = MEM_callocN(sizeof(*tmp_dw),
MDeformWeight *tmp_dw = BLI_cellalloc_calloc(sizeof(*tmp_dw),
"layerInterp_mdeformvert tmp_dw");
tmp_dw->def_nr = dw->def_nr;
tmp_dw->weight = dw->weight * interp_weight;
@ -184,10 +185,10 @@ static void layerInterp_mdeformvert(void **sources, float *weights,
}
/* now we know how many unique deform weights there are, so realloc */
if(dvert->dw) MEM_freeN(dvert->dw);
if(dvert->dw) BLI_cellalloc_free(dvert->dw);
if(totweight) {
dvert->dw = MEM_callocN(sizeof(*dvert->dw) * totweight,
dvert->dw = BLI_cellalloc_calloc(sizeof(*dvert->dw) * totweight,
"layerInterp_mdeformvert dvert->dw");
dvert->totweight = totweight;

@ -60,6 +60,7 @@
#include "BLI_blenlib.h"
#include "BLI_arithb.h"
#include "BLI_cellalloc.h"
#ifdef HAVE_CONFIG_H
#include <config.h>

@ -132,6 +132,45 @@ static void BMEdit_RecalcTesselation_intern(BMEditMesh *tm)
if (tm->looptris) MEM_freeN(tm->looptris);
#if 0 //simple quad/triangle code for performance testing purposes
looptris = MEM_callocN(sizeof(void*)*bm->totface*8, "looptris");
f = BMIter_New(&iter, bm, BM_FACES_OF_MESH, NULL);
for ( ; f; f=BMIter_Step(&iter)) {
EditVert *v, *lastv=NULL, *firstv=NULL;
EditEdge *e;
EditFace *efa;
/*don't consider two-edged faces*/
if (f->len < 3) continue;
//V_GROW(looptris);
//V_GROW(looptris);
//V_GROW(looptris);
looptris[i*3] = f->loopbase;
looptris[i*3+1] = f->loopbase->head.next;
looptris[i*3+2] = f->loopbase->head.next->next;
i++;
if (f->len > 3) {
//V_GROW(looptris);
//V_GROW(looptris);
//V_GROW(looptris);
looptris[i*3] = f->loopbase;
looptris[i*3+1] = f->loopbase->head.next->next;
looptris[i*3+2] = f->loopbase->head.next->next->next;
i++;
}
}
tm->tottri = i;
tm->looptris = looptris;
return;
#endif
f = BMIter_New(&iter, bm, BM_FACES_OF_MESH, NULL);
for ( ; f; f=BMIter_Step(&iter)) {
EditVert *v, *lastv=NULL, *firstv=NULL;
@ -360,7 +399,7 @@ static void bmDM_recalcTesselation(DerivedMesh *dm)
{
EditDerivedBMesh *bmdm= (EditDerivedBMesh*) dm;
bmdm_recalc_lookups(bmdm);
//bmdm_recalc_lookups(bmdm);
}
static void bmDM_foreachMappedVert(DerivedMesh *dm, void (*func)(void *userData, int index, float *co, float *no_f, short *no_s), void *userData)
@ -1463,9 +1502,9 @@ static void bmDM_release(void *dm)
MEM_freeN(bmdm->faceNos);
}
BLI_ghash_free(bmdm->fhash, NULL, NULL);
BLI_ghash_free(bmdm->ehash, NULL, NULL);
BLI_ghash_free(bmdm->vhash, NULL, NULL);
if (bmdm->fhash) BLI_ghash_free(bmdm->fhash, NULL, NULL);
if (bmdm->ehash) BLI_ghash_free(bmdm->ehash, NULL, NULL);
if (bmdm->vhash) BLI_ghash_free(bmdm->vhash, NULL, NULL);
if (bmdm->vtable) MEM_freeN(bmdm->vtable);
if (bmdm->etable) MEM_freeN(bmdm->etable);
@ -1630,7 +1669,7 @@ DerivedMesh *getEditDerivedBMesh(BMEditMesh *em, Object *ob,
}
}
bmdm_recalc_lookups(bmdm);
//bmdm_recalc_lookups(bmdm);
return (DerivedMesh*) bmdm;
}

@ -58,6 +58,7 @@
#include "BKE_utildefines.h"
#include "BLI_blenlib.h"
#include "BLI_cellalloc.h"
#ifdef HAVE_CONFIG_H

@ -40,6 +40,7 @@
#include "BLI_blenlib.h"
#include "BLI_arithb.h"
#include "BLI_cellalloc.h"
#include "DNA_armature_types.h"
#include "DNA_mesh_types.h"

@ -246,7 +246,7 @@ void copy_dverts(MDeformVert *dst, MDeformVert *src, int copycount)
for (i=0; i<copycount; i++){
if (src[i].dw){
dst[i].dw = MEM_callocN (sizeof(MDeformWeight)*src[i].totweight, "copy_deformWeight");
dst[i].dw = BLI_cellalloc_calloc (sizeof(MDeformWeight)*src[i].totweight, "copy_deformWeight");
memcpy (dst[i].dw, src[i].dw, sizeof (MDeformWeight)*src[i].totweight);
}
}

@ -50,6 +50,7 @@
#include "BLI_edgehash.h"
#include "BLI_ghash.h"
#include "BLI_memarena.h"
#include "BLI_cellalloc.h"
#include "MEM_guardedalloc.h"

@ -45,6 +45,7 @@
#include "BLI_edgehash.h"
#include "BLI_ghash.h"
#include "BLI_memarena.h"
#include "BLI_cellalloc.h"
#include "MEM_guardedalloc.h"
@ -288,7 +289,7 @@ static DerivedMesh *arrayModifier_doArray(ArrayModifierData *amd,
int numVerts, numEdges, numFaces;
int maxVerts, maxEdges, maxFaces;
int finalVerts, finalEdges, finalFaces;
int *indexMap;
int *indexMap = NULL;
DerivedMesh *result, *start_cap = NULL, *end_cap = NULL;
MVert *src_mvert;
@ -406,7 +407,6 @@ static DerivedMesh *arrayModifier_doArray(ArrayModifierData *amd,
#define _E(s, i) ((BMVert**)(s)->data.buf)[i]
#if 0
/*calculate merge mapping*/
if (j == 0) {
BMOperator findop;
@ -444,20 +444,19 @@ static DerivedMesh *arrayModifier_doArray(ArrayModifierData *amd,
BMO_Finish_Op(em->bm, &findop);
}
#endif
/*generate merge mappping using index map. we do this by using the
operator slots as lookup arrays.*/
#define E(i) (i) < s1->len ? _E(s1, i) : _E(s2, (i)-s1->len)
/*for (i=0; i<indexLen; i++) {
for (i=0; i<indexLen; i++) {
if (!indexMap[i]) continue;
v = E(i);
v2 = E(indexMap[i]-1);
BMO_Insert_MapPointer(em->bm, &weldop, "targetmap", v, v2);
}*/
}
#undef E
#undef _E
@ -477,7 +476,7 @@ static DerivedMesh *arrayModifier_doArray(ArrayModifierData *amd,
cddm = CDDM_from_BMEditMesh(em, NULL);
BMEdit_Free(em);
//MEM_freeN(indexMap);
MEM_freeN(indexMap);
return cddm;
}

@ -56,6 +56,7 @@
#include "BLI_linklist.h"
#include "BLI_rand.h"
#include "BLI_threads.h"
#include "BLI_cellalloc.h"
#include "BKE_anim.h"

@ -70,6 +70,7 @@ variables on the UI for now
#include "BLI_arithb.h"
#include "BLI_ghash.h"
#include "BLI_threads.h"
#include "BLI_cellalloc.h"
#include "BKE_curve.h"
#include "BKE_effect.h"

@ -0,0 +1,43 @@
/**
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* The Original Code is Copyright (C) 2008 by Blender Foundation.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
*
* ***** END GPL LICENSE BLOCK *****
*/
/*
Simple, fast memory allocator that uses many BLI_mempools for allocation.
use for allocations < 1024 bytes that simply cannot be rewritten to use
mempools directly or memarenas.
Should really become part of guardedalloc, but I'm not sure how to
get it linking with blenlib on all build systems. - joeedh
*/
void *BLI_cellalloc_malloc(long size, char *tag);
void *BLI_cellalloc_calloc(long size, char *tag);
void BLI_cellalloc_free(void *mem);
void BLI_cellalloc_printleaks(void);
int BLI_cellalloc_get_totblock(void);
void BLI_cellalloc_destroy(void);

@ -0,0 +1,151 @@
/**
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* The Original Code is Copyright (C) 2008 by Blender Foundation.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): Joseph Eagar
*
* ***** END GPL LICENSE BLOCK *****
*/
/*
Simple, fast memory allocator that uses many BLI_mempools for allocation.
this is meant to be used by lots of relatively small objects.
*/
#include "MEM_guardedalloc.h"
#include "BKE_utildefines.h"
#include "BLI_blenlib.h"
#include "BLI_linklist.h"
#include "DNA_listBase.h"
#include "BLI_mempool.h"
#include <string.h>
static BLI_mempool **pools;
static int totpool = 0;
ListBase active_mem = {NULL, NULL};
static int celltotblock = 0;
#define MEMIDCHECK ('a' | ('b' << 4) | ('c' << 8) | ('d' << 12))
typedef struct MemHeader {
struct MemHeader *next, *prev;
int size;
char *tag;
int idcheck;
} MemHeader;
void *BLI_cellalloc_malloc(long size, char *tag)
{
MemHeader *memh;
int slot = size + sizeof(MemHeader);
if (!slot)
return NULL;
if (slot >= totpool) {
void *tmp;
tmp = calloc(1, sizeof(void*)*(slot+1));
if (pools) {
memcpy(tmp, pools, totpool*sizeof(void*));
}
pools = tmp;
totpool = slot+1;
}
if (!pools[slot]) {
pools[slot] = BLI_mempool_create(slot, 1, 128);
}
memh = BLI_mempool_alloc(pools[slot]);
memh->size = size;
memh->idcheck = MEMIDCHECK;
memh->tag = tag;
BLI_addtail(&active_mem, memh);
celltotblock++;
return memh + 1;
}
void *BLI_cellalloc_calloc(long size, char *tag)
{
void *mem = BLI_cellalloc_malloc(size, tag);
BMEMSET(mem, 0, size);
return mem;
}
void BLI_cellalloc_free(void *mem)
{
MemHeader *memh = mem;
if (!memh)
return;
memh--;
if (memh->idcheck != MEMIDCHECK) {
printf("Error in BLI_cellalloc: attempt to free invalid block.\n");
return;
}
if (memh->size > 0 && memh->size+sizeof(MemHeader) < totpool) {
BLI_remlink(&active_mem, memh);
BLI_mempool_free(pools[memh->size+sizeof(MemHeader)], memh);
celltotblock--;
} else {
printf("Error in BLI_cellalloc: attempt to free corrupted block.\n");
}
}
void BLI_cellalloc_printleaks(void)
{
MemHeader *memh;
if (!active_mem.first) return;
for (memh=active_mem.first; memh; memh=memh->next) {
printf("%s %d %p\n", memh->tag, memh->size, memh+1);
}
}
int BLI_cellalloc_get_totblock(void)
{
return celltotblock;
}
void BLI_cellalloc_destroy(void)
{
int i;
for (i=0; i<totpool; i++) {
if (pools[i]) {
BLI_mempool_destroy(pools[i]);
pools[i] = NULL;
}
}
}

@ -21,7 +21,7 @@
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
* Contributor(s): Geoffery Bantle
*
* ***** END GPL LICENSE BLOCK *****
*/

@ -107,6 +107,7 @@
#include "BLI_blenlib.h"
#include "BLI_arithb.h"
#include "BLI_storage_types.h" // for relname flags
#include "BLI_cellalloc.h"
#include "BKE_animsys.h"
#include "BKE_action.h"

@ -188,16 +188,19 @@ void BM_Compute_Normals(BMesh *bm)
BMVert *v;
BMFace *f;
BMLoop *l;
BMIter verts;
BMIter faces;
BMIter loops;
unsigned int maxlength = 0;
float (*projectverts)[3];
//return;
/*first, find out the largest face in mesh*/
for(f = BMIter_New(&faces, bm, BM_FACES_OF_MESH, bm ); f; f = BMIter_Step(&faces)){
if (BM_TestHFlag(f, BM_HIDDEN))
continue;
if(f->len > maxlength) maxlength = f->len;
}
@ -209,22 +212,36 @@ void BM_Compute_Normals(BMesh *bm)
/*calculate all face normals*/
for(f = BMIter_New(&faces, bm, BM_FACES_OF_MESH, bm ); f; f = BMIter_Step(&faces)){
if (f->head.flag & BM_NONORMCALC) continue;
if (BM_TestHFlag(f, BM_HIDDEN))
continue;
if (f->head.flag & BM_NONORMCALC)
continue;
bmesh_update_face_normal(bm, f, projectverts);
}
/*Zero out vertex normals*/
for(v = BMIter_New(&verts, bm, BM_VERTS_OF_MESH, bm ); v; v = BMIter_Step(&verts))
for(v = BMIter_New(&verts, bm, BM_VERTS_OF_MESH, bm ); v; v = BMIter_Step(&verts)) {
if (BM_TestHFlag(v, BM_HIDDEN))
continue;
v->no[0] = v->no[1] = v->no[2] = 0.0;
}
/*add face normals to vertices*/
for(f = BMIter_New(&faces, bm, BM_FACES_OF_MESH, bm ); f; f = BMIter_Step(&faces)){
if (BM_TestHFlag(f, BM_HIDDEN))
continue;
for(l = BMIter_New(&loops, bm, BM_LOOPS_OF_FACE, f ); l; l = BMIter_Step(&loops))
VecAddf(l->v->no, l->v->no, f->no);
}
/*average the vertex normals*/
for(v = BMIter_New(&verts, bm, BM_VERTS_OF_MESH, bm ); v; v= BMIter_Step(&verts)){
if (BM_TestHFlag(v, BM_HIDDEN))
continue;
if (Normalize(v->no)==0.0) {
VECCOPY(v->no, v->co);
Normalize(v->no);

@ -47,6 +47,7 @@
#include "BLI_blenlib.h"
#include "BLI_editVert.h"
#include "BLI_cellalloc.h"
#include "BKE_context.h"
#include "BKE_customdata.h"
@ -185,10 +186,10 @@ MDeformWeight *verify_defweight (MDeformVert *dv, int defgroup)
if (newdw)
return newdw;
newdw = MEM_callocN (sizeof(MDeformWeight)*(dv->totweight+1), "deformWeight");
newdw = BLI_cellalloc_calloc(sizeof(MDeformWeight)*(dv->totweight+1), "deformWeight");
if (dv->dw){
memcpy (newdw, dv->dw, sizeof(MDeformWeight)*dv->totweight);
MEM_freeN (dv->dw);
BLI_cellalloc_free (dv->dw);
}
dv->dw=newdw;
@ -577,13 +578,13 @@ void remove_vert_def_nr (Object *ob, int def_nr, int vertnum)
* deform weight, and reshuffle the others
*/
if (dvert->totweight) {
newdw = MEM_mallocN (sizeof(MDeformWeight)*(dvert->totweight),
newdw = BLI_cellalloc_malloc (sizeof(MDeformWeight)*(dvert->totweight),
"deformWeight");
if (dvert->dw){
memcpy (newdw, dvert->dw, sizeof(MDeformWeight)*i);
memcpy (newdw+i, dvert->dw+i+1,
sizeof(MDeformWeight)*(dvert->totweight-i));
MEM_freeN (dvert->dw);
BLI_cellalloc_free (dvert->dw);
}
dvert->dw=newdw;
}
@ -591,7 +592,7 @@ void remove_vert_def_nr (Object *ob, int def_nr, int vertnum)
* left then just remove the deform weight
*/
else {
MEM_freeN (dvert->dw);
BLI_cellalloc_free (dvert->dw);
dvert->dw = NULL;
break;
}
@ -677,11 +678,11 @@ void add_vert_defnr (Object *ob, int def_nr, int vertnum,
/* if we are doing an additive assignment, then
* we need to create the deform weight
*/
newdw = MEM_callocN (sizeof(MDeformWeight)*(dv->totweight+1),
newdw = BLI_cellalloc_calloc (sizeof(MDeformWeight)*(dv->totweight+1),
"deformWeight");
if (dv->dw){
memcpy (newdw, dv->dw, sizeof(MDeformWeight)*dv->totweight);
MEM_freeN (dv->dw);
BLI_cellalloc_free (dv->dw);
}
dv->dw=newdw;
@ -772,10 +773,10 @@ void assign_verts_defgroup (Object *ob, float weight)
}
/* If not: Add the group and set its weight */
if (!done){
newdw = MEM_callocN (sizeof(MDeformWeight)*(dvert->totweight+1), "deformWeight");
newdw = BLI_cellalloc_calloc (sizeof(MDeformWeight)*(dvert->totweight+1), "deformWeight");
if (dvert->dw){
memcpy (newdw, dvert->dw, sizeof(MDeformWeight)*dvert->totweight);
MEM_freeN (dvert->dw);
BLI_cellalloc_free (dvert->dw);
}
dvert->dw=newdw;
@ -918,7 +919,7 @@ void remove_verts_defgroup (Object *ob, int allverts)
if (eg == dg){
dvert->totweight--;
if (dvert->totweight){
newdw = MEM_mallocN (sizeof(MDeformWeight)*(dvert->totweight), "deformWeight");
newdw = BLI_cellalloc_malloc (sizeof(MDeformWeight)*(dvert->totweight), "deformWeight");
if (dvert->dw){
memcpy (newdw, dvert->dw, sizeof(MDeformWeight)*i);
@ -928,7 +929,7 @@ void remove_verts_defgroup (Object *ob, int allverts)
dvert->dw=newdw;
}
else{
MEM_freeN (dvert->dw);
BLI_cellalloc_free (dvert->dw);
dvert->dw=NULL;
break;
}

@ -44,6 +44,7 @@
#include "BLI_blenlib.h"
#include "BLI_arithb.h"
#include "BLI_memarena.h"
#include "BLI_cellalloc.h"
#include "MTC_matrixops.h"
#include "DNA_action_types.h"

@ -55,6 +55,7 @@
#include "BKE_packedFile.h"
#include "BLI_blenlib.h"
#include "BLI_cellalloc.h"
#include "RE_pipeline.h" /* RE_ free stuff */
@ -268,8 +269,11 @@ void WM_exit(bContext *C)
SYS_DeleteSystem(SYS_GetSystem());
if(MEM_get_memory_blocks_in_use()!=0) {
printf("Error Totblock: %d\n", MEM_get_memory_blocks_in_use());
if(MEM_get_memory_blocks_in_use()!=0 || BLI_cellalloc_get_totblock()!=0) {
printf("Error Totblock: %d\n",
BLI_cellalloc_get_totblock()+MEM_get_memory_blocks_in_use());
BLI_cellalloc_printleaks();
BLI_cellalloc_destroy();
MEM_printmemlist();
}
// delete_autosave();