summaryrefslogtreecommitdiff
path: root/kernel/lib/stack.c
blob: 7f318676352315f55a9bf74a89c8582d534d8a95 (plain)
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
#include <kmalloc.h>
#include <lib/stack.h>
#include <stddef.h>

void stack_init(struct stack *s) {
  s->head = NULL;
}

// 1 = Success
// 0 = Failure
int stack_push(struct stack *s, void *data) {
  struct entry *new_entry = kmalloc(sizeof(struct entry));
  if (NULL == new_entry) {
    return 0;
  }
  new_entry->ptr = data;

  new_entry->next = s->head;
  s->head = new_entry;
  return 1;
}

// Returns data on success
// Returns NULL on failure
void *stack_pop(struct stack *s) {
  struct entry *recieved_entry = s->head;
  if (NULL == recieved_entry) {
    return NULL;
  }
  s->head = recieved_entry->next;

  void *r = recieved_entry->ptr;
  kfree(recieved_entry);
  return r;
}