diff options
author | Campbell Barton <ideasman42@gmail.com> | 2015-06-03 11:06:47 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2015-06-03 11:07:30 +0300 |
commit | 1dbcccf5f965187532a7fc543ca85bdbf79e99f5 (patch) | |
tree | a940db7440d67fd2df050062dba586e4e06bee74 | |
parent | ebebc4ded11e8ecb38b57aa47f5ba80d26b9567c (diff) |
BLI_stack, add clear function.
-rw-r--r-- | source/blender/blenlib/BLI_stack.h | 1 | ||||
-rw-r--r-- | source/blender/blenlib/intern/stack.c | 35 |
2 files changed, 36 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_stack.h b/source/blender/blenlib/BLI_stack.h index a8c4478c450..6d41e5feb88 100644 --- a/source/blender/blenlib/BLI_stack.h +++ b/source/blender/blenlib/BLI_stack.h @@ -48,6 +48,7 @@ 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(); +void BLI_stack_clear(BLI_Stack *stack) ATTR_NONNULL(); size_t BLI_stack_count(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 2d3a2f77a3e..3c9e1a52cda 100644 --- a/source/blender/blenlib/intern/stack.c +++ b/source/blender/blenlib/intern/stack.c @@ -216,6 +216,41 @@ void BLI_stack_discard(BLI_Stack *stack) } } +/** + * Discards all elements without freeing. + */ +void BLI_stack_clear(BLI_Stack *stack) +{ +#ifdef USE_TOTELEM + if (UNLIKELY(stack->totelem == 0)) { + return; + } + stack->totelem = 0; +#else + if (UNLIKELY(stack->chunk_curr == NULL)) { + return; + } +#endif + + stack->chunk_index = stack->chunk_elem_max - 1; + + if (stack->chunk_free) { + if (stack->chunk_curr) { + /* move all used chunks into tail of free list */ + struct StackChunk *chunk_free_last = stack->chunk_free; + while (chunk_free_last->next) { + chunk_free_last = chunk_free_last->next; + } + chunk_free_last->next = stack->chunk_curr; + stack->chunk_curr = NULL; + } + } + else { + stack->chunk_free = stack->chunk_curr; + stack->chunk_curr = NULL; + } +} + size_t BLI_stack_count(const BLI_Stack *stack) { #ifdef USE_TOTELEM |