lib: add rbtree data structure
This also switches the kmalloc allocator over to the new rbtree structure. As a balanced binary search tree, it is much more efficient than the previous method
This commit is contained in:
parent
d3e9b728dc
commit
4e36cfd90b
6 changed files with 430 additions and 42 deletions
2
Makefile
2
Makefile
|
|
@ -16,7 +16,7 @@ define DEFAULT_VAR =
|
|||
endef
|
||||
|
||||
# User controllable CFLAGS.
|
||||
CFLAGS ?= -g -O2 -pipe -Wall -Wextra
|
||||
CFLAGS ?= -g -O0 -pipe -Wall -Wextra
|
||||
|
||||
# User controllable linker flags. We set none by default.
|
||||
LDFLAGS ?=
|
||||
|
|
|
|||
33
include/kernel/rbtree.h
Normal file
33
include/kernel/rbtree.h
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _RBTREE_H_
|
||||
#define _RBTREE_H_
|
||||
|
||||
#include <kernel/common.h>
|
||||
|
||||
#define RB_RED 0
|
||||
#define RB_BLACK 1
|
||||
|
||||
struct rbnode {
|
||||
struct rbnode *parent;
|
||||
struct rbnode *left;
|
||||
struct rbnode *right;
|
||||
int color;
|
||||
|
||||
uint64_t key;
|
||||
uint64_t value; /* extra value */
|
||||
};
|
||||
|
||||
/* we use this rbtree structure because the root node will potentially be
|
||||
* changed when we insert or delete a node
|
||||
*/
|
||||
struct rbtree {
|
||||
struct rbnode *root;
|
||||
};
|
||||
|
||||
struct rbnode *rbt_insert(struct rbtree *tree, uint64_t key);
|
||||
void rbt_delete(struct rbtree *tree, struct rbnode *del);
|
||||
struct rbnode *rbt_search(struct rbtree *tree, uint64_t key);
|
||||
bool rbt_verify(struct rbtree *tree);
|
||||
void rbt_slab_init();
|
||||
|
||||
#endif /* _RBTREE_H_ */
|
||||
|
|
@ -39,4 +39,6 @@ void *kmalloc(size_t size, uint64_t flags);
|
|||
void *kzalloc(size_t size, uint64_t flags);
|
||||
void kfree(void *ptr);
|
||||
|
||||
void slabtypes_init();
|
||||
|
||||
#endif /* _SLAB_H_ */
|
||||
|
|
|
|||
|
|
@ -68,7 +68,8 @@ void kmain(void)
|
|||
const uintptr_t one_gb = 0x40000000;
|
||||
char kmem[one_gb] ALIGN(0x1000);
|
||||
mem_early_init(kmem, one_gb);
|
||||
kmalloc_init();
|
||||
|
||||
slabtypes_init();
|
||||
|
||||
struct limine_kernel_file_response *resp = module_req.response;
|
||||
|
||||
|
|
|
|||
345
lib/rbtree.c
Normal file
345
lib/rbtree.c
Normal file
|
|
@ -0,0 +1,345 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/mem.h>
|
||||
#include <kernel/slab.h>
|
||||
#include <kernel/rbtree.h>
|
||||
|
||||
static struct slab *rbt_slab;
|
||||
|
||||
void rbt_slab_init()
|
||||
{
|
||||
rbt_slab = slab_create(sizeof(struct rbnode), 1 * MB, 0);
|
||||
}
|
||||
|
||||
static void rbt_rotate_right(struct rbtree *tree, struct rbnode *node)
|
||||
{
|
||||
struct rbnode *left = node->left;
|
||||
|
||||
node->left = left->right;
|
||||
if (left->right != NULL)
|
||||
left->right->parent = node;
|
||||
|
||||
left->parent = node->parent;
|
||||
|
||||
if (node->parent == NULL) {
|
||||
tree->root = left;
|
||||
} else if (node == node->parent->right) {
|
||||
node->parent->right = left;
|
||||
} else {
|
||||
node->parent->left = left;
|
||||
}
|
||||
|
||||
left->right = node;
|
||||
node->parent = left;
|
||||
}
|
||||
|
||||
static void rbt_rotate_left(struct rbtree *tree, struct rbnode *node)
|
||||
{
|
||||
struct rbnode *right = node->right;
|
||||
|
||||
node->right = right->left;
|
||||
if (right->left != NULL)
|
||||
right->left->parent = node;
|
||||
|
||||
right->parent = node->parent;
|
||||
|
||||
if (node->parent == NULL) {
|
||||
tree->root = right;
|
||||
} else if (node == node->parent->left) {
|
||||
node->parent->left = right;
|
||||
} else {
|
||||
node->parent->right = right;
|
||||
}
|
||||
|
||||
right->left = node;
|
||||
node->parent = right;
|
||||
}
|
||||
|
||||
static struct rbnode *rbt_uncle(struct rbnode *node)
|
||||
{
|
||||
if (node->parent == NULL || node->parent->parent == NULL)
|
||||
return NULL;
|
||||
|
||||
if (node->parent == node->parent->parent->left)
|
||||
return node->parent->parent->right;
|
||||
else
|
||||
return node->parent->parent->left;
|
||||
}
|
||||
|
||||
static void rbt_insert_fixup(struct rbtree *tree, struct rbnode *node)
|
||||
{
|
||||
/* Case 0: node is root */
|
||||
if ((uintptr_t)tree == (uintptr_t)node || node->parent == NULL) {
|
||||
node->color = RB_BLACK;
|
||||
return;
|
||||
}
|
||||
|
||||
while (node->parent && node->parent->color == RB_RED) {
|
||||
/* Case 1: node's uncle is red */
|
||||
struct rbnode *uncle_node = rbt_uncle(node);
|
||||
|
||||
/* NULL nodes are black */
|
||||
if (uncle_node && uncle_node->color == RB_RED) {
|
||||
uncle_node->color = RB_BLACK;
|
||||
node->parent->color = RB_BLACK;
|
||||
node->parent->parent->color = RB_RED;
|
||||
node = node->parent->parent;
|
||||
}
|
||||
|
||||
/* required for further cases */
|
||||
if (!node->parent || !node->parent->parent)
|
||||
return;
|
||||
|
||||
/* Case 2: node's uncle is black and node is a right child of a parent
|
||||
* that is a left child, or vice versa
|
||||
*
|
||||
* This will also set the node equal to its former parent (new child) to
|
||||
* fallthrough to case 3
|
||||
*/
|
||||
if (node == node->parent->right && node->parent == node->parent->parent->left) {
|
||||
rbt_rotate_left(tree, node->parent);
|
||||
node = node->left;
|
||||
} else if (node == node->parent->left && node->parent == node->parent->parent->right) {
|
||||
rbt_rotate_right(tree, node->parent);
|
||||
node = node->right;
|
||||
}
|
||||
|
||||
/* Case 3: Node's uncle is black and node, parent and grandparent form
|
||||
* a line
|
||||
*/
|
||||
|
||||
/* check again because case 2 might have changed the parent */
|
||||
if (!node->parent || !node->parent->parent)
|
||||
return;
|
||||
|
||||
if (node == node->parent->left && node->parent == node->parent->parent->left) {
|
||||
node->parent->color = RB_BLACK;
|
||||
node->parent->parent->color = RB_RED;
|
||||
rbt_rotate_right(tree, node->parent->parent);
|
||||
} else if (node == node->parent->right && node->parent == node->parent->parent->right) {
|
||||
node->parent->color = RB_BLACK;
|
||||
node->parent->parent->color = RB_RED;
|
||||
rbt_rotate_left(tree, node->parent->parent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct rbnode *rbt_insert(struct rbtree *tree, uint64_t key)
|
||||
{
|
||||
struct rbnode *node = tree->root;
|
||||
struct rbnode *parent = NULL;
|
||||
|
||||
while (node != NULL) {
|
||||
parent = node;
|
||||
if (key < node->key) {
|
||||
node = node->left;
|
||||
} else if (key > node->key) {
|
||||
node = node->right;
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
struct rbnode *new = slab_alloc(rbt_slab);
|
||||
if (new == NULL)
|
||||
return NULL;
|
||||
|
||||
new->key = key;
|
||||
new->parent = parent;
|
||||
new->left = NULL;
|
||||
new->right = NULL;
|
||||
new->color = RB_RED;
|
||||
|
||||
if (parent == NULL) {
|
||||
tree->root = new;
|
||||
} else if (key < parent->key) {
|
||||
parent->left = new;
|
||||
} else {
|
||||
parent->right = new;
|
||||
}
|
||||
|
||||
rbt_insert_fixup(tree, new);
|
||||
return new;
|
||||
}
|
||||
|
||||
static void rbt_transplant(struct rbtree *tree, struct rbnode *old, struct rbnode *new)
|
||||
{
|
||||
if (old->parent == NULL) {
|
||||
tree->root = new;
|
||||
} else if (old == old->parent->left) {
|
||||
old->parent->left = new;
|
||||
} else {
|
||||
old->parent->right = new;
|
||||
}
|
||||
|
||||
if (new != NULL) {
|
||||
new->parent = old->parent;
|
||||
}
|
||||
}
|
||||
|
||||
static void rbt_delete_fixup(struct rbtree *tree, struct rbnode *node)
|
||||
{
|
||||
if (!node)
|
||||
return;
|
||||
|
||||
struct rbnode *sibling;
|
||||
while (node != tree->root && node->color == RB_BLACK) {
|
||||
if (node == node->parent->left) {
|
||||
sibling = node->parent->right;
|
||||
|
||||
if (!sibling || !sibling->left || !sibling->right) {
|
||||
node = node->parent;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (sibling && sibling->color == RB_RED) {
|
||||
sibling->color = RB_BLACK;
|
||||
node->parent->color = RB_RED;
|
||||
rbt_rotate_left(tree, node->parent);
|
||||
sibling = node->parent->right;
|
||||
}
|
||||
|
||||
if (sibling->left->color == RB_BLACK && sibling->right->color == RB_BLACK) {
|
||||
sibling->color = RB_RED;
|
||||
node = node->parent;
|
||||
continue;
|
||||
} else {
|
||||
if (sibling->right->color == RB_BLACK) {
|
||||
sibling->left->color = RB_BLACK;
|
||||
sibling->color = RB_RED;
|
||||
rbt_rotate_right(tree, sibling);
|
||||
sibling = node->parent->right;
|
||||
}
|
||||
|
||||
sibling->color = node->parent->color;
|
||||
node->parent->color = RB_BLACK;
|
||||
sibling->right->color = RB_BLACK;
|
||||
rbt_rotate_left(tree, node->parent);
|
||||
node = tree->root;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
node->color = RB_BLACK;
|
||||
}
|
||||
|
||||
static struct rbnode *rbt_minimum(struct rbnode *node)
|
||||
{
|
||||
/* find the leftmost node */
|
||||
while (node->left != NULL)
|
||||
node = node->left;
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
void rbt_delete(struct rbtree *tree, struct rbnode *del)
|
||||
{
|
||||
/* case 0: left is NULL */
|
||||
if (!del->left) {
|
||||
rbt_transplant(tree, del, del->right);
|
||||
rbt_delete_fixup(tree, del->right);
|
||||
slab_free(rbt_slab, del);
|
||||
|
||||
}
|
||||
|
||||
/* case 1: right is NULL */
|
||||
else if (!del->right) {
|
||||
rbt_transplant(tree, del, del->left);
|
||||
rbt_delete_fixup(tree, del->left);
|
||||
slab_free(rbt_slab, del);
|
||||
}
|
||||
|
||||
/* case 2: neither is NULL */
|
||||
else {
|
||||
struct rbnode *y = rbt_minimum(del->right);
|
||||
struct rbnode *x = y->right;
|
||||
int orig_color = y->color;
|
||||
|
||||
if (y->parent == del) {
|
||||
if (x)
|
||||
x->parent = y;
|
||||
} else {
|
||||
rbt_transplant(tree, y, y->right);
|
||||
y->right = del->right;
|
||||
if (y->right)
|
||||
y->right->parent = y;
|
||||
}
|
||||
|
||||
rbt_transplant(tree, del, y);
|
||||
y->left = del->left;
|
||||
if (y->left)
|
||||
y->left->parent = y;
|
||||
y->color = del->color;
|
||||
|
||||
if (orig_color == RB_BLACK)
|
||||
rbt_delete_fixup(tree, x);
|
||||
}
|
||||
}
|
||||
|
||||
struct rbnode *rbt_search(struct rbtree *tree, uint64_t key)
|
||||
{
|
||||
struct rbnode *node = tree->root;
|
||||
|
||||
while (node != NULL) {
|
||||
if (key < node->key) {
|
||||
node = node->left;
|
||||
} else if (key > node->key) {
|
||||
node = node->right;
|
||||
} else {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int rbt_black_height(struct rbtree *tree)
|
||||
{
|
||||
struct rbnode *node = tree->root;
|
||||
int black_count = 0;
|
||||
|
||||
while (node != NULL) {
|
||||
if (node->color == RB_BLACK)
|
||||
black_count++;
|
||||
|
||||
node = node->left;
|
||||
}
|
||||
|
||||
return black_count;
|
||||
}
|
||||
|
||||
/* verify that the tree is a valid red-black tree */
|
||||
bool rbt_verify(struct rbtree *tree)
|
||||
{
|
||||
struct rbnode *node = tree->root;
|
||||
struct rbnode *prev = NULL;
|
||||
int black_count = 0;
|
||||
|
||||
while (node != NULL) {
|
||||
if (node->left != NULL && node->left->parent != node)
|
||||
return false;
|
||||
|
||||
if (node->right != NULL && node->right->parent != node)
|
||||
return false;
|
||||
|
||||
if (node->color == RB_BLACK)
|
||||
black_count++;
|
||||
|
||||
if (node->left != NULL && node->left->key > node->key)
|
||||
return false;
|
||||
|
||||
if (node->right != NULL && node->right->key < node->key)
|
||||
return false;
|
||||
|
||||
prev = node;
|
||||
node = node->left;
|
||||
}
|
||||
|
||||
if (prev != NULL && prev != rbt_minimum(tree->root))
|
||||
return false;
|
||||
|
||||
if (black_count != rbt_black_height(tree))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
89
mem/slab.c
89
mem/slab.c
|
|
@ -1,6 +1,7 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/mem.h>
|
||||
#include <kernel/rbtree.h>
|
||||
#include <kernel/slab.h>
|
||||
|
||||
#define SLAB_ALIGN 7
|
||||
|
|
@ -10,6 +11,8 @@ static struct slab *slab_cache[ARRAY_SIZE(slab_sizes)];
|
|||
static size_t dma_sizes[] = { 0x1000, 0x10000, 0x100000 };
|
||||
static struct slab *slab_cache_dma[ARRAY_SIZE(slab_sizes)];
|
||||
|
||||
static struct rbtree kmalloc_tree = { NULL };
|
||||
|
||||
static inline void slab_range(struct slab *slab, uintptr_t *o_start, uintptr_t *o_end)
|
||||
{
|
||||
uintptr_t sslab = (uintptr_t)slab;
|
||||
|
|
@ -135,7 +138,7 @@ void slab_free(struct slab *slab, void *ptr)
|
|||
void kmalloc_init()
|
||||
{
|
||||
for (size_t i = 0; i < ARRAY_SIZE(slab_sizes); i++)
|
||||
slab_cache[i] = slab_create(slab_sizes[i], 128 * KB,0);
|
||||
slab_cache[i] = slab_create(slab_sizes[i], 128 * KB, 0);
|
||||
for (size_t i = 0; i < ARRAY_SIZE(dma_sizes); i++)
|
||||
slab_cache_dma[i] = slab_create(dma_sizes[i], 8 * MB, SLAB_DMA_64K);
|
||||
}
|
||||
|
|
@ -143,19 +146,34 @@ void kmalloc_init()
|
|||
static void *kmalloc_table(size_t size, size_t *sizes, size_t nsizes, struct slab **cache, bool fb_disable)
|
||||
{
|
||||
struct slab *slab = NULL;
|
||||
void *ret = NULL;
|
||||
if (size > sizes[nsizes - 1] && !fb_disable) {
|
||||
return buddy_alloc(npow2(size));
|
||||
ret = buddy_alloc(npow2(size));
|
||||
} else {
|
||||
for (size_t i = 0; i < nsizes; i++) {
|
||||
if (size <= sizes[i]) {
|
||||
size = sizes[i];
|
||||
slab = cache[i];
|
||||
return slab_alloc(slab);
|
||||
ret = slab_alloc(slab);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
if (ret == NULL)
|
||||
return NULL;
|
||||
|
||||
struct rbnode *n = rbt_insert(&kmalloc_tree, (uint64_t)ret);
|
||||
if (n == NULL) {
|
||||
if (slab != NULL)
|
||||
slab_free(slab, ret);
|
||||
else
|
||||
buddy_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
n->value = (uint64_t)slab;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void *kmalloc(size_t size, uint64_t flags)
|
||||
|
|
@ -168,9 +186,6 @@ void *kmalloc(size_t size, uint64_t flags)
|
|||
ret = kmalloc_table(size, slab_sizes, ARRAY_SIZE(slab_sizes), slab_cache, false);
|
||||
}
|
||||
|
||||
if (ret == NULL)
|
||||
return NULL;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
@ -184,45 +199,37 @@ void *kzalloc(size_t size, uint64_t flags)
|
|||
return ret;
|
||||
}
|
||||
|
||||
static bool kfree_helper(void *ptr, struct slab *slab)
|
||||
{
|
||||
while (slab != NULL) {
|
||||
uintptr_t low = 0;
|
||||
uintptr_t high = 0;
|
||||
|
||||
slab_range(slab, &low, &high);
|
||||
if ((uintptr_t)ptr >= low && (uintptr_t)ptr <= high) {
|
||||
slab_free(slab, ptr);
|
||||
return true;
|
||||
}
|
||||
slab = slab->next;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void kfree(void *ptr)
|
||||
{
|
||||
if (ptr == NULL)
|
||||
return;
|
||||
|
||||
size_t nslabsizes = ARRAY_SIZE(slab_sizes);
|
||||
size_t ndmasizes = ARRAY_SIZE(dma_sizes);
|
||||
size_t max = MAX(nslabsizes, ndmasizes);
|
||||
for (size_t i = 0; i < max; i++) {
|
||||
if (i < nslabsizes) {
|
||||
struct slab *slab = slab_cache[i];
|
||||
if (kfree_helper(ptr, slab))
|
||||
return;
|
||||
}
|
||||
struct rbnode *n = rbt_search(&kmalloc_tree, (uint64_t)ptr);
|
||||
if (n == NULL)
|
||||
return;
|
||||
|
||||
if (i < ndmasizes) {
|
||||
struct slab *slab = slab_cache_dma[i];
|
||||
if (kfree_helper(ptr, slab))
|
||||
return;
|
||||
}
|
||||
}
|
||||
struct slab *slab = (struct slab *)n->value;
|
||||
if (slab != NULL)
|
||||
slab_free(slab, ptr);
|
||||
else
|
||||
buddy_free(ptr);
|
||||
|
||||
/* if we get here, it's a buddy allocation */
|
||||
buddy_free(ptr);
|
||||
rbt_delete(&kmalloc_tree, n);
|
||||
}
|
||||
|
||||
void slabtypes_init()
|
||||
{
|
||||
rbt_slab_init();
|
||||
|
||||
/* LAST! */
|
||||
kmalloc_init();
|
||||
}
|
||||
|
||||
void malloc_post()
|
||||
{
|
||||
bool verify = rbt_verify(&kmalloc_tree);
|
||||
if(!verify)
|
||||
printf(LOG_ERROR, "kmalloc tree is corrupted!\n");
|
||||
else
|
||||
printf(LOG_SUCCESS "kmalloc tree is OK!\n");
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue