Browse Source

Unify list, tree, hashmap implementations with userspace/kernel

K. Lange 2 years ago
parent
commit
5dd763780a

+ 0 - 47
base/usr/include/kernel/hashmap.h

@@ -1,47 +0,0 @@
-#pragma once
-
-#ifdef _KERNEL_
-#	include <kernel/list.h>
-#	include <kernel/system.h>
-#else
-#	include <toaru/list.h>
-#	include <string.h>
-#	include <stddef.h>
-#	include <stdlib.h>
-#endif
-
-typedef unsigned int (*hashmap_hash_t) (void * key);
-typedef int (*hashmap_comp_t) (void * a, void * b);
-typedef void (*hashmap_free_t) (void *);
-typedef void * (*hashmap_dupe_t) (void *);
-
-typedef struct hashmap_entry {
-	char * key;
-	void * value;
-	struct hashmap_entry * next;
-} hashmap_entry_t;
-
-typedef struct hashmap {
-	hashmap_hash_t hash_func;
-	hashmap_comp_t hash_comp;
-	hashmap_dupe_t hash_key_dup;
-	hashmap_free_t hash_key_free;
-	hashmap_free_t hash_val_free;
-	size_t         size;
-	hashmap_entry_t ** entries;
-} hashmap_t;
-
-hashmap_t * hashmap_create(int size);
-hashmap_t * hashmap_create_int(int size);
-void * hashmap_set(hashmap_t * map, void * key, void * value);
-void * hashmap_get(hashmap_t * map, void * key);
-void * hashmap_remove(hashmap_t * map, void * key);
-int hashmap_has(hashmap_t * map, void * key);
-list_t * hashmap_keys(hashmap_t * map);
-list_t * hashmap_values(hashmap_t * map);
-void hashmap_free(hashmap_t * map);
-
-unsigned int hashmap_string_hash(void * key);
-int hashmap_string_comp(void * a, void * b);
-void * hashmap_string_dupe(void * key);
-

+ 0 - 50
base/usr/include/kernel/list.h

@@ -1,50 +0,0 @@
-/* vim: tabstop=4 shiftwidth=4 noexpandtab
- * 
- * General-purpose list implementations.
- */
-#pragma once
-
-#ifdef _KERNEL_
-#	include <kernel/types.h>
-#else
-#	include <stdint.h>
-#	include <stddef.h>
-#	include <assert.h>
-#endif
-
-typedef struct node {
-	struct node * next;
-	struct node * prev;
-	void * value;
-	void * owner;
-} __attribute__((packed)) node_t;
-
-typedef struct {
-	node_t * head;
-	node_t * tail;
-	size_t length;
-} __attribute__((packed)) list_t;
-
-void list_destroy(list_t * list);
-void list_free(list_t * list);
-void list_append(list_t * list, node_t * item);
-node_t * list_insert(list_t * list, void * item);
-list_t * list_create(void);
-node_t * list_find(list_t * list, void * value);
-int list_index_of(list_t * list, void * value);
-void list_remove(list_t * list, size_t index);
-void list_delete(list_t * list, node_t * node);
-node_t * list_pop(list_t * list);
-node_t * list_dequeue(list_t * list);
-list_t * list_copy(list_t * original);
-void list_merge(list_t * target, list_t * source);
-
-void list_append_after(list_t * list, node_t * before, node_t * node);
-node_t * list_insert_after(list_t * list, node_t * before, void * item);
-
-void list_append_before(list_t * list, node_t * after, node_t * node);
-node_t * list_insert_before(list_t * list, node_t * after, void * item);
-
-#define foreach(i, list) for (node_t * i = (list)->head; i != NULL; i = i->next)
-#define foreachr(i, list) for (node_t * i = (list)->tail; i != NULL; i = i->prev)
-

+ 2 - 1
base/usr/include/kernel/module.h

@@ -1,7 +1,8 @@
 #pragma once
 
 #include <kernel/types.h>
