[Bf-blender-cvs] [74a2fa3] master: BLI_Buffer: add BLI_buffer_reinit

Campbell Barton noreply at git.blender.org
Sun Oct 4 05:41:01 CEST 2015


Commit: 74a2fa309555da9b76abdc205cec43909872fc20
Author: Campbell Barton
Date:   Sun Oct 4 14:14:28 2015 +1100
Branches: master
https://developer.blender.org/rB74a2fa309555da9b76abdc205cec43909872fc20

BLI_Buffer: add BLI_buffer_reinit

Useful for re-using a buffer when the existing data can be thrown away.

===================================================================

M	source/blender/blenkernel/intern/mesh_mapping.c
M	source/blender/blenkernel/intern/pbvh_bmesh.c
M	source/blender/blenlib/BLI_buffer.h
M	source/blender/blenlib/intern/buffer.c
M	source/blender/bmesh/operators/bmo_extrude.c
M	source/blender/editors/mesh/editmesh_utils.c
M	source/blender/editors/uvedit/uvedit_draw.c

===================================================================

diff --git a/source/blender/blenkernel/intern/mesh_mapping.c b/source/blender/blenkernel/intern/mesh_mapping.c
index cef570a..8dce3c3 100644
--- a/source/blender/blenkernel/intern/mesh_mapping.c
+++ b/source/blender/blenkernel/intern/mesh_mapping.c
@@ -97,7 +97,7 @@ UvVertMap *BKE_mesh_uv_vert_map_create(
 			float (*tf_uv)[2] = NULL;
 
 			if (use_winding) {
-				tf_uv = (float (*)[2])BLI_buffer_resize_data(&tf_uv_buf, vec2f, (size_t)mp->totloop);
+				tf_uv = (float (*)[2])BLI_buffer_reinit_data(&tf_uv_buf, vec2f, (size_t)mp->totloop);
 			}
 
 			nverts = mp->totloop;
diff --git a/source/blender/blenkernel/intern/pbvh_bmesh.c b/source/blender/blenkernel/intern/pbvh_bmesh.c
index 3e23607..ff4cae0 100644
--- a/source/blender/blenkernel/intern/pbvh_bmesh.c
+++ b/source/blender/blenkernel/intern/pbvh_bmesh.c
@@ -575,7 +575,7 @@ static void pbvh_bmesh_edge_loops(BLI_Buffer *buf, BMEdge *e)
 		buf->count = 2;
 	}
 	else {
-		BLI_buffer_resize(buf, BM_edge_face_count(e));
+		BLI_buffer_reinit(buf, BM_edge_face_count(e));
 		BM_iter_as_array(NULL, BM_LOOPS_OF_EDGE, e, buf->data, buf->count);
 	}
 }
diff --git a/source/blender/blenlib/BLI_buffer.h b/source/blender/blenlib/BLI_buffer.h
index 941c1c9..a9ecd3a 100644
--- a/source/blender/blenlib/BLI_buffer.h
+++ b/source/blender/blenlib/BLI_buffer.h
@@ -64,7 +64,7 @@ enum {
 #define BLI_buffer_at(buffer_, type_, index_) ( \
 	(((type_ *)(buffer_)->data)[ \
 	        (BLI_assert(sizeof(type_) == (buffer_)->elem_size)), \
-	        (BLI_assert(index_ >= 0 && index_ < (buffer_)->count)), \
+	        (BLI_assert((int)index_ >= 0 && (size_t)index_ < (buffer_)->count)), \
 	        index_]))
 
 #define BLI_buffer_array(buffer_, type_) ( \
@@ -73,6 +73,9 @@ enum {
 #define BLI_buffer_resize_data(buffer_, type_, new_count_) ( \
 	(BLI_buffer_resize(buffer_, new_count_), new_count_ ? BLI_buffer_array(buffer_, type_) : NULL))
 
+#define BLI_buffer_reinit_data(buffer_, type_, new_count_) ( \
+	(BLI_buffer_reinit(buffer_, new_count_), new_count_ ? BLI_buffer_array(buffer_, type_) : NULL))
+
 #define BLI_buffer_append(buffer_, type_, val_)  ( \
 	BLI_buffer_resize(buffer_, (buffer_)->count + 1), \
 	(BLI_buffer_at(buffer_, type_, (buffer_)->count - 1) = val_) \
