aboutsummaryrefslogtreecommitdiff
path: root/src/kernel/lib/data
diff options
context:
space:
mode:
authorMarvin Borner2020-04-21 14:18:20 +0200
committerMarvin Borner2020-04-21 14:18:20 +0200
commit40b98b3eb3f897d3d12199c79b30e2fd9151baf9 (patch)
tree0f10c78c95bd24a4b5cef2a3948dd3abbc1a4eb7 /src/kernel/lib/data
parentc83104329214bd618444d6e0242bb0317f52dbbc (diff)
Added very basic vfs
Diffstat (limited to 'src/kernel/lib/data')
-rw-r--r--src/kernel/lib/data/generic_tree.c102
-rw-r--r--src/kernel/lib/data/generic_tree.h36
-rw-r--r--src/kernel/lib/data/list.c220
-rw-r--r--src/kernel/lib/data/list.h60
4 files changed, 0 insertions, 418 deletions
diff --git a/src/kernel/lib/data/generic_tree.c b/src/kernel/lib/data/generic_tree.c
deleted file mode 100644
index 38caccc..0000000
--- a/src/kernel/lib/data/generic_tree.c
+++ /dev/null
@@ -1,102 +0,0 @@
-#include <kernel/lib/data/generic_tree.h>
-#include <kernel/lib/data/list.h>
-#include <kernel/memory/alloc.h>
-
-gtree_t *tree_create()
-{
- return (gtree_t *)kcalloc(sizeof(gtree_t), 1);
-}
-
-gtreenode_t *treenode_create(void *value)
-{
- gtreenode_t *n = kcalloc(sizeof(gtreenode_t), 1);
- n->value = value;
- n->children = list_create();
- return n;
-}
-
-gtreenode_t *tree_insert(gtree_t *tree, gtreenode_t *subroot, void *value)
-{
- gtreenode_t *treenode = kcalloc(sizeof(gtreenode_t), 1);
- treenode->children = list_create();
- treenode->value = value;
-
- if (!tree->root) {
- tree->root = treenode;
- return treenode;
- }
- list_insert_front(subroot->children, treenode);
- return treenode;
-}
-
-gtreenode_t *tree_find_parent(gtree_t *tree, gtreenode_t *remove_node, int *child_index)
-{
- if (remove_node == tree->root)
- return NULL;
- return tree_find_parent_recur(tree, remove_node, tree->root, child_index);
-}
-
-gtreenode_t *tree_find_parent_recur(gtree_t *tree, gtreenode_t *remove_node, gtreenode_t *subroot,
- int *child_index)
-{
- int idx;
- if ((idx = list_contain(subroot->children, remove_node)) != -1) {
- *child_index = idx;
- return subroot;
- }
- foreach(child, subroot->children)
- {
- gtreenode_t *ret =
- tree_find_parent_recur(tree, remove_node, child->val, child_index);
- if (ret != NULL) {
- return ret;
- }
- }
- return NULL;
-}
-
-void tree_remove(gtree_t *tree, gtreenode_t *remove_node)
-{
- int child_index = -1;
- gtreenode_t *parent = tree_find_parent(tree, remove_node, &child_index);
- if (parent != NULL) {
- gtreenode_t *freethis = list_remove_by_index(parent->children, child_index);
- kfree(freethis);
- }
-}
-
-void tree2list_recur(gtreenode_t *subroot, list_t *list)
-{
- if (subroot == NULL)
- return;
- foreach(child, subroot->children)
- {
- gtreenode_t *curr_treenode = (gtreenode_t *)child->val;
- void *curr_val = curr_treenode->value;
- list_insert_back(list, curr_val);
- tree2list_recur(child->val, list);
- }
-}
-
-void tree2list(gtree_t *tree, list_t *list)
-{
- tree2list_recur(tree->root, list);
-}
-
-void tree2array(gtree_t *tree, void **array, int *size)
-{
- tree2array_recur(tree->root, array, size);
-}
-
-void tree2array_recur(gtreenode_t *subroot, void **array, int *size)
-{
- if (subroot == NULL)
- return;
- void *curr_val = (void *)subroot->value;
- array[*size] = curr_val;
- *size = *size + 1;
- foreach(child, subroot->children)
- {
- tree2array_recur(child->val, array, size);
- }
-} \ No newline at end of file
diff --git a/src/kernel/lib/data/generic_tree.h b/src/kernel/lib/data/generic_tree.h
deleted file mode 100644
index 3a630bc..0000000
--- a/src/kernel/lib/data/generic_tree.h
+++ /dev/null
@@ -1,36 +0,0 @@
-#ifndef MELVIX_GENERIC_TREE_H
-#define MELVIX_GENERIC_TREE_H
-
-#include <kernel/lib/data/list.h>
-
-typedef struct gtreenode {
- list_t *children;
- void *value;
-} gtreenode_t;
-
-typedef struct gtree {
- gtreenode_t *root;
-} gtree_t;
-
-gtree_t *tree_create();
-
-gtreenode_t *treenode_create(void *value);
-
-gtreenode_t *tree_insert(gtree_t *tree, gtreenode_t *subroot, void *value);
-
-gtreenode_t *tree_find_parent(gtree_t *tree, gtreenode_t *remove_node, int *child_index);
-
-gtreenode_t *tree_find_parent_recur(gtree_t *tree, gtreenode_t *remove_node, gtreenode_t *subroot,
- int *child_index);
-
-void tree_remove(gtree_t *tree, gtreenode_t *remove_node);
-
-void tree2list_recur(gtreenode_t *subroot, list_t *list);
-
-void tree2list(gtree_t *tree, list_t *list);
-
-void tree2array(gtree_t *tree, void **array, int *size);
-
-void tree2array_recur(gtreenode_t *subroot, void **array, int *size);
-
-#endif \ No newline at end of file
diff --git a/src/kernel/lib/data/list.c b/src/kernel/lib/data/list.c
deleted file mode 100644
index 7a0cff2..0000000
--- a/src/kernel/lib/data/list.c
+++ /dev/null
@@ -1,220 +0,0 @@
-#include <kernel/lib/data/list.h>
-#include <kernel/memory/alloc.h>
-#include <kernel/lib/stdlib.h>
-#include <kernel/lib/lib.h>
-
-list_t *list_create()
-{
- list_t *list = kcalloc(sizeof(list_t), 1);
- return list;
-}
-
-uint32_t list_size(list_t *list)
-{
- if (!list)
- return 0;
- return list->size;
-}
-
-void *list_remove_node(list_t *list, listnode_t *node)
-{
- void *val = node->val;
- if (list->head == node)
- return list_remove_front(list);
- else if (list->tail == node)
- return list_remove_back(list);
- else {
- node->next->prev = node->prev;
- node->prev->next = node->next;
- list->size--;
- kfree(node);
- }
- return val;
-}
-
-listnode_t *list_insert_front(list_t *list, void *val)
-{
- listnode_t *t = kcalloc(sizeof(listnode_t), 1);
- list->head->prev = t;
- t->next = list->head;
- t->val = val;
-
- if (!list->head)
- list->tail = t;
-
- list->head = t;
- list->size++;
- return t;
-}
-
-void list_insert_back(list_t *list, void *val)
-{
- listnode_t *t = kcalloc(sizeof(listnode_t), 1);
- t->prev = list->tail;
- if (list->tail)
- list->tail->next = t;
- t->val = val;
-
- if (!list->head)
- list->head = t;
-
- list->tail = t;
- list->size++;
-}
-
-void *list_remove_front(list_t *list)
-{
- if (!list->head)
- return 0;
- listnode_t *t = list->head;
- void *val = t->val;
- list->head = t->next;
- if (list->head)
- list->head->prev = NULL;
- kfree(t);
- list->size--;
- return val;
-}
-
-void *list_remove_back(list_t *list)
-{
- if (!list->head)
- return NULL;
- listnode_t *t = list->tail;
- void *val = t->val;
- list->tail = t->prev;
- if (list->tail)
- list->tail->next = NULL;
- kfree(t);
- list->size--;
- return val;
-}
-
-void list_push(list_t *list, void *val)
-{
- list_insert_back(list, val);
-}
-
-listnode_t *list_pop(list_t *list)
-{
- if (!list->head)
- return NULL;
- listnode_t *t = list->tail;
- list->tail = t->prev;
- if (list->tail)
- list->tail->next = NULL;
- list->size--;
- return t;
-}
-
-void list_enqueue(list_t *list, void *val)
-{
- list_insert_front(list, val);
-}
-
-listnode_t *list_dequeue(list_t *list)
-{
- return list_pop(list);
-}
-
-void *list_peek_front(list_t *list)
-{
- if (!list->head)
- return NULL;
- return list->head->val;
-}
-
-void *list_peek_back(list_t *list)
-{
- if (!list->tail)
- return NULL;
- return list->tail->val;
-}
-
-int list_contain(list_t *list, void *val)
-{
- int idx = 0;
- foreach(listnode, list)
- {
- if (listnode->val == val)
- return idx;
- idx++;
- }
- return -1;
-}
-
-listnode_t *list_get_node_by_index(list_t *list, int index)
-{
- if (index < 0 || (uint32_t)index >= list_size(list))
- return NULL;
- int curr = 0;
- foreach(listnode, list)
- {
- if (index == curr)
- return listnode;
- curr++;
- }
- return NULL;
-}
-
-void *list_remove_by_index(list_t *list, int index)
-{
- listnode_t *node = list_get_node_by_index(list, index);
- return list_remove_node(list, node);
-}
-
-void list_destroy(list_t *list)
-{
- listnode_t *node = list->head;
- while (node != NULL) {
- listnode_t *save = node;
- node = node->next;
- kfree(save);
- }
- kfree(list);
-}
-
-void listnode_destroy(listnode_t *node)
-{
- kfree(node);
-}
-
-char *list2str(list_t *list, const char *delim)
-{
- char *ret = kmalloc(256);
- memset(ret, 0, 256);
- int len = 0, ret_len = 256;
- while (list_size(list) > 0) {
- char *temp = list_pop(list)->val;
- int len_temp = strlen(temp);
- if (len + len_temp + 1 + 1 > ret_len) {
- ret_len = ret_len * 2;
- ret = krealloc(ret, ret_len);
- len = len + len_temp + 1;
- }
- strcat(ret, delim);
- strcat(ret, temp);
- }
- return ret;
-}
-
-list_t *str_split(const char *str, const char *delim, uint32_t *numtokens)
-{
- list_t *ret_list = list_create();
- char *s = strdup(str);
- char *token, *rest = s;
- while ((token = strsep(&rest, delim)) != NULL) {
- if (!strcmp(token, "."))
- continue;
- if (!strcmp(token, "..")) {
- if (list_size(ret_list) > 0)
- list_pop(ret_list);
- continue;
- }
- list_push(ret_list, strdup(token));
- if (numtokens)
- (*numtokens)++;
- }
- kfree(s);
- return ret_list;
-} \ No newline at end of file
diff --git a/src/kernel/lib/data/list.h b/src/kernel/lib/data/list.h
deleted file mode 100644
index eeda9b3..0000000
--- a/src/kernel/lib/data/list.h
+++ /dev/null
@@ -1,60 +0,0 @@
-#ifndef MELVIX_LIST_H
-#define MELVIX_LIST_H
-
-#include <stdint.h>
-
-typedef struct listnode {
- struct listnode *prev;
- struct listnode *next;
- void *val;
-} listnode_t;
-
-typedef struct list {
- listnode_t *head;
- listnode_t *tail;
- uint32_t size;
-} list_t;
-
-#define foreach(t, list) for (listnode_t *t = list->head; t != NULL; t = t->next)
-
-list_t *list_create();
-
-uint32_t list_size(list_t *list);
-
-listnode_t *list_insert_front(list_t *list, void *val);
-
-void list_insert_back(list_t *list, void *val);
-
-void *list_remove_node(list_t *list, listnode_t *node);
-
-void *list_remove_front(list_t *list);
-
-void *list_remove_back(list_t *list);
-
-void list_push(list_t *list, void *val);
-
-listnode_t *list_pop(list_t *list);
-
-void list_enqueue(list_t *list, void *val);
-
-listnode_t *list_dequeue(list_t *list);
-
-void *list_peek_front(list_t *list);
-
-void *list_peek_back(list_t *list);
-
-void list_destroy(list_t *list);
-
-void listnode_destroy(listnode_t *node);
-
-int list_contain(list_t *list, void *val);
-
-listnode_t *list_get_node_by_index(list_t *list, int index);
-
-void *list_remove_by_index(list_t *list, int index);
-
-char *list2str(list_t *list, const char *delim);
-
-list_t *str_split(const char *str, const char *delim, uint32_t *numtokens);
-
-#endif \ No newline at end of file