summaryrefslogtreecommitdiff
path: root/src/util/stack.c
diff options
context:
space:
mode:
authorTyler Murphy <tylerm@tylerm.dev>2023-05-02 00:37:30 -0400
committerTyler Murphy <tylerm@tylerm.dev>2023-05-02 00:37:30 -0400
commitab7109065ced6feac485e3a5621c0f9c52f7aeec (patch)
treea242b446b39ab8d4dcd248ec2c5a75f2c45522c0 /src/util/stack.c
parentupdate makefile (diff)
downloadlazysphere-ab7109065ced6feac485e3a5621c0f9c52f7aeec.tar.gz
lazysphere-ab7109065ced6feac485e3a5621c0f9c52f7aeec.tar.bz2
lazysphere-ab7109065ced6feac485e3a5621c0f9c52f7aeec.zip
tac, ls fixes
Diffstat (limited to 'src/util/stack.c')
-rw-r--r--src/util/stack.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/util/stack.c b/src/util/stack.c
new file mode 100644
index 0000000..15d5a8e
--- /dev/null
+++ b/src/util/stack.c
@@ -0,0 +1,32 @@
+#include "stack.h"
+
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+void stack_init(struct Stack* stack, size_t size) {
+ stack->size = 0;
+ stack->capacity = size;
+ stack->data = malloc(size);
+}
+
+void stack_push(struct Stack* stack, void* data, size_t len) {
+ size_t new_size = stack->size + len;
+ if (new_size >= stack->capacity) {
+ stack->capacity = new_size * 2;
+ stack->data = realloc(stack->data, stack->capacity);
+ }
+ memcpy((uint8_t*)stack->data + stack->size, data, len);
+ stack->size += len;
+}
+
+void* stack_pop(struct Stack* stack, size_t len) {
+ if (stack->size < len) return NULL;
+ stack->size -= len;
+ return (uint8_t*)stack->data + stack->size;
+}
+
+void stack_free(struct Stack *stack) {
+ free(stack->data);
+}