btcs

bitcoin script parser/evaluator/compiler/decompiler
git clone git://jb55.com/btcs
Log | Files | Refs | README | LICENSE

commit 6c4dbbd7c10e1e8b8d56a3233330012fd7d3fb46
parent b05dea298816c666b3e6ad58824c8140fa115f6b
Author: William Casarin <jb55@jb55.com>
Date:   Wed, 25 Oct 2017 20:14:59 -0700

stack: implement expand

Diffstat:
Mstack.c | 27+++++++++++++++++++++++++--
1 file changed, 25 insertions(+), 2 deletions(-)

diff --git a/stack.c b/stack.c @@ -40,9 +40,32 @@ stack_free(struct stack *stack) { } -void +int stack_expand(struct stack *stack) { - assert(!"Implement expand"); + size_t newcap = stack->capacity * 2; + void **bottom = 0; + int size = stack_size(stack); + +#if DEBUG + printf("expanding s(%d) %d -> %d\n", size, stack->capacity, newcap); +#endif + + // XXX: might want to relax this to <= eventually + assert(size == stack->capacity); + + if (stack->capacity <= DEFAULT_STACK_SIZE) { + bottom = malloc(newcap * sizeof(void*)); + if (!bottom) return 0; + memcpy(bottom, stack->bottom, size * sizeof(void*)); + } + else { + bottom = realloc(stack->bottom, newcap * sizeof(void*)); + if (!bottom) return 0; + } + stack->capacity = newcap; + stack->bottom = bottom; + stack->top = bottom+size; + return 1; } void