-#include <kernel/hashmap.h>
+
+#include <toaru/hashmap.h>
 
 typedef struct {
     char * name;

+ 2 - 1
base/usr/include/kernel/process.h

@@ -3,10 +3,11 @@
 
 #pragma once
 
-#include <kernel/tree.h>
 #include <kernel/signal.h>
 #include <kernel/task.h>
 
+#include <toaru/tree.h>
+
 #define KERNEL_STACK_SIZE 0x8000
 
 typedef signed int    pid_t;

+ 2 - 1
base/usr/include/kernel/system.h

@@ -5,11 +5,12 @@
 #include <kernel/types.h>
 #include <kernel/fs.h>
 #include <kernel/va_list.h>
-#include <kernel/list.h>
 #include <kernel/task.h>
 #include <kernel/process.h>
 #include <kernel/libc.h>
 
+#include <toaru/list.h>
+
 #define ASSUME(cond) __extension__ ({ if (!(cond)) { __builtin_unreachable(); } })
 
 #define STR(x) #x

+ 0 - 37
base/usr/include/kernel/tree.h

@@ -1,37 +0,0 @@
-/* vim: tabstop=4 shiftwidth=4 noexpandtab
- * 
- * General-purpose tree implementation
- */
-#pragma once
-
-#include <kernel/list.h>
-
-typedef struct tree_node {
-	void * value;
-	list_t * children;
-	struct tree_node * parent;
-} tree_node_t;
-
-typedef struct {
-	size_t nodes;
-	tree_node_t * root;
-} tree_t;
-
-typedef uint8_t (*tree_comparator_t) (void *, void *);
-
-tree_t * tree_create(void);
-void tree_set_root(tree_t * tree, void * value);
-void tree_node_destroy(tree_node_t * node);
-void tree_destroy(tree_t * tree);
-void tree_free(tree_t * tree);
-tree_node_t * tree_node_create(void * value);
-void tree_node_insert_child_node(tree_t * tree, tree_node_t * parent, tree_node_t * node);
-tree_node_t * tree_node_insert_child(tree_t * tree, tree_node_t * parent, void * value);
-tree_node_t * tree_node_find_parent(tree_node_t * haystack, tree_node_t * needle);
-void tree_node_parent_remove(tree_t * tree, tree_node_t * parent, tree_node_t * node);
-void tree_node_remove(tree_t * tree, tree_node_t * node);
-void tree_remove(tree_t * tree, tree_node_t * node);
-tree_node_t * tree_find(tree_t * tree, void * value, tree_comparator_t comparator);
-void tree_break_off(tree_t * tree, tree_node_t * node);
-
-

+ 2 - 2
base/usr/include/toaru/hashmap.h

@@ -2,15 +2,15 @@
 
 
 #ifdef _KERNEL_
-#	include <kernel/list.h>
 #	include <kernel/system.h>
 #else
-#	include <toaru/list.h>
 #	include <string.h>
 #	include <stddef.h>
 #	include <stdlib.h>
 #endif
 
+#include <toaru/list.h>
+
 typedef unsigned int (*hashmap_hash_t) (void * key);
 typedef int (*hashmap_comp_t) (void * a, void * b);
 typedef void (*hashmap_free_t) (void *);

+ 0 - 217
kernel/ds/hashmap.c

@@ -1,217 +0,0 @@
-/* vim: tabstop=4 shiftwidth=4 noexpandtab
- * This file is part of ToaruOS and is released under the terms
- * of the NCSA / University of Illinois License - see LICENSE.md
- * Copyright (C) 2013-2014 Kevin Lange
- */
-#include <kernel/list.h>
-#include <kernel/hashmap.h>
-
-unsigned int hashmap_string_hash(void * _key) {
-	unsigned int hash = 0;
-	char * key = (char *)_key;
-	int c;
-	/* This is the so-called "sdbm" hash. It comes from a piece of
-	 * public domain code from a clone of ndbm. */
-	while ((c = *key++)) {
-		hash = c + (hash << 6) + (hash << 16) - hash;
-	}
-	return hash;
-}
-
-int hashmap_string_comp(void * a, void * b) {
-	return !strcmp(a,b);
-}
-
-void * hashmap_string_dupe(void * key) {
-	return strdup(key);
-}
-
-unsigned int hashmap_int_hash(void * key) {
-	return (unsigned int)key;
-}
-
-int hashmap_int_comp(void * a, void * b) {
-	return (int)a == (int)b;
-}
-
-void * hashmap_int_dupe(void * key) {
-	return key;
-}
-
-static void hashmap_int_free(void * ptr) {
-	return;
-}
-
-
-hashmap_t * hashmap_create(int size) {
-	hashmap_t * map = malloc(sizeof(hashmap_t));
-
-	map->hash_func     = &hashmap_string_hash;
-	map->hash_comp     = &hashmap_string_comp;
-	map->hash_key_dup  = &hashmap_string_dupe;
-	map->hash_key_free = &free;
-	map->hash_val_free = &free;
-
-	map->size = size;
-	map->entries = malloc(sizeof(hashmap_entry_t *) * size);
-	memset(map->entries, 0x00, sizeof(hashmap_entry_t *) * size);
-
-	return map;
-}
-
-hashmap_t * hashmap_create_int(int size) {
-	hashmap_t * map = malloc(sizeof(hashmap_t));
-
-	map->hash_func     = &hashmap_int_hash;
-	map->hash_comp     = &hashmap_int_comp;
-	map->hash_key_dup  = &hashmap_int_dupe;
-	map->hash_key_free = &hashmap_int_free;
-	map->hash_val_free = &free;
-
-	map->size = size;
-	map->entries = malloc(sizeof(hashmap_entry_t *) * size);
-	memset(map->entries, 0x00, sizeof(hashmap_entry_t *) * size);
-
-	return map;
-}
-
-void * hashmap_set(hashmap_t * map, void * key, void * value) {
-	unsigned int hash = map->hash_func(key) % map->size;
-
-	hashmap_entry_t * x = map->entries[hash];
-	if (!x) {
-		hashmap_entry_t * e = malloc(sizeof(hashmap_entry_t));
-		e->key   = map->hash_key_dup(key);
-		e->value = value;
-		e->next = NULL;
-		map->entries[hash] = e;
-		return NULL;
-	} else {
-		hashmap_entry_t * p = NULL;
-		do {
-			if (map->hash_comp(x->key, key)) {
-				void * out = x->value;
-				x->value = value;
-				return out;
-			} else {
-				p = x;
-				x = x->next;
-			}
-		} while (x);
-		hashmap_entry_t * e = malloc(sizeof(hashmap_entry_t));
-		e->key   = map->hash_key_dup(key);
-		e->value = value;
-		e->next = NULL;
-
-		p->next = e;
-		return NULL;
-	}
-}
-
-void * hashmap_get(hashmap_t * map, void * key) {
-	unsigned int hash = map->hash_func(key) % map->size;
-
-	hashmap_entry_t * x = map->entries[hash];
-	if (!x) {
-		return NULL;
-	} else {
-		do {
-			if (map->hash_comp(x->key, key)) {
-				return x->value;
-			}
-			x = x->next;
-		} while (x);
-		return NULL;
-	}
-}
-
-void * hashmap_remove(hashmap_t * map, void * key) {
-	unsigned int hash = map->hash_func(key) % map->size;
-
-	hashmap_entry_t * x = map->entries[hash];
-	if (!x) {
-		return NULL;
-	} else {
-		if (map->hash_comp(x->key, key)) {
-			void * out = x->value;
-			map->entries[hash] = x->next;
-			map->hash_key_free(x->key);
-			map->hash_val_free(x);
-			return out;
-		} else {
-			hashmap_entry_t * p = x;
-			x = x->next;
-			do {
-				if (map->hash_comp(x->key, key)) {
-					void * out = x->value;
-					p->next = x->next;
-					map->hash_key_free(x->key);
-					map->hash_val_free(x);
-					return out;
-				}
-				p = x;
-				x = x->next;
-			} while (x);
-		}
-		return NULL;
-	}
-}
-
-int hashmap_has(hashmap_t * map, void * key) {
-	unsigned int hash = map->hash_func(key) % map->size;
-
-	hashmap_entry_t * x = map->entries[hash];
-	if (!x) {
-		return 0;
-	} else {
-		do {
-			if (map->hash_comp(x->key, key)) {
-				return 1;
-			}
-			x = x->next;
-		} while (x);
-		return 0;
-	}
-
-}
-
-list_t * hashmap_keys(hashmap_t * map) {
-	list_t * l = list_create();
-
-	for (unsigned int i = 0; i < map->size; ++i) {
-		hashmap_entry_t * x = map->entries[i];
-		while (x) {
-			list_insert(l, x->key);
-			x = x->next;
-		}
-	}
-
-	return l;
-}
-
-list_t * hashmap_values(hashmap_t * map) {
-	list_t * l = list_create();
-
-	for (unsigned int i = 0; i < map->size; ++i) {
-		hashmap_entry_t * x = map->entries[i];
-		while (x) {
-			list_insert(l, x->value);
-			x = x->next;
-		}
-	}
-
-	return l;
-}
-
-void hashmap_free(hashmap_t * map) {
-	for (unsigned int i = 0; i < map->size; ++i) {
-		hashmap_entry_t * x = map->entries[i], * p;
-		while (x) {
-			p = x;
-			x = x->next;
-			map->hash_key_free(p->key);
-			map->hash_val_free(p);
-		}
-	}
-	free(map->entries);
-}

+ 1 - 0
kernel/ds/hashmap.c

@@ -0,0 +1 @@
+../../lib/hashmap.c

+ 0 - 248
kernel/ds/list.c

@@ -1,248 +0,0 @@
-/* vim: tabstop=4 shiftwidth=4 noexpandtab
- * This file is part of ToaruOS and is released under the terms
- * of the NCSA / University of Illinois License - see LICENSE.md
- * Copyright (C) 2011-2014 Kevin Lange
- *
- * General-purpose list implementations.
- */
-
-#ifdef _KERNEL_
-#	include <kernel/list.h>
-#	include <kernel/system.h>
-#else
-#	include <stddef.h>
-#	include <stdlib.h>
-#	include <toaru/list.h>
-#endif
-
-void list_destroy(list_t * list) {
-	/* Free all of the contents of a list */
-	node_t * n = list->head;
-	while (n) {
-		free(n->value);
-		n = n->next;
-	}
-}
-
-void list_free(list_t * list) {
-	/* Free the actual structure of a list */
-	node_t * n = list->head;
-	while (n) {
-		node_t * s = n->next;
-		free(n);
-		n = s;
-	}
-}
-
-void list_append(list_t * list, node_t * node) {
-	assert(!(node->next || node->prev) && "Node is already in a list.");
-	node->next = NULL;
-	/* Insert a node onto the end of a list */
-	node->owner = list;
-	if (!list->length) {
-		list->head = node;
-		list->tail = node;
-		node->prev = NULL;
-		node->next = NULL;
-		list->length++;
-		return;
-	}
-	list->tail->next = node;
-	node->prev = list->tail;
-	list->tail = node;
-	list->length++;
-}
-
-node_t * list_insert(list_t * list, void * item) {
-	/* Insert an item into a list */
-	node_t * node = malloc(sizeof(node_t));
-	node->value = item;
-	node->next  = NULL;
-	node->prev  = NULL;
-	node->owner = NULL;
-	list_append(list, node);
-
-	return node;
-}
-
-void list_append_after(list_t * list, node_t * before, node_t * node) {
-	assert(!(node->next || node->prev) && "Node is already in a list.");
-	node->owner = list;
-	if (!list->length) {
-		list_append(list, node);
-		return;
-	}
-	if (before == NULL) {
-		node->next = list->head;
-		node->prev = NULL;
-		list->head->prev = node;
-		list->head = node;
-		list->length++;
-		return;
-	}
-	if (before == list->tail) {
-		list->tail = node;
-	} else {
-		before->next->prev = node;
-		node->next = before->next;
-	}
-	node->prev = before;
-	before->next = node;
-	list->length++;
-}
-
-node_t * list_insert_after(list_t * list, node_t * before, void * item) {
-	node_t * node = malloc(sizeof(node_t));
-	node->value = item;
-	node->next  = NULL;
-	node->prev  = NULL;
-	node->owner = NULL;
-	list_append_after(list, before, node);
-	return node;
-}
-
-void list_append_before(list_t * list, node_t * after, node_t * node) {
-	assert(!(node->next || node->prev) && "Node is already in a list.");
-	node->owner = list;
-	if (!list->length) {
-		list_append(list, node);
-		return;
-	}
-	if (after == NULL) {
-		node->next = NULL;
-		node->prev = list->tail;
-		list->tail->next = node;
-		list->tail = node;
-		list->length++;
-		return;
-	}
-	if (after == list->head) {
-		list->head = node;
-	} else {
-		after->prev->next = node;
-		node->prev = after->prev;
-	}
-	node->next = after;
-	after->prev = node;
-	list->length++;
-}
-
-node_t * list_insert_before(list_t * list, node_t * after, void * item) {
-	node_t * node = malloc(sizeof(node_t));
-	node->value = item;
-	node->next  = NULL;
-	node->prev  = NULL;
-	node->owner = NULL;
-	list_append_before(list, after, node);
-	return node;
-}
-
-list_t * list_create(void) {
-	/* Create a fresh list */
-	list_t * out = malloc(sizeof(list_t));
-	out->head = NULL;
-	out->tail = NULL;
-	out->length = 0;
-	return out;
-}
-
-node_t * list_find(list_t * list, void * value) {
-	foreach(item, list) {
-		if (item->value == value) {
-			return item;
-		}
-	}
-	return NULL;
-}
-
-int list_index_of(list_t * list, void * value) {
-	int i = 0;
-	foreach(item, list) {
-		if (item->value == value) {
-			return i;
-		}
-		i++;
-	}
-	return -1; /* not find */
-}
-
-void list_remove(list_t * list, size_t index) {
-	/* remove index from the list */
-	if (index > list->length) return;
-	size_t i = 0;
-	node_t * n = list->head;
-	while (i < index) {
-		n = n->next;
-		i++;
-	}
-	list_delete(list, n);
-}
-
-void list_delete(list_t * list, node_t * node) {
-	/* remove node from the list */
-	assert(node->owner == list && "Tried to remove a list node from a list it does not belong to.");
-	if (node == list->head) {
-		list->head = node->next;
-	}
-	if (node == list->tail) {
-		list->tail = node->prev;
-	}
-	if (node->prev) {
-		node->prev->next = node->next;
-	}
-	if (node->next) {
-		node->next->prev = node->prev;
-	}
-	node->prev = NULL;
-	node->next = NULL;
-	node->owner = NULL;
-	list->length--;
-}
-
-node_t * list_pop(list_t * list) {
-	/* Remove and return the last value in the list
-	 * If you don't need it, you still probably want to free it!
-	 * Try free(list_pop(list)); !
-	 * */
-	if (!list->tail) return NULL;
-	node_t * out = list->tail;
-	list_delete(list, out);
-	return out;
-}
-
-node_t * list_dequeue(list_t * list) {
-	if (!list->head) return NULL;
-	node_t * out = list->head;
-	list_delete(list, out);
-	return out;
-}
-
-list_t * list_copy(list_t * original) {
-	/* Create a new copy of original */
-	list_t * out = list_create();
-	node_t * node = original->head;
-	while (node) {
-		list_insert(out, node->value);
-	}
-	return out;
-}
-
-void list_merge(list_t * target, list_t * source) {
-	/* Destructively merges source into target */
-	foreach(node, source) {
-		node->owner = target;
-	}
-	if (source->head) {
-		source->head->prev = target->tail;
-	}
-	if (target->tail) {
-		target->tail->next = source->head;
-	} else {
-		target->head = source->head;
-	}
-	if (source->tail) {
-		target->tail = source->tail;
-	}
-	target->length += source->length;
-	free(source);
-}

+ 1 - 0
kernel/ds/list.c

@@ -0,0 +1 @@
+../../lib/list.c

+ 0 - 192
kernel/ds/tree.c

@@ -1,192 +0,0 @@
-/* vim: tabstop=4 shiftwidth=4 noexpandtab
- * This file is part of ToaruOS and is released under the terms
- * of the NCSA / University of Illinois License - see LICENSE.md
- * Copyright (C) 2011-2014 Kevin Lange
- *
- * General-purpose tree implementation
- */
-
-#ifdef _KERNEL_
-#	include <kernel/tree.h>
-#	include <kernel/system.h>
-#else
-#	include <toaru/tree.h>
-#	include <stddef.h>
-#	include <stdlib.h>
-#endif
-
-tree_t * tree_create(void) {
-	/* Create a new tree */
-	tree_t * out = malloc(sizeof(tree_t));
-	out->nodes  = 0;
-	out->root   = NULL;
-	return out;
-}
-
-void tree_set_root(tree_t * tree, void * value) {
-	/* Set the root node for a new tree. */
-	tree_node_t * root = tree_node_create(value);
-	tree->root = root;
-	tree->nodes = 1;
-}
-
-void tree_node_destroy(tree_node_t * node) {
-	/* Free the contents of a node and its children, but not the nodes themselves */
-	foreach(child, node->children) {
-		tree_node_destroy((tree_node_t *)child->value);
-	}
-	free(node->value);
-}
-
-void tree_destroy(tree_t * tree) {
-	/* Free the contents of a tree, but not the nodes */
-	if (tree->root) {
-		tree_node_destroy(tree->root);
-	}
-}
-
-void tree_node_free(tree_node_t * node) {
-	/* Free a node and its children, but not their contents */
-	if (!node) return;
-	foreach(child, node->children) {
-		tree_node_free(child->value);
-	}
-	free(node);
-}
-
-void tree_free(tree_t * tree) {
-	/* Free all of the nodes in a tree, but not their contents */
-	tree_node_free(tree->root);
-}
-
-tree_node_t * tree_node_create(void * value) {
-	/* Create a new tree node pointing to the given value */
-	tree_node_t * out = malloc(sizeof(tree_node_t));
-	out->value = value;
-	out->children = list_create();
-	out->parent = NULL;
-	return out;
-}
-
-void tree_node_insert_child_node(tree_t * tree, tree_node_t * parent, tree_node_t * node) {
-	/* Insert a node as a child of parent */
-	list_insert(parent->children, node);
-	node->parent = parent;
-	tree->nodes++;
-}
-
-tree_node_t * tree_node_insert_child(tree_t * tree, tree_node_t * parent, void * value) {
-	/* Insert a (fresh) node as a child of parent */
-	tree_node_t * out = tree_node_create(value);
-	tree_node_insert_child_node(tree, parent, out);
-	return out;
-}
-
-tree_node_t * tree_node_find_parent(tree_node_t * haystack, tree_node_t * needle) {
-	/* Recursive node part of tree_find_parent */
-	tree_node_t * found = NULL;
-	foreach(child, haystack->children) {
-		if (child->value == needle) {
-			return haystack;
-		}
-		found = tree_node_find_parent((tree_node_t *)child->value, needle);
-		if (found) {
-			break;
-		}
-	}
-	return found;
-}
-
-tree_node_t * tree_find_parent(tree_t * tree, tree_node_t * node) {
-	/* Return the parent of a node, inefficiently. */
-	if (!tree->root) return NULL;
-	return tree_node_find_parent(tree->root, node);
-}
-
-size_t tree_count_children(tree_node_t * node) {
-	/* return the number of children this node has */
-	if (!node) return 0;
-	if (!node->children) return 0;
-	size_t out = node->children->length;
-	foreach(child, node->children) {
-		out += tree_count_children((tree_node_t *)child->value);
-	}
-	return out;
-}
-
-void tree_node_parent_remove(tree_t * tree, tree_node_t * parent, tree_node_t * node) {
-	/* remove a node when we know its parent; update node counts for the tree */
-	tree->nodes -= tree_count_children(node) + 1;
-	list_delete(parent->children, list_find(parent->children, node));
-	tree_node_free(node);
-}
-
-void tree_node_remove(tree_t * tree, tree_node_t * node) {
-	/* remove an entire branch given its root */
-	tree_node_t * parent = node->parent;
-	if (!parent) {
-		if (node == tree->root) {
-			tree->nodes = 0;
-			tree->root  = NULL;
-			tree_node_free(node);
-		}
-	}
-	tree_node_parent_remove(tree, parent, node);
-}
-
-void tree_remove(tree_t * tree, tree_node_t * node) {
-	/* Remove this node and move its children into its parent's list of children */
-	tree_node_t * parent = node->parent;
-	/* This is something we just can't do. We don't know how to merge our
-	 * children into our "parent" because then we'd have more than one root node.
-	 * A good way to think about this is actually what this tree struct
-	 * primarily exists for: processes. Trying to remove the root is equivalent
-	 * to trying to kill init! Which is bad. We immediately fault on such
-	 * a case anyway ("Tried to kill init, shutting down!").
-	 */
-	if (!parent) return;
-	tree->nodes--;
-	list_delete(parent->children, list_find(parent->children, node));
-	foreach(child, node->children) {
-		/* Reassign the parents */
-		((tree_node_t *)child->value)->parent = parent;
-	}
-	list_merge(parent->children, node->children);
-	free(node);
-}
-
-void tree_remove_reparent_root(tree_t * tree, tree_node_t * node) {
-	/* Remove this node and move its children into the root children */
-	tree_node_t * parent = node->parent;
-	if (!parent) return;
-	tree->nodes--;
-	list_delete(parent->children, list_find(parent->children, node));
-	foreach(child, node->children) {
-		/* Reassign the parents */
-		((tree_node_t *)child->value)->parent = tree->root;
-	}
-	list_merge(tree->root->children, node->children);
-	free(node);
-}
-
-void tree_break_off(tree_t * tree, tree_node_t * node) {
-	tree_node_t * parent = node->parent;
-	if (!parent) return;
-	list_delete(parent->children, list_find(parent->children, node));
-}
-
-tree_node_t * tree_node_find(tree_node_t * node, void * search, tree_comparator_t comparator) {
-	if (comparator(node->value,search)) {
-		return node;
-	}
-	tree_node_t * found;
-	foreach(child, node->children) {
-		found = tree_node_find((tree_node_t *)child->value, search, comparator);
-		if (found) return found;
-	}
-	return NULL;
-}
-
-tree_node_t * tree_find(tree_t * tree, void * value, tree_comparator_t comparator) {
-	return tree_node_find(tree->root, value, comparator);
-}

+ 1 - 0
kernel/ds/tree.c

@@ -0,0 +1 @@
+../../lib/tree.c

+ 3 - 2
kernel/fs/vfs.c

@@ -11,10 +11,11 @@
 #include <kernel/system.h>
 #include <kernel/fs.h>
 #include <kernel/printf.h>
-#include <kernel/list.h>
 #include <kernel/process.h>
 #include <kernel/logging.h>
-#include <kernel/hashmap.h>
+
+#include <toaru/list.h>
+#include <toaru/hashmap.h>
 
 #define MAX_SYMLINK_DEPTH 8
 #define MAX_SYMLINK_SIZE 4096

+ 2 - 1
kernel/mem/mem.c

@@ -12,9 +12,10 @@
 #include <kernel/process.h>
 #include <kernel/logging.h>
 #include <kernel/signal.h>
-#include <kernel/hashmap.h>
 #include <kernel/module.h>
 
+#include <toaru/hashmap.h>
+
 #define KERNEL_HEAP_INIT 0x00800000
 #define KERNEL_HEAP_END  0x20000000
 

+ 3 - 2
kernel/mem/shm.c

@@ -11,8 +11,9 @@
 #include <kernel/logging.h>
 #include <kernel/shm.h>
 #include <kernel/mem.h>
-#include <kernel/tree.h>
-#include <kernel/list.h>
+
+#include <toaru/tree.h>
+#include <toaru/list.h>
 
 
 //static volatile uint8_t bsl; // big shm lock

+ 2 - 1
kernel/misc/args.c

@@ -14,9 +14,10 @@
 #include <kernel/system.h>
 #include <kernel/logging.h>
 #include <kernel/args.h>
-#include <kernel/hashmap.h>
 #include <kernel/tokenize.h>
 
+#include <toaru/hashmap.h>
+
 char * cmdline = NULL;
 
 hashmap_t * kernel_args_map = NULL;

+ 2 - 1
kernel/misc/logging.c

@@ -10,11 +10,12 @@
  */
 
 #include <kernel/system.h>
-#include <kernel/list.h>
 #include <kernel/logging.h>
 #include <kernel/va_list.h>
 #include <kernel/printf.h>
 
+#include <toaru/list.h>
+
 log_type_t debug_level = NOTICE;
 void * debug_file = NULL;
 void (*debug_hook)(void *, char *) = NULL;

+ 2 - 1
kernel/sys/module.c

@@ -6,10 +6,11 @@
 #include <kernel/system.h>
 #include <kernel/logging.h>
 #include <kernel/fs.h>
-#include <kernel/hashmap.h>
 #include <kernel/elf.h>
 #include <kernel/module.h>
 
+#include <toaru/hashmap.h>
+
 #define SYMBOLTABLE_HASHMAP_SIZE 10
 #define MODULE_HASHMAP_SIZE 10
 

+ 3 - 2
kernel/sys/process.c

@@ -12,13 +12,14 @@
  */
 #include <kernel/system.h>
 #include <kernel/process.h>
-#include <kernel/tree.h>
-#include <kernel/list.h>
 #include <kernel/bitset.h>
 #include <kernel/logging.h>
 #include <kernel/shm.h>
 #include <kernel/printf.h>
 
+#include <toaru/list.h>
+#include <toaru/tree.h>
+
 tree_t * process_tree;  /* Parent->Children tree */
 list_t * process_list;  /* Flat storage */
 list_t * process_queue; /* Ready queue */

+ 1 - 0
lib/hashmap.c

@@ -3,6 +3,7 @@
  * of the NCSA / University of Illinois License - see LICENSE.md
  * Copyright (C) 2013-2014 Kevin Lange
  */
+
 #include <toaru/list.h>
 #include <toaru/hashmap.h>
 

+ 7 - 1
lib/list.c

@@ -6,7 +6,13 @@
  * General-purpose list implementations.
  */
 
-#include <string.h>
+#ifdef _KERNEL_
+#	include <kernel/system.h>
+#else
+#	include <stddef.h>
+#	include <stdlib.h>
+#endif
+
 #include <toaru/list.h>
 
 void list_destroy(list_t * list) {

+ 2 - 2
lib/tree.c

@@ -7,14 +7,14 @@
  */
 
 #ifdef _KERNEL_
-#	include <kernel/tree.h>
 #	include <kernel/system.h>
 #else
 #	include <stddef.h>
 #	include <stdlib.h>
-#	include <toaru/tree.h>
 #endif
 
+#include <toaru/tree.h>
+
 tree_t * tree_create(void) {
 	/* Create a new tree */
 	tree_t * out = malloc(sizeof(tree_t));

+ 2 - 1
modules/ata.c

@@ -13,12 +13,13 @@
 #include <kernel/module.h>
 #include <kernel/fs.h>
 #include <kernel/printf.h>
-#include <kernel/list.h>
 #include <kernel/pci.h>
 
 /* TODO: Move this to mod/ata.h */
 #include <kernel/ata.h>
 
+#include <toaru/list.h>
+
 static char ata_drive_char = 'a';
 static int  cdrom_number = 0;
 static uint32_t ata_pci = 0x00000000;

+ 3 - 1
modules/debug_shell.c

@@ -12,7 +12,6 @@
 #include <kernel/process.h>
 #include <kernel/version.h>
 #include <kernel/tokenize.h>
-#include <kernel/hashmap.h>
 #include <kernel/pci.h>
 #include <kernel/pipe.h>
 #include <kernel/elf.h>
@@ -20,6 +19,9 @@
 #include <kernel/args.h>
 #include <kernel/mod/shell.h>
 
+#include <toaru/list.h>
+#include <toaru/hashmap.h>
+
 #include <sys/termios.h>
 
 /*

+ 2 - 1
modules/e1000.c

@@ -8,11 +8,12 @@
 #include <kernel/printf.h>
 #include <kernel/pci.h>
 #include <kernel/mem.h>
-#include <kernel/list.h>
 #include <kernel/pipe.h>
 #include <kernel/ipv4.h>
 #include <kernel/mod/net.h>
 
+#include <toaru/list.h>
+
 static uint32_t e1000_device_pci = 0x00000000;
 static int e1000_irq = 0;
 static uintptr_t mem_base = 0;

+ 3 - 2
modules/iso9660.c

@@ -12,10 +12,11 @@
 #include <kernel/module.h>
 #include <kernel/args.h>
 #include <kernel/printf.h>
-#include <kernel/hashmap.h>
-#include <kernel/list.h>
 #include <kernel/tokenize.h>
 
+#include <toaru/list.h>
+#include <toaru/hashmap.h>
+
 #define ISO_SECTOR_SIZE 2048
 
 #define FLAG_HIDDEN      0x01

+ 3 - 1
modules/net.c

@@ -5,12 +5,14 @@
  */
 #include <kernel/module.h>
 #include <kernel/logging.h>
-#include <kernel/hashmap.h>
 #include <kernel/ipv4.h>
 #include <kernel/printf.h>
 #include <kernel/tokenize.h>
 #include <kernel/mod/net.h>
 
+#include <toaru/list.h>
+#include <toaru/hashmap.h>
+
 static hashmap_t * dns_cache;
 static list_t * dns_waiters = NULL;
 static uint32_t _dns_server;

+ 2 - 1
modules/pcnet.c

@@ -8,11 +8,12 @@
 #include <kernel/printf.h>
 #include <kernel/pci.h>
 #include <kernel/mem.h>
-#include <kernel/list.h>
 #include <kernel/pipe.h>
 #include <kernel/ipv4.h>
 #include <kernel/mod/net.h>
 
+#include <toaru/list.h>
+
 static list_t * net_queue = NULL;
 static spin_lock_t net_queue_lock = { 0 };
 static list_t * rx_wait;

+ 2 - 1
modules/rtl.c

@@ -8,11 +8,12 @@
 #include <kernel/printf.h>
 #include <kernel/pci.h>
 #include <kernel/mem.h>
-#include <kernel/list.h>
 #include <kernel/pipe.h>
 #include <kernel/ipv4.h>
 #include <kernel/mod/net.h>
 
+#include <toaru/list.h>
+
 /* XXX move this to ipv4? */
 extern size_t print_dns_name(fs_node_t * tty, struct dns_packet * dns, size_t offset);
 

+ 1 - 1
modules/snd.c

@@ -11,12 +11,12 @@
  * really support multiple devices despite the interface suggesting it might...
  */
 
-#include <kernel/list.h>
 #include <kernel/module.h>
 #include <kernel/ringbuffer.h>
 #include <kernel/system.h>
 #include <kernel/mod/snd.h>
 
+#include <toaru/list.h>
 #include <errno.h>
 
 /* Utility macros */

+ 2 - 1
modules/test.c

@@ -4,12 +4,13 @@
  * Copyright (C) 2014 Kevin Lange
  */
 #include <kernel/system.h>
-#include <kernel/hashmap.h>
 #include <kernel/module.h>
 #include <kernel/logging.h>
 #include <kernel/printf.h>
 #include <kernel/mod/shell.h>
 
+#include <toaru/hashmap.h>
+
 extern char * special_thing;
 
 char test_module_string[] = "I am a char[] in the module.";

+ 0 - 1
modules/testb.c

@@ -4,7 +4,6 @@
  * Copyright (C) 2014 Kevin Lange
  */
 #include <kernel/system.h>
-#include <kernel/hashmap.h>
 #include <kernel/module.h>
 #include <kernel/logging.h>
 

+ 0 - 1
modules/vidset.c

@@ -6,7 +6,6 @@
  * Module to provide a debug shell command to set display mode.
  */
 #include <kernel/system.h>
-#include <kernel/hashmap.h>
 #include <kernel/module.h>
 #include <kernel/logging.h>
 #include <kernel/printf.h>