summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Zabka <zabkaste@hu-berlin.de>2020-06-11 01:00:56 +0200
committerStefan Zabka <zabkaste@hu-berlin.de>2020-06-11 01:00:56 +0200
commitddb7d43143ff664948f27cd2cd4e18bdfcce6118 (patch)
treed36d4579b73e7197262e92f8acc83fc247d4f013
parentfad28509e7463082a508b761c797773538df7d27 (diff)
downloadbetriebssysteme-ddb7d43143ff664948f27cd2cd4e18bdfcce6118.tar.gz
betriebssysteme-ddb7d43143ff664948f27cd2cd4e18bdfcce6118.zip
Forgot arena folder
-rw-r--r--04_exercise/arena/arena_list.c63
-rw-r--r--04_exercise/arena/arena_list.h36
-rw-r--r--04_exercise/arena/arena_test.c18
3 files changed, 117 insertions, 0 deletions
diff --git a/04_exercise/arena/arena_list.c b/04_exercise/arena/arena_list.c
new file mode 100644
index 0000000..f4b4eb3
--- /dev/null
+++ b/04_exercise/arena/arena_list.c
@@ -0,0 +1,63 @@
+//
+// Created by stefan on 10.06.20.
+//
+#include "arena_list.h"
+
+Node *listPopFront(List *list) {
+ Node *front = list->first;
+ if (front == NULL) {
+ return NULL;
+ }
+
+ list->first = front->next;
+ if (front == list->last) {
+ // Only Node in the list
+ list->last = NULL;
+ return front;
+ }
+
+ if (front->next != NULL) {
+ list->first->prev = NULL;
+ front->next = NULL;
+ }
+ return front;
+}
+
+Node *listPopBack(List list) { return NULL; }
+
+void listPushFront(List *list, Node *new) {
+ if (list->first == NULL) {
+ // List was empty
+ list->first = new;
+ list->last = new;
+ return;
+ }
+ new->next = list->first;
+ new->next->prev = new;
+ list->first = new;
+}
+
+void listPushBack(List list, Node *value) {}
+
+ArenaList alInit(Node *arena, size_t size) {
+ ArenaList al;
+ al.activeList = (List){.first = NULL, .last = NULL};
+ al.freeList = (List){.first = arena, .last = &arena[size - 1]};
+ arena[0] = (Node){.value = NULL, .prev = NULL, .next = &arena[1]};
+ for (size_t i = 1; i < size - 1; ++i) {
+ arena[i] = (Node){.value = NULL, .prev = &arena[i - 1], .next = &arena[i + 1]};
+ }
+ arena[size - 1] = (Node){.value = NULL, .prev = &arena[size - 2], .next = NULL};
+ return al;
+}
+
+int alPush(ArenaList* al, void *value) {
+ Node *current = listPopFront(&al->freeList);
+ // List is empty
+ if (current == NULL) {
+ return -1;
+ }
+ current->value = value;
+ listPushFront(&al->activeList, current);
+ return 0;
+} \ No newline at end of file
diff --git a/04_exercise/arena/arena_list.h b/04_exercise/arena/arena_list.h
new file mode 100644
index 0000000..86bf805
--- /dev/null
+++ b/04_exercise/arena/arena_list.h
@@ -0,0 +1,36 @@
+//
+// Created by stefan on 10.06.20.
+//
+
+#ifndef BETRIEBSYSTEME_ARENA_LIST_H
+#define BETRIEBSYSTEME_ARENA_LIST_H
+
+#include <stdlib.h>
+typedef struct Node {
+ void * value;
+ struct Node * next;
+ struct Node * prev;
+} Node;
+
+typedef struct List {
+ Node * first;
+ Node * last;
+} List;
+// This structure manages an arena / memory slab to be used
+typedef struct ArenaList {
+ List freeList;
+ List activeList;
+} ArenaList;
+
+//Initializes the node array
+ArenaList alInit(Node * arena, size_t size);
+// Return -1 if the List is full
+int alPush(ArenaList* al, void * value);
+
+// Return -1 if the Node was already deleted
+int alRemove(ArenaList* al, Node * node);
+
+// Return -1 if the Node was already deleted
+int alRemoveElem(ArenaList* al, void * value);
+
+#endif // BETRIEBSYSTEME_ARENA_LIST_H
diff --git a/04_exercise/arena/arena_test.c b/04_exercise/arena/arena_test.c
new file mode 100644
index 0000000..b0927f1
--- /dev/null
+++ b/04_exercise/arena/arena_test.c
@@ -0,0 +1,18 @@
+//
+// Created by stefan on 10.06.20.
+//
+#include "arena_list.h"
+#include <stdio.h>
+int main() {
+ Node arena[5];
+ ArenaList al = alInit(arena, 5);
+ int data[5] = {1, 2, 3, 4, 5};
+ for (int i = 0; i < 5; ++i) {
+ alPush(&al, &data[4 - i]);
+ }
+
+ for (Node *cur = al.activeList.first; cur != NULL; cur = cur->next) {
+ printf("Got digit %d \n", *(int *)cur->value);
+ }
+
+}