@@ -85,6 +88,9 @@ enum {
 /* Never decreases the amount of memory allocated */
 void BLI_buffer_resize(BLI_Buffer *buffer, const size_t new_count);
 
+/* Ensure size, throwing away old data, respecting BLI_BUFFER_USE_CALLOC */
+void BLI_buffer_reinit(BLI_Buffer *buffer, const size_t new_count);
+
 /* Does not free the buffer structure itself */
 void _bli_buffer_free(BLI_Buffer *buffer);
 #define BLI_buffer_free(name_) { \
diff --git a/source/blender/blenlib/intern/buffer.c b/source/blender/blenlib/intern/buffer.c
index 7588a6e..6ab5722 100644
--- a/source/blender/blenlib/intern/buffer.c
+++ b/source/blender/blenlib/intern/buffer.c
@@ -102,6 +102,37 @@ void BLI_buffer_resize(BLI_Buffer *buffer, const size_t new_count)
 	buffer->count = new_count;
 }
 
+/**
+ * Similar to #BLI_buffer_resize, but use when the existing data can be:
+ * - Ignored (malloc'd)
+ * - Cleared (when BLI_BUFFER_USE_CALLOC is set)
+ */
+void BLI_buffer_reinit(BLI_Buffer *buffer, size_t new_count)
+{
+	if (UNLIKELY(new_count > buffer->alloc_count)) {
+		if ((buffer->flag & BLI_BUFFER_USE_STATIC) == 0) {
+			MEM_freeN(buffer->data);
+		}
+
+		if (buffer->alloc_count && (new_count < buffer->alloc_count * 2)) {
+			buffer->alloc_count *= 2;
+		}
+		else {
+			buffer->alloc_count = new_count;
+		}
+
+		buffer->data = buffer_alloc(buffer, new_count);
+	}
+	else {
+		if (buffer->flag & BLI_BUFFER_USE_CALLOC) {
+			memset(buffer->data, 0,
+			       buffer->elem_size * new_count);
+		}
+	}
+
+	buffer->count = new_count;
+}
+
 /* callers use BLI_buffer_free */
 void _bli_buffer_free(BLI_Buffer *buffer)
 {
diff --git a/source/blender/bmesh/operators/bmo_extrude.c b/source/blender/bmesh/operators/bmo_extrude.c
index 4449f22..3eae98b 100644
--- a/source/blender/bmesh/operators/bmo_extrude.c
+++ b/source/blender/bmesh/operators/bmo_extrude.c
@@ -728,9 +728,9 @@ static void solidify_add_thickness(BMesh *bm, const float dist)
 		if (BMO_elem_flag_test(bm, f, FACE_MARK)) {
 
 			/* array for passing verts to angle_poly_v3 */
-			float  *face_angles = BLI_buffer_resize_data(&face_angles_buf, float, f->len);
+			float  *face_angles = BLI_buffer_reinit_data(&face_angles_buf, float, f->len);
 			/* array for receiving angles from angle_poly_v3 */
-			float **verts = BLI_buffer_resize_data(&verts_buf, float *, f->len);
+			float **verts = BLI_buffer_reinit_data(&verts_buf, float *, f->len);
 
 			BM_ITER_ELEM_INDEX (l, &loopIter, f, BM_LOOPS_OF_FACE, i) {
 				verts[i] = l->v->co;
diff --git a/source/blender/editors/mesh/editmesh_utils.c b/source/blender/editors/mesh/editmesh_utils.c
index d521b2c..c101b41 100644
--- a/source/blender/editors/mesh/editmesh_utils.c
+++ b/source/blender/editors/mesh/editmesh_utils.c
@@ -671,7 +671,7 @@ UvVertMap *BM_uv_vert_map_create(
 			float (*tf_uv)[2];
 
 			if (use_winding) {
-				tf_uv = (float (*)[2])BLI_buffer_resize_data(&tf_uv_buf, vec2f, efa->len);
+				tf_uv = (float (*)[2])BLI_buffer_reinit_data(&tf_uv_buf, vec2f, efa->len);
 			}
 
 			BM_ITER_ELEM_INDEX(l, &liter, efa, BM_LOOPS_OF_FACE, i) {
@@ -823,7 +823,7 @@ UvElementMap *BM_uv_element_map_create(
 			float (*tf_uv)[2];
 
 			if (use_winding) {
-				tf_uv = (float (*)[2])BLI_buffer_resize_data(&tf_uv_buf, vec2f, efa->len);
+				tf_uv = (float (*)[2])BLI_buffer_reinit_data(&tf_uv_buf, vec2f, efa->len);
 			}
 
 			BM_ITER_ELEM_INDEX (l, &liter, efa, BM_LOOPS_OF_FACE, i) {
diff --git a/source/blender/editors/uvedit/uvedit_draw.c b/source/blender/editors/uvedit/uvedit_draw.c
index 4e9ab68..f4eed3e 100644
--- a/source/blender/editors/uvedit/uvedit_draw.c
+++ b/source/blender/editors/uvedit/uvedit_draw.c
@@ -187,8 +187,8 @@ static void draw_uvs_stretch(SpaceImage *sima, Scene *scene, BMEditMesh *em, MTe
 			
 			BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
 				const int efa_len = efa->len;
-				float (*tf_uv)[2]     = (float (*)[2])BLI_buffer_resize_data(&tf_uv_buf,     vec2f, efa_len);
-				float (*tf_uvorig)[2] = (float (*)[2])BLI_buffer_resize_data(&tf_uvorig_buf, vec2f, efa_len);
+				float (*tf_uv)[2]     = (float (*)[2])BLI_buffer_reinit_data(&tf_uv_buf,     vec2f, efa_len);
+				float (*tf_uvorig)[2] = (float (*)[2])BLI_buffer_reinit_data(&tf_uvorig_buf, vec2f, efa_len);
 				tf = BM_ELEM_CD_GET_VOID_P(efa, cd_poly_tex_offset);
 
 				BM_ITER_ELEM_INDEX (l, &liter, efa, BM_LOOPS_OF_FACE, i) {
@@ -230,8 +230,8 @@ static void draw_uvs_stretch(SpaceImage *sima, Scene *scene, BMEditMesh *em, MTe
 				BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
 					if (BM_elem_flag_test(efa, BM_ELEM_TAG)) {
 						const int efa_len = efa->len;
-						float (*tf_uv)[2]     = (float (*)[2])BLI_buffer_resize_data(&tf_uv_buf,     vec2f, efa_len);
-						float (*tf_uvorig)[2] = (float (*)[2])BLI_buffer_resize_data(&tf_uvorig_buf, vec2f, efa_len);
+						float (*tf_uv)[2]     = (float (*)[2])BLI_buffer_reinit_data(&tf_uv_buf,     vec2f, efa_len);
+						float (*tf_uvorig)[2] = (float (*)[2])BLI_buffer_reinit_data(&tf_uvorig_buf, vec2f, efa_len);
 
 						area = BM_face_calc_area(efa) / totarea;
 
@@ -284,12 +284,12 @@ static void draw_uvs_stretch(SpaceImage *sima, Scene *scene, BMEditMesh *em, MTe
 				
 				if (uvedit_face_visible_test(scene, ima, efa, tf)) {
 					const int efa_len = efa->len;
-					float (*tf_uv)[2]     = (float (*)[2])BLI_buffer_resize_data(&tf_uv_buf,     vec2f, efa_len);
-					float (*tf_uvorig)[2] = (float (*)[2])BLI_buffer_resize_data(&tf_uvorig_buf, vec2f, efa_len);
-					float *uvang = BLI_buffer_resize_data(&uvang_buf, float, efa_len);
-					float *ang   = BLI_buffer_resize_data(&ang_buf,   float, efa_len);
-					float (*av)[3]  = (float (*)[3])BLI_buffer_resize_data(&av_buf, vec3f, efa_len);
-					float (*auv)[2] = (float (*)[2])BLI_buffer_resize_data(&auv_buf, vec2f, efa_len);
+					float (*tf_uv)[2]     = (float (*)[2])BLI_buffer_reinit_data(&tf_uv_buf,     vec2f, efa_len);
+					float (*tf_uvorig)[2] = (float (*)[2])BLI_buffer_reinit_data(&tf_uvorig_buf, vec2f, efa_len);
+					float *uvang = BLI_buffer_reinit_data(&uvang_buf, float, efa_len);
+					float *ang   = BLI_buffer_reinit_data(&ang_buf,   float, efa_len);
+					float (*av)[3]  = (float (*)[3])BLI_buffer_reinit_data(&av_buf, vec3f, efa_len);
+					float (*auv)[2] = (float (*)[2])BLI_buffer_reinit_data(&auv_buf, vec2f, efa_len);
 					int j;
 
 					BM_elem_flag_enable(efa, BM_ELEM_TAG);




More information about the Bf-blender-cvs mailing list