diff options
author | Marvin Borner | 2021-01-10 16:52:06 +0100 |
---|---|---|
committer | Marvin Borner | 2021-01-10 17:07:29 +0100 |
commit | 856b362a4b68d5776ea37dc30a50e7414288d877 (patch) | |
tree | fab26d13a1495e49d7adeda68fc71bd3ae566825 | |
parent | 3619c820f8ff5918cf122a031bde6305d32f6528 (diff) |
Added very simple stack implementation for procfs
This probably has several bugs, but idc
-rw-r--r-- | libc/Makefile | 1 | ||||
-rw-r--r-- | libc/inc/stack.h | 27 | ||||
-rw-r--r-- | libc/stack.c | 119 |
3 files changed, 147 insertions, 0 deletions
diff --git a/libc/Makefile b/libc/Makefile index 1a8e0f0..a3d1300 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -10,6 +10,7 @@ COBJS = str.o \ cpu.o \ sys.o \ list.o \ + stack.o \ random.o CC = ccache ../cross/opt/bin/i686-elf-gcc LD = ccache ../cross/opt/bin/i686-elf-ld diff --git a/libc/inc/stack.h b/libc/inc/stack.h new file mode 100644 index 0000000..8fec25f --- /dev/null +++ b/libc/inc/stack.h @@ -0,0 +1,27 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#ifndef STACK_H +#define STACK_H + +#include <def.h> + +struct stack_node { + void *data; + int nonce; + struct stack_node *next; + struct stack_node *prev; +}; + +struct stack { + struct stack_node *tail; +}; + +struct stack *stack_new(); +void stack_destroy(struct stack *stack); +u32 stack_empty(struct stack *stack); +u32 stack_push_bot(struct stack *stack, void *data); +u32 stack_push(struct stack *stack, void *data); +void *stack_pop(struct stack *stack); +void *stack_peek(struct stack *stack); + +#endif diff --git a/libc/stack.c b/libc/stack.c new file mode 100644 index 0000000..dac18a2 --- /dev/null +++ b/libc/stack.c @@ -0,0 +1,119 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#include <def.h> +#include <mem.h> +#include <stack.h> + +static int nonce = 0; + +struct stack *stack_new() +{ + struct stack *stack = malloc(sizeof(*stack)); + stack->tail = NULL; + return stack; +} + +void stack_destroy(struct stack *stack) +{ + struct stack_node *iterator = stack->tail; + while (iterator) { + if (!iterator->prev) { + free(iterator); + break; + } + iterator = iterator->prev; + free(iterator->next); + } + stack->tail = NULL; + free(stack); + stack = NULL; +} + +struct stack_node *stack_new_node() +{ + struct stack_node *node = malloc(sizeof(*node)); + node->data = NULL; + node->prev = NULL; + node->next = NULL; + node->nonce = nonce++; + return node; +} + +u32 stack_push_bot_node(struct stack *stack, struct stack_node *node) +{ + if (!stack || !node) + return 0; + + if (stack->tail) { + struct stack_node *iterator = stack->tail; + while (iterator) { + if (!iterator->prev) + break; + iterator = iterator->prev; + } + iterator->prev = node; + node->next = iterator; + } else { + stack->tail = node; + } + + return 1; +} + +u32 stack_push_node(struct stack *stack, struct stack_node *node) +{ + if (!stack || !node) + return 0; + + if (stack->tail) { + stack->tail->next = node; + node->prev = stack->tail; + stack->tail = node; + } else { + stack->tail = node; + } + + return 1; +} + +u32 stack_empty(struct stack *stack) +{ + return !stack->tail; +} + +u32 stack_push_bot(struct stack *stack, void *data) +{ + struct stack_node *node = stack_new_node(); + node->data = data; + return stack_push_bot_node(stack, node); +} + +u32 stack_push(struct stack *stack, void *data) +{ + struct stack_node *node = stack_new_node(); + node->data = data; + return stack_push_node(stack, node); +} + +void *stack_pop(struct stack *stack) +{ + if (!stack || !stack->tail) + return NULL; + + struct stack_node *prev = stack->tail; + + stack->tail->prev->next = NULL; + stack->tail = stack->tail->prev; + + void *data = prev->data; + free(prev); + return data; +} + +void *stack_peek(struct stack *stack) +{ + if (!stack || !stack->tail) + return NULL; + + return stack->tail; +} |