1 /*
2   SPDX-License-Identifier: GPL-2.0-or-later
3 
4   Red Black Trees
5   (C) 1999  Andrea Arcangeli <andrea@suse.de>
6 
7   linux/include/linux/rbtree.h
8 
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...
13 
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.
20 
21 -----------------------------------------------------------------------
22 static inline struct page * rb_search_page_cache(struct inode * inode,
23 						 unsigned long offset)
24 {
25 	struct rb_node * n = inode->i_rb_page_cache.rb_node;
26 	struct page * page;
27 
28 	while (n)
29 	{
30 		page = rb_entry(n, struct page, rb_page_cache);
31 
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;
38 	}
39 	return NULL;
40 }
41 
42 static inline struct page * __rb_insert_page_cache(struct inode * inode,
43 						   unsigned long offset,
44 						   struct rb_node * node)
45 {
46 	struct rb_node ** p = &inode->i_rb_page_cache.rb_node;
47 	struct rb_node * parent = NULL;
48 	struct page * page;
49 
50 	while (*p)
51 	{
52 		parent = *p;
53 		page = rb_entry(parent, struct page, rb_page_cache);
54 
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;
61 	}
62 
63 	rb_link_node(node, parent, p);
64 
65 	return NULL;
66 }
67 
68 static inline struct page * rb_insert_page_cache(struct inode * inode,
69 						 unsigned long offset,
70 						 struct rb_node * node)
71 {
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;
78 }
79 -----------------------------------------------------------------------
80 */
81 
82 #ifndef	_LINUX_RBTREE_H
83 #define	_LINUX_RBTREE_H
84 
85 #include <stddef.h>
86 
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.
92  *
93  */
94 #define container_of(ptr, type, member) ({			\
95 	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
96 	(type *)( (char *)__mptr - offsetof(type,member) );})
97 
98 struct rb_node
99 {
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 */
107 
108 struct rb_root
109 {
110 	struct rb_node *rb_node;
111 };
112 
113 
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)
120 
rb_set_parent(struct rb_node * rb,struct rb_node * p)121 static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
122 {
123 	rb->rb_parent_color = (rb->rb_parent_color & 3) | (unsigned long)p;
124 }
rb_set_color(struct rb_node * rb,int color)125 static inline void rb_set_color(struct rb_node *rb, int color)
126 {
127 	rb->rb_parent_color = (rb->rb_parent_color & ~1) | color;
128 }
129 
130 #define RB_ROOT	(struct rb_root) { NULL, }
131 #define	rb_entry(ptr, type, member) container_of(ptr, type, member)
132 
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))
136 
137 extern void rb_insert_color(struct rb_node *, struct rb_root *);
138 extern void rb_erase(struct rb_node *, struct rb_root *);
139 
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 *);
145 
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);
149 
rb_link_node(struct rb_node * node,struct rb_node * parent,struct rb_node ** rb_link)150 static inline void rb_link_node(struct rb_node * node, struct rb_node * parent,
151 				struct rb_node ** rb_link)
152 {
153 	node->rb_parent_color = (unsigned long )parent;
154 	node->rb_left = node->rb_right = NULL;
155 
156 	*rb_link = node;
157 }
158 
159 #endif	/* _LINUX_RBTREE_H */
160