From ddb7d43143ff664948f27cd2cd4e18bdfcce6118 Mon Sep 17 00:00:00 2001 From: Stefan Zabka Date: Thu, 11 Jun 2020 01:00:56 +0200 Subject: Forgot arena folder --- 04_exercise/arena/arena_list.c | 63 ++++++++++++++++++++++++++++++++++++++++++ 04_exercise/arena/arena_list.h | 36 ++++++++++++++++++++++++ 04_exercise/arena/arena_test.c | 18 ++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 04_exercise/arena/arena_list.c create mode 100644 04_exercise/arena/arena_list.h create mode 100644 04_exercise/arena/arena_test.c 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 +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 +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); + } + +} -- cgit v1.2.3-54-g00ecf