diff options
-rw-r--r-- | source/blender/blenlib/BLI_stack.h | 3 | ||||
-rw-r--r-- | source/blender/blenlib/intern/stack.c | 42 | ||||
-rw-r--r-- | tests/gtests/blenlib/BLI_stack_test.cc | 23 |
3 files changed, 51 insertions, 17 deletions
diff --git a/source/blender/blenlib/BLI_stack.h b/source/blender/blenlib/BLI_stack.h index 1e0b29bc7e8..a8c4478c450 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 58029120de9..2d3a2f77a3e 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 c4884cb8940..08701356816 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}; |