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
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_ */
|
||||
|
|
|
|||
Loading…
Reference in a new issue