aboutsummaryrefslogtreecommitdiff
path: root/src/stack.c
blob: 5c4d0ca65629392edf16913bd6ab3d3da8b86d66 (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
#include "include/stack.h"

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_inf("stack_print(): %p", stack);
   for (int i = stack_len(stack) - 1; i >= 0; --i) {
      log_inf("%d: %p", i, stack[i]);
   }
}