[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [58849] branches/ soc-2013-meshdata_transfer/source/blender/bmesh/tools/bmesh_data_transfer.c : Vertex Colors transfer through projection: fixing the memory leaks and optimizing the reallocations to occur only if the size was bigger than both the expected and the max in the history

Walid Shouman eng.walidshouman at gmail.com
Sat Aug 3 10:28:38 CEST 2013


Revision: 58849
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=58849
Author:   walid
Date:     2013-08-03 08:28:38 +0000 (Sat, 03 Aug 2013)
Log Message:
-----------
Vertex Colors transfer through projection: fixing the memory leaks and optimizing the reallocations to occur only if the size was bigger than both the expected and the max in the history

Modified Paths:
--------------
    branches/soc-2013-meshdata_transfer/source/blender/bmesh/tools/bmesh_data_transfer.c

Modified: branches/soc-2013-meshdata_transfer/source/blender/bmesh/tools/bmesh_data_transfer.c
===================================================================
--- branches/soc-2013-meshdata_transfer/source/blender/bmesh/tools/bmesh_data_transfer.c	2013-08-03 06:57:03 UTC (rev 58848)
+++ branches/soc-2013-meshdata_transfer/source/blender/bmesh/tools/bmesh_data_transfer.c	2013-08-03 08:28:38 UTC (rev 58849)
@@ -2197,10 +2197,12 @@
 	BMVert *v2;
 	float (*v_co_list_src)[3];
 	float (*v_co_list_dst)[3];
-	int v_src_count;
-	int v_dst_count;
+	int v_src_count = 0;
+	int v_dst_count = 0;
+	int v_src_max_count, v_dst_max_count;
 
-	int a, b;//, c, d, g, h, i;
+	int a;//, b, c, d, g, h, i;
+	const int exp_vert_per_face = 10;
 	//====algorithm definitions end
 
 	int CD_src, CD_dst;
@@ -2227,13 +2229,16 @@
 	bmtree_src = BKE_bmbvh_new(em_src, 0, NULL, false);
 
 
-	v_co_list_dst = MEM_mallocN(sizeof(*v_co_list_dst), "v_co_list_dst bmesh_data_transfer.c");
-	v_co_list_src = MEM_mallocN(sizeof(*v_co_list_src), "v_co_list bmesh_data_transfer.c");
+	v_co_list_dst = MEM_mallocN(sizeof(*v_co_list_dst) * exp_vert_per_face, "v_co_list_dst bmesh_data_transfer.c");
+	v_co_list_src = MEM_mallocN(sizeof(*v_co_list_src) * exp_vert_per_face, "v_co_list bmesh_data_transfer.c");
 
 	tot_layer_src = CustomData_number_of_layers(&bm_src->ldata, CD_MLOOPCOL);//to change the last one
 	tot_layer_dst = CustomData_number_of_layers(&bm_dst->ldata, CD_MLOOPCOL);	//get the number of Shapekey layers
 																				//within the target
 
+	//its unlikely to have faces with more than a certain number of vertices ...
+	//we'll later reallocate only if this threshold got exceeded
+	tmp_weight = MEM_mallocN(sizeof(*tmp_weight) * exp_vert_per_face, "tmp_weight bmesh_data_transfer.c");
 	if (replace_mode == ST_APPEND_SHAPEKEY_GROUPS) {
 		//add 1 to skip the basis
 		src_lay_start = 0;
@@ -2270,7 +2275,13 @@
 			//get a coordinate list of the f_dst verts
 			//used to get the the f_mid_dst for mid_poly_v3
 			BM_ITER_ELEM_INDEX (v, &iter, f_dst, BM_VERTS_OF_FACE, v_dst_count) {
-				v_co_list_dst = MEM_reallocN(v_co_list_dst, sizeof(*v_co_list_dst) * (v_dst_count + 1));
+				if (v_dst_count > exp_vert_per_face) {
+					if (v_dst_count > v_dst_max_count) {
+						v_co_list_dst = MEM_reallocN(v_co_list_dst, sizeof(*v_co_list_dst) * (v_dst_count + 1));
+						v_dst_max_count = v_dst_count;
+					}
+				}
+
 				copy_v3_v3(v_co_list_dst[v_dst_count], v->co);
 			}
 
@@ -2288,14 +2299,28 @@
 
 			//get a coordinate list of the f verts
 			BM_ITER_ELEM_INDEX (v2, &iter2, f_src, BM_VERTS_OF_FACE, v_src_count) {
-				v_co_list_src = MEM_reallocN(v_co_list_src, sizeof(*v_co_list_src) * (v_src_count + 1));
+				//reallocate if the verts/faces were more than expected
+				if (v_src_count > exp_vert_per_face) {
+					//and according to the previous records only allocate if that more than max already allocated
+					if (v_src_count > v_src_max_count) {
+						v_co_list_src = MEM_reallocN(v_co_list_src, sizeof(*v_co_list_src) * (v_src_count + 1));
+						v_src_max_count = v_src_count;
+					}
+				}
+
 				copy_v3_v3(v_co_list_src[v_src_count], v2->co);
 			}
 			zero_v3(f_mid_src);
 			mid_poly_v3(f_mid_src, v_co_list_src, v_src_count);	//get the mid point of the source face
 
-			BM_ITER_ELEM_INDEX (l, &liter, f_dst, BM_LOOPS_OF_FACE, b) {
+			// Prepare memory for later interpolation
+			// it's unlikely that we've src_count more than 10!! so we'll reallocate only if our threshold was passed
+			if (v_src_count > exp_vert_per_face) {
+				tmp_weight = MEM_reallocN(tmp_weight, sizeof(*tmp_weight) * v_src_count);
+			}
 
+			BM_ITER_ELEM (l, &liter, f_dst, BM_LOOPS_OF_FACE) {
+
 				zero_v3(tmp_co);
 				// Transform into target space.
 				mul_v3_m4v3(tmp_co, tmp_mat, l->v->co);
@@ -2304,14 +2329,13 @@
 				project_v3_plane(tmp_co, f_src->no, f_mid_src);
 
 				// Interpolate weights over face.
-				//check that v_count will equal to n not n-1!!
-				tmp_weight = MEM_mallocN(sizeof(*tmp_weight) * v_src_count, "tmp_weight bmesh_data_transfer.c");
 
-				//spatially finding the weights from the face's vertices
+				//spatially finding the weights from the face's vertices (no need to reset the weights/ it already gets
+				//rewritten in the interp_weights_poly_v3()
 				interp_weights_poly_v3(tmp_weight, v_co_list_src, v_src_count, tmp_co);
 
 				// Interpolating according to the spatially found weights
-				zero_v4(weight_accu);
+				zero_v4(weight_accu);	//this variable is added only for consistency with the other transfer functions
 				BM_ITER_ELEM_INDEX (l2, &liter2, f_src, BM_LOOPS_OF_FACE, a) {
 					madd_v4_v4fl(weight_accu, BM_ELEM_CD_GET_VOID_P(l2, CD_src), tmp_weight[a]);
 				}
@@ -2326,5 +2350,8 @@
 
 	}
 
+	MEM_freeN(v_co_list_dst);
+	MEM_freeN(v_co_list_src);
+	MEM_freeN(tmp_weight);
 	return true;
 }




More information about the Bf-blender-cvs mailing list