[Bf-blender-cvs] [a4c3b92] master: BLI_Stack add BLI_stack_peek, BLI_stack_discard

Campbell Barton noreply at git.blender.org
Sun Sep 28 07:09:16 CEST 2014


Commit: a4c3b9229448e93f10a7706800822b1e0119c033
Author: Campbell Barton
Date:   Sun Sep 28 13:24:01 2014 +1000
Branches: master
https://developer.blender.org/rBa4c3b9229448e93f10a7706800822b1e0119c033

BLI_Stack add BLI_stack_peek, BLI_stack_discard

also remove own incorrect assert

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

M	source/blender/blenlib/BLI_stack.h
M	source/blender/blenlib/intern/stack.c
M	tests/gtests/blenlib/BLI_stack_test.cc

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

diff --git a/source/blender/blenlib/BLI_stack.h b/source/blender/blenlib/BLI_stack.h
index 1e0b29b..a8c4478 100644
--- a/source/blender/blenlib/BLI_stack.h
+++ b/source/blender/blenlib/BLI_stack.h
@@ -46,6 +46,9 @@ void  BLI_stack_push(BLI_Stack *stack, const void *src) ATTR_NONNULL();
 void BLI_stack_pop_n(BLI_Stack *stack, void *dst, unsigned int n) ATTR_NONNULL();
 void BLI_stack_pop(BLI_Stack *stack, void *dst) ATTR_NONNULL();
 
+void *BLI_stack_peek(BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
+void  BLI_stack_discard(BLI_Stack *stack) ATTR_NONNULL();
+
 size_t BLI_stack_count(const BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
 
 bool BLI_stack_is_empty(const BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
diff --git a/source/blender/blenlib/intern/stack.c b/source/blender/blenlib/intern/stack.c
index 5802912..2d3a2f7 100644
--- a/source/blender/blenlib/intern/stack.c
+++ b/source/blender/blenlib/intern/stack.c
@@ -48,8 +48,6 @@
 	((void)0, (((char *)(_stack)->chunk_curr->data) + \
 	           ((_stack)->elem_size * (_stack)->chunk_index)))
 
-#define IS_POW2(a) (((a) & ((a) - 1)) == 0)
-
 struct StackChunk {
 	struct StackChunk *next;
 	char data[0];
@@ -96,11 +94,6 @@ BLI_Stack *BLI_stack_new_ex(const size_t elem_size, const char *description,
 	/* force init */
 	stack->chunk_index = stack->chunk_elem_max - 1;
 
-	/* ensure we have a correctly rounded size */
-	BLI_assert((IS_POW2(stack->elem_size) == 0) ||
-	           (IS_POW2((stack->chunk_elem_max * stack->elem_size) +
-	                    (sizeof(struct StackChunk) + MEM_SIZE_OVERHEAD))));
-
 	return stack;
 }
 
@@ -182,6 +175,31 @@ void BLI_stack_pop(BLI_Stack *stack, void *dst)
 	BLI_assert(BLI_stack_is_empty(stack) == false);
 
 	memcpy(dst, CHUNK_LAST_ELEM(stack), stack->elem_size);
+
+	BLI_stack_discard(stack);
+}
+
+void BLI_stack_pop_n(BLI_Stack *stack, void *dst, unsigned int n)
+{
+	BLI_assert(n <= BLI_stack_count(stack));
+
+	while (n--) {
+		BLI_stack_pop(stack, dst);
+		dst = (void *)((char *)dst + stack->elem_size);
+	}
+}
+
+void *BLI_stack_peek(BLI_Stack *stack)
+{
+	BLI_assert(BLI_stack_is_empty(stack) == false);
+
+	return CHUNK_LAST_ELEM(stack);
+}
+
+void BLI_stack_discard(BLI_Stack *stack)
+{
+	BLI_assert(BLI_stack_is_empty(stack) == false);
+
 #ifdef USE_TOTELEM
 	stack->totelem--;
 #endif
@@ -198,16 +216,6 @@ void BLI_stack_pop(BLI_Stack *stack, void *dst)
 	}
 }
 
-void BLI_stack_pop_n(BLI_Stack *stack, void *dst, unsigned int n)
-{
-	BLI_assert(n <= BLI_stack_count(stack));
-
-	while (n--) {
-		BLI_stack_pop(stack, dst);
-		dst = (void *)((char *)dst + stack->elem_size);
-	}
-}
-
 size_t BLI_stack_count(const BLI_Stack *stack)
 {
 #ifdef USE_TOTELEM
diff --git a/tests/gtests/blenlib/BLI_stack_test.cc b/tests/gtests/blenlib/BLI_stack_test.cc
index c4884cb..0870135 100644
--- a/tests/gtests/blenlib/BLI_stack_test.cc
+++ b/tests/gtests/blenlib/BLI_stack_test.cc
@@ -88,6 +88,29 @@ TEST(stack, String)
 	BLI_stack_free(stack);
 }
 
+TEST(stack, Peek)
+{
+	const int tot = SIZE;
+	int i;
+
+	BLI_Stack *stack;
+	const short in[] = {1, 10, 100, 1000};
+
+	stack = BLI_stack_new(sizeof(*in), __func__);
+
+	for (i = 0; i < tot; i++) {
+		BLI_stack_push(stack, &in[i % ARRAY_SIZE(in)]);
+	}
+
+	for (i = tot - 1; i >= 0; i--, BLI_stack_discard(stack)) {
+		short *ret = (short *)BLI_stack_peek(stack);
+		EXPECT_EQ(*ret, in[i % ARRAY_SIZE(in)]);
+	}
+
+	EXPECT_EQ(BLI_stack_is_empty(stack), true);
+}
+
+
 TEST(stack, Reuse)
 {
 	const int sizes[] = {3, 11, 81, 400, 999, 12, 1, 9721, 7, 99, 5, 0};




More information about the Bf-blender-cvs mailing list