aboutsummaryrefslogtreecommitdiff
path: root/src/stack.c
blob: c6360e7d1388430b70d47091a0b212403cc23e33 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "include/stack.h"

// Not actually used anywhere, might be name collision on clang.

stack_t* stack_init() {
   stack_t* stack = emalloc(sizeof(stack_t));
   
   stack->sp = 0;
   for (int i = 0; i < STACK_MAXLEN; stack->val[i++] = 0);
   
   return stack;
}

void stack_destroy(stack_t* stack) {
   free(stack);
}

void stack_push(stack_t* stack, void* val) {
   stack->val[stack->sp++] = val;
}

void* stack_pop(stack_t* stack) {
   void* tmpval;

   tmpval = stack->val[--stack->sp];
   stack->val[stack->sp] = NULL;

   return tmpval;
}

size_t stack_len(stack_t* stack) {
   return stack->sp - 1;
}

void stack_print(stack_t* stack) {
   LOG_INFF("stack_print(): %p", stack);
   for (int i = stack_len(stack) - 1; i >= 0; --i) {
      LOG_INFF("%d: %p", i, stack[i]);
   }
}