57 lines
977 B
C
57 lines
977 B
C
/* This software is licensed by the MIT License, see LICENSE file */
|
|
/* Copyright © 2022 Gregory Lirent */
|
|
|
|
#include <stdlib.h>
|
|
#include "__internal/include.h"
|
|
#undef malloc
|
|
|
|
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_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;
|
|
}
|