add option to initialize heap with a known number of elements, since this may be known in advance - it avoids re-allocing too much.

This commit is contained in:
Campbell Barton 2012-10-19 10:40:32 +00:00
parent b7642348e4
commit e527ce552e
4 changed files with 43 additions and 4 deletions

@ -42,6 +42,7 @@ typedef void (*HeapFreeFP)(void *ptr);
/* Creates a new heap. BLI_memarena is used for allocating nodes. Removed nodes
* are recycled, so memory usage will not shrink. */
Heap *BLI_heap_new_ex(unsigned int tot_reserve);
Heap *BLI_heap_new(void);
void BLI_heap_free(Heap *heap, HeapFreeFP ptrfreefp);

@ -67,16 +67,22 @@ struct Heap {
/***/
Heap *BLI_heap_new(void)
/* use when the size of the heap is known in advance */
Heap *BLI_heap_new_ex(unsigned int tot_reserve)
{
Heap *heap = (Heap *)MEM_callocN(sizeof(Heap), __func__);
heap->bufsize = 1;
heap->tree = (HeapNode **)MEM_mallocN(sizeof(HeapNode *), "BLIHeapTree");
heap->bufsize = tot_reserve;
heap->tree = (HeapNode **)MEM_mallocN(tot_reserve * sizeof(HeapNode *), "BLIHeapTree");
heap->arena = BLI_memarena_new(1 << 16, "heap arena");
return heap;
}
Heap *BLI_heap_new(void)
{
return BLI_heap_new_ex(1);
}
void BLI_heap_free(Heap *heap, HeapFreeFP ptrfreefp)
{
int i;

@ -531,7 +531,7 @@ void BM_mesh_decimate(BMesh *bm, const float factor)
/* alloc vars */
vquadrics = MEM_callocN(sizeof(Quadric) * bm->totvert, __func__);
eheap = BLI_heap_new();
eheap = BLI_heap_new_ex(bm->totedge);
eheap_table = MEM_callocN(sizeof(HeapNode *) * bm->totedge, __func__);
tot_edge_orig = bm->totedge;

@ -47,6 +47,10 @@
#include "BKE_particle.h"
#include "BKE_cdderivedmesh.h"
#include "BKE_tessmesh.h"
/* testing only! - Campbell */
// #define USE_DECIMATE_BMESH
#ifdef WITH_MOD_DECIMATE
#include "LOD_decimation.h"
@ -70,6 +74,33 @@ static void copyData(ModifierData *md, ModifierData *target)
}
#ifdef WITH_MOD_DECIMATE
#ifdef USE_DECIMATE_BMESH
#include "bmesh.h"
static DerivedMesh *applyModifier(ModifierData *md, Object *UNUSED(ob),
DerivedMesh *derivedData,
ModifierApplyFlag UNUSED(flag))
{
DecimateModifierData *dmd = (DecimateModifierData *) md;
DerivedMesh *dm = derivedData, *result = NULL;
BMEditMesh *em;
BMesh *bm;
em = DM_to_editbmesh(dm, NULL, FALSE);
bm = em->bm;
BM_mesh_decimate(bm, dmd->percent);
BLI_assert(em->looptris == NULL);
result = CDDM_from_BMEditMesh(em, NULL, TRUE, FALSE);
BMEdit_Free(em);
MEM_freeN(em);
return result;
}
#else
static DerivedMesh *applyModifier(ModifierData *md, Object *UNUSED(ob),
DerivedMesh *derivedData,
ModifierApplyFlag UNUSED(flag))
@ -192,6 +223,7 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *UNUSED(ob),
return dm;
}
}
#endif // USE_DECIMATE_BMESH
#else // WITH_MOD_DECIMATE
static DerivedMesh *applyModifier(ModifierData *UNUSED(md), Object *UNUSED(ob),
DerivedMesh *derivedData,