btcs

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

commit 51a91bdc10099bfcafe00b6e581ef73e6d6f0fe1
parent 82655221e65fd26595486186bbbb6ae459fb6009
Author: William Casarin <jb55@jb55.com>
Date:   Wed, 25 Oct 2017 11:43:00 -0700

stack: forgot this file

Diffstat:
Astack.c | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Astack.h | 39+++++++++++++++++++++++++++++++++++++++
2 files changed, 94 insertions(+), 0 deletions(-)

diff --git a/stack.c b/stack.c @@ -0,0 +1,55 @@ + +#include "stack.h" +#include <stdlib.h> +#include <string.h> + +void +stack_clear(struct stack *stack) { + memset(stack->bottom, 0, stack->capacity); +} + +int +stack_init_size(struct stack *stack, int capacity) { + if (capacity > DEFAULT_STACK_SIZE) { + u8 *bottom = (u8*)malloc(capacity); + if (!bottom) return 0; + stack->bottom = bottom; + stack->top = bottom; + stack->capacity = capacity; + } + else { + stack->bottom = stack->_data; + stack->top = stack->bottom; + stack->capacity = DEFAULT_STACK_SIZE; + } + stack_clear(stack); + return 1; +} + +int +stack_init(struct stack *stack) { + return stack_init_size(stack, DEFAULT_STACK_SIZE); +} + +void +stack_free(struct stack *stack) { + if (stack->capacity > DEFAULT_STACK_SIZE) + free(stack->bottom); + stack->bottom = stack->_data; +} + + +void +stack_expand(struct stack *stack) { + assert(!"Implement expand"); +} + +void +stack_push(struct stack *stack, u8 byte) { + if ((stack_size(stack) + 1) > stack->capacity) { + stack_expand(stack); + } + + *stack->top++ = byte; +} + diff --git a/stack.h b/stack.h @@ -0,0 +1,39 @@ + +#ifndef BTCS_STACK_H +#define BTCS_STACK_H + +#include <assert.h> +#include "misc.h" + +// Maximum number of values on script interpreter stack +static const int MAX_STACK_SIZE = 1000; + +// Maximum number of values on script interpreter stack +static const int DEFAULT_STACK_SIZE = 64; + +struct stack { + u8 _data[DEFAULT_STACK_SIZE]; + u8 * bottom; + u8 * top; + int capacity; +}; + +int stack_init(struct stack *); +int stack_init_size(struct stack *, int size); +void stack_free(struct stack *); +void stack_clear(struct stack *); +void stack_push(struct stack *, u8 val); + +static inline u8 +stack_top(struct stack *stack, int ind) { + u8 *p = stack->top + ind; + assert(p >= stack->bottom); + return *p; +} + +static inline int +stack_size(struct stack *stack) { + return stack->top - stack->bottom; +} + +#endif /* BTCS_STACK_H */