add MEM_SIZE_OPTIMAL to avoid memory fragmentation & waste lost to slop-space.

This commit is contained in:
Campbell Barton
2013-10-10 18:18:13 +00:00
parent 8fd52b3433
commit e220d3228f
11 changed files with 18 additions and 14 deletions

View File

@@ -173,6 +173,10 @@ extern "C" {
#define MEM_SAFE_FREE(v) if (v) { MEM_freeN(v); v = NULL; } (void)0
/* overhead for lockfree allocator (use to avoid slop-space) */
#define MEM_SIZE_OVERHEAD sizeof(size_t)
#define MEM_SIZE_OPTIMAL(size) ((size) - MEM_SIZE_OVERHEAD)
#ifndef NDEBUG
extern const char *(*MEM_name_ptr)(void *vmemh);
#endif

View File

@@ -409,7 +409,7 @@ static void layer_bucket_init_dummy(MaskRasterLayer *layer)
static void layer_bucket_init(MaskRasterLayer *layer, const float pixel_size)
{
MemArena *arena = BLI_memarena_new(1 << 16, __func__);
MemArena *arena = BLI_memarena_new(MEM_SIZE_OPTIMAL(1 << 16), __func__);
const float bucket_dim_x = BLI_rctf_size_x(&layer->bounds);
const float bucket_dim_y = BLI_rctf_size_y(&layer->bounds);

View File

@@ -170,7 +170,7 @@ static CCGSubSurf *_getSubSurf(CCGSubSurf *prevSS, int subdivLevels,
if (useArena) {
CCGAllocatorIFC allocatorIFC;
CCGAllocatorHDL allocator = BLI_memarena_new((1 << 16), "subsurf arena");
CCGAllocatorHDL allocator = BLI_memarena_new(MEM_SIZE_OPTIMAL(1 << 16), "subsurf arena");
allocatorIFC.alloc = arena_alloc;
allocatorIFC.realloc = arena_realloc;

View File

@@ -47,7 +47,7 @@ extern "C" {
* enough to not cause much internal fragmentation,
* small enough not to waste resources
*/
#define BLI_MEMARENA_STD_BUFSIZE (1 << 14)
#define BLI_MEMARENA_STD_BUFSIZE MEM_SIZE_OPTIMAL(1 << 14)
struct MemArena;
typedef struct MemArena MemArena;

View File

@@ -51,7 +51,7 @@ typedef struct ScanFillContext {
struct MemArena *arena;
} ScanFillContext;
#define BLI_SCANFILL_ARENA_SIZE 16384
#define BLI_SCANFILL_ARENA_SIZE MEM_SIZE_OPTIMAL(1 << 14)
typedef struct ScanFillVert {
struct ScanFillVert *next, *prev;

View File

@@ -127,7 +127,7 @@ Heap *BLI_heap_new_ex(unsigned int tot_reserve)
/* ensure we have at least one so we can keep doubling it */
heap->bufsize = MAX2(1, tot_reserve);
heap->tree = (HeapNode **)MEM_mallocN(heap->bufsize * sizeof(HeapNode *), "BLIHeapTree");
heap->arena = BLI_memarena_new(1 << 16, "heap arena");
heap->arena = BLI_memarena_new(MEM_SIZE_OPTIMAL(1 << 16), "heap arena");
return heap;
}

View File

@@ -2389,7 +2389,7 @@ void BM_mesh_bevel(BMesh *bm, const float offset, const float segments,
if (bp.offset > 0) {
/* primary alloc */
bp.vert_hash = BLI_ghash_ptr_new(__func__);
bp.mem_arena = BLI_memarena_new((1 << 16), __func__);
bp.mem_arena = BLI_memarena_new(MEM_SIZE_OPTIMAL(1 << 16), __func__);
BLI_memarena_use_calloc(bp.mem_arena);
if (limit_offset)

View File

@@ -2131,7 +2131,7 @@ static void knifenet_fill_faces(KnifeTool_OpData *kcd)
facenet_entry *entry;
ListBase *face_nets = MEM_callocN(sizeof(ListBase) * bm->totface, "face_nets");
BMFace **faces = MEM_callocN(sizeof(BMFace *) * bm->totface, "faces knife");
MemArena *arena = BLI_memarena_new(1 << 16, "knifenet_fill_faces");
MemArena *arena = BLI_memarena_new(MEM_SIZE_OPTIMAL(1 << 16), "knifenet_fill_faces");
SmallHash shash;
RNG *rng;
int i, j, k = 0, totface = bm->totface;
@@ -3159,7 +3159,7 @@ static void knifetool_init(bContext *C, KnifeTool_OpData *kcd,
(only_select ? BMBVH_RESPECT_SELECT : BMBVH_RESPECT_HIDDEN),
kcd->cagecos, false);
kcd->arena = BLI_memarena_new(1 << 15, "knife");
kcd->arena = BLI_memarena_new(MEM_SIZE_OPTIMAL(1 << 15), "knife");
kcd->vthresh = KMAXDIST - 1;
kcd->ethresh = KMAXDIST;

View File

@@ -3129,7 +3129,7 @@ static void project_paint_begin(ProjPaintState *ps)
ps->thread_tot = 1;
for (a = 0; a < ps->thread_tot; a++) {
ps->arena_mt[a] = BLI_memarena_new(1 << 16, "project paint arena");
ps->arena_mt[a] = BLI_memarena_new(MEM_SIZE_OPTIMAL(1 << 16), "project paint arena");
}
arena = ps->arena_mt[0];
@@ -3841,10 +3841,10 @@ static void *do_projectpaint_thread(void *ph_v)
pos_ofs[0] = pos[0] - lastpos[0];
pos_ofs[1] = pos[1] - lastpos[1];
smearArena = BLI_memarena_new(1 << 16, "paint smear arena");
smearArena = BLI_memarena_new(MEM_SIZE_OPTIMAL(1 << 16), "paint smear arena");
}
else if (tool == PAINT_TOOL_SOFTEN) {
softenArena = BLI_memarena_new(1 << 16, "paint soften arena");
softenArena = BLI_memarena_new(MEM_SIZE_OPTIMAL(1 << 16), "paint soften arena");
}
/* printf("brush bounds %d %d %d %d\n", bucketMin[0], bucketMin[1], bucketMax[0], bucketMax[1]); */

View File

@@ -2788,7 +2788,7 @@ static void vpaint_build_poly_facemap(struct VPaintData *vd, Mesh *me)
int *origIndex;
int i;
vd->polyfacemap_arena = BLI_memarena_new(1 << 13, "vpaint tmp");
vd->polyfacemap_arena = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, "vpaint tmp");
BLI_memarena_use_calloc(vd->polyfacemap_arena);
vd->polyfacemap = BLI_memarena_alloc(vd->polyfacemap_arena, sizeof(ListBase) * me->totpoly);

View File

@@ -4123,7 +4123,7 @@ static void p_smooth(PChart *chart)
MEM_freeN(nodesx);
MEM_freeN(nodesy);
arena = BLI_memarena_new(1 << 16, "param smooth arena");
arena = BLI_memarena_new(MEM_SIZE_OPTIMAL(1 << 16), "param smooth arena");
root = p_node_new(arena, tri, esize * 2, minv, maxv, 0);
for (v = chart->verts; v; v = v->nextlink)
@@ -4143,7 +4143,7 @@ ParamHandle *param_construct_begin(void)
PHandle *handle = MEM_callocN(sizeof(*handle), "PHandle");
handle->construction_chart = p_chart_new(handle);
handle->state = PHANDLE_STATE_ALLOCATED;
handle->arena = BLI_memarena_new((1 << 16), "param construct arena");
handle->arena = BLI_memarena_new(MEM_SIZE_OPTIMAL(1 << 16), "param construct arena");
handle->aspx = 1.0f;
handle->aspy = 1.0f;
handle->do_aspect = FALSE;