~ubuntu-branches/debian/stretch/btrfs-tools/stretch

« back to all changes in this revision

Viewing changes to rbtree.h

  • Committer: Package Import Robot
  • Author(s): Dimitri John Ledkov
  • Date: 2014-10-23 22:04:07 UTC
  • mfrom: (1.2.15) (6.1.40 sid)
  • Revision ID: package-import@ubuntu.com-20141023220407-skt9hy0ft4oim95o
Tags: 3.17-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
  I know it's not the cleaner way,  but in C (not in C++) to get
24
24
  performances and genericity...
25
25
 
26
 
  Some example of insert and search follows here. The search is a plain
27
 
  normal search over an ordered tree. The insert instead must be implemented
28
 
  int two steps: as first thing the code must insert the element in
29
 
  order as a red leaf in the tree, then the support library function
30
 
  rb_insert_color() must be called. Such function will do the
31
 
  not trivial work to rebalance the rbtree if necessary.
32
 
 
33
 
-----------------------------------------------------------------------
34
 
static inline struct page * rb_search_page_cache(struct inode * inode,
35
 
                                                 unsigned long offset)
36
 
{
37
 
        struct rb_node * n = inode->i_rb_page_cache.rb_node;
38
 
        struct page * page;
39
 
 
40
 
        while (n)
41
 
        {
42
 
                page = rb_entry(n, struct page, rb_page_cache);
43
 
 
44
 
                if (offset < page->offset)
45
 
                        n = n->rb_left;
46
 
                else if (offset > page->offset)
47
 
                        n = n->rb_right;
48
 
                else
49
 
                        return page;
50
 
        }
51
 
        return NULL;
52
 
}
53
 
 
54
 
static inline struct page * __rb_insert_page_cache(struct inode * inode,
55
 
                                                   unsigned long offset,
56
 
                                                   struct rb_node * node)
57
 
{
58
 
        struct rb_node ** p = &inode->i_rb_page_cache.rb_node;
59
 
        struct rb_node * parent = NULL;
60
 
        struct page * page;
61
 
 
62
 
        while (*p)
63
 
        {
64
 
                parent = *p;
65
 
                page = rb_entry(parent, struct page, rb_page_cache);
66
 
 
67
 
                if (offset < page->offset)
68
 
                        p = &(*p)->rb_left;
69
 
                else if (offset > page->offset)
70
 
                        p = &(*p)->rb_right;
71
 
                else
72
 
                        return page;
73
 
        }
74
 
 
75
 
        rb_link_node(node, parent, p);
76
 
 
77
 
        return NULL;
78
 
}
79
 
 
80
 
static inline struct page * rb_insert_page_cache(struct inode * inode,
81
 
                                                 unsigned long offset,
82
 
                                                 struct rb_node * node)
83
 
{
84
 
        struct page * ret;
85
 
        if ((ret = __rb_insert_page_cache(inode, offset, node)))
86
 
                goto out;
87
 
        rb_insert_color(node, &inode->i_rb_page_cache);
88
 
 out:
89
 
        return ret;
90
 
}
91
 
-----------------------------------------------------------------------
 
26
  See Documentation/rbtree.txt for documentation and samples.
92
27
*/
93
28
 
94
29
#ifndef _LINUX_RBTREE_H
98
33
#else
99
34
#include <btrfs/kerncompat.h>
100
35
#endif /* BTRFS_FLAT_INCLUDES */
101
 
struct rb_node
102
 
