pahole: Describe expected use of 'default' in the man page
[dwarves.git] / rbtree.h
blob3eb99f9ed50c66fc3f9291fe26377b2da3019caa
1 /*
2 SPDX-License-Identifier: GPL-2.0-or-later
4 Red Black Trees
5 (C) 1999 Andrea Arcangeli <andrea@suse.de>
7 linux/include/linux/rbtree.h
9 To use rbtrees you'll have to implement your own insert and search cores.
10 This will avoid us to use callbacks and to drop drammatically performances.
11 I know it's not the cleaner way, but in C (not in C++) to get
12 performances and genericity...
14 Some example of insert and search follows here. The search is a plain
15 normal search over an ordered tree. The insert instead must be implemented
16 int two steps: as first thing the code must insert the element in
17 order as a red leaf in the tree, then the support library function
18 rb_insert_color() must be called. Such function will do the
19 not trivial work to rebalance the rbtree if necessary.
21 -----------------------------------------------------------------------
22 static inline struct page * rb_search_page_cache(struct inode * inode,
23 unsigned long offset)
25 struct rb_node * n = inode->i_rb_page_cache.rb_node;
26 struct page * page;
28 while (n)
30 page = rb_entry(n, struct page, rb_page_cache);
32 if (offset < page->offset)
33 n = n->rb_left;
34 else if (offset > page->offset)
35 n = n->rb_right;
36 else
37 return page;
39 return NULL;
42 static inline struct page * __rb_insert_page_cache(struct inode * inode,
43 unsigned long offset,
44 struct rb_node * node)
46 struct rb_node ** p = &inode->i_rb_page_cache.rb_node;
47 struct rb_node * parent = NULL;
48 struct page * page;
50 while (*p)
52 parent = *p;
53 page = rb_entry(parent, struct page, rb_page_cache);
55 if (offset < page->offset)
56 p = &(*p)->rb_left;
57 else if (offset > page->offset)
58 p = &(*p)->rb_right;
59 else
60 return page;
63 rb_link_node(node, parent, p);
65 return NULL;
68 static inline struct page * rb_insert_page_cache(struct inode * inode,
69 unsigned long offset,
70 struct rb_node * node)
72 struct page * ret;
73 if ((ret = __rb_insert_page_cache(inode, offset, node)))
74 goto out;
75 rb_insert_color(node, &inode->i_rb_page_cache);
76 out:
77 return ret;
79 -----------------------------------------------------------------------
82 #ifndef _LINUX_RBTREE_H
83 #define _LINUX_RBTREE_H
85 #include <stddef.h>
87 /**
88 * container_of - cast a member of a structure out to the containing structure
89 * @ptr: the pointer to the member.
90 * @type: the type of the container struct this is embedded in.
91 * @member: the name of the member within the struct.
94 #define container_of(ptr, type, member) ({ \
95 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
96 (type *)( (char *)__mptr - offsetof(type,member) );})
98 struct rb_node
100 unsigned long rb_parent_color;
101 #define RB_RED 0
102 #define RB_BLACK 1
103 struct rb_node *rb_right;
104 struct rb_node *rb_left;
105 } __attribute__((aligned(sizeof(long))));
106 /* The alignment might seem pointless, but allegedly CRIS needs it */
108 struct rb_root
110 struct rb_node *rb_node;
114 #define rb_parent(r) ((struct rb_node *)((r)->rb_parent_color & ~3))
115 #define rb_color(r) ((r)->rb_parent_color & 1)
116 #define rb_is_red(r) (!rb_color(r))
117 #define rb_is_black(r) rb_color(r)
118 #define rb_set_red(r) do { (r)->rb_parent_color &= ~1; } while (0)
119 #define rb_set_black(r) do { (r)->rb_parent_color |= 1; } while (0)
121 static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
123 rb->rb_parent_color = (rb->rb_parent_color & 3) | (unsigned long)p;
125 static inline void rb_set_color(struct rb_node *rb, int color)
127 rb->rb_parent_color = (rb->rb_parent_color & ~1) | color;
130 #define RB_ROOT (struct rb_root) { NULL, }
131 #define rb_entry(ptr, type, member) container_of(ptr, type, member)
133 #define RB_EMPTY_ROOT(root) ((root)->rb_node == NULL)
134 #define RB_EMPTY_NODE(node) (rb_parent(node) == node)
135 #define RB_CLEAR_NODE(node) (rb_set_parent(node, node))
137 extern void rb_insert_color(struct rb_node *, struct rb_root *);
138 extern void rb_erase(struct rb_node *, struct rb_root *);
140 /* Find logical next and previous nodes in a tree */
141 extern struct rb_node *rb_next(const struct rb_node *);
142 extern struct rb_node *rb_prev(const struct rb_node *);
143 extern struct rb_node *rb_first(const struct rb_root *);
144 extern struct rb_node *rb_last(const struct rb_root *);
146 /* Fast replacement of a single node without remove/rebalance/add/rebalance */
147 extern void rb_replace_node(struct rb_node *victim, struct rb_node *new,
148 struct rb_root *root);
150 static inline void rb_link_node(struct rb_node * node, struct rb_node * parent,
151 struct rb_node ** rb_link)
153 node->rb_parent_color = (unsigned long )parent;
154 node->rb_left = node->rb_right = NULL;
156 *rb_link = node;
159 #endif /* _LINUX_RBTREE_H */