Add stack
This commit is contained in:
+53
@@ -81,6 +81,59 @@ char* libcdsb_strndup(const char* s, size_t n) {
|
||||
/*#####################################################################################################################*/
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/*#####################################################################################################################*/
|
||||
|
||||
|
||||
size_t libcdsb_strlen(const char* s) {
|
||||
static const size_t m = (sizeof(size_t) == 8) ? 0x8080808080808080UL : 0x80808080UL;
|
||||
static const size_t d = (sizeof(size_t) == 8) ? 0x0101010101010101UL : 0x01010101UL;
|
||||
|
||||
Reference in New Issue
Block a user