#include "lslib.h" #include #include #include #include void stack_init(struct Stack* stack, size_t size) { stack->size = 0; stack->capacity = size; stack->data = xalloc(size); } void stack_push(struct Stack* stack, void* data, size_t len) { size_t new_size = stack->size + len; if (new_size >= stack->capacity) { stack->capacity = new_size * 2; stack->data = xrealloc(stack->data, stack->capacity); } memcpy((uint8_t*)stack->data + stack->size, data, len); stack->size += len; } void* stack_pop(struct Stack* stack, size_t len) { if (stack->size < len) return NULL; stack->size -= len; return (uint8_t*)stack->data + stack->size; } void stack_free(struct Stack *stack) { free(stack->data); } void stack_push_int(struct Stack *stack, int value) { stack_push(stack, &value, sizeof(int)); } bool stack_pop_int(struct Stack *stack, int* value) { void* d = stack_pop(stack, sizeof(int)); if (d == NULL) return false; *value = *(int*)(d); return true; }