123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- #ifdef __GNUC__
- #ifndef alloca
- #define alloca __builtin_alloca
- #endif
- #else
- #include <alloca.h>
- #endif
- #ifndef ZEPHYR_INCLUDE_SYS_RB_H_
- #define ZEPHYR_INCLUDE_SYS_RB_H_
- #include <stdbool.h>
- #include <stdint.h>
- struct rbnode {
- struct rbnode *children[2];
- };
- #define Z_TBITS(t) ((sizeof(t)) < 8 ? 2 : 3)
- #define Z_PBITS(t) (8 * sizeof(t))
- #define Z_MAX_RBTREE_DEPTH (2 * (Z_PBITS(int *) - Z_TBITS(int *) - 1) + 1)
-
- typedef bool (*rb_lessthan_t)(struct rbnode *a, struct rbnode *b);
- struct rbtree {
- struct rbnode *root;
- rb_lessthan_t lessthan_fn;
- int max_depth;
- #ifdef CONFIG_MISRA_SANE
- struct rbnode *iter_stack[Z_MAX_RBTREE_DEPTH];
- unsigned char iter_left[Z_MAX_RBTREE_DEPTH];
- #endif
- };
- typedef void (*rb_visit_t)(struct rbnode *node, void *cookie);
- struct rbnode *z_rb_child(struct rbnode *node, uint8_t side);
- int z_rb_is_black(struct rbnode *node);
- #ifndef CONFIG_MISRA_SANE
- void z_rb_walk(struct rbnode *node, rb_visit_t visit_fn, void *cookie);
- #endif
- struct rbnode *z_rb_get_minmax(struct rbtree *tree, uint8_t side);
- void rb_insert(struct rbtree *tree, struct rbnode *node);
- void rb_remove(struct rbtree *tree, struct rbnode *node);
- static inline struct rbnode *rb_get_min(struct rbtree *tree)
- {
- return z_rb_get_minmax(tree, 0U);
- }
- static inline struct rbnode *rb_get_max(struct rbtree *tree)
- {
- return z_rb_get_minmax(tree, 1U);
- }
- bool rb_contains(struct rbtree *tree, struct rbnode *node);
- #ifndef CONFIG_MISRA_SANE
- static inline void rb_walk(struct rbtree *tree, rb_visit_t visit_fn,
- void *cookie)
- {
- z_rb_walk(tree->root, visit_fn, cookie);
- }
- #endif
- struct _rb_foreach {
- struct rbnode **stack;
- uint8_t *is_left;
- int32_t top;
- };
- #ifdef CONFIG_MISRA_SANE
- #define _RB_FOREACH_INIT(tree, node) { \
- .stack = &(tree)->iter_stack[0], \
- .is_left = &(tree)->iter_left[0], \
- .top = -1 \
- }
- #else
- #define _RB_FOREACH_INIT(tree, node) { \
- .stack = (struct rbnode **) \
- alloca((tree)->max_depth * sizeof(struct rbnode *)), \
- .is_left = (uint8_t *)alloca((tree)->max_depth * sizeof(uint8_t)),\
- .top = -1 \
- }
- #endif
- struct rbnode *z_rb_foreach_next(struct rbtree *tree, struct _rb_foreach *f);
- #define RB_FOR_EACH(tree, node) \
- for (struct _rb_foreach __f = _RB_FOREACH_INIT(tree, node); \
- (node = z_rb_foreach_next(tree, &__f)); \
- )
- #define RB_FOR_EACH_CONTAINER(tree, node, field) \
- for (struct _rb_foreach __f = _RB_FOREACH_INIT(tree, node); \
- ({struct rbnode *n = z_rb_foreach_next(tree, &__f); \
- node = n ? CONTAINER_OF(n, __typeof__(*(node)), \
- field) : NULL; }) != NULL; \
- )
- #endif
|