[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [59199] trunk/blender/source/blender: add linklist stack macros, use where over allocating an array was previously done.

Campbell Barton ideasman42 at gmail.com
Sat Aug 17 07:33:56 CEST 2013


Revision: 59199
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=59199
Author:   campbellbarton
Date:     2013-08-17 05:33:55 +0000 (Sat, 17 Aug 2013)
Log Message:
-----------
add linklist stack macros, use where over allocating an array was previously done.

Modified Paths:
--------------
    trunk/blender/source/blender/blenlib/CMakeLists.txt
    trunk/blender/source/blender/bmesh/operators/bmo_connect.c
    trunk/blender/source/blender/bmesh/operators/bmo_connect_nonplanar.c
    trunk/blender/source/blender/bmesh/operators/bmo_normals.c
    trunk/blender/source/blender/editors/mesh/editmesh_select.c
    trunk/blender/source/blender/editors/transform/transform_conversions.c

Added Paths:
-----------
    trunk/blender/source/blender/blenlib/BLI_linklist_stack.h

Added: trunk/blender/source/blender/blenlib/BLI_linklist_stack.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_linklist_stack.h	                        (rev 0)
+++ trunk/blender/source/blender/blenlib/BLI_linklist_stack.h	2013-08-17 05:33:55 UTC (rev 59199)
@@ -0,0 +1,90 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): none yet.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+ 
+#ifndef __BLI_LINKLIST_STACK_H__
+#define __BLI_LINKLIST_STACK_H__
+
+/** \file BLI_linklist_stack.h
+ * \ingroup bli
+ * \brief BLI_LINKSTACK_*** wrapper macros for using a \a LinkNode
+ *        to store a stack of pointers, using a single linked list
+ *        allocated from a mempool.
+ *
+ * \note These macros follow STACK_* macros defined in 'BLI_utildefines.h'
+ *       and should be kept (mostly) interchangeable.
+ *
+ * \note _##var##_type is a dummy var only used for typechecks.
+ */
+
+#define BLI_LINKSTACK_DECLARE(var, type) \
+	LinkNode *var; \
+	BLI_mempool *_##var##_pool; \
+	type _##var##_type
+
+#define BLI_LINKSTACK_INIT(var)  { \
+	var = NULL; \
+	_##var##_pool = BLI_mempool_create(sizeof(LinkNode), 1, 64, 0); \
+} (void)0
+
+#define BLI_LINKSTACK_SIZE(var) \
+	BLI_mempool_count(_##var##_pool)
+
+/* check for typeof() */
+#ifdef __GNUC__
+#define BLI_LINKSTACK_PUSH(var, ptr)  ( \
+	CHECK_TYPE_INLINE(ptr, typeof(_##var##_type)), \
+	BLI_linklist_prepend_pool(&(var), ptr, _##var##_pool))
+#define BLI_LINKSTACK_POP(var) \
+	(var ? (typeof(_##var##_type))BLI_linklist_pop_pool(&(var), _##var##_pool) : NULL)
+#define BLI_LINKSTACK_POP_ELSE(var, r) \
+	(var ? (typeof(_##var##_type))BLI_linklist_pop_pool(&(var), _##var##_pool) : r)
+#else  /* non gcc */
+#define BLI_LINKSTACK_PUSH(var, ptr)  ( \
+	BLI_linklist_prepend_pool(&(var), ptr, _##var##_pool))
+#define BLI_LINKSTACK_POP(var) \
+	(var ? BLI_linklist_pop_pool(&(var), _##var##_pool) : NULL)
+#define BLI_LINKSTACK_POP_ELSE(var, r) \
+	(var ? BLI_linklist_pop_pool(&(var), _##var##_pool) : r)
+#endif  /* gcc check */
+
+#define BLI_LINKSTACK_SWAP(var_a, var_b)  { \
+	CHECK_TYPE_PAIR(_##var_a##_type, _##var_b##_type); \
+	SWAP(LinkNode *, var_a, var_b); \
+	SWAP(BLI_mempool *, _##var_a##_pool, _##var_b##_pool); \
+} (void)0
+
+#define BLI_LINKSTACK_FREE(var)  { \
+	BLI_mempool_destroy(_##var##_pool); \
+	_##var##_pool = NULL; (void)_##var##_pool; \
+	var = NULL; (void)var; \
+	(void)_##var##_type; \
+} (void)0
+
+#include "BLI_linklist.h"
+#include "BLI_mempool.h"
+
+#endif  /* __BLI_LINKLIST_STACK_H__ */


Property changes on: trunk/blender/source/blender/blenlib/BLI_linklist_stack.h
___________________________________________________________________
Added: svn:eol-style
   + native

Modified: trunk/blender/source/blender/blenlib/CMakeLists.txt
===================================================================
--- trunk/blender/source/blender/blenlib/CMakeLists.txt	2013-08-17 05:27:58 UTC (rev 59198)
+++ trunk/blender/source/blender/blenlib/CMakeLists.txt	2013-08-17 05:33:55 UTC (rev 59199)
@@ -25,7 +25,7 @@
 
 # XXX allowing blenkernel and RNA includes in blenlib is a hack,
 # but needed in a few places atm (bpath.c for instance)
-set(INC 
+set(INC
 	.
 	# ../blenkernel  # dont add this back!
 	../makesdna
@@ -125,6 +125,7 @@
 	BLI_kdtree.h
 	BLI_lasso.h
 	BLI_linklist.h
+	BLI_linklist_stack.h
 	BLI_listbase.h
 	BLI_math.h
 	BLI_math_base.h

Modified: trunk/blender/source/blender/bmesh/operators/bmo_connect.c
===================================================================
--- trunk/blender/source/blender/bmesh/operators/bmo_connect.c	2013-08-17 05:27:58 UTC (rev 59198)
+++ trunk/blender/source/blender/bmesh/operators/bmo_connect.c	2013-08-17 05:33:55 UTC (rev 59199)
@@ -31,6 +31,7 @@
 #include "BLI_math.h"
 #include "BLI_utildefines.h"
 #include "BLI_alloca.h"
+#include "BLI_linklist_stack.h"
 
 #include "bmesh.h"
 
@@ -117,10 +118,9 @@
 	BMIter iter;
 	BMVert *v;
 	BMFace *f;
-	BMFace **faces = MEM_mallocN(sizeof(BMFace *) * bm->totface, __func__);
-	STACK_DECLARE(faces);
+	BLI_LINKSTACK_DECLARE(faces, BMFace *);
 
-	STACK_INIT(faces);
+	BLI_LINKSTACK_INIT(faces);
 
 	/* add all faces connected to verts */
 	BMO_ITER (v, &siter, op->slots_in, "verts", BM_VERT) {
@@ -129,20 +129,20 @@
 			if (!BMO_elem_flag_test(bm, f, FACE_TAG)) {
 				BMO_elem_flag_enable(bm, f, FACE_TAG);
 				if (f->len > 3) {
-					STACK_PUSH(faces, f);
+					BLI_LINKSTACK_PUSH(faces, f);
 				}
 			}
 		}
 	}
 
 	/* connect faces */
-	while ((f = STACK_POP(faces))) {
+	while ((f = BLI_LINKSTACK_POP(faces))) {
 		if (bm_face_connect_verts(bm, f) == -1) {
 			BMO_error_raise(bm, op, BMERR_CONNECTVERT_FAILED, NULL);
 		}
 	}
 
-	MEM_freeN(faces);
+	BLI_LINKSTACK_FREE(faces);
 
 	BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "edges.out", BM_EDGE, EDGE_OUT);
 }

Modified: trunk/blender/source/blender/bmesh/operators/bmo_connect_nonplanar.c
===================================================================
--- trunk/blender/source/blender/bmesh/operators/bmo_connect_nonplanar.c	2013-08-17 05:27:58 UTC (rev 59198)
+++ trunk/blender/source/blender/bmesh/operators/bmo_connect_nonplanar.c	2013-08-17 05:33:55 UTC (rev 59199)
@@ -31,6 +31,7 @@
 #include "BLI_math.h"
 #include "BLI_utildefines.h"
 #include "BLI_alloca.h"
+#include "BLI_linklist_stack.h"
 
 #include "bmesh.h"
 
@@ -179,9 +180,7 @@
 	BMOIter siter;
 	BMFace *f;
 	int totface = 0, totloop = 0;
-	int tottris;
-	BMFace **fstack;
-	STACK_DECLARE(fstack);
+	BLI_LINKSTACK_DECLARE(fstack, BMFace *);
 
 	const float angle_limit = BMO_slot_float_get(op->slots_in, "angle_limit");
 
@@ -197,32 +196,28 @@
 		return;
 	}
 
-	/* over alloc, if we split every face */
-	tottris = poly_to_tri_count(totface, totloop);
-	fstack = MEM_mallocN(sizeof(BMFace *) * tottris, __func__);
+	BLI_LINKSTACK_INIT(fstack);
 
-	STACK_INIT(fstack);
-
 	BMO_ITER (f, &siter, op->slots_in, "faces", BM_FACE) {
 		if (f->len > 3) {
-			STACK_PUSH(fstack, f);
+			BLI_LINKSTACK_PUSH(fstack, f);
 		}
 	}
 
-	while ((f = STACK_POP(fstack))) {
+	while ((f = BLI_LINKSTACK_POP(fstack))) {
 		BMFace *f_pair[2];
 		if (bm_face_split_by_angle(bm, f, f_pair, angle_limit)) {
 			int j;
 			for (j = 0; j < 2; j++) {
 				BM_face_normal_update(f_pair[j]);
 				if (f_pair[j]->len > 3) {
-					STACK_PUSH(fstack, f_pair[j]);
+					BLI_LINKSTACK_PUSH(fstack, f_pair[j]);
 				}
 			}
 		}
 	}
 
-	MEM_freeN(fstack);
+	BLI_LINKSTACK_FREE(fstack);
 
 	BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "edges.out", BM_EDGE, EDGE_OUT);
 	BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "faces.out", BM_FACE, FACE_OUT);

Modified: trunk/blender/source/blender/bmesh/operators/bmo_normals.c
===================================================================
--- trunk/blender/source/blender/bmesh/operators/bmo_normals.c	2013-08-17 05:27:58 UTC (rev 59198)
+++ trunk/blender/source/blender/bmesh/operators/bmo_normals.c	2013-08-17 05:33:55 UTC (rev 59199)
@@ -29,6 +29,7 @@
 #include "MEM_guardedalloc.h"
 
 #include "BLI_math.h"
+#include "BLI_linklist_stack.h"
 
 #include "bmesh.h"
 
@@ -65,8 +66,7 @@
 	float f_len_best;
 	BMFace *f;
 
-	BMFace **fstack = MEM_mallocN(sizeof(*fstack) * faces_len, __func__);
-	STACK_DECLARE(fstack);
+	BLI_LINKSTACK_DECLARE(fstack, BMFace *);
 
 	zero_v3(cent);
 
@@ -103,12 +103,12 @@
 	 * have the same winding.  this is done recursively, using a manual
 	 * stack (if we use simple function recursion, we'd end up overloading
 	 * the stack on large meshes). */
-	STACK_INIT(fstack);
+	BLI_LINKSTACK_INIT(fstack);
 
-	STACK_PUSH(fstack, faces[f_start_index]);
+	BLI_LINKSTACK_PUSH(fstack, faces[f_start_index]);
 	BMO_elem_flag_enable(bm, faces[f_start_index], FACE_TEMP);
 
-	while ((f = STACK_POP(fstack))) {
+	while ((f = BLI_LINKSTACK_POP(fstack))) {
 		const bool flip_state = BMO_elem_flag_test_bool(bm, f, FACE_FLIP);
 		BMLoop *l_iter, *l_first;
 
@@ -120,13 +120,13 @@
 				if (!BMO_elem_flag_test(bm, l_other->f, FACE_TEMP)) {
 					BMO_elem_flag_enable(bm, l_other->f, FACE_TEMP);
 					BMO_elem_flag_set(bm, l_other->f, FACE_FLIP, (l_other->v == l_iter->v) != flip_state);
-					STACK_PUSH(fstack, l_other->f);
+					BLI_LINKSTACK_PUSH(fstack, l_other->f);
 				}
 			}
 		} while ((l_iter = l_iter->next) != l_first);
 	}
 
-	MEM_freeN(fstack);
+	BLI_LINKSTACK_FREE(fstack);
 
 	/* apply flipping to oflag'd faces */
 	for (i = 0; i < faces_len; i++) {

Modified: trunk/blender/source/blender/editors/mesh/editmesh_select.c
===================================================================
--- trunk/blender/source/blender/editors/mesh/editmesh_select.c	2013-08-17 05:27:58 UTC (rev 59198)
+++ trunk/blender/source/blender/editors/mesh/editmesh_select.c	2013-08-17 05:33:55 UTC (rev 59199)
@@ -33,6 +33,7 @@
 
 #include "BLI_listbase.h"
 #include "BLI_linklist.h"
+#include "BLI_linklist_stack.h"
 #include "BLI_math.h"
 #include "BLI_rand.h"
 #include "BLI_array.h"
@@ -2609,8 +2610,7 @@
 	BMEditMesh *em = BKE_editmesh_from_object(obedit);
 	BMesh *bm = em->bm;
 
-	BMFace **stack = MEM_mallocN(sizeof(BMFace *) * bm->totface, __func__);
-	STACK_DECLARE(stack);
+	BLI_LINKSTACK_DECLARE(stack, BMFace *);
 
 	BMIter iter, liter, liter2;
 	BMFace *f;
@@ -2628,7 +2628,7 @@
 			continue;
 		}
 
-		STACK_INIT(stack);
+		BLI_LINKSTACK_INIT(stack);
 
 		do {
 			BM_face_select_set(bm, f, true);
@@ -2648,17 +2648,15 @@
 					angle = angle_normalized_v3v3(f->no, l2->f->no);
 
 					if (angle < angle_limit) {
-						STACK_PUSH(stack, l2->f);
+						BLI_LINKSTACK_PUSH(stack, l2->f);
 					}
 				}
 			}
-		} while ((f = STACK_POP(stack)));
+		} while ((f = BLI_LINKSTACK_POP(stack)));
 	}
 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list