Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
rb.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7/* Our SDK/toolchains integration seems to be inconsistent about
8 * whether they expose alloca.h or not. On gcc it's a moot point as
9 * it's always builtin.
10 */
11#ifdef __GNUC__
12#ifndef alloca
13#define alloca __builtin_alloca
14#endif
15#else
16#include <alloca.h>
17#endif
18
43#ifndef ZEPHYR_INCLUDE_SYS_RB_H_
44#define ZEPHYR_INCLUDE_SYS_RB_H_
45
46#include <stdbool.h>
47#include <stdint.h>
48
49struct rbnode {
50 struct rbnode *children[2];
51};
52
53/* Theoretical maximum depth of tree based on pointer size. If memory
54 * is filled with 2-pointer nodes, and the tree can be twice as a
55 * packed binary tree, plus root... Works out to 59 entries for 32
56 * bit pointers and 121 at 64 bits.
57 */
58#define Z_TBITS(t) ((sizeof(t)) < 8 ? 2 : 3)
59#define Z_PBITS(t) (8 * sizeof(t))
60#define Z_MAX_RBTREE_DEPTH (2 * (Z_PBITS(int *) - Z_TBITS(int *) - 1) + 1)
61
62
81typedef bool (*rb_lessthan_t)(struct rbnode *a, struct rbnode *b);
82
83struct rbtree {
84 struct rbnode *root;
87#ifdef CONFIG_MISRA_SANE
88 struct rbnode *iter_stack[Z_MAX_RBTREE_DEPTH];
89 unsigned char iter_left[Z_MAX_RBTREE_DEPTH];
90#endif
91};
92
93typedef void (*rb_visit_t)(struct rbnode *node, void *cookie);
94
95struct rbnode *z_rb_child(struct rbnode *node, uint8_t side);
96int z_rb_is_black(struct rbnode *node);
97#ifndef CONFIG_MISRA_SANE
98void z_rb_walk(struct rbnode *node, rb_visit_t visit_fn, void *cookie);
99#endif
100struct rbnode *z_rb_get_minmax(struct rbtree *tree, uint8_t side);
101
105void rb_insert(struct rbtree *tree, struct rbnode *node);
106
110void rb_remove(struct rbtree *tree, struct rbnode *node);
111
115static inline struct rbnode *rb_get_min(struct rbtree *tree)
116{
117 return z_rb_get_minmax(tree, 0U);
118}
119
123static inline struct rbnode *rb_get_max(struct rbtree *tree)
124{
125 return z_rb_get_minmax(tree, 1U);
126}
127
137bool rb_contains(struct rbtree *tree, struct rbnode *node);
138
139#ifndef CONFIG_MISRA_SANE
148static inline void rb_walk(struct rbtree *tree, rb_visit_t visit_fn,
149 void *cookie)
150{
151 z_rb_walk(tree->root, visit_fn, cookie);
152}
153#endif
154
155struct _rb_foreach {
156 struct rbnode **stack;
157 uint8_t *is_left;
158 int32_t top;
159};
160
161#ifdef CONFIG_MISRA_SANE
162#define _RB_FOREACH_INIT(tree, node) { \
163 .stack = &(tree)->iter_stack[0], \
164 .is_left = &(tree)->iter_left[0], \
165 .top = -1 \
166}
167#else
168#define _RB_FOREACH_INIT(tree, node) { \
169 .stack = (struct rbnode **) \
170 alloca((tree)->max_depth * sizeof(struct rbnode *)), \
171 .is_left = (uint8_t *)alloca((tree)->max_depth * sizeof(uint8_t)),\
172 .top = -1 \
173}
174#endif
175
176struct rbnode *z_rb_foreach_next(struct rbtree *tree, struct _rb_foreach *f);
177
199#define RB_FOR_EACH(tree, node) \
200 for (struct _rb_foreach __f = _RB_FOREACH_INIT(tree, node); \
201 (node = z_rb_foreach_next(tree, &__f)); \
202 )
203
214#define RB_FOR_EACH_CONTAINER(tree, node, field) \
215 for (struct _rb_foreach __f = _RB_FOREACH_INIT(tree, node); \
216 ({struct rbnode *n = z_rb_foreach_next(tree, &__f); \
217 node = n ? CONTAINER_OF(n, __typeof__(*(node)), \
218 field) : NULL; }) != NULL; \
219 )
220
223#endif /* ZEPHYR_INCLUDE_SYS_RB_H_ */
void
Definition: eswifi_shell.c:15
static struct rbnode * rb_get_max(struct rbtree *tree)
Returns the highest-sorted member of the tree.
Definition: rb.h:123
bool(* rb_lessthan_t)(struct rbnode *a, struct rbnode *b)
Red/black tree comparison predicate.
Definition: rb.h:81
void(* rb_visit_t)(struct rbnode *node, void *cookie)
Definition: rb.h:93
static struct rbnode * rb_get_min(struct rbtree *tree)
Returns the lowest-sorted member of the tree.
Definition: rb.h:115
void rb_insert(struct rbtree *tree, struct rbnode *node)
Insert node into tree.
static void rb_walk(struct rbtree *tree, rb_visit_t visit_fn, void *cookie)
Walk/enumerate a rbtree.
Definition: rb.h:148
void rb_remove(struct rbtree *tree, struct rbnode *node)
Remove node from tree.
bool rb_contains(struct rbtree *tree, struct rbnode *node)
Returns true if the given node is part of the tree.
struct k_futex f
Definition: kobject.c:1319
struct k_stack stack
Definition: test_stack_contexts.c:18
#define bool
Definition: stdbool.h:13
__INT32_TYPE__ int32_t
Definition: stdint.h:44
__UINT8_TYPE__ uint8_t
Definition: stdint.h:58
Definition: rb.h:49
struct rbnode * children[2]
Definition: rb.h:50
Definition: rb.h:83
int max_depth
Definition: rb.h:86
struct rbnode * root
Definition: rb.h:84
rb_lessthan_t lessthan_fn
Definition: rb.h:85