]> git.proxmox.com Git - mirror_frr.git/blame - lib/typerb.h
lib: null out deleted pointers in typesafe containers
[mirror_frr.git] / lib / typerb.h
CommitLineData
80911bc2
DL
1/*
2 * The following Red-Black tree implementation is based off code with
3 * original copyright:
4 *
5 * Copyright (c) 2016 David Gwynne <dlg@openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#ifndef _FRR_TYPERB_H
21#define _FRR_TYPERB_H
22
23#include "typesafe.h"
24
eea3c899
RW
25#ifdef __cplusplus
26extern "C" {
27#endif
28
80911bc2
DL
29struct typed_rb_entry {
30 struct typed_rb_entry *rbt_parent;
31 struct typed_rb_entry *rbt_left;
32 struct typed_rb_entry *rbt_right;
33 unsigned int rbt_color;
34};
35
36struct typed_rb_root {
37 struct typed_rb_entry *rbt_root;
38 size_t count;
39};
40
571a045b 41struct typed_rb_entry *typed_rb_insert(struct typed_rb_root *rbt,
80911bc2
DL
42 struct typed_rb_entry *rbe,
43 int (*cmpfn)(
44 const struct typed_rb_entry *a,
45 const struct typed_rb_entry *b));
571a045b 46struct typed_rb_entry *typed_rb_remove(struct typed_rb_root *rbt,
da098d78 47 struct typed_rb_entry *rbe);
daf3441d 48const struct typed_rb_entry *typed_rb_find(const struct typed_rb_root *rbt,
80911bc2
DL
49 const struct typed_rb_entry *rbe,
50 int (*cmpfn)(
51 const struct typed_rb_entry *a,
52 const struct typed_rb_entry *b));
daf3441d 53const struct typed_rb_entry *typed_rb_find_gteq(const struct typed_rb_root *rbt,
80911bc2
DL
54 const struct typed_rb_entry *rbe,
55 int (*cmpfn)(
56 const struct typed_rb_entry *a,
57 const struct typed_rb_entry *b));
daf3441d 58const struct typed_rb_entry *typed_rb_find_lt(const struct typed_rb_root *rbt,
80911bc2
DL
59 const struct typed_rb_entry *rbe,
60 int (*cmpfn)(
61 const struct typed_rb_entry *a,
62 const struct typed_rb_entry *b));
daf3441d
DL
63struct typed_rb_entry *typed_rb_min(const struct typed_rb_root *rbt);
64struct typed_rb_entry *typed_rb_next(const struct typed_rb_entry *rbe);
80911bc2
DL
65
66#define _PREDECL_RBTREE(prefix) \
67struct prefix ## _head { struct typed_rb_root rr; }; \
960b9a53
DL
68struct prefix ## _item { struct typed_rb_entry re; }; \
69MACRO_REQUIRE_SEMICOLON() /* end */
80911bc2
DL
70
71#define INIT_RBTREE_UNIQ(var) { }
72#define INIT_RBTREE_NONUNIQ(var) { }
73
74#define _DECLARE_RBTREE(prefix, type, field, cmpfn_nuq, cmpfn_uq) \
75 \
76macro_inline void prefix ## _init(struct prefix##_head *h) \
77{ \
78 memset(h, 0, sizeof(*h)); \
79} \
80macro_inline void prefix ## _fini(struct prefix##_head *h) \
81{ \
82 memset(h, 0, sizeof(*h)); \
83} \
84macro_inline type *prefix ## _add(struct prefix##_head *h, type *item) \
85{ \
86 struct typed_rb_entry *re; \
87 re = typed_rb_insert(&h->rr, &item->field.re, cmpfn_uq); \
88 return container_of_null(re, type, field.re); \
89} \
daf3441d
DL
90macro_inline const type *prefix ## _const_find_gteq( \
91 const struct prefix##_head *h, const type *item) \
80911bc2 92{ \
daf3441d 93 const struct typed_rb_entry *re; \
80911bc2
DL
94 re = typed_rb_find_gteq(&h->rr, &item->field.re, cmpfn_nuq); \
95 return container_of_null(re, type, field.re); \
96} \
daf3441d
DL
97macro_inline const type *prefix ## _const_find_lt( \
98 const struct prefix##_head *h, const type *item) \
80911bc2 99{ \
daf3441d 100 const struct typed_rb_entry *re; \
80911bc2
DL
101 re = typed_rb_find_lt(&h->rr, &item->field.re, cmpfn_nuq); \
102 return container_of_null(re, type, field.re); \
103} \
daf3441d 104TYPESAFE_FIND_CMP(prefix, type) \
da098d78 105macro_inline type *prefix ## _del(struct prefix##_head *h, type *item) \
80911bc2 106{ \
da098d78
SW
107 struct typed_rb_entry *re; \
108 re = typed_rb_remove(&h->rr, &item->field.re); \
109 return container_of_null(re, type, field.re); \
80911bc2
DL
110} \
111macro_inline type *prefix ## _pop(struct prefix##_head *h) \
112{ \
113 struct typed_rb_entry *re; \
114 re = typed_rb_min(&h->rr); \
115 if (!re) \
116 return NULL; \
117 typed_rb_remove(&h->rr, re); \
118 return container_of(re, type, field.re); \
119} \
507e0e5d 120TYPESAFE_SWAP_ALL_SIMPLE(prefix) \
daf3441d 121macro_pure const type *prefix ## _const_first(const struct prefix##_head *h) \
80911bc2 122{ \
daf3441d 123 const struct typed_rb_entry *re; \
80911bc2
DL
124 re = typed_rb_min(&h->rr); \
125 return container_of_null(re, type, field.re); \
126} \
daf3441d
DL
127macro_pure const type *prefix ## _const_next(const struct prefix##_head *h, \
128 const type *item) \
80911bc2 129{ \
daf3441d 130 const struct typed_rb_entry *re; \
80911bc2
DL
131 re = typed_rb_next(&item->field.re); \
132 return container_of_null(re, type, field.re); \
133} \
daf3441d 134TYPESAFE_FIRST_NEXT(prefix, type) \
80911bc2
DL
135macro_pure type *prefix ## _next_safe(struct prefix##_head *h, type *item) \
136{ \
137 struct typed_rb_entry *re; \
138 re = item ? typed_rb_next(&item->field.re) : NULL; \
139 return container_of_null(re, type, field.re); \
140} \
0ca16c58 141macro_pure size_t prefix ## _count(const struct prefix##_head *h) \
80911bc2
DL
142{ \
143 return h->rr.count; \
144} \
960b9a53 145MACRO_REQUIRE_SEMICOLON() /* end */
80911bc2
DL
146
147#define PREDECL_RBTREE_UNIQ(prefix) \
148 _PREDECL_RBTREE(prefix)
149#define DECLARE_RBTREE_UNIQ(prefix, type, field, cmpfn) \
150 \
151macro_inline int prefix ## __cmp(const struct typed_rb_entry *a, \
152 const struct typed_rb_entry *b) \
153{ \
154 return cmpfn(container_of(a, type, field.re), \
155 container_of(b, type, field.re)); \
156} \
daf3441d
DL
157macro_inline const type *prefix ## _const_find(const struct prefix##_head *h, \
158 const type *item) \
80911bc2 159{ \
daf3441d 160 const struct typed_rb_entry *re; \
80911bc2
DL
161 re = typed_rb_find(&h->rr, &item->field.re, &prefix ## __cmp); \
162 return container_of_null(re, type, field.re); \
163} \
daf3441d 164TYPESAFE_FIND(prefix, type) \
80911bc2 165 \
960b9a53
DL
166_DECLARE_RBTREE(prefix, type, field, prefix ## __cmp, prefix ## __cmp); \
167MACRO_REQUIRE_SEMICOLON() /* end */
80911bc2
DL
168
169#define PREDECL_RBTREE_NONUNIQ(prefix) \
170 _PREDECL_RBTREE(prefix)
171#define DECLARE_RBTREE_NONUNIQ(prefix, type, field, cmpfn) \
172 \
173macro_inline int prefix ## __cmp(const struct typed_rb_entry *a, \
174 const struct typed_rb_entry *b) \
175{ \
176 return cmpfn(container_of(a, type, field.re), \
177 container_of(b, type, field.re)); \
178} \
179macro_inline int prefix ## __cmp_uq(const struct typed_rb_entry *a, \
180 const struct typed_rb_entry *b) \
181{ \
182 int cmpval = cmpfn(container_of(a, type, field.re), \
183 container_of(b, type, field.re)); \
184 if (cmpval) \
185 return cmpval; \
186 if (a < b) \
187 return -1; \
188 if (a > b) \
189 return 1; \
190 return 0; \
191} \
192 \
960b9a53
DL
193_DECLARE_RBTREE(prefix, type, field, prefix ## __cmp, prefix ## __cmp_uq); \
194MACRO_REQUIRE_SEMICOLON() /* end */
80911bc2 195
eea3c899
RW
196#ifdef __cplusplus
197}
198#endif
199
80911bc2 200#endif /* _FRR_TYPERB_H */