/* This software is licensed by the MIT License, see LICENSE file */ /* Copyright © 2022 Gregory Lirent */ #include #include #include "__internal/include.h" #undef malloc #undef free void libcdsb_stack_init(stack_t* x) { memset(x, 0, sizeof(*x)); } void libcdsb_stack_push(stack_t* x, void* value) { stack_t* n; if (x->value) { if (!(n = malloc(sizeof(*n)))) abort(); n->prev = x->prev; n->value = x->value; x->prev = n; } x->value = value; } void libcdsb_stack_push_many(stack_t* x, size_t c, ...) { va_list args; stack_t* n; va_start(args, c); if (c) { if (!x->value) { x->value = va_arg(args, void*); --c; } while (c--) { if (!(n = malloc(sizeof(*n)))) abort(); n->prev = x->prev; n->value = x->value; x->prev = n; } } va_end(args); } void* libcdsb_stack_pop(stack_t* x) { stack_t* n; void* v; v = x->value; if (x->prev) { n = x->prev; x->prev = n->prev; x->value = n->value; free(n); } else x->value = 0; return v; } void libcdsb_stack_flush(stack_t* stack) { stack_t* c; while (stack->prev) { c = stack->prev; stack->prev = c->prev; free(c); } stack->value = 0; }