LCOV - code coverage report
Current view: top level - /home/ubuntu/zephyrproject/zephyr/lib/utils - rb.c (source / functions) Coverage Total Hit
Test: lcov.info Lines: 0.0 % 255 0
Test Date: 2026-11-06 14:43:19 Functions: 0.0 % 20 0
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0.0 % 104 0

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * Copyright (c) 2018 Intel Corporation
       3                 :             :  *
       4                 :             :  * SPDX-License-Identifier: Apache-2.0
       5                 :             :  */
       6                 :             : 
       7                 :             : /* Internal consistency assertions are very useful when debugging the tree
       8                 :             :  * code itself, but produce significant performance degradation as they are
       9                 :             :  * checked many times per operation.  They are therefore compiled out: define
      10                 :             :  * CHECK() as __ASSERT_NO_MSG() instead when working on the rbtree code itself.
      11                 :             :  */
      12                 :             : #define CHECK(n) /**/
      13                 :             : 
      14                 :             : #include <zephyr/kernel.h>
      15                 :             : #include <zephyr/sys/rb.h>
      16                 :             : #include <stdbool.h>
      17                 :             : 
      18                 :             : enum rb_color { RED = 0U, BLACK = 1U };
      19                 :             : 
      20                 :           0 : static struct rbnode *get_child(struct rbnode *n, uint8_t side)
      21                 :             : {
      22                 :           0 :         CHECK(n);
      23         [ #  # ]:           0 :         if (side != 0U) {
      24                 :           0 :                 return n->children[1];
      25                 :             :         }
      26                 :             : 
      27                 :           0 :         uintptr_t l = (uintptr_t) n->children[0];
      28                 :             : 
      29                 :           0 :         l &= ~1UL;
      30                 :           0 :         return (struct rbnode *) l;
      31                 :             : }
      32                 :             : 
      33                 :           0 : static void set_child(struct rbnode *n, uint8_t side, void *val)
      34                 :             : {
      35                 :           0 :         CHECK(n);
      36         [ #  # ]:           0 :         if (side != 0U) {
      37                 :           0 :                 n->children[1] = val;
      38                 :             :         } else {
      39                 :           0 :                 uintptr_t old = (uintptr_t) n->children[0];
      40                 :           0 :                 uintptr_t new = (uintptr_t) val;
      41                 :             : 
      42                 :           0 :                 n->children[0] = (void *) (new | (old & 1UL));
      43                 :             :         }
      44                 :           0 : }
      45                 :             : 
      46                 :           0 : static enum rb_color get_color(struct rbnode *n)
      47                 :             : {
      48                 :           0 :         CHECK(n);
      49                 :           0 :         return ((uintptr_t)n->children[0]) & 1UL;
      50                 :             : }
      51                 :             : 
      52                 :           0 : static bool is_black(struct rbnode *n)
      53                 :             : {
      54                 :           0 :         return get_color(n) == BLACK;
      55                 :             : }
      56                 :             : 
      57                 :           0 : static bool is_red(struct rbnode *n)
      58                 :             : {
      59                 :           0 :         return get_color(n) == RED;
      60                 :             : }
      61                 :             : 
      62                 :           0 : static void set_color(struct rbnode *n, enum rb_color color)
      63                 :             : {
      64                 :           0 :         CHECK(n);
      65                 :             : 
      66                 :           0 :         uintptr_t *p = (void *) &n->children[0];
      67                 :             : 
      68                 :           0 :         *p = (*p & ~1UL) | (uint8_t)color;
      69                 :           0 : }
      70                 :             : 
      71                 :             : /* Searches the tree down to a node that is either identical with the
      72                 :             :  * "node" argument or has an empty/leaf child pointer where "node"
      73                 :             :  * should be, leaving all nodes found in the resulting stack.  Note
      74                 :             :  * that tree must not be empty and that stack should be allocated to
      75                 :             :  * contain at least tree->max_depth entries!  Returns the number of
      76                 :             :  * entries pushed onto the stack.
      77                 :             :  */
      78                 :           0 : static int find_and_stack(struct rbtree *tree, struct rbnode *node,
      79                 :             :                           struct rbnode **stack)
      80                 :             : {
      81                 :           0 :         int sz = 0;
      82                 :             : 
      83                 :           0 :         stack[sz] = tree->root;
      84                 :           0 :         ++sz;
      85                 :             : 
      86         [ #  # ]:           0 :         while (stack[sz - 1] != node) {
      87                 :           0 :                 uint8_t side = tree->lessthan_fn(node, stack[sz - 1]) ? 0U : 1U;
      88                 :           0 :                 struct rbnode *ch = get_child(stack[sz - 1], side);
      89                 :             : 
      90         [ #  # ]:           0 :                 if (ch != NULL) {
      91                 :           0 :                         stack[sz] = ch;
      92                 :           0 :                         ++sz;
      93                 :             :                 } else {
      94                 :             :                         break;
      95                 :             :                 }
      96                 :             :         }
      97                 :             : 
      98                 :           0 :         return sz;
      99                 :             : }
     100                 :             : 
     101                 :           0 : struct rbnode *z_rb_get_minmax(struct rbtree *tree, uint8_t side)
     102                 :             : {
     103                 :           0 :         struct rbnode *n;
     104                 :             : 
     105   [ #  #  #  # ]:           0 :         for (n = tree->root; (n != NULL) && (get_child(n, side) != NULL);
     106                 :             :                         n = get_child(n, side)) {
     107                 :             :                 ;
     108                 :             :         }
     109                 :           0 :         return n;
     110                 :             : }
     111                 :             : 
     112                 :           0 : static uint8_t get_side(struct rbnode *parent, struct rbnode *child)
     113                 :             : {
     114                 :           0 :         CHECK(get_child(parent, 0U) == child || get_child(parent, 1U) == child);
     115                 :             : 
     116         [ #  # ]:           0 :         return (get_child(parent, 1U) == child) ? 1U : 0U;
     117                 :             : }
     118                 :             : 
     119                 :             : /* Swaps the position of the two nodes at the top of the provided
     120                 :             :  * stack, modifying the stack accordingly. Does not change the color
     121                 :             :  * of either node.  That is, it effects the following transition (or
     122                 :             :  * its mirror if N is on the other side of P, of course):
     123                 :             :  *
     124                 :             :  *    P          N
     125                 :             :  *  N  c  -->  a   P
     126                 :             :  * a b            b c
     127                 :             :  *
     128                 :             :  */
     129                 :           0 : static void rotate(struct rbnode **stack, int stacksz)
     130                 :             : {
     131                 :           0 :         CHECK(stacksz >= 2);
     132                 :             : 
     133                 :           0 :         struct rbnode *parent = stack[stacksz - 2];
     134                 :           0 :         struct rbnode *child = stack[stacksz - 1];
     135                 :           0 :         uint8_t side = get_side(parent, child);
     136                 :           0 :         struct rbnode *a = get_child(child, side);
     137                 :           0 :         struct rbnode *b = get_child(child, (side == 0U) ? 1U : 0U);
     138                 :             : 
     139         [ #  # ]:           0 :         if (stacksz >= 3) {
     140                 :           0 :                 struct rbnode *grandparent = stack[stacksz - 3];
     141                 :             : 
     142                 :           0 :                 set_child(grandparent, get_side(grandparent, parent), child);
     143                 :             :         }
     144                 :             : 
     145                 :           0 :         set_child(child, side, a);
     146                 :           0 :         set_child(child, (side == 0U) ? 1U : 0U, parent);
     147                 :           0 :         set_child(parent, side, b);
     148                 :           0 :         stack[stacksz - 2] = child;
     149                 :           0 :         stack[stacksz - 1] = parent;
     150                 :           0 : }
     151                 :             : 
     152                 :             : /* The node at the top of the provided stack is red, and its parent is
     153                 :             :  * too.  Iteratively fix the tree so it becomes a valid red black tree
     154                 :             :  * again
     155                 :             :  */
     156                 :           0 : static void fix_extra_red(struct rbnode **stack, int stacksz)
     157                 :             : {
     158         [ #  # ]:           0 :         while (stacksz > 1) {
     159                 :           0 :                 struct rbnode *node = stack[stacksz - 1];
     160                 :           0 :                 struct rbnode *parent = stack[stacksz - 2];
     161                 :             : 
     162                 :             :                 /* Correct child colors are a precondition of the loop */
     163                 :             :                 CHECK((get_child(node, 0U) == NULL) ||
     164                 :           0 :                       is_black(get_child(node, 0U)));
     165                 :             :                 CHECK((get_child(node, 1U) == NULL) ||
     166                 :           0 :                       is_black(get_child(node, 1U)));
     167                 :             : 
     168         [ #  # ]:           0 :                 if (is_black(parent)) {
     169                 :             :                         return;
     170                 :             :                 }
     171                 :             : 
     172                 :             :                 /* We are guaranteed to have a grandparent if our
     173                 :             :                  * parent is red, as red nodes cannot be the root
     174                 :             :                  */
     175                 :           0 :                 CHECK(stacksz >= 2);
     176                 :             : 
     177                 :           0 :                 struct rbnode *grandparent = stack[stacksz - 3];
     178                 :           0 :                 uint8_t side = get_side(grandparent, parent);
     179                 :           0 :                 struct rbnode *aunt = get_child(grandparent,
     180                 :             :                                                 (side == 0U) ? 1U : 0U);
     181                 :             : 
     182   [ #  #  #  # ]:           0 :                 if ((aunt != NULL) && is_red(aunt)) {
     183                 :           0 :                         set_color(grandparent, RED);
     184                 :           0 :                         set_color(parent, BLACK);
     185                 :           0 :                         set_color(aunt, BLACK);
     186                 :             : 
     187                 :             :                         /* We colored the grandparent red, which might
     188                 :             :                          * have a red parent, so continue iterating
     189                 :             :                          * from there.
     190                 :             :                          */
     191                 :           0 :                         stacksz -= 2;
     192                 :           0 :                         continue;
     193                 :             :                 }
     194                 :             : 
     195                 :             :                 /* We can rotate locally to fix the whole tree.  First
     196                 :             :                  * make sure that node is on the same side of parent
     197                 :             :                  * as parent is of grandparent.
     198                 :             :                  */
     199                 :           0 :                 uint8_t parent_side = get_side(parent, node);
     200                 :             : 
     201         [ #  # ]:           0 :                 if (parent_side != side) {
     202                 :           0 :                         rotate(stack, stacksz);
     203                 :             :                 }
     204                 :             : 
     205                 :             :                 /* Rotate the grandparent with parent, swapping colors */
     206                 :           0 :                 rotate(stack, stacksz - 1);
     207                 :           0 :                 set_color(stack[stacksz - 3], BLACK);
     208                 :           0 :                 set_color(stack[stacksz - 2], RED);
     209                 :           0 :                 return;
     210                 :             :         }
     211                 :             : 
     212                 :             :         /* If we exit the loop, it's because our node is now the root,
     213                 :             :          * which must be black.
     214                 :             :          */
     215                 :           0 :         set_color(stack[0], BLACK);
     216                 :             : }
     217                 :             : 
     218                 :           0 : void rb_insert(struct rbtree *tree, struct rbnode *node)
     219                 :           0 : {
     220                 :           0 :         set_child(node, 0U, NULL);
     221                 :           0 :         set_child(node, 1U, NULL);
     222                 :             : 
     223         [ #  # ]:           0 :         if (tree->root == NULL) {
     224                 :           0 :                 tree->root = node;
     225                 :           0 :                 tree->max_depth = 1;
     226                 :           0 :                 set_color(node, BLACK);
     227                 :           0 :                 return;
     228                 :             :         }
     229                 :             : 
     230                 :             : #ifdef CONFIG_MISRA_SANE
     231                 :             :         struct rbnode **stack = &tree->iter_stack[0];
     232                 :             : #else
     233                 :           0 :         struct rbnode *stack[tree->max_depth + 1];
     234                 :             : #endif
     235                 :             : 
     236                 :           0 :         int stacksz = find_and_stack(tree, node, stack);
     237                 :             : 
     238                 :           0 :         struct rbnode *parent = stack[stacksz - 1];
     239                 :             : 
     240                 :           0 :         uint8_t side = tree->lessthan_fn(node, parent) ? 0U : 1U;
     241                 :             : 
     242                 :           0 :         set_child(parent, side, node);
     243                 :           0 :         set_color(node, RED);
     244                 :             : 
     245                 :           0 :         stack[stacksz] = node;
     246                 :           0 :         ++stacksz;
     247                 :           0 :         fix_extra_red(stack, stacksz);
     248                 :             : 
     249         [ #  # ]:           0 :         if (stacksz > tree->max_depth) {
     250                 :           0 :                 tree->max_depth = stacksz;
     251                 :             :         }
     252                 :             : 
     253                 :             :         /* We may have rotated up into the root! */
     254                 :           0 :         tree->root = stack[0];
     255                 :           0 :         CHECK(is_black(tree->root));
     256                 :             : }
     257                 :             : 
     258                 :             : /* Called for a node N (at the top of the stack) which after a
     259                 :             :  * deletion operation is "missing a black" in its subtree.  By
     260                 :             :  * construction N must be black (because if it was red it would be
     261                 :             :  * trivially fixed by recoloring and we wouldn't be here).  Fixes up
     262                 :             :  * the tree to preserve red/black rules.  The "null_node" pointer is
     263                 :             :  * for situations where we are removing a childless black node.  The
     264                 :             :  * tree munging needs a real node for simplicity, so we use it and
     265                 :             :  * then clean it up (replace it with a simple NULL child in the
     266                 :             :  * parent) when finished.
     267                 :             :  */
     268                 :           0 : static void fix_missing_black(struct rbnode **stack, int stacksz,
     269                 :             :                               struct rbnode *null_node)
     270                 :             : {
     271                 :             :         /* Loop upward until we reach the root */
     272         [ #  # ]:           0 :         while (stacksz > 1) {
     273                 :           0 :                 struct rbnode *c0, *c1, *inner, *outer;
     274                 :           0 :                 struct rbnode *n = stack[stacksz - 1];
     275                 :           0 :                 struct rbnode *parent = stack[stacksz - 2];
     276                 :           0 :                 uint8_t n_side = get_side(parent, n);
     277                 :           0 :                 struct rbnode *sib = get_child(parent,
     278                 :             :                                                (n_side == 0U) ? 1U : 0U);
     279                 :             : 
     280                 :           0 :                 CHECK(is_black(n));
     281                 :             : 
     282                 :             :                 /* Guarantee the sibling is black, rotating N down a
     283                 :             :                  * level if needed (after rotate() our parent is the
     284                 :             :                  * child of our previous-sibling, so N is lower in the
     285                 :             :                  * tree)
     286                 :             :                  */
     287         [ #  # ]:           0 :                 if (!is_black(sib)) {
     288                 :           0 :                         stack[stacksz - 1] = sib;
     289                 :           0 :                         rotate(stack, stacksz);
     290                 :           0 :                         set_color(parent, RED);
     291                 :           0 :                         set_color(sib, BLACK);
     292                 :           0 :                         stack[stacksz] = n;
     293                 :           0 :                         ++stacksz;
     294                 :             : 
     295                 :           0 :                         parent = stack[stacksz - 2];
     296                 :           0 :                         sib = get_child(parent, (n_side == 0U) ? 1U : 0U);
     297                 :             :                 }
     298                 :             : 
     299                 :           0 :                 CHECK(sib);
     300                 :             : 
     301                 :             :                 /* Cases where the sibling has only black children
     302                 :             :                  * have simple resolutions
     303                 :             :                  */
     304                 :           0 :                 c0 = get_child(sib, 0U);
     305                 :           0 :                 c1 = get_child(sib, 1U);
     306   [ #  #  #  #  :           0 :                 if (((c0 == NULL) || is_black(c0)) && ((c1 == NULL) ||
                   #  # ]
     307         [ #  # ]:           0 :                                         is_black(c1))) {
     308         [ #  # ]:           0 :                         if (n == null_node) {
     309                 :           0 :                                 set_child(parent, n_side, NULL);
     310                 :             :                         }
     311                 :             : 
     312                 :           0 :                         set_color(sib, RED);
     313         [ #  # ]:           0 :                         if (is_black(parent)) {
     314                 :             :                                 /* Balance the sibling's subtree by
     315                 :             :                                  * coloring it red, then our parent
     316                 :             :                                  * has a missing black so iterate
     317                 :             :                                  * upward
     318                 :             :                                  */
     319                 :           0 :                                 stacksz--;
     320                 :           0 :                                 continue;
     321                 :             :                         } else {
     322                 :             :                                 /* Recoloring makes the whole tree OK */
     323                 :           0 :                                 set_color(parent, BLACK);
     324                 :           0 :                                 return;
     325                 :             :                         }
     326                 :             :                 }
     327                 :             : 
     328                 :           0 :                 CHECK((c0 && is_red(c0)) || (c1 && is_red(c1)));
     329                 :             : 
     330                 :             :                 /* We know sibling has at least one red child.  Fix it
     331                 :             :                  * so that the far/outer position (i.e. on the
     332                 :             :                  * opposite side from N) is definitely red.
     333                 :             :                  */
     334                 :           0 :                 outer = get_child(sib, (n_side == 0U) ? 1U : 0U);
     335   [ #  #  #  # ]:           0 :                 if (!((outer != NULL) && is_red(outer))) {
     336                 :           0 :                         inner = get_child(sib, n_side);
     337                 :             : 
     338                 :           0 :                         stack[stacksz - 1] = sib;
     339                 :           0 :                         stack[stacksz] = inner;
     340                 :           0 :                         ++stacksz;
     341                 :           0 :                         rotate(stack, stacksz);
     342                 :           0 :                         set_color(sib, RED);
     343                 :           0 :                         set_color(inner, BLACK);
     344                 :             : 
     345                 :             :                         /* Restore stack state to have N on the top
     346                 :             :                          * and make sib reflect the new sibling
     347                 :             :                          */
     348                 :           0 :                         sib = stack[stacksz - 2];
     349                 :           0 :                         outer = get_child(sib, (n_side == 0U) ? 1U : 0U);
     350                 :           0 :                         stack[stacksz - 2] = n;
     351                 :           0 :                         stacksz--;
     352                 :             :                 }
     353                 :             : 
     354                 :             :                 /* Finally, the sibling must have a red child in the
     355                 :             :                  * far/outer slot.  We can rotate sib with our parent
     356                 :             :                  * and recolor to produce a valid tree.
     357                 :             :                  */
     358                 :           0 :                 CHECK(is_red(outer));
     359                 :           0 :                 set_color(sib, get_color(parent));
     360                 :           0 :                 set_color(parent, BLACK);
     361                 :           0 :                 set_color(outer, BLACK);
     362                 :           0 :                 stack[stacksz - 1] = sib;
     363                 :           0 :                 rotate(stack, stacksz);
     364         [ #  # ]:           0 :                 if (n == null_node) {
     365                 :           0 :                         set_child(parent, n_side, NULL);
     366                 :             :                 }
     367                 :             :                 return;
     368                 :             :         }
     369                 :             : }
     370                 :             : 
     371                 :           0 : void rb_remove(struct rbtree *tree, struct rbnode *node)
     372                 :           0 : {
     373                 :           0 :         struct rbnode *tmp;
     374                 :             : #ifdef CONFIG_MISRA_SANE
     375                 :             :         struct rbnode **stack = &tree->iter_stack[0];
     376                 :             : #else
     377                 :           0 :         struct rbnode *stack[tree->max_depth + 1];
     378                 :             : #endif
     379                 :             : 
     380                 :           0 :         int stacksz = find_and_stack(tree, node, stack);
     381                 :             : 
     382         [ #  # ]:           0 :         if (node != stack[stacksz - 1]) {
     383                 :           0 :                 return;
     384                 :             :         }
     385                 :             : 
     386                 :             :         /* We can only remove a node with zero or one child, if we
     387                 :             :          * have two then pick the "biggest" child of side 0 (smallest
     388                 :             :          * of 1 would work too) and swap our spot in the tree with
     389                 :             :          * that one
     390                 :             :          */
     391   [ #  #  #  # ]:           0 :         if ((get_child(node, 0U) != NULL) && (get_child(node, 1U) != NULL)) {
     392                 :           0 :                 int stacksz0 = stacksz;
     393                 :           0 :                 struct rbnode *hiparent, *loparent;
     394                 :           0 :                 struct rbnode *node2 = get_child(node, 0U);
     395                 :             : 
     396         [ #  # ]:           0 :                 hiparent = (stacksz > 1) ? stack[stacksz - 2] : NULL;
     397                 :           0 :                 stack[stacksz] = node2;
     398                 :           0 :                 ++stacksz;
     399         [ #  # ]:           0 :                 while (get_child(node2, 1U) != NULL) {
     400                 :           0 :                         node2 = get_child(node2, 1U);
     401                 :           0 :                         stack[stacksz] = node2;
     402                 :           0 :                         ++stacksz;
     403                 :             :                 }
     404                 :             : 
     405                 :           0 :                 loparent = stack[stacksz - 2];
     406                 :             : 
     407                 :             :                 /* Now swap the position of node/node2 in the tree.
     408                 :             :                  * Design note: this is a spot where being an
     409                 :             :                  * intrusive data structure hurts us fairly badly.
     410                 :             :                  * The trees you see in textbooks do this by swapping
     411                 :             :                  * the "data" pointers between the two nodes, but we
     412                 :             :                  * have a few special cases to check.  In principle
     413                 :             :                  * this works by swapping the child pointers between
     414                 :             :                  * the nodes and retargeting the nodes pointing to
     415                 :             :                  * them from their parents, but: (1) the upper node
     416                 :             :                  * may be the root of the tree and not have a parent,
     417                 :             :                  * and (2) the lower node may be a direct child of the
     418                 :             :                  * upper node.  Remember to swap the color bits of the
     419                 :             :                  * two nodes also.  And of course we don't have parent
     420                 :             :                  * pointers, so the stack tracking this structure
     421                 :             :                  * needs to be swapped too!
     422                 :             :                  */
     423         [ #  # ]:           0 :                 if (hiparent != NULL) {
     424                 :           0 :                         set_child(hiparent, get_side(hiparent, node), node2);
     425                 :             :                 } else {
     426                 :           0 :                         tree->root = node2;
     427                 :             :                 }
     428                 :             : 
     429         [ #  # ]:           0 :                 if (loparent == node) {
     430                 :           0 :                         set_child(node, 0U, get_child(node2, 0U));
     431                 :           0 :                         set_child(node2, 0U, node);
     432                 :             :                 } else {
     433                 :           0 :                         set_child(loparent, get_side(loparent, node2), node);
     434                 :           0 :                         tmp = get_child(node, 0U);
     435                 :           0 :                         set_child(node, 0U, get_child(node2, 0U));
     436                 :           0 :                         set_child(node2, 0U, tmp);
     437                 :             :                 }
     438                 :             : 
     439                 :           0 :                 set_child(node2, 1U, get_child(node, 1U));
     440                 :           0 :                 set_child(node, 1U, NULL);
     441                 :             : 
     442                 :           0 :                 tmp = stack[stacksz0 - 1];
     443                 :           0 :                 stack[stacksz0 - 1] = stack[stacksz - 1];
     444                 :           0 :                 stack[stacksz - 1] = tmp;
     445                 :             : 
     446                 :           0 :                 enum rb_color ctmp = get_color(node);
     447                 :             : 
     448                 :           0 :                 set_color(node, get_color(node2));
     449                 :           0 :                 set_color(node2, ctmp);
     450                 :             :         }
     451                 :             : 
     452                 :             :         CHECK((get_child(node, 0U) == NULL) ||
     453                 :           0 :               (get_child(node, 1U) == NULL));
     454                 :             : 
     455                 :           0 :         struct rbnode *child = get_child(node, 0U);
     456                 :             : 
     457         [ #  # ]:           0 :         if (child == NULL) {
     458                 :           0 :                 child = get_child(node, 1U);
     459                 :             :         }
     460                 :             : 
     461                 :             :         /* Removing the root */
     462         [ #  # ]:           0 :         if (stacksz < 2) {
     463                 :           0 :                 tree->root = child;
     464         [ #  # ]:           0 :                 if (child != NULL) {
     465                 :           0 :                         set_color(child, BLACK);
     466                 :             :                 } else {
     467                 :           0 :                         tree->max_depth = 0;
     468                 :             :                 }
     469                 :           0 :                 return;
     470                 :             :         }
     471                 :             : 
     472                 :           0 :         struct rbnode *parent = stack[stacksz - 2];
     473                 :             : 
     474                 :             :         /* Special case: if the node to be removed is childless, then
     475                 :             :          * we leave it in place while we do the missing black
     476                 :             :          * rotations, which will replace it with a proper NULL when
     477                 :             :          * they isolate it.
     478                 :             :          */
     479         [ #  # ]:           0 :         if (child == NULL) {
     480         [ #  # ]:           0 :                 if (is_black(node)) {
     481                 :           0 :                         fix_missing_black(stack, stacksz, node);
     482                 :             :                 } else {
     483                 :             :                         /* Red childless nodes can just be dropped */
     484                 :           0 :                         set_child(parent, get_side(parent, node), NULL);
     485                 :             :                 }
     486                 :             :         } else {
     487                 :           0 :                 set_child(parent, get_side(parent, node), child);
     488                 :             : 
     489                 :             :                 /* Check colors, if one was red (at least one must have been
     490                 :             :                  * black in a valid tree), then we're done.
     491                 :             :                  */
     492   [ #  #  #  # ]:           0 :                 __ASSERT(is_black(node) || is_black(child), "both nodes red?!");
     493   [ #  #  #  # ]:           0 :                 if (is_red(node) || is_red(child)) {
     494                 :           0 :                         set_color(child, BLACK);
     495                 :             :                 }
     496                 :             :         }
     497                 :             : 
     498                 :             :         /* We may have rotated up into the root! */
     499                 :           0 :         tree->root = stack[0];
     500                 :             : }
     501                 :             : 
     502                 :             : #ifndef CONFIG_MISRA_SANE
     503                 :           0 : void z_rb_walk(struct rbnode *node, rb_visit_t visit_fn, void *cookie)
     504                 :             : {
     505         [ #  # ]:           0 :         if (node != NULL) {
     506                 :           0 :                 z_rb_walk(get_child(node, 0U), visit_fn, cookie);
     507                 :           0 :                 visit_fn(node, cookie);
     508                 :           0 :                 z_rb_walk(get_child(node, 1U), visit_fn, cookie);
     509                 :             :         }
     510                 :           0 : }
     511                 :             : #endif
     512                 :             : 
     513                 :           0 : struct rbnode *z_rb_child(struct rbnode *node, uint8_t side)
     514                 :             : {
     515                 :           0 :         return get_child(node, side);
     516                 :             : }
     517                 :             : 
     518                 :           0 : int z_rb_is_black(struct rbnode *node)
     519                 :             : {
     520                 :           0 :         return is_black(node);
     521                 :             : }
     522                 :             : 
     523                 :           0 : bool rb_contains(struct rbtree *tree, struct rbnode *node)
     524                 :             : {
     525                 :           0 :         struct rbnode *n = tree->root;
     526                 :             : 
     527         [ #  # ]:           0 :         while ((n != NULL) && (n != node)) {
     528                 :           0 :                 n = get_child(n, tree->lessthan_fn(n, node));
     529                 :             :         }
     530                 :             : 
     531                 :           0 :         return n == node;
     532                 :             : }
     533                 :             : 
     534                 :             : /* Pushes the node and its chain of left-side children onto the stack
     535                 :             :  * in the foreach struct, returning the last node, which is the next
     536                 :             :  * node to iterate.  By construction node will always be a right child
     537                 :             :  * or the root, so is_left must be false.
     538                 :             :  */
     539                 :           0 : static inline struct rbnode *stack_left_limb(struct rbnode *n,
     540                 :             :                                              struct _rb_foreach *f)
     541                 :             : {
     542                 :           0 :         f->top++;
     543                 :           0 :         f->stack[f->top] = n;
     544                 :           0 :         f->is_left[f->top] = 0U;
     545                 :             : 
     546         [ #  # ]:           0 :         for (n = get_child(n, 0U); n != NULL; n = get_child(n, 0U)) {
     547                 :           0 :                 f->top++;
     548                 :           0 :                 f->stack[f->top] = n;
     549                 :           0 :                 f->is_left[f->top] = 1;
     550                 :             :         }
     551                 :             : 
     552                 :           0 :         return f->stack[f->top];
     553                 :             : }
     554                 :             : 
     555                 :             : /* The foreach tracking works via a dynamic stack allocated via
     556                 :             :  * alloca().  The current node is found in stack[top] (and its parent
     557                 :             :  * is thus stack[top-1]).  The side of each stacked node from its
     558                 :             :  * parent is stored in is_left[] (i.e. if is_left[top] is true, then
     559                 :             :  * node/stack[top] is the left child of stack[top-1]).  The special
     560                 :             :  * case of top == -1 indicates that the stack is uninitialized and we
     561                 :             :  * need to push an initial stack starting at the root.
     562                 :             :  */
     563                 :           0 : struct rbnode *z_rb_foreach_next(struct rbtree *tree, struct _rb_foreach *f)
     564                 :             : {
     565                 :           0 :         struct rbnode *n;
     566                 :             : 
     567         [ #  # ]:           0 :         if (tree->root == NULL) {
     568                 :             :                 return NULL;
     569                 :             :         }
     570                 :             : 
     571                 :             :         /* Initialization condition, pick the leftmost child of the
     572                 :             :          * root as our first node, initializing the stack on the way.
     573                 :             :          */
     574         [ #  # ]:           0 :         if (f->top == -1) {
     575                 :           0 :                 return stack_left_limb(tree->root, f);
     576                 :             :         }
     577                 :             : 
     578                 :             :         /* The next child from a given node is the leftmost child of
     579                 :             :          * it's right subtree if it has a right child
     580                 :             :          */
     581                 :           0 :         n = get_child(f->stack[f->top], 1U);
     582         [ #  # ]:           0 :         if (n != NULL) {
     583                 :           0 :                 return stack_left_limb(n, f);
     584                 :             :         }
     585                 :             : 
     586                 :             :         /* Otherwise if the node is a left child of its parent, the
     587                 :             :          * next node is the parent (note that the root is stacked
     588                 :             :          * above with is_left set to 0, so this condition still works
     589                 :             :          * even if node has no parent).
     590                 :             :          */
     591         [ #  # ]:           0 :         if (f->is_left[f->top] != 0U) {
     592                 :           0 :                 return f->stack[--f->top];
     593                 :             :         }
     594                 :             : 
     595                 :             :         /* If we had no left tree and are a right child then our
     596                 :             :          * parent was already walked, so walk up the stack looking for
     597                 :             :          * a left child (whose parent is unwalked, and thus next).
     598                 :             :          */
     599   [ #  #  #  # ]:           0 :         while ((f->top > 0) && (f->is_left[f->top] == 0U)) {
     600                 :           0 :                 f->top--;
     601                 :             :         }
     602                 :             : 
     603                 :           0 :         f->top--;
     604         [ #  # ]:           0 :         return (f->top >= 0) ? f->stack[f->top] : NULL;
     605                 :             : }
        

Generated by: LCOV version 2.0-1