ktree.h
3 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* ktree.h - Some generic list helpers, extending struct list_head a bit.
*
* Implementations are found in lib/ktree.c
*
*
* Copyright (C) 2005 Patrick Mochel
*
* This file is rleased under the GPL v2.
*/
#ifndef _LINUX_KTREE_H
#define _LINUX_KTREE_H
#include <linux/spinlock.h>
#include <linux/kref.h>
#include <linux/list.h>
struct ktree_node;
struct ktree {
spinlock_t lock; /* protects shared access to tree */
struct ktree_node *root;
void (*get)(struct ktree_node *);
void (*put)(struct ktree_node *);
};
#define KTREE_INIT(_name, _get, _put) \
{ .lock = __SPIN_LOCK_UNLOCKED(_name.lock), \
.get = _get, \
.put = _put, }
#define DEFINE_KTREE(_name, _get, _put) \
struct ktree _name = KTREE_INIT(_name, _get, _put)
void ktree_init(struct ktree *ktree, void (*get)(struct ktree_node *),
void (*put)(struct ktree_node *));
struct ktree_node {
struct ktree *ktree;
bool deleted;
struct ktree_node *parent;
struct list_head siblings;
struct list_head children;
struct kref refcount;
};
void ktree_set_root(struct ktree *ktree, struct ktree_node *root);
static inline struct ktree_node *ktree_get_node(struct ktree_node *node)
{
if (node)
kref_get(&node->refcount);
return node;
}
static inline struct ktree_node *ktree_get_root(struct ktree *ktree)
{
return ktree_get_node(ktree->root);
}
static inline struct ktree_node *ktree_get_parent(struct ktree_node *node)
{
return ktree_get_node(node->parent);
}
void ktree_put_node(struct ktree_node *node);
void ktree_add_child_after(struct ktree_node *parent, struct ktree_node *child,
struct ktree_node *pos);
void ktree_add_child_before(struct ktree_node *parent, struct ktree_node *child,
struct ktree_node *pos);
static inline void ktree_add_child_first(struct ktree_node *parent,
struct ktree_node *child)
{
ktree_add_child_after(parent, child, NULL);
}
static inline void ktree_add_child_last(struct ktree_node *parent,
struct ktree_node *child)
{
ktree_add_child_before(parent, child, NULL);
}
static inline void ktree_add_sibling_after(struct ktree_node *child,
struct ktree_node *pos)
{
ktree_add_child_after(pos->parent, child, pos);
}
static inline void ktree_add_sibling_before(struct ktree_node *child,
struct ktree_node *pos)
{
ktree_add_child_before(pos->parent, child, pos);
}
void ktree_del_node(struct ktree_node *node);
void ktree_remove_node(struct ktree_node *node);
static inline void ktree_del_tree(struct ktree *ktree)
{
if (ktree->root)
ktree_del_node(ktree->root);
}
static inline void ktree_remove_tree(struct ktree *ktree)
{
if (ktree->root)
ktree_remove_node(ktree->root);
}
static inline bool ktree_is_leaf(struct ktree_node *node)
{
return list_empty(&node->children);
}
int ktree_for_each_child(struct ktree_node *parent,
int (*visitor)(struct ktree_node *child, void *arg),
void *arg);
void ktree_sort_children(struct ktree_node *parent,
int (*cmp)(struct ktree_node *a, struct ktree_node *b,
void *arg),
void *arg);
#endif