Zephyr API Documentation 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
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
39
40#ifndef ZEPHYR_INCLUDE_SYS_RB_H_
41#define ZEPHYR_INCLUDE_SYS_RB_H_
42
43#include <stdbool.h>
44#include <stdint.h>
45
46/* Our SDK/toolchains integration seems to be inconsistent about
47 * whether they expose alloca.h or not. On gcc it's a moot point as
48 * it's always builtin.
49 */
50#ifdef __GNUC__
51#ifndef alloca
52#define alloca __builtin_alloca
53#endif
54#else
55#include <alloca.h>
56#endif
57
61struct rbnode {
63 struct rbnode *children[2];
65};
66
67/* Theoretical maximum depth of tree based on pointer size. If memory
68 * is filled with 2-pointer nodes, and the tree can be twice as a
69 * packed binary tree, plus root... Works out to 59 entries for 32
70 * bit pointers and 121 at 64 bits.
71 */
72#define Z_TBITS(t) ((sizeof(t)) < sizeof(uint64_t) ? 2 : 3)
73#define Z_PBITS(t) (BITS_PER_BYTE * sizeof(t))
74#define Z_MAX_RBTREE_DEPTH (2 * (Z_PBITS(int *) - Z_TBITS(int *) - 1) + 1)
75
89typedef bool (*rb_lessthan_t)(struct rbnode *a, struct rbnode *b);
90
94struct rbtree {
96 struct rbnode *root;
100 int max_depth;
101#ifdef CONFIG_MISRA_SANE
102 struct rbnode *iter_stack[Z_MAX_RBTREE_DEPTH];
103 unsigned char iter_left[Z_MAX_RBTREE_DEPTH];
104#endif
106};
107
113typedef void (*rb_visit_t)(struct rbnode *node, void *cookie);
114
115struct rbnode *z_rb_child(struct rbnode *node, uint8_t side);
116int z_rb_is_black(struct rbnode *node);
117#ifndef CONFIG_MISRA_SANE
118void z_rb_walk(struct rbnode *node, rb_visit_t visit_fn, void *cookie);
119#endif
120struct rbnode *z_rb_get_minmax(struct rbtree *tree, uint8_t side);
121
125void rb_insert(struct rbtree *tree, struct rbnode *node);
126
130void rb_remove(struct rbtree *tree, struct rbnode *node);
131
135static inline struct rbnode *rb_get_min(struct rbtree *tree)
136{
137 return z_rb_get_minmax(tree, 0U);
138}
139
143static inline struct rbnode *rb_get_max(struct rbtree *tree)
144{
145 return z_rb_get_minmax(tree, 1U);
146}
147
157bool rb_contains(struct rbtree *tree, struct rbnode *node);
158
159#ifndef CONFIG_MISRA_SANE
168static inline void rb_walk(struct rbtree *tree, rb_visit_t visit_fn,
169 void *cookie)
170{
171 z_rb_walk(tree->root, visit_fn, cookie);
172}
173#endif
174
175struct _rb_foreach {
176 struct rbnode **stack;
177 uint8_t *is_left;
178 int32_t top;
179};
180
181#ifdef CONFIG_MISRA_SANE
182#define _RB_FOREACH_INIT(tree, node) { \
183 .stack = &(tree)->iter_stack[0], \
184 .is_left = &(tree)->iter_left[0], \
185 .top = -1 \
186}
187#else
188#define _RB_FOREACH_INIT(tree, node) { \
189 .stack = (struct rbnode **) \
190 alloca((tree)->max_depth * sizeof(struct rbnode *)), \
191 .is_left = (uint8_t *)alloca((tree)->max_depth * sizeof(uint8_t)),\
192 .top = -1 \
193}
194#endif
195
196struct rbnode *z_rb_foreach_next(struct rbtree *tree, struct _rb_foreach *f);
197
219#define RB_FOR_EACH(tree, node) \
220 for (struct _rb_foreach __f = _RB_FOREACH_INIT(tree, node); \
221 ((node) = z_rb_foreach_next((tree), &__f)); \
222 /**/)
223
234#define RB_FOR_EACH_CONTAINER(tree, node, field) \
235 for (struct _rb_foreach __f = _RB_FOREACH_INIT(tree, node); \
236 ({struct rbnode *n = z_rb_foreach_next(tree, &__f); \
237 (node) = n ? CONTAINER_OF(n, __typeof__(*(node)), \
238 field) : NULL; (node); }) != NULL; \
239 /**/)
240
242
243#endif /* ZEPHYR_INCLUDE_SYS_RB_H_ */
static struct rbnode * rb_get_max(struct rbtree *tree)
Returns the highest-sorted member of the tree.
Definition rb.h:143
void(* rb_visit_t)(struct rbnode *node, void *cookie)
Prototype for node visitor callback.
Definition rb.h:113
static struct rbnode * rb_get_min(struct rbtree *tree)
Returns the lowest-sorted member of the tree.
Definition rb.h:135
void rb_insert(struct rbtree *tree, struct rbnode *node)
Insert node into tree.
bool(* rb_lessthan_t)(struct rbnode *a, struct rbnode *b)
Red/black tree comparison predicate.
Definition rb.h:89
static void rb_walk(struct rbtree *tree, rb_visit_t visit_fn, void *cookie)
Walk/enumerate a rbtree.
Definition rb.h:168
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.
#define bool
Definition stdbool.h:13
__INT32_TYPE__ int32_t
Definition stdint.h:74
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
Balanced red/black tree node structure.
Definition rb.h:61
Balanced red/black tree structure.
Definition rb.h:94
struct rbnode * root
Root node of the tree.
Definition rb.h:96
rb_lessthan_t lessthan_fn
Comparison function for nodes in the tree.
Definition rb.h:98