[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [42241] branches/bmesh/blender/source/ blender: macro for defining a fixed size array which may be heap or stack allocated , replaces inline code.

Campbell Barton ideasman42 at gmail.com
Tue Nov 29 07:07:09 CET 2011


Revision: 42241
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=42241
Author:   campbellbarton
Date:     2011-11-29 06:06:59 +0000 (Tue, 29 Nov 2011)
Log Message:
-----------
macro for defining a fixed size array which may be heap or stack allocated, replaces inline code.

BLI_array_fixedstack_declare()
BLI_array_fixedstack_free()

Modified Paths:
--------------
    branches/bmesh/blender/source/blender/blenkernel/intern/mesh.c
    branches/bmesh/blender/source/blender/blenlib/BLI_array.h
    branches/bmesh/blender/source/blender/bmesh/intern/bmesh_polygon.c

Modified: branches/bmesh/blender/source/blender/blenkernel/intern/mesh.c
===================================================================
--- branches/bmesh/blender/source/blender/blenkernel/intern/mesh.c	2011-11-29 05:18:54 UTC (rev 42240)
+++ branches/bmesh/blender/source/blender/blenkernel/intern/mesh.c	2011-11-29 06:06:59 UTC (rev 42241)
@@ -2594,12 +2594,10 @@
 	}
 	else {
 		int i;
-		float area, polynorm_local[3], (*vertexcos)[3] = NULL;
+		float area, polynorm_local[3], (*vertexcos)[3];
 		float *no= polynormal ? polynormal : polynorm_local;
-		BLI_array_staticdeclare(vertexcos, BM_NGON_STACK_SIZE);
+		BLI_array_fixedstack_declare(vertexcos, BM_NGON_STACK_SIZE, mpoly->totloop, __func__);
 
-		BLI_array_growitems(vertexcos, mpoly->totloop);
-
 		/* pack vertex cos into an array for area_poly_v3 */
 		for (i = 0; i < mpoly->totloop; i++) {
 			copy_v3_v3(vertexcos[i], mvarray[(loopstart++)->v].co);
@@ -2613,7 +2611,7 @@
 		/* finally calculate the area */
 		area = area_poly_v3(mpoly->totloop, vertexcos, no);
 
-		BLI_array_free(vertexcos);
+		BLI_array_fixedstack_free(vertexcos);
 
 		return area;
 	}

Modified: branches/bmesh/blender/source/blender/blenlib/BLI_array.h
===================================================================
--- branches/bmesh/blender/source/blender/blenlib/BLI_array.h	2011-11-29 05:18:54 UTC (rev 42240)
+++ branches/bmesh/blender/source/blender/blenlib/BLI_array.h	2011-11-29 06:06:59 UTC (rev 42241)
@@ -171,3 +171,19 @@
 	(void)_##arr##_count,                                                     \
 	(void)_##arr##_tmp,                                                       \
 	(void)_##arr##_static
+
+
+/* not part of the 'API' but handy funcs,
+ * same purpose as BLI_array_staticdeclare()
+ * but use when the max size is known ahead of time */
+#define BLI_array_fixedstack_declare(arr, maxstatic, realsize, allocstr)      \
+	char _##arr##_static[maxstatic*sizeof(*arr)];                             \
+	const int _##arr##_is_static= ((void *)_##arr##_static) != (              \
+	    arr= (realsize <= maxstatic) ?                                        \
+	        (void *)_##arr##_static :                                         \
+	        MEM_mallocN(sizeof(*arr)*realsize, allocstr)                      \
+	    )                                                                     \
+
+#define BLI_array_fixedstack_free(arr)                                        \
+	if (_##arr##_is_static) MEM_freeN(arr)                                    \
+

Modified: branches/bmesh/blender/source/blender/bmesh/intern/bmesh_polygon.c
===================================================================
--- branches/bmesh/blender/source/blender/bmesh/intern/bmesh_polygon.c	2011-11-29 05:18:54 UTC (rev 42240)
+++ branches/bmesh/blender/source/blender/bmesh/intern/bmesh_polygon.c	2011-11-29 06:06:59 UTC (rev 42241)
@@ -225,13 +225,11 @@
 {
 	BMLoop *l;
 	BMIter iter;
-	float (*verts)[3], stackv[BM_NGON_STACK_SIZE][3];
+	float (*verts)[3];
 	float area, center[3];
 	int i;
 
-	if (f->len <= BM_NGON_STACK_SIZE)
-		verts = stackv;
-	else verts = MEM_callocN(sizeof(float)*f->len*3, "bm_face_area tmp");
+	BLI_array_fixedstack_declare(verts, BM_NGON_STACK_SIZE, f->len, __func__);
 
 	i = 0;
 	BM_ITER(l, &iter, NULL, BM_LOOPS_OF_FACE, f) {
@@ -241,8 +239,7 @@
 
 	compute_poly_center(center, &area, verts, f->len);
 
-	if (f->len > BM_NGON_STACK_SIZE)
-		MEM_freeN(verts);
+	BLI_array_fixedstack_free(verts);
 
 	return area;
 }
@@ -418,14 +415,15 @@
 
 void BM_Face_UpdateNormal(BMesh *bm, BMFace *f)
 {
-	float projverts[BM_NGON_STACK_SIZE][3];
-	float (*proj)[3] = f->len < BM_NGON_STACK_SIZE ? projverts : MEM_mallocN(sizeof(float)*f->len*3, "projvertsn");
+	if (f->len >= 3) {
+		float (*proj)[3];
 
-	if (f->len < 3) return;
+		BLI_array_fixedstack_declare(proj, BM_NGON_STACK_SIZE, f->len, __func__);
 
-	bmesh_update_face_normal(bm, f, proj);
+		bmesh_update_face_normal(bm, f, proj);
 
-	if (projverts != proj) MEM_freeN(proj);
+		BLI_array_fixedstack_free(proj);
+	}
 }
 
 void BM_Edge_UpdateNormals(BMesh *bm, BMEdge *e)
@@ -868,15 +866,13 @@
 	BMLoop *l;
 	float v1[3], v2[3], v3[3]/*, v4[3]*/, no[3], mid[3], *p1, *p2, *p3, *p4;
 	float out[3] = {-234324.0f, -234324.0f, 0.0f};
-	float projectverts[BM_NGON_STACK_SIZE][3];
-	float edgevertsstack[BM_NGON_STACK_SIZE * 2][3];
-	float (*projverts)[3] = projectverts;
-	float (*edgeverts)[3] = edgevertsstack;
+	float (*projverts)[3];
+	float (*edgeverts)[3];
 	float fac1 = 1.0000001f, fac2 = 0.9f; //9999f; //0.999f;
 	int i, j, a=0, clen;
 
-	if (f->len > BM_NGON_STACK_SIZE) projverts = MEM_mallocN(sizeof(float)*3*f->len, "projvertsb");
-	if (len > (BM_NGON_STACK_SIZE * 2)) edgeverts = MEM_mallocN(sizeof(float)*3*2*len, "edgevertsb");
+	BLI_array_fixedstack_declare(projverts, BM_NGON_STACK_SIZE, f->len,         "projvertsb");
+	BLI_array_fixedstack_declare(edgeverts, BM_NGON_STACK_SIZE * 2, len * 2, "edgevertsb");
 	
 	i = 0;
 	l = BMIter_New(&iter, bm, BM_LOOPS_OF_FACE, f);
@@ -989,7 +985,7 @@
 			}
 		}
 	}
-	
-	if (projverts != projectverts) MEM_freeN(projverts);
-	if (edgeverts != edgevertsstack) MEM_freeN(edgeverts);
+
+	BLI_array_fixedstack_free(projverts);
+	BLI_array_fixedstack_free(edgeverts);
 }




More information about the Bf-blender-cvs mailing list