]>
git.proxmox.com Git - mirror_frr.git/blob - lib/typesafe.h
2 * Copyright (c) 2016-2019 David Lamparter, for NetDEF, Inc.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #ifndef _FRR_TYPESAFE_H
18 #define _FRR_TYPESAFE_H
26 /* generic macros for all list-like types */
28 #define for_each(prefix, head, item) \
29 for (item = prefix##_first(head); item; \
30 item = prefix##_next(head, item))
31 #define for_each_safe(prefix, head, item) \
32 for (typeof(prefix##_next_safe(head, NULL)) prefix##_safe = \
33 prefix##_next_safe(head, \
34 (item = prefix##_first(head))); \
36 item = prefix##_safe, \
37 prefix##_safe = prefix##_next_safe(head, prefix##_safe))
38 #define for_each_from(prefix, head, item, from) \
39 for (item = from, from = prefix##_next_safe(head, item); \
41 item = from, from = prefix##_next_safe(head, from))
43 /* single-linked list, unsorted/arbitrary.
44 * can be used as queue with add_tail / pop
47 /* don't use these structs directly */
49 struct slist_item
*next
;
53 struct slist_item
*first
, **last_next
;
57 static inline void typesafe_list_add(struct slist_head
*head
,
58 struct slist_item
**pos
, struct slist_item
*item
)
62 if (pos
== head
->last_next
)
63 head
->last_next
= &item
->next
;
69 * PREDECL_LIST(namelist)
71 * struct namelist_item nlitem;
73 * DECLARE_LIST(namelist, struct name, nlitem)
75 #define PREDECL_LIST(prefix) \
76 struct prefix ## _head { struct slist_head sh; }; \
77 struct prefix ## _item { struct slist_item si; };
79 #define INIT_LIST(var) { .sh = { .last_next = &var.sh.first, }, }
81 #define DECLARE_LIST(prefix, type, field) \
83 macro_inline void prefix ## _init(struct prefix##_head *h) \
85 memset(h, 0, sizeof(*h)); \
86 h->sh.last_next = &h->sh.first; \
88 macro_inline void prefix ## _fini(struct prefix##_head *h) \
90 memset(h, 0, sizeof(*h)); \
92 macro_inline void prefix ## _add_head(struct prefix##_head *h, type *item) \
94 typesafe_list_add(&h->sh, &h->sh.first, &item->field.si); \
96 macro_inline void prefix ## _add_tail(struct prefix##_head *h, type *item) \
98 typesafe_list_add(&h->sh, h->sh.last_next, &item->field.si); \
100 macro_inline void prefix ## _add_after(struct prefix##_head *h, \
101 type *after, type *item) \
103 struct slist_item **nextp; \
104 nextp = after ? &after->field.si.next : &h->sh.first; \
105 typesafe_list_add(&h->sh, nextp, &item->field.si); \
107 /* TODO: del_hint */ \
108 macro_inline void prefix ## _del(struct prefix##_head *h, type *item) \
110 struct slist_item **iter = &h->sh.first; \
111 while (*iter && *iter != &item->field.si) \
112 iter = &(*iter)->next; \
116 *iter = item->field.si.next; \
117 if (!item->field.si.next) \
118 h->sh.last_next = iter; \
120 macro_inline type *prefix ## _pop(struct prefix##_head *h) \
122 struct slist_item *sitem = h->sh.first; \
126 h->sh.first = sitem->next; \
127 if (h->sh.first == NULL) \
128 h->sh.last_next = &h->sh.first; \
129 return container_of(sitem, type, field.si); \
131 macro_pure type *prefix ## _first(struct prefix##_head *h) \
133 return container_of_null(h->sh.first, type, field.si); \
135 macro_pure type *prefix ## _next(struct prefix##_head * h, type *item) \
137 struct slist_item *sitem = &item->field.si; \
138 return container_of_null(sitem->next, type, field.si); \
140 macro_pure type *prefix ## _next_safe(struct prefix##_head *h, type *item) \
142 struct slist_item *sitem; \
145 sitem = &item->field.si; \
146 return container_of_null(sitem->next, type, field.si); \
148 macro_pure size_t prefix ## _count(struct prefix##_head *h) \
150 return h->sh.count; \
154 /* single-linked list, sorted.
155 * can be used as priority queue with add / pop
158 /* don't use these structs directly */
160 struct ssort_item
*next
;
164 struct ssort_item
*first
;
170 * PREDECL_SORTLIST(namelist)
172 * struct namelist_item nlitem;
174 * DECLARE_SORTLIST(namelist, struct name, nlitem)
176 #define _PREDECL_SORTLIST(prefix) \
177 struct prefix ## _head { struct ssort_head sh; }; \
178 struct prefix ## _item { struct ssort_item si; };
180 #define INIT_SORTLIST_UNIQ(var) { }
181 #define INIT_SORTLIST_NONUNIQ(var) { }
183 #define PREDECL_SORTLIST_UNIQ(prefix) \
184 _PREDECL_SORTLIST(prefix)
185 #define PREDECL_SORTLIST_NONUNIQ(prefix) \
186 _PREDECL_SORTLIST(prefix)
188 #define _DECLARE_SORTLIST(prefix, type, field, cmpfn_nuq, cmpfn_uq) \
190 macro_inline void prefix ## _init(struct prefix##_head *h) \
192 memset(h, 0, sizeof(*h)); \
194 macro_inline void prefix ## _fini(struct prefix##_head *h) \
196 memset(h, 0, sizeof(*h)); \
198 macro_inline type *prefix ## _add(struct prefix##_head *h, type *item) \
200 struct ssort_item **np = &h->sh.first; \
202 while (*np && (c = cmpfn_uq( \
203 container_of(*np, type, field.si), item)) < 0) \
206 return container_of(*np, type, field.si); \
207 item->field.si.next = *np; \
208 *np = &item->field.si; \
212 macro_inline type *prefix ## _find_gteq(struct prefix##_head *h, \
215 struct ssort_item *sitem = h->sh.first; \
217 while (sitem && (cmpval = cmpfn_nuq( \
218 container_of(sitem, type, field.si), item) < 0)) \
219 sitem = sitem->next; \
220 return container_of_null(sitem, type, field.si); \
222 macro_inline type *prefix ## _find_lt(struct prefix##_head *h, \
225 struct ssort_item *prev = NULL, *sitem = h->sh.first; \
227 while (sitem && (cmpval = cmpfn_nuq( \
228 container_of(sitem, type, field.si), item) < 0)) \
229 sitem = (prev = sitem)->next; \
230 return container_of_null(prev, type, field.si); \
232 /* TODO: del_hint */ \
233 macro_inline void prefix ## _del(struct prefix##_head *h, type *item) \
235 struct ssort_item **iter = &h->sh.first; \
236 while (*iter && *iter != &item->field.si) \
237 iter = &(*iter)->next; \
241 *iter = item->field.si.next; \
243 macro_inline type *prefix ## _pop(struct prefix##_head *h) \
245 struct ssort_item *sitem = h->sh.first; \
249 h->sh.first = sitem->next; \
250 return container_of(sitem, type, field.si); \
252 macro_pure type *prefix ## _first(struct prefix##_head *h) \
254 return container_of_null(h->sh.first, type, field.si); \
256 macro_pure type *prefix ## _next(struct prefix##_head *h, type *item) \
258 struct ssort_item *sitem = &item->field.si; \
259 return container_of_null(sitem->next, type, field.si); \
261 macro_pure type *prefix ## _next_safe(struct prefix##_head *h, type *item) \
263 struct ssort_item *sitem; \
266 sitem = &item->field.si; \
267 return container_of_null(sitem->next, type, field.si); \
269 macro_pure size_t prefix ## _count(struct prefix##_head *h) \
271 return h->sh.count; \
275 #define DECLARE_SORTLIST_UNIQ(prefix, type, field, cmpfn) \
276 _DECLARE_SORTLIST(prefix, type, field, cmpfn, cmpfn) \
278 macro_inline type *prefix ## _find(const struct prefix##_head *h, const type *item) \
280 struct ssort_item *sitem = h->sh.first; \
282 while (sitem && (cmpval = cmpfn( \
283 container_of(sitem, type, field.si), item) < 0)) \
284 sitem = sitem->next; \
285 if (!sitem || cmpval > 0) \
287 return container_of(sitem, type, field.si); \
291 #define DECLARE_SORTLIST_NONUNIQ(prefix, type, field, cmpfn) \
292 macro_inline int _ ## prefix ## _cmp(const type *a, const type *b) \
294 int cmpval = cmpfn(a, b); \
303 _DECLARE_SORTLIST(prefix, type, field, cmpfn, _ ## prefix ## _cmp) \
307 /* hash, "sorted" by hash value
310 /* don't use these structs directly */
312 struct thash_item
*next
;
317 struct thash_item
**entries
;
321 uint8_t minshift
, maxshift
;
324 #define _HASH_SIZE(tabshift) \
325 ((1U << (tabshift)) >> 1)
326 #define HASH_SIZE(head) \
327 _HASH_SIZE((head).tabshift)
328 #define _HASH_KEY(tabshift, val) \
329 ((val) >> (33 - (tabshift)))
330 #define HASH_KEY(head, val) \
331 _HASH_KEY((head).tabshift, val)
332 #define HASH_GROW_THRESHOLD(head) \
333 ((head).count >= HASH_SIZE(head))
334 #define HASH_SHRINK_THRESHOLD(head) \
335 ((head).count <= (HASH_SIZE(head) - 1) / 2)
337 extern void typesafe_hash_grow(struct thash_head
*head
);
338 extern void typesafe_hash_shrink(struct thash_head
*head
);
342 * PREDECL_HASH(namelist)
344 * struct namelist_item nlitem;
346 * DECLARE_HASH(namelist, struct name, nlitem, cmpfunc, hashfunc)
348 #define PREDECL_HASH(prefix) \
349 struct prefix ## _head { struct thash_head hh; }; \
350 struct prefix ## _item { struct thash_item hi; };
352 #define INIT_HASH(var) { }
354 #define DECLARE_HASH(prefix, type, field, cmpfn, hashfn) \
356 macro_inline void prefix ## _init(struct prefix##_head *h) \
358 memset(h, 0, sizeof(*h)); \
360 macro_inline void prefix ## _fini(struct prefix##_head *h) \
362 assert(h->hh.count == 0); \
363 h->hh.minshift = 0; \
364 typesafe_hash_shrink(&h->hh); \
365 memset(h, 0, sizeof(*h)); \
367 macro_inline type *prefix ## _add(struct prefix##_head *h, type *item) \
370 if (!h->hh.tabshift || HASH_GROW_THRESHOLD(h->hh)) \
371 typesafe_hash_grow(&h->hh); \
373 uint32_t hval = hashfn(item), hbits = HASH_KEY(h->hh, hval); \
374 item->field.hi.hashval = hval; \
375 struct thash_item **np = &h->hh.entries[hbits]; \
376 while (*np && (*np)->hashval < hval) \
378 if (*np && cmpfn(container_of(*np, type, field.hi), item) == 0) { \
380 return container_of(*np, type, field.hi); \
382 item->field.hi.next = *np; \
383 *np = &item->field.hi; \
386 macro_inline type *prefix ## _find(const struct prefix##_head *h, const type *item) \
388 if (!h->hh.tabshift) \
390 uint32_t hval = hashfn(item), hbits = HASH_KEY(h->hh, hval); \
391 struct thash_item *hitem = h->hh.entries[hbits]; \
392 while (hitem && hitem->hashval < hval) \
393 hitem = hitem->next; \
394 while (hitem && hitem->hashval == hval) { \
395 if (!cmpfn(container_of(hitem, type, field.hi), item)) \
396 return container_of(hitem, type, field.hi); \
397 hitem = hitem->next; \
401 macro_inline void prefix ## _del(struct prefix##_head *h, type *item) \
403 if (!h->hh.tabshift) \
405 uint32_t hval = item->field.hi.hashval, hbits = HASH_KEY(h->hh, hval); \
406 struct thash_item **np = &h->hh.entries[hbits]; \
407 while (*np && (*np)->hashval < hval) \
409 while (*np && *np != &item->field.hi && (*np)->hashval == hval) \
411 if (*np != &item->field.hi) \
413 *np = item->field.hi.next; \
414 item->field.hi.next = NULL; \
416 if (HASH_SHRINK_THRESHOLD(h->hh)) \
417 typesafe_hash_shrink(&h->hh); \
419 macro_inline type *prefix ## _pop(struct prefix##_head *h) \
422 for (i = 0; i < HASH_SIZE(h->hh); i++) \
423 if (h->hh.entries[i]) { \
424 struct thash_item *hitem = h->hh.entries[i]; \
425 h->hh.entries[i] = hitem->next; \
427 hitem->next = NULL; \
428 if (HASH_SHRINK_THRESHOLD(h->hh)) \
429 typesafe_hash_shrink(&h->hh); \
430 return container_of(hitem, type, field.hi); \
434 macro_pure type *prefix ## _first(struct prefix##_head *h) \
437 for (i = 0; i < HASH_SIZE(h->hh); i++) \
438 if (h->hh.entries[i]) \
439 return container_of(h->hh.entries[i], type, field.hi); \
442 macro_pure type *prefix ## _next(struct prefix##_head *h, type *item) \
444 struct thash_item *hitem = &item->field.hi; \
446 return container_of(hitem->next, type, field.hi); \
447 uint32_t i = HASH_KEY(h->hh, hitem->hashval) + 1; \
448 for (; i < HASH_SIZE(h->hh); i++) \
449 if (h->hh.entries[i]) \
450 return container_of(h->hh.entries[i], type, field.hi); \
453 macro_pure type *prefix ## _next_safe(struct prefix##_head *h, type *item) \
457 return prefix ## _next(h, item); \
459 macro_pure size_t prefix ## _count(struct prefix##_head *h) \
461 return h->hh.count; \
466 * can be used as priority queue with add / pop
469 /* don't use these structs directly */
470 #define SKIPLIST_MAXDEPTH 16
471 #define SKIPLIST_EMBED 4
472 #define SKIPLIST_OVERFLOW (SKIPLIST_EMBED - 1)
475 struct sskip_item
*next
[SKIPLIST_EMBED
];
478 struct sskip_overflow
{
479 struct sskip_item
*next
[SKIPLIST_MAXDEPTH
- SKIPLIST_OVERFLOW
];
483 struct sskip_item hitem
;
484 struct sskip_item
*overflow
[SKIPLIST_MAXDEPTH
- SKIPLIST_OVERFLOW
];
490 * PREDECL_SKIPLIST(namelist)
492 * struct namelist_item nlitem;
494 * DECLARE_SKIPLIST(namelist, struct name, nlitem, cmpfunc)
496 #define _PREDECL_SKIPLIST(prefix) \
497 struct prefix ## _head { struct sskip_head sh; }; \
498 struct prefix ## _item { struct sskip_item si; };
500 #define INIT_SKIPLIST_UNIQ(var) { }
501 #define INIT_SKIPLIST_NONUNIQ(var) { }
503 #define _DECLARE_SKIPLIST(prefix, type, field, cmpfn_nuq, cmpfn_uq) \
505 macro_inline void prefix ## _init(struct prefix##_head *h) \
507 memset(h, 0, sizeof(*h)); \
508 h->sh.hitem.next[SKIPLIST_OVERFLOW] = (struct sskip_item *) \
509 ((uintptr_t)h->sh.overflow | 1); \
511 macro_inline void prefix ## _fini(struct prefix##_head *h) \
513 memset(h, 0, sizeof(*h)); \
515 macro_inline type *prefix ## _add(struct prefix##_head *h, type *item) \
517 struct sskip_item *si; \
518 si = typesafe_skiplist_add(&h->sh, &item->field.si, cmpfn_uq); \
519 return container_of_null(si, type, field.si); \
521 macro_inline type *prefix ## _find_gteq(struct prefix##_head *h, \
524 struct sskip_item *sitem = typesafe_skiplist_find_gteq(&h->sh, \
525 &item->field.si, cmpfn_nuq); \
526 return container_of_null(sitem, type, field.si); \
528 macro_inline type *prefix ## _find_lt(struct prefix##_head *h, \
531 struct sskip_item *sitem = typesafe_skiplist_find_lt(&h->sh, \
532 &item->field.si, cmpfn_nuq); \
533 return container_of_null(sitem, type, field.si); \
535 macro_inline void prefix ## _del(struct prefix##_head *h, type *item) \
537 typesafe_skiplist_del(&h->sh, &item->field.si, cmpfn_uq); \
539 macro_inline type *prefix ## _pop(struct prefix##_head *h) \
541 struct sskip_item *sitem = h->sh.hitem.next[0]; \
544 typesafe_skiplist_del(&h->sh, sitem, cmpfn_uq); \
545 return container_of(sitem, type, field.si); \
547 macro_pure type *prefix ## _first(struct prefix##_head *h) \
549 struct sskip_item *first = h->sh.hitem.next[0]; \
550 return container_of_null(first, type, field.si); \
552 macro_pure type *prefix ## _next(struct prefix##_head *h, type *item) \
554 struct sskip_item *next = item->field.si.next[0]; \
555 return container_of_null(next, type, field.si); \
557 macro_pure type *prefix ## _next_safe(struct prefix##_head *h, type *item) \
559 struct sskip_item *next; \
560 next = item ? item->field.si.next[0] : NULL; \
561 return container_of_null(next, type, field.si); \
563 macro_pure size_t prefix ## _count(struct prefix##_head *h) \
565 return h->sh.count; \
569 #define PREDECL_SKIPLIST_UNIQ(prefix) \
570 _PREDECL_SKIPLIST(prefix)
571 #define DECLARE_SKIPLIST_UNIQ(prefix, type, field, cmpfn) \
573 macro_inline int prefix ## __cmp(const struct sskip_item *a, \
574 const struct sskip_item *b) \
576 return cmpfn(container_of(a, type, field.si), \
577 container_of(b, type, field.si)); \
579 macro_inline type *prefix ## _find(const struct prefix##_head *h, const type *item) \
581 struct sskip_item *sitem = typesafe_skiplist_find(&h->sh, \
582 &item->field.si, &prefix ## __cmp); \
583 return container_of_null(sitem, type, field.si); \
586 _DECLARE_SKIPLIST(prefix, type, field, \
587 prefix ## __cmp, prefix ## __cmp) \
590 #define PREDECL_SKIPLIST_NONUNIQ(prefix) \
591 _PREDECL_SKIPLIST(prefix)
592 #define DECLARE_SKIPLIST_NONUNIQ(prefix, type, field, cmpfn) \
594 macro_inline int prefix ## __cmp(const struct sskip_item *a, \
595 const struct sskip_item *b) \
597 return cmpfn(container_of(a, type, field.si), \
598 container_of(b, type, field.si)); \
600 macro_inline int prefix ## __cmp_uq(const struct sskip_item *a, \
601 const struct sskip_item *b) \
603 int cmpval = cmpfn(container_of(a, type, field.si), \
604 container_of(b, type, field.si)); \
614 _DECLARE_SKIPLIST(prefix, type, field, \
615 prefix ## __cmp, prefix ## __cmp_uq) \
619 extern struct sskip_item
*typesafe_skiplist_add(struct sskip_head
*head
,
620 struct sskip_item
*item
, int (*cmpfn
)(
621 const struct sskip_item
*a
,
622 const struct sskip_item
*b
));
623 extern struct sskip_item
*typesafe_skiplist_find(struct sskip_head
*head
,
624 const struct sskip_item
*item
, int (*cmpfn
)(
625 const struct sskip_item
*a
,
626 const struct sskip_item
*b
));
627 extern struct sskip_item
*typesafe_skiplist_find_gteq(struct sskip_head
*head
,
628 const struct sskip_item
*item
, int (*cmpfn
)(
629 const struct sskip_item
*a
,
630 const struct sskip_item
*b
));
631 extern struct sskip_item
*typesafe_skiplist_find_lt(struct sskip_head
*head
,
632 const struct sskip_item
*item
, int (*cmpfn
)(
633 const struct sskip_item
*a
,
634 const struct sskip_item
*b
));
635 extern void typesafe_skiplist_del(struct sskip_head
*head
,
636 struct sskip_item
*item
, int (*cmpfn
)(
637 const struct sskip_item
*a
,
638 const struct sskip_item
*b
));
640 /* this needs to stay at the end because both files include each other.
641 * the resolved order is typesafe.h before typerb.h
645 #endif /* _FRR_TYPESAFE_H */