-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbthash.c
68 lines (59 loc) · 1.31 KB
/
rbthash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// SPDX-License-Identifier: GPL-2.0
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/err.h>
#include <linux/scatterlist.h>
#include <linux/rbtree.h>
#include <linux/slab.h>
#include "ouichefs.h"
struct rbt_node *hb_search(struct rb_root *tree, struct hash_sha256 *hash)
{
struct rbt_node *data;
struct rb_node *node = tree->rb_node;
while (node) {
// container_of : ptr, type, member
data = container_of(node, struct rbt_node, node);
switch (hash_cmp(hash, &data->hash)) {
case -1:
node = node->rb_left;
break;
case 1:
node = node->rb_right;
break;
default:
return data;
}
}
return NULL;
}
int hb_insert(struct rb_root *tree, struct rbt_node *data)
{
struct rb_node **new = &(tree->rb_node);
struct rb_node *parent = NULL;
while (*new) {
struct rbt_node *this = container_of(*new, struct rbt_node, node);
switch (hash_cmp(&data->hash, &this->hash)) {
case -1:
new = &((*new)->rb_left);
break;
case 1:
new = &((*new)->rb_right);
break;
default:
return 0;
}
}
rb_link_node(&data->node, parent, new);
rb_insert_color(&data->node, tree);
return 1;
}
void hb_free(struct rb_root *tree)
{
struct rbt_node *pos;
struct rbt_node *n;
rbtree_postorder_for_each_entry_safe(pos, n, tree, node) {
rb_erase(&pos->node, tree);
kfree(pos);
}
kfree(tree);
}