git://scm.lunaixsky.com
/
lunaix-os.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
refactor: add a async read/write variant to device ops, with allow async io to be...
[lunaix-os.git]
/
lunaix-os
/
kernel
/
ds
/
btrie.c
diff --git
a/lunaix-os/kernel/ds/btrie.c
b/lunaix-os/kernel/ds/btrie.c
index 92f034bebce60979324d3bf992da019c16e090b4..781def41d1d979e1ddd61c81d7fa2d4eec9cb53a 100644
(file)
--- a/
lunaix-os/kernel/ds/btrie.c
+++ b/
lunaix-os/kernel/ds/btrie.c
@@
-16,16
+16,16
@@
#define BTRIE_INSERT 1
struct btrie_node*
#define BTRIE_INSERT 1
struct btrie_node*
-__btrie_traversal(struct btrie* root, u
int
32_t index, int options)
+__btrie_traversal(struct btrie* root, u32_t index, int options)
{
index = index >> root->truncated;
{
index = index >> root->truncated;
- u
int32_t lz = index ? ROUNDDOWN(31 - __builtin_
clz(index), BTRIE_BITS) : 0;
- u
int
32_t bitmask = ((1 << BTRIE_BITS) - 1) << lz;
- u
int
32_t i = 0;
+ u
32_t lz = index ? ROUNDDOWN(31 -
clz(index), BTRIE_BITS) : 0;
+ u32_t bitmask = ((1 << BTRIE_BITS) - 1) << lz;
+ u32_t i = 0;
struct btrie_node* tree = root->btrie_root;
// Time complexity: O(log_2(log_2(N))) where N is the index to lookup
struct btrie_node* tree = root->btrie_root;
// Time complexity: O(log_2(log_2(N))) where N is the index to lookup
- while (
lz
&& tree) {
+ while (
bitmask
&& tree) {
i = (index & bitmask) >> lz;
struct btrie_node *subtree = 0, *pos, *n;
i = (index & bitmask) >> lz;
struct btrie_node *subtree = 0, *pos, *n;
@@
-57,15
+57,16
@@
__btrie_traversal(struct btrie* root, uint32_t index, int options)
}
void
}
void
-btrie_init(struct btrie* btrie, u
int
32_t trunc_bits)
+btrie_init(struct btrie* btrie, u32_t trunc_bits)
{
btrie->btrie_root = vzalloc(sizeof(struct btrie_node));
llist_init_head(&btrie->btrie_root->nodes);
{
btrie->btrie_root = vzalloc(sizeof(struct btrie_node));
llist_init_head(&btrie->btrie_root->nodes);
+ llist_init_head(&btrie->btrie_root->children);
btrie->truncated = trunc_bits;
}
void*
btrie->truncated = trunc_bits;
}
void*
-btrie_get(struct btrie* root, u
int
32_t index)
+btrie_get(struct btrie* root, u32_t index)
{
struct btrie_node* node = __btrie_traversal(root, index, 0);
if (!node) {
{
struct btrie_node* node = __btrie_traversal(root, index, 0);
if (!node) {
@@
-75,35
+76,40
@@
btrie_get(struct btrie* root, uint32_t index)
}
void
}
void
-btrie_set(struct btrie* root, u
int
32_t index, void* data)
+btrie_set(struct btrie* root, u32_t index, void* data)
{
struct btrie_node* node = __btrie_traversal(root, index, BTRIE_INSERT);
node->data = data;
}
void
{
struct btrie_node* node = __btrie_traversal(root, index, BTRIE_INSERT);
node->data = data;
}
void
-__btrie_r
emo
ve(struct btrie_node* node)
+__btrie_r
m_recursi
ve(struct btrie_node* node)
{
struct btrie_node* parent = node->parent;
{
struct btrie_node* parent = node->parent;
+
if (parent) {
llist_delete(&node->siblings);
llist_delete(&node->nodes);
vfree(node);
if (parent) {
llist_delete(&node->siblings);
llist_delete(&node->nodes);
vfree(node);
- if (llist_empty(&parent->children)) {
- __btrie_r
emo
ve(parent);
+ if (llist_empty(&parent->children)
&& !parent->data
) {
+ __btrie_r
m_recursi
ve(parent);
}
}
}
void*
}
}
}
void*
-btrie_remove(struct btrie* root, u
int
32_t index)
+btrie_remove(struct btrie* root, u32_t index)
{
struct btrie_node* node = __btrie_traversal(root, index, 0);
if (!node) {
return 0;
}
void* data = node->data;
{
struct btrie_node* node = __btrie_traversal(root, index, 0);
if (!node) {
return 0;
}
void* data = node->data;
- __btrie_remove(node);
+ if (!llist_empty(&node->children)) {
+ node->data = NULL;
+ } else {
+ __btrie_rm_recursive(node);
+ }
return data;
}
return data;
}