~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/Documentation/core-api/rbtree.rst

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /Documentation/core-api/rbtree.rst (Version linux-6.11.5) and /Documentation/core-api/rbtree.rst (Version linux-5.16.20)


  1 =================================                   1 =================================
  2 Red-black Trees (rbtree) in Linux                   2 Red-black Trees (rbtree) in Linux
  3 =================================                   3 =================================
  4                                                     4 
  5                                                     5 
  6 :Date: January 18, 2007                             6 :Date: January 18, 2007
  7 :Author: Rob Landley <rob@landley.net>               7 :Author: Rob Landley <rob@landley.net>
  8                                                     8 
  9 What are red-black trees, and what are they fo      9 What are red-black trees, and what are they for?
 10 ----------------------------------------------     10 ------------------------------------------------
 11                                                    11 
 12 Red-black trees are a type of self-balancing b     12 Red-black trees are a type of self-balancing binary search tree, used for
 13 storing sortable key/value data pairs.  This d     13 storing sortable key/value data pairs.  This differs from radix trees (which
 14 are used to efficiently store sparse arrays an     14 are used to efficiently store sparse arrays and thus use long integer indexes
 15 to insert/access/delete nodes) and hash tables     15 to insert/access/delete nodes) and hash tables (which are not kept sorted to
 16 be easily traversed in order, and must be tune     16 be easily traversed in order, and must be tuned for a specific size and
 17 hash function where rbtrees scale gracefully s     17 hash function where rbtrees scale gracefully storing arbitrary keys).
 18                                                    18 
 19 Red-black trees are similar to AVL trees, but      19 Red-black trees are similar to AVL trees, but provide faster real-time bounded
 20 worst case performance for insertion and delet     20 worst case performance for insertion and deletion (at most two rotations and
 21 three rotations, respectively, to balance the      21 three rotations, respectively, to balance the tree), with slightly slower
 22 (but still O(log n)) lookup time.                  22 (but still O(log n)) lookup time.
 23                                                    23 
 24 To quote Linux Weekly News:                        24 To quote Linux Weekly News:
 25                                                    25 
 26     There are a number of red-black trees in u     26     There are a number of red-black trees in use in the kernel.
 27     The deadline and CFQ I/O schedulers employ     27     The deadline and CFQ I/O schedulers employ rbtrees to
 28     track requests; the packet CD/DVD driver d     28     track requests; the packet CD/DVD driver does the same.
 29     The high-resolution timer code uses an rbt     29     The high-resolution timer code uses an rbtree to organize outstanding
 30     timer requests.  The ext3 filesystem track     30     timer requests.  The ext3 filesystem tracks directory entries in a
 31     red-black tree.  Virtual memory areas (VMA     31     red-black tree.  Virtual memory areas (VMAs) are tracked with red-black
 32     trees, as are epoll file descriptors, cryp     32     trees, as are epoll file descriptors, cryptographic keys, and network
 33     packets in the "hierarchical token bucket"     33     packets in the "hierarchical token bucket" scheduler.
 34                                                    34 
 35 This document covers use of the Linux rbtree i     35 This document covers use of the Linux rbtree implementation.  For more
 36 information on the nature and implementation o     36 information on the nature and implementation of Red Black Trees,  see:
 37                                                    37 
 38   Linux Weekly News article on red-black trees     38   Linux Weekly News article on red-black trees
 39     https://lwn.net/Articles/184495/               39     https://lwn.net/Articles/184495/
 40                                                    40 
 41   Wikipedia entry on red-black trees               41   Wikipedia entry on red-black trees
 42     https://en.wikipedia.org/wiki/Red-black_tr     42     https://en.wikipedia.org/wiki/Red-black_tree
 43                                                    43 
 44 Linux implementation of red-black trees            44 Linux implementation of red-black trees
 45 ---------------------------------------            45 ---------------------------------------
 46                                                    46 
 47 Linux's rbtree implementation lives in the fil     47 Linux's rbtree implementation lives in the file "lib/rbtree.c".  To use it,
 48 "#include <linux/rbtree.h>".                       48 "#include <linux/rbtree.h>".
 49                                                    49 
 50 The Linux rbtree implementation is optimized f     50 The Linux rbtree implementation is optimized for speed, and thus has one
 51 less layer of indirection (and better cache lo     51 less layer of indirection (and better cache locality) than more traditional
 52 tree implementations.  Instead of using pointe     52 tree implementations.  Instead of using pointers to separate rb_node and data
 53 structures, each instance of struct rb_node is     53 structures, each instance of struct rb_node is embedded in the data structure
 54 it organizes.  And instead of using a comparis     54 it organizes.  And instead of using a comparison callback function pointer,
 55 users are expected to write their own tree sea     55 users are expected to write their own tree search and insert functions
 56 which call the provided rbtree functions.  Loc     56 which call the provided rbtree functions.  Locking is also left up to the
 57 user of the rbtree code.                           57 user of the rbtree code.
 58                                                    58 
 59 Creating a new rbtree                              59 Creating a new rbtree
 60 ---------------------                              60 ---------------------
 61                                                    61 
 62 Data nodes in an rbtree tree are structures co     62 Data nodes in an rbtree tree are structures containing a struct rb_node member::
 63                                                    63 
 64   struct mytype {                                  64   struct mytype {
 65         struct rb_node node;                       65         struct rb_node node;
 66         char *keystring;                           66         char *keystring;
 67   };                                               67   };
 68                                                    68 
 69 When dealing with a pointer to the embedded st     69 When dealing with a pointer to the embedded struct rb_node, the containing data
 70 structure may be accessed with the standard co     70 structure may be accessed with the standard container_of() macro.  In addition,
 71 individual members may be accessed directly vi     71 individual members may be accessed directly via rb_entry(node, type, member).
 72                                                    72 
 73 At the root of each rbtree is an rb_root struc     73 At the root of each rbtree is an rb_root structure, which is initialized to be
 74 empty via:                                         74 empty via:
 75                                                    75 
 76   struct rb_root mytree = RB_ROOT;                 76   struct rb_root mytree = RB_ROOT;
 77                                                    77 
 78 Searching for a value in an rbtree                 78 Searching for a value in an rbtree
 79 ----------------------------------                 79 ----------------------------------
 80                                                    80 
 81 Writing a search function for your tree is fai     81 Writing a search function for your tree is fairly straightforward: start at the
 82 root, compare each value, and follow the left      82 root, compare each value, and follow the left or right branch as necessary.
 83                                                    83 
 84 Example::                                          84 Example::
 85                                                    85 
 86   struct mytype *my_search(struct rb_root *roo     86   struct mytype *my_search(struct rb_root *root, char *string)
 87   {                                                87   {
 88         struct rb_node *node = root->rb_node;      88         struct rb_node *node = root->rb_node;
 89                                                    89 
 90         while (node) {                             90         while (node) {
 91                 struct mytype *data = containe     91                 struct mytype *data = container_of(node, struct mytype, node);
 92                 int result;                        92                 int result;
 93                                                    93 
 94                 result = strcmp(string, data->     94                 result = strcmp(string, data->keystring);
 95                                                    95 
 96                 if (result < 0)                    96                 if (result < 0)
 97                         node = node->rb_left;      97                         node = node->rb_left;
 98                 else if (result > 0)               98                 else if (result > 0)
 99                         node = node->rb_right;     99                         node = node->rb_right;
100                 else                              100                 else
101                         return data;              101                         return data;
102         }                                         102         }
103         return NULL;                              103         return NULL;
104   }                                               104   }
105                                                   105 
106 Inserting data into an rbtree                     106 Inserting data into an rbtree
107 -----------------------------                     107 -----------------------------
108                                                   108 
109 Inserting data in the tree involves first sear    109 Inserting data in the tree involves first searching for the place to insert the
110 new node, then inserting the node and rebalanc    110 new node, then inserting the node and rebalancing ("recoloring") the tree.
111                                                   111 
112 The search for insertion differs from the prev    112 The search for insertion differs from the previous search by finding the
113 location of the pointer on which to graft the     113 location of the pointer on which to graft the new node.  The new node also
114 needs a link to its parent node for rebalancin    114 needs a link to its parent node for rebalancing purposes.
115                                                   115 
116 Example::                                         116 Example::
117                                                   117 
118   int my_insert(struct rb_root *root, struct m    118   int my_insert(struct rb_root *root, struct mytype *data)
119   {                                               119   {
120         struct rb_node **new = &(root->rb_node    120         struct rb_node **new = &(root->rb_node), *parent = NULL;
121                                                   121 
122         /* Figure out where to put new node */    122         /* Figure out where to put new node */
123         while (*new) {                            123         while (*new) {
124                 struct mytype *this = containe    124                 struct mytype *this = container_of(*new, struct mytype, node);
125                 int result = strcmp(data->keys    125                 int result = strcmp(data->keystring, this->keystring);
126                                                   126 
127                 parent = *new;                    127                 parent = *new;
128                 if (result < 0)                   128                 if (result < 0)
129                         new = &((*new)->rb_lef    129                         new = &((*new)->rb_left);
130                 else if (result > 0)              130                 else if (result > 0)
131                         new = &((*new)->rb_rig    131                         new = &((*new)->rb_right);
132                 else                              132                 else
133                         return FALSE;             133                         return FALSE;
134         }                                         134         }
135                                                   135 
136         /* Add new node and rebalance tree. */    136         /* Add new node and rebalance tree. */
137         rb_link_node(&data->node, parent, new)    137         rb_link_node(&data->node, parent, new);
138         rb_insert_color(&data->node, root);       138         rb_insert_color(&data->node, root);
139                                                   139 
140         return TRUE;                              140         return TRUE;
141   }                                               141   }
142                                                   142 
143 Removing or replacing existing data in an rbtr    143 Removing or replacing existing data in an rbtree
144 ----------------------------------------------    144 ------------------------------------------------
145                                                   145 
146 To remove an existing node from a tree, call::    146 To remove an existing node from a tree, call::
147                                                   147 
148   void rb_erase(struct rb_node *victim, struct    148   void rb_erase(struct rb_node *victim, struct rb_root *tree);
149                                                   149 
150 Example::                                         150 Example::
151                                                   151 
152   struct mytype *data = mysearch(&mytree, "wal    152   struct mytype *data = mysearch(&mytree, "walrus");
153                                                   153 
154   if (data) {                                     154   if (data) {
155         rb_erase(&data->node, &mytree);           155         rb_erase(&data->node, &mytree);
156         myfree(data);                             156         myfree(data);
157   }                                               157   }
158                                                   158 
159 To replace an existing node in a tree with a n    159 To replace an existing node in a tree with a new one with the same key, call::
160                                                   160 
161   void rb_replace_node(struct rb_node *old, st    161   void rb_replace_node(struct rb_node *old, struct rb_node *new,
162                         struct rb_root *tree);    162                         struct rb_root *tree);
163                                                   163 
164 Replacing a node this way does not re-sort the    164 Replacing a node this way does not re-sort the tree: If the new node doesn't
165 have the same key as the old node, the rbtree     165 have the same key as the old node, the rbtree will probably become corrupted.
166                                                   166 
167 Iterating through the elements stored in an rb    167 Iterating through the elements stored in an rbtree (in sort order)
168 ----------------------------------------------    168 ------------------------------------------------------------------
169                                                   169 
170 Four functions are provided for iterating thro    170 Four functions are provided for iterating through an rbtree's contents in
171 sorted order.  These work on arbitrary trees,     171 sorted order.  These work on arbitrary trees, and should not need to be
172 modified or wrapped (except for locking purpos    172 modified or wrapped (except for locking purposes)::
173                                                   173 
174   struct rb_node *rb_first(struct rb_root *tre    174   struct rb_node *rb_first(struct rb_root *tree);
175   struct rb_node *rb_last(struct rb_root *tree    175   struct rb_node *rb_last(struct rb_root *tree);
176   struct rb_node *rb_next(struct rb_node *node    176   struct rb_node *rb_next(struct rb_node *node);
177   struct rb_node *rb_prev(struct rb_node *node    177   struct rb_node *rb_prev(struct rb_node *node);
178                                                   178 
179 To start iterating, call rb_first() or rb_last    179 To start iterating, call rb_first() or rb_last() with a pointer to the root
180 of the tree, which will return a pointer to th    180 of the tree, which will return a pointer to the node structure contained in
181 the first or last element in the tree.  To con    181 the first or last element in the tree.  To continue, fetch the next or previous
182 node by calling rb_next() or rb_prev() on the     182 node by calling rb_next() or rb_prev() on the current node.  This will return
183 NULL when there are no more nodes left.           183 NULL when there are no more nodes left.
184                                                   184 
185 The iterator functions return a pointer to the    185 The iterator functions return a pointer to the embedded struct rb_node, from
186 which the containing data structure may be acc    186 which the containing data structure may be accessed with the container_of()
187 macro, and individual members may be accessed     187 macro, and individual members may be accessed directly via
188 rb_entry(node, type, member).                     188 rb_entry(node, type, member).
189                                                   189 
190 Example::                                         190 Example::
191                                                   191 
192   struct rb_node *node;                           192   struct rb_node *node;
193   for (node = rb_first(&mytree); node; node =     193   for (node = rb_first(&mytree); node; node = rb_next(node))
194         printk("key=%s\n", rb_entry(node, stru    194         printk("key=%s\n", rb_entry(node, struct mytype, node)->keystring);
195                                                   195 
196 Cached rbtrees                                    196 Cached rbtrees
197 --------------                                    197 --------------
198                                                   198 
199 Computing the leftmost (smallest) node is quit    199 Computing the leftmost (smallest) node is quite a common task for binary
200 search trees, such as for traversals or users     200 search trees, such as for traversals or users relying on a the particular
201 order for their own logic. To this end, users     201 order for their own logic. To this end, users can use 'struct rb_root_cached'
202 to optimize O(logN) rb_first() calls to a simp    202 to optimize O(logN) rb_first() calls to a simple pointer fetch avoiding
203 potentially expensive tree iterations. This is    203 potentially expensive tree iterations. This is done at negligible runtime
204 overhead for maintenance; albeit larger memory    204 overhead for maintenance; albeit larger memory footprint.
205                                                   205 
206 Similar to the rb_root structure, cached rbtre    206 Similar to the rb_root structure, cached rbtrees are initialized to be
207 empty via::                                       207 empty via::
208                                                   208 
209   struct rb_root_cached mytree = RB_ROOT_CACHE    209   struct rb_root_cached mytree = RB_ROOT_CACHED;
210                                                   210 
211 Cached rbtree is simply a regular rb_root with    211 Cached rbtree is simply a regular rb_root with an extra pointer to cache the
212 leftmost node. This allows rb_root_cached to e    212 leftmost node. This allows rb_root_cached to exist wherever rb_root does,
213 which permits augmented trees to be supported     213 which permits augmented trees to be supported as well as only a few extra
214 interfaces::                                      214 interfaces::
215                                                   215 
216   struct rb_node *rb_first_cached(struct rb_ro    216   struct rb_node *rb_first_cached(struct rb_root_cached *tree);
217   void rb_insert_color_cached(struct rb_node *    217   void rb_insert_color_cached(struct rb_node *, struct rb_root_cached *, bool);
218   void rb_erase_cached(struct rb_node *node, s    218   void rb_erase_cached(struct rb_node *node, struct rb_root_cached *);
219                                                   219 
220 Both insert and erase calls have their respect    220 Both insert and erase calls have their respective counterpart of augmented
221 trees::                                           221 trees::
222                                                   222 
223   void rb_insert_augmented_cached(struct rb_no    223   void rb_insert_augmented_cached(struct rb_node *node, struct rb_root_cached *,
224                                   bool, struct    224                                   bool, struct rb_augment_callbacks *);
225   void rb_erase_augmented_cached(struct rb_nod    225   void rb_erase_augmented_cached(struct rb_node *, struct rb_root_cached *,
226                                  struct rb_aug    226                                  struct rb_augment_callbacks *);
227                                                   227 
228                                                   228 
229 Support for Augmented rbtrees                     229 Support for Augmented rbtrees
230 -----------------------------                     230 -----------------------------
231                                                   231 
232 Augmented rbtree is an rbtree with "some" addi    232 Augmented rbtree is an rbtree with "some" additional data stored in
233 each node, where the additional data for node     233 each node, where the additional data for node N must be a function of
234 the contents of all nodes in the subtree roote    234 the contents of all nodes in the subtree rooted at N. This data can
235 be used to augment some new functionality to r    235 be used to augment some new functionality to rbtree. Augmented rbtree
236 is an optional feature built on top of basic r    236 is an optional feature built on top of basic rbtree infrastructure.
237 An rbtree user who wants this feature will hav    237 An rbtree user who wants this feature will have to call the augmentation
238 functions with the user provided augmentation     238 functions with the user provided augmentation callback when inserting
239 and erasing nodes.                                239 and erasing nodes.
240                                                   240 
241 C files implementing augmented rbtree manipula    241 C files implementing augmented rbtree manipulation must include
242 <linux/rbtree_augmented.h> instead of <linux/r    242 <linux/rbtree_augmented.h> instead of <linux/rbtree.h>. Note that
243 linux/rbtree_augmented.h exposes some rbtree i    243 linux/rbtree_augmented.h exposes some rbtree implementations details
244 you are not expected to rely on; please stick     244 you are not expected to rely on; please stick to the documented APIs
245 there and do not include <linux/rbtree_augment    245 there and do not include <linux/rbtree_augmented.h> from header files
246 either so as to minimize chances of your users    246 either so as to minimize chances of your users accidentally relying on
247 such implementation details.                      247 such implementation details.
248                                                   248 
249 On insertion, the user must update the augment    249 On insertion, the user must update the augmented information on the path
250 leading to the inserted node, then call rb_lin    250 leading to the inserted node, then call rb_link_node() as usual and
251 rb_augment_inserted() instead of the usual rb_    251 rb_augment_inserted() instead of the usual rb_insert_color() call.
252 If rb_augment_inserted() rebalances the rbtree    252 If rb_augment_inserted() rebalances the rbtree, it will callback into
253 a user provided function to update the augment    253 a user provided function to update the augmented information on the
254 affected subtrees.                                254 affected subtrees.
255                                                   255 
256 When erasing a node, the user must call rb_era    256 When erasing a node, the user must call rb_erase_augmented() instead of
257 rb_erase(). rb_erase_augmented() calls back in    257 rb_erase(). rb_erase_augmented() calls back into user provided functions
258 to updated the augmented information on affect    258 to updated the augmented information on affected subtrees.
259                                                   259 
260 In both cases, the callbacks are provided thro    260 In both cases, the callbacks are provided through struct rb_augment_callbacks.
261 3 callbacks must be defined:                      261 3 callbacks must be defined:
262                                                   262 
263 - A propagation callback, which updates the au    263 - A propagation callback, which updates the augmented value for a given
264   node and its ancestors, up to a given stop p    264   node and its ancestors, up to a given stop point (or NULL to update
265   all the way to the root).                       265   all the way to the root).
266                                                   266 
267 - A copy callback, which copies the augmented     267 - A copy callback, which copies the augmented value for a given subtree
268   to a newly assigned subtree root.               268   to a newly assigned subtree root.
269                                                   269 
270 - A tree rotation callback, which copies the a    270 - A tree rotation callback, which copies the augmented value for a given
271   subtree to a newly assigned subtree root AND    271   subtree to a newly assigned subtree root AND recomputes the augmented
272   information for the former subtree root.        272   information for the former subtree root.
273                                                   273 
274 The compiled code for rb_erase_augmented() may    274 The compiled code for rb_erase_augmented() may inline the propagation and
275 copy callbacks, which results in a large funct    275 copy callbacks, which results in a large function, so each augmented rbtree
276 user should have a single rb_erase_augmented()    276 user should have a single rb_erase_augmented() call site in order to limit
277 compiled code size.                               277 compiled code size.
278                                                   278 
279                                                   279 
280 Sample usage                                      280 Sample usage
281 ^^^^^^^^^^^^                                      281 ^^^^^^^^^^^^
282                                                   282 
283 Interval tree is an example of augmented rb tr    283 Interval tree is an example of augmented rb tree. Reference -
284 "Introduction to Algorithms" by Cormen, Leiser    284 "Introduction to Algorithms" by Cormen, Leiserson, Rivest and Stein.
285 More details about interval trees:                285 More details about interval trees:
286                                                   286 
287 Classical rbtree has a single key and it canno    287 Classical rbtree has a single key and it cannot be directly used to store
288 interval ranges like [lo:hi] and do a quick lo    288 interval ranges like [lo:hi] and do a quick lookup for any overlap with a new
289 lo:hi or to find whether there is an exact mat    289 lo:hi or to find whether there is an exact match for a new lo:hi.
290                                                   290 
291 However, rbtree can be augmented to store such    291 However, rbtree can be augmented to store such interval ranges in a structured
292 way making it possible to do efficient lookup     292 way making it possible to do efficient lookup and exact match.
293                                                   293 
294 This "extra information" stored in each node i    294 This "extra information" stored in each node is the maximum hi
295 (max_hi) value among all the nodes that are it    295 (max_hi) value among all the nodes that are its descendants. This
296 information can be maintained at each node jus    296 information can be maintained at each node just be looking at the node
297 and its immediate children. And this will be u    297 and its immediate children. And this will be used in O(log n) lookup
298 for lowest match (lowest start address among a    298 for lowest match (lowest start address among all possible matches)
299 with something like::                             299 with something like::
300                                                   300 
301   struct interval_tree_node *                     301   struct interval_tree_node *
302   interval_tree_first_match(struct rb_root *ro    302   interval_tree_first_match(struct rb_root *root,
303                             unsigned long star    303                             unsigned long start, unsigned long last)
304   {                                               304   {
305         struct interval_tree_node *node;          305         struct interval_tree_node *node;
306                                                   306 
307         if (!root->rb_node)                       307         if (!root->rb_node)
308                 return NULL;                      308                 return NULL;
309         node = rb_entry(root->rb_node, struct     309         node = rb_entry(root->rb_node, struct interval_tree_node, rb);
310                                                   310 
311         while (true) {                            311         while (true) {
312                 if (node->rb.rb_left) {           312                 if (node->rb.rb_left) {
313                         struct interval_tree_n    313                         struct interval_tree_node *left =
314                                 rb_entry(node-    314                                 rb_entry(node->rb.rb_left,
315                                          struc    315                                          struct interval_tree_node, rb);
316                         if (left->__subtree_la    316                         if (left->__subtree_last >= start) {
317                                 /*                317                                 /*
318                                  * Some nodes     318                                  * Some nodes in left subtree satisfy Cond2.
319                                  * Iterate to     319                                  * Iterate to find the leftmost such node N.
320                                  * If it also     320                                  * If it also satisfies Cond1, that's the match
321                                  * we are look    321                                  * we are looking for. Otherwise, there is no
322                                  * matching in    322                                  * matching interval as nodes to the right of N
323                                  * can't satis    323                                  * can't satisfy Cond1 either.
324                                  */               324                                  */
325                                 node = left;      325                                 node = left;
326                                 continue;         326                                 continue;
327                         }                         327                         }
328                 }                                 328                 }
329                 if (node->start <= last) {        329                 if (node->start <= last) {              /* Cond1 */
330                         if (node->last >= star    330                         if (node->last >= start)        /* Cond2 */
331                                 return node;      331                                 return node;    /* node is leftmost match */
332                         if (node->rb.rb_right)    332                         if (node->rb.rb_right) {
333                                 node = rb_entr    333                                 node = rb_entry(node->rb.rb_right,
334                                         struct    334                                         struct interval_tree_node, rb);
335                                 if (node->__su    335                                 if (node->__subtree_last >= start)
336                                         contin    336                                         continue;
337                         }                         337                         }
338                 }                                 338                 }
339                 return NULL;    /* No match */    339                 return NULL;    /* No match */
340         }                                         340         }
341   }                                               341   }
342                                                   342 
343 Insertion/removal are defined using the follow    343 Insertion/removal are defined using the following augmented callbacks::
344                                                   344 
345   static inline unsigned long                     345   static inline unsigned long
346   compute_subtree_last(struct interval_tree_no    346   compute_subtree_last(struct interval_tree_node *node)
347   {                                               347   {
348         unsigned long max = node->last, subtre    348         unsigned long max = node->last, subtree_last;
349         if (node->rb.rb_left) {                   349         if (node->rb.rb_left) {
350                 subtree_last = rb_entry(node->    350                 subtree_last = rb_entry(node->rb.rb_left,
351                         struct interval_tree_n    351                         struct interval_tree_node, rb)->__subtree_last;
352                 if (max < subtree_last)           352                 if (max < subtree_last)
353                         max = subtree_last;       353                         max = subtree_last;
354         }                                         354         }
355         if (node->rb.rb_right) {                  355         if (node->rb.rb_right) {
356                 subtree_last = rb_entry(node->    356                 subtree_last = rb_entry(node->rb.rb_right,
357                         struct interval_tree_n    357                         struct interval_tree_node, rb)->__subtree_last;
358                 if (max < subtree_last)           358                 if (max < subtree_last)
359                         max = subtree_last;       359                         max = subtree_last;
360         }                                         360         }
361         return max;                               361         return max;
362   }                                               362   }
363                                                   363 
364   static void augment_propagate(struct rb_node    364   static void augment_propagate(struct rb_node *rb, struct rb_node *stop)
365   {                                               365   {
366         while (rb != stop) {                      366         while (rb != stop) {
367                 struct interval_tree_node *nod    367                 struct interval_tree_node *node =
368                         rb_entry(rb, struct in    368                         rb_entry(rb, struct interval_tree_node, rb);
369                 unsigned long subtree_last = c    369                 unsigned long subtree_last = compute_subtree_last(node);
370                 if (node->__subtree_last == su    370                 if (node->__subtree_last == subtree_last)
371                         break;                    371                         break;
372                 node->__subtree_last = subtree    372                 node->__subtree_last = subtree_last;
373                 rb = rb_parent(&node->rb);        373                 rb = rb_parent(&node->rb);
374         }                                         374         }
375   }                                               375   }
376                                                   376 
377   static void augment_copy(struct rb_node *rb_    377   static void augment_copy(struct rb_node *rb_old, struct rb_node *rb_new)
378   {                                               378   {
379         struct interval_tree_node *old =          379         struct interval_tree_node *old =
380                 rb_entry(rb_old, struct interv    380                 rb_entry(rb_old, struct interval_tree_node, rb);
381         struct interval_tree_node *new =          381         struct interval_tree_node *new =
382                 rb_entry(rb_new, struct interv    382                 rb_entry(rb_new, struct interval_tree_node, rb);
383                                                   383 
384         new->__subtree_last = old->__subtree_l    384         new->__subtree_last = old->__subtree_last;
385   }                                               385   }
386                                                   386 
387   static void augment_rotate(struct rb_node *r    387   static void augment_rotate(struct rb_node *rb_old, struct rb_node *rb_new)
388   {                                               388   {
389         struct interval_tree_node *old =          389         struct interval_tree_node *old =
390                 rb_entry(rb_old, struct interv    390                 rb_entry(rb_old, struct interval_tree_node, rb);
391         struct interval_tree_node *new =          391         struct interval_tree_node *new =
392                 rb_entry(rb_new, struct interv    392                 rb_entry(rb_new, struct interval_tree_node, rb);
393                                                   393 
394         new->__subtree_last = old->__subtree_l    394         new->__subtree_last = old->__subtree_last;
395         old->__subtree_last = compute_subtree_    395         old->__subtree_last = compute_subtree_last(old);
396   }                                               396   }
397                                                   397 
398   static const struct rb_augment_callbacks aug    398   static const struct rb_augment_callbacks augment_callbacks = {
399         augment_propagate, augment_copy, augme    399         augment_propagate, augment_copy, augment_rotate
400   };                                              400   };
401                                                   401 
402   void interval_tree_insert(struct interval_tr    402   void interval_tree_insert(struct interval_tree_node *node,
403                             struct rb_root *ro    403                             struct rb_root *root)
404   {                                               404   {
405         struct rb_node **link = &root->rb_node    405         struct rb_node **link = &root->rb_node, *rb_parent = NULL;
406         unsigned long start = node->start, las    406         unsigned long start = node->start, last = node->last;
407         struct interval_tree_node *parent;        407         struct interval_tree_node *parent;
408                                                   408 
409         while (*link) {                           409         while (*link) {
410                 rb_parent = *link;                410                 rb_parent = *link;
411                 parent = rb_entry(rb_parent, s    411                 parent = rb_entry(rb_parent, struct interval_tree_node, rb);
412                 if (parent->__subtree_last < l    412                 if (parent->__subtree_last < last)
413                         parent->__subtree_last    413                         parent->__subtree_last = last;
414                 if (start < parent->start)        414                 if (start < parent->start)
415                         link = &parent->rb.rb_    415                         link = &parent->rb.rb_left;
416                 else                              416                 else
417                         link = &parent->rb.rb_    417                         link = &parent->rb.rb_right;
418         }                                         418         }
419                                                   419 
420         node->__subtree_last = last;              420         node->__subtree_last = last;
421         rb_link_node(&node->rb, rb_parent, lin    421         rb_link_node(&node->rb, rb_parent, link);
422         rb_insert_augmented(&node->rb, root, &    422         rb_insert_augmented(&node->rb, root, &augment_callbacks);
423   }                                               423   }
424                                                   424 
425   void interval_tree_remove(struct interval_tr    425   void interval_tree_remove(struct interval_tree_node *node,
426                             struct rb_root *ro    426                             struct rb_root *root)
427   {                                               427   {
428         rb_erase_augmented(&node->rb, root, &a    428         rb_erase_augmented(&node->rb, root, &augment_callbacks);
429   }                                               429   }
                                                      

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php