{
103
 
        unsigned long  rb_parent_color;
104
 
#define RB_RED          0
105
 
#define RB_BLACK        1
 
36
 
 
37
struct rb_node {
 
38
        unsigned long  __rb_parent_color;
106
39
        struct rb_node *rb_right;
107
40
        struct rb_node *rb_left;
108
41
} __attribute__((aligned(sizeof(long))));
109
42
    /* The alignment might seem pointless, but allegedly CRIS needs it */
110
43
 
111
 
struct rb_root
112
 
{
 
44
struct rb_root {
113
45
        struct rb_node *rb_node;
114
46
};
115
47
 
116
 
#define rb_parent(r)   ((struct rb_node *)((r)->rb_parent_color & ~3))
117
 
#define rb_color(r)   ((r)->rb_parent_color & 1)
118
 
#define rb_is_red(r)   (!rb_color(r))
119
 
#define rb_is_black(r) rb_color(r)
120
 
#define rb_set_red(r)  do { (r)->rb_parent_color &= ~1; } while (0)
121
 
#define rb_set_black(r)  do { (r)->rb_parent_color |= 1; } while (0)
122
48
 
123
 
static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
124
 
{
125
 
        rb->rb_parent_color = (rb->rb_parent_color & 3) | (unsigned long)p;
126
 
}
127
 
static inline void rb_set_color(struct rb_node *rb, int color)
128
 
{
129
 
        rb->rb_parent_color = (rb->rb_parent_color & ~1) | color;
130
 
}
 
49
#define rb_parent(r)   ((struct rb_node *)((r)->__rb_parent_color & ~3))
131
50
 
132
51
#define RB_ROOT (struct rb_root) { NULL, }
133
52
#define rb_entry(ptr, type, member) container_of(ptr, type, member)
134
53
 
135
 
#define RB_EMPTY_ROOT(root)     ((root)->rb_node == NULL)
136
 
#define RB_EMPTY_NODE(node)     (rb_parent(node) == node)
137
 
#define RB_CLEAR_NODE(node)     (rb_set_parent(node, node))
 
54
#define RB_EMPTY_ROOT(root)  ((root)->rb_node == NULL)
 
55
 
 
56
/* 'empty' nodes are nodes that are known not to be inserted in an rbree */
 
57
#define RB_EMPTY_NODE(node)  \
 
58
        ((node)->__rb_parent_color == (unsigned long)(node))
 
59
#define RB_CLEAR_NODE(node)  \
 
60
        ((node)->__rb_parent_color = (unsigned long)(node))
 
61
 
138
62
 
139
63
extern void rb_insert_color(struct rb_node *, struct rb_root *);
140
64
extern void rb_erase(struct rb_node *, struct rb_root *);
141
65
 
 
66
 
142
67
/* Find logical next and previous nodes in a tree */
143
 
extern struct rb_node *rb_next(struct rb_node *);
144
 
extern struct rb_node *rb_prev(struct rb_node *);
145
 
extern struct rb_node *rb_first(struct rb_root *);
146
 
extern struct rb_node *rb_last(struct rb_root *);
 
68
extern struct rb_node *rb_next(const struct rb_node *);
 
69
extern struct rb_node *rb_prev(const struct rb_node *);
 
70
extern struct rb_node *rb_first(const struct rb_root *);
 
71
extern struct rb_node *rb_last(const struct rb_root *);
 
72
 
 
73
/* Postorder iteration - always visit the parent after its children */
 
74
extern struct rb_node *rb_first_postorder(const struct rb_root *);
 
75
extern struct rb_node *rb_next_postorder(const struct rb_node *);
147
76
 
148
77
/* Fast replacement of a single node without remove/rebalance/add/rebalance */
149
 
extern void rb_replace_node(struct rb_node *victim, struct rb_node *xnew,
 
78
extern void rb_replace_node(struct rb_node *victim, struct rb_node *new, 
150
79
                            struct rb_root *root);
151
80
 
152
81
static inline void rb_link_node(struct rb_node * node, struct rb_node * parent,
153
82
                                struct rb_node ** rb_link)
154
83
{
155
 
        node->rb_parent_color = (unsigned long )parent;
 
84
        node->__rb_parent_color = (unsigned long)parent;
156
85
        node->rb_left = node->rb_right = NULL;
157
86
 
158
87
        *rb_link = node;
159
88
}
160
89
 
161
 
/* The common insert/search/free functions */
162
 
typedef int (*rb_compare_nodes)(struct rb_node *node1, struct rb_node *node2);
163
 
typedef int (*rb_compare_keys)(struct rb_node *node, void *key);
164
 
typedef void (*rb_free_node)(struct rb_node *node);
 
90
#define rb_entry_safe(ptr, type, member) \
 
91
        ({ typeof(ptr) ____ptr = (ptr); \
 
92
           ____ptr ? rb_entry(____ptr, type, member) : NULL; \
 
93
        })
165
94
 
166
 
int rb_insert(struct rb_root *root, struct rb_node *node,
167
 
              rb_compare_nodes comp);
168
 
/*
169
 
 * In some cases, we need return the next node if we don't find the node we
170
 
 * specify. At this time, we can use next_ret.
 
95
/**
 
96
 * rbtree_postorder_for_each_entry_safe - iterate over rb_root in post order of
 
97
 * given type safe against removal of rb_node entry
 
98
 *
 
99
 * @pos:        the 'type *' to use as a loop cursor.
 
100
 * @n:          another 'type *' to use as temporary storage
 
101
 * @root:       'rb_root *' of the rbtree.
 
102
 * @field:      the name of the rb_node field within 'type'.
171
103
 */
172
 
struct rb_node *rb_search(struct rb_root *root, void *key, rb_compare_keys comp,
173
 
                          struct rb_node **next_ret);
174
 
void rb_free_nodes(struct rb_root *root, rb_free_node free_node);
175
 
 
176
 
#define FREE_RB_BASED_TREE(name, free_func)             \
177
 
static void free_##name##_tree(struct rb_root *root)    \
178
 
{                                                       \
179
 
        rb_free_nodes(root, free_func);                 \
180
 
}
 
104
#define rbtree_postorder_for_each_entry_safe(pos, n, root, field) \
 
105
        for (pos = rb_entry_safe(rb_first_postorder(root), typeof(*pos), field); \
 
106
             pos && ({ n = rb_entry_safe(rb_next_postorder(&pos->field), \
 
107
                        typeof(*pos), field); 1; }); \
 
108
             pos = n)
181
109
 
182
110
#endif  /* _LINUX_RBTREE_H */