]>
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
30 /* generic macros for all list-like types */
32 #define frr_each(prefix, head, item) \
33 for (item = prefix##_first(head); item; \
34 item = prefix##_next(head, item))
35 #define frr_each_safe(prefix, head, item) \
36 for (typeof(prefix##_next_safe(head, NULL)) prefix##_safe = \
37 prefix##_next_safe(head, \
38 (item = prefix##_first(head))); \
40 item = prefix##_safe, \
41 prefix##_safe = prefix##_next_safe(head, prefix##_safe))
42 #define frr_each_from(prefix, head, item, from) \
43 for (item = from, from = prefix##_next_safe(head, item); \
45 item = from, from = prefix##_next_safe(head, from))
47 /* single-linked list, unsorted/arbitrary.
48 * can be used as queue with add_tail / pop
51 /* don't use these structs directly */
53 struct slist_item
*next
;
57 struct slist_item
*first
, **last_next
;
61 static inline void typesafe_list_add(struct slist_head
*head
,
62 struct slist_item
**pos
, struct slist_item
*item
)
66 if (pos
== head
->last_next
)
67 head
->last_next
= &item
->next
;
73 * PREDECL_LIST(namelist)
75 * struct namelist_item nlitem;
77 * DECLARE_LIST(namelist, struct name, nlitem)
79 #define PREDECL_LIST(prefix) \
80 struct prefix ## _head { struct slist_head sh; }; \
81 struct prefix ## _item { struct slist_item si; };
83 #define INIT_LIST(var) { .sh = { .last_next = &var.sh.first, }, }
85 #define DECLARE_LIST(prefix, type, field) \
87 macro_inline void prefix ## _init(struct prefix##_head *h) \
89 memset(h, 0, sizeof(*h)); \
90 h->sh.last_next = &h->sh.first; \
92 macro_inline void prefix ## _fini(struct prefix##_head *h) \
94 memset(h, 0, sizeof(*h)); \
96 macro_inline void prefix ## _add_head(struct prefix##_head *h, type *item) \
98 typesafe_list_add(&h->sh, &h->sh.first, &item->field.si); \
100 macro_inline void prefix ## _add_tail(struct prefix##_head *h, type *item) \
102 typesafe_list_add(&h->sh, h->sh.last_next, &item->field.si); \
104 macro_inline void prefix ## _add_after(struct prefix##_head *h, \
105 type *after, type *item) \
107 struct slist_item **nextp; \
108 nextp = after ? &after->field.si.next : &h->sh.first; \
109 typesafe_list_add(&h->sh, nextp, &item->field.si); \
111 /* TODO: del_hint */ \
112 macro_inline type *prefix ## _del(struct prefix##_head *h, type *item) \
114 struct slist_item **iter = &h->sh.first; \
115 while (*iter && *iter != &item->field.si) \
116 iter = &(*iter)->next; \
120 *iter = item->field.si.next; \
121 if (!item->field.si.next) \
122 h->sh.last_next = iter; \
125 macro_inline type *prefix ## _pop(struct prefix##_head *h) \
127 struct slist_item *sitem = h->sh.first; \
131 h->sh.first = sitem->next; \
132 if (h->sh.first == NULL) \
133 h->sh.last_next = &h->sh.first; \
134 return container_of(sitem, type, field.si); \
136 macro_pure type *prefix ## _first(struct prefix##_head *h) \
138 return container_of_null(h->sh.first, type, field.si); \
140 macro_pure type *prefix ## _next(struct prefix##_head * h, type *item) \
142 struct slist_item *sitem = &item->field.si; \
143 return container_of_null(sitem->next, type, field.si); \
145 macro_pure type *prefix ## _next_safe(struct prefix##_head *h, type *item) \
147 struct slist_item *sitem; \
150 sitem = &item->field.si; \
151 return container_of_null(sitem->next, type, field.si); \
153 macro_pure size_t prefix ## _count(const struct prefix##_head *h) \
155 return h->sh.count; \
159 /* don't use these structs directly */
161 struct dlist_item
*next
;
162 struct dlist_item
*prev
;
166 struct dlist_item hitem
;
170 static inline void typesafe_dlist_add(struct dlist_head
*head
,
171 struct dlist_item
*prev
, struct dlist_item
*item
)
173 item
->next
= prev
->next
;
174 item
->next
->prev
= item
;
180 /* double-linked list, for fast item deletion
182 #define PREDECL_DLIST(prefix) \
183 struct prefix ## _head { struct dlist_head dh; }; \
184 struct prefix ## _item { struct dlist_item di; };
186 #define INIT_DLIST(var) { .dh = { \
187 .hitem = { &var.dh.hitem, &var.dh.hitem }, }, }
189 #define DECLARE_DLIST(prefix, type, field) \
191 macro_inline void prefix ## _init(struct prefix##_head *h) \
193 memset(h, 0, sizeof(*h)); \
194 h->dh.hitem.prev = &h->dh.hitem; \
195 h->dh.hitem.next = &h->dh.hitem; \
197 macro_inline void prefix ## _fini(struct prefix##_head *h) \
199 memset(h, 0, sizeof(*h)); \
201 macro_inline void prefix ## _add_head(struct prefix##_head *h, type *item) \
203 typesafe_dlist_add(&h->dh, &h->dh.hitem, &item->field.di); \
205 macro_inline void prefix ## _add_tail(struct prefix##_head *h, type *item) \
207 typesafe_dlist_add(&h->dh, h->dh.hitem.prev, &item->field.di); \
209 macro_inline void prefix ## _add_after(struct prefix##_head *h, \
210 type *after, type *item) \
212 struct dlist_item *prev; \
213 prev = after ? &after->field.di : &h->dh.hitem; \
214 typesafe_dlist_add(&h->dh, prev, &item->field.di); \
216 macro_inline type *prefix ## _del(struct prefix##_head *h, type *item) \
218 struct dlist_item *ditem = &item->field.di; \
219 ditem->prev->next = ditem->next; \
220 ditem->next->prev = ditem->prev; \
222 ditem->prev = ditem->next = NULL; \
225 macro_inline type *prefix ## _pop(struct prefix##_head *h) \
227 struct dlist_item *ditem = h->dh.hitem.next; \
228 if (ditem == &h->dh.hitem) \
230 ditem->prev->next = ditem->next; \
231 ditem->next->prev = ditem->prev; \
233 return container_of(ditem, type, field.di); \
235 macro_pure type *prefix ## _first(struct prefix##_head *h) \
237 struct dlist_item *ditem = h->dh.hitem.next; \
238 if (ditem == &h->dh.hitem) \
240 return container_of(ditem, type, field.di); \
242 macro_pure type *prefix ## _next(struct prefix##_head * h, type *item) \
244 struct dlist_item *ditem = &item->field.di; \
245 if (ditem->next == &h->dh.hitem) \
247 return container_of(ditem->next, type, field.di); \
249 macro_pure type *prefix ## _next_safe(struct prefix##_head *h, type *item) \
253 return prefix ## _next(h, item); \
255 macro_pure size_t prefix ## _count(const struct prefix##_head *h) \
257 return h->dh.count; \
261 /* note: heap currently caps out at 4G items */
264 typedef uint32_t heap_index_i
;
271 struct heap_item
**array
;
272 uint32_t arraysz
, count
;
275 #define HEAP_RESIZE_TRESH_UP(h) \
276 (h->hh.count + 1 >= h->hh.arraysz)
277 #define HEAP_RESIZE_TRESH_DN(h) \
278 (h->hh.count == 0 || \
279 h->hh.arraysz - h->hh.count > (h->hh.count + 1024) / 2)
281 #define PREDECL_HEAP(prefix) \
282 struct prefix ## _head { struct heap_head hh; }; \
283 struct prefix ## _item { struct heap_item hi; };
285 #define INIT_HEAP(var) { }
287 #define DECLARE_HEAP(prefix, type, field, cmpfn) \
289 macro_inline void prefix ## _init(struct prefix##_head *h) \
291 memset(h, 0, sizeof(*h)); \
293 macro_inline void prefix ## _fini(struct prefix##_head *h) \
295 assert(h->hh.count == 0); \
296 memset(h, 0, sizeof(*h)); \
298 macro_inline int prefix ## __cmp(const struct heap_item *a, \
299 const struct heap_item *b) \
301 return cmpfn(container_of(a, type, field.hi), \
302 container_of(b, type, field.hi)); \
304 macro_inline type *prefix ## _add(struct prefix##_head *h, type *item) \
306 if (HEAP_RESIZE_TRESH_UP(h)) \
307 typesafe_heap_resize(&h->hh, true); \
308 typesafe_heap_pullup(&h->hh, h->hh.count, &item->field.hi, \
313 macro_inline type *prefix ## _del(struct prefix##_head *h, type *item) \
315 struct heap_item *other; \
316 uint32_t index = item->field.hi.index; \
317 assert(h->hh.array[index] == &item->field.hi); \
319 other = h->hh.array[h->hh.count]; \
320 if (cmpfn(container_of(other, type, field.hi), item) < 0) \
321 typesafe_heap_pullup(&h->hh, index, other, prefix ## __cmp); \
323 typesafe_heap_pushdown(&h->hh, index, other, prefix ## __cmp); \
324 if (HEAP_RESIZE_TRESH_DN(h)) \
325 typesafe_heap_resize(&h->hh, false); \
328 macro_inline type *prefix ## _pop(struct prefix##_head *h) \
330 struct heap_item *hitem, *other; \
331 if (h->hh.count == 0) \
333 hitem = h->hh.array[0]; \
335 other = h->hh.array[h->hh.count]; \
336 typesafe_heap_pushdown(&h->hh, 0, other, prefix ## __cmp); \
337 if (HEAP_RESIZE_TRESH_DN(h)) \
338 typesafe_heap_resize(&h->hh, false); \
339 return container_of(hitem, type, field.hi); \
341 macro_pure type *prefix ## _first(struct prefix##_head *h) \
343 if (h->hh.count == 0) \
345 return container_of(h->hh.array[0], type, field.hi); \
347 macro_pure type *prefix ## _next(struct prefix##_head *h, type *item) \
349 uint32_t idx = item->field.hi.index + 1; \
350 if (idx >= h->hh.count) \
352 return container_of(h->hh.array[idx], type, field.hi); \
354 macro_pure type *prefix ## _next_safe(struct prefix##_head *h, type *item) \
358 return prefix ## _next(h, item); \
360 macro_pure size_t prefix ## _count(const struct prefix##_head *h) \
362 return h->hh.count; \
366 extern void typesafe_heap_resize(struct heap_head
*head
, bool grow
);
367 extern void typesafe_heap_pushdown(struct heap_head
*head
, uint32_t index
,
368 struct heap_item
*item
,
369 int (*cmpfn
)(const struct heap_item
*a
,
370 const struct heap_item
*b
));
371 extern void typesafe_heap_pullup(struct heap_head
*head
, uint32_t index
,
372 struct heap_item
*item
,
373 int (*cmpfn
)(const struct heap_item
*a
,
374 const struct heap_item
*b
));
376 /* single-linked list, sorted.
377 * can be used as priority queue with add / pop
380 /* don't use these structs directly */
382 struct ssort_item
*next
;
386 struct ssort_item
*first
;
392 * PREDECL_SORTLIST(namelist)
394 * struct namelist_item nlitem;
396 * DECLARE_SORTLIST(namelist, struct name, nlitem)
398 #define _PREDECL_SORTLIST(prefix) \
399 struct prefix ## _head { struct ssort_head sh; }; \
400 struct prefix ## _item { struct ssort_item si; };
402 #define INIT_SORTLIST_UNIQ(var) { }
403 #define INIT_SORTLIST_NONUNIQ(var) { }
405 #define PREDECL_SORTLIST_UNIQ(prefix) \
406 _PREDECL_SORTLIST(prefix)
407 #define PREDECL_SORTLIST_NONUNIQ(prefix) \
408 _PREDECL_SORTLIST(prefix)
410 #define _DECLARE_SORTLIST(prefix, type, field, cmpfn_nuq, cmpfn_uq) \
412 macro_inline void prefix ## _init(struct prefix##_head *h) \
414 memset(h, 0, sizeof(*h)); \
416 macro_inline void prefix ## _fini(struct prefix##_head *h) \
418 memset(h, 0, sizeof(*h)); \
420 macro_inline type *prefix ## _add(struct prefix##_head *h, type *item) \
422 struct ssort_item **np = &h->sh.first; \
424 while (*np && (c = cmpfn_uq( \
425 container_of(*np, type, field.si), item)) < 0) \
428 return container_of(*np, type, field.si); \
429 item->field.si.next = *np; \
430 *np = &item->field.si; \
434 macro_inline type *prefix ## _find_gteq(struct prefix##_head *h, \
437 struct ssort_item *sitem = h->sh.first; \
439 while (sitem && (cmpval = cmpfn_nuq( \
440 container_of(sitem, type, field.si), item)) < 0) \
441 sitem = sitem->next; \
442 return container_of_null(sitem, type, field.si); \
444 macro_inline type *prefix ## _find_lt(struct prefix##_head *h, \
447 struct ssort_item *prev = NULL, *sitem = h->sh.first; \
449 while (sitem && (cmpval = cmpfn_nuq( \
450 container_of(sitem, type, field.si), item)) < 0) \
451 sitem = (prev = sitem)->next; \
452 return container_of_null(prev, type, field.si); \
454 /* TODO: del_hint */ \
455 macro_inline type *prefix ## _del(struct prefix##_head *h, type *item) \
457 struct ssort_item **iter = &h->sh.first; \
458 while (*iter && *iter != &item->field.si) \
459 iter = &(*iter)->next; \
463 *iter = item->field.si.next; \
466 macro_inline type *prefix ## _pop(struct prefix##_head *h) \
468 struct ssort_item *sitem = h->sh.first; \
472 h->sh.first = sitem->next; \
473 return container_of(sitem, type, field.si); \
475 macro_pure type *prefix ## _first(struct prefix##_head *h) \
477 return container_of_null(h->sh.first, type, field.si); \
479 macro_pure type *prefix ## _next(struct prefix##_head *h, type *item) \
481 struct ssort_item *sitem = &item->field.si; \
482 return container_of_null(sitem->next, type, field.si); \
484 macro_pure type *prefix ## _next_safe(struct prefix##_head *h, type *item) \
486 struct ssort_item *sitem; \
489 sitem = &item->field.si; \
490 return container_of_null(sitem->next, type, field.si); \
492 macro_pure size_t prefix ## _count(const struct prefix##_head *h) \
494 return h->sh.count; \
498 #define DECLARE_SORTLIST_UNIQ(prefix, type, field, cmpfn) \
499 _DECLARE_SORTLIST(prefix, type, field, cmpfn, cmpfn) \
501 macro_inline type *prefix ## _find(struct prefix##_head *h, const type *item) \
503 struct ssort_item *sitem = h->sh.first; \
505 while (sitem && (cmpval = cmpfn( \
506 container_of(sitem, type, field.si), item)) < 0) \
507 sitem = sitem->next; \
508 if (!sitem || cmpval > 0) \
510 return container_of(sitem, type, field.si); \
514 #define DECLARE_SORTLIST_NONUNIQ(prefix, type, field, cmpfn) \
515 macro_inline int _ ## prefix ## _cmp(const type *a, const type *b) \
517 int cmpval = cmpfn(a, b); \
526 _DECLARE_SORTLIST(prefix, type, field, cmpfn, _ ## prefix ## _cmp) \
530 /* hash, "sorted" by hash value
533 /* don't use these structs directly */
535 struct thash_item
*next
;
540 struct thash_item
**entries
;
544 uint8_t minshift
, maxshift
;
547 #define _HASH_SIZE(tabshift) \
548 ((1U << (tabshift)) >> 1)
549 #define HASH_SIZE(head) \
550 _HASH_SIZE((head).tabshift)
551 #define _HASH_KEY(tabshift, val) \
552 ((val) >> (33 - (tabshift)))
553 #define HASH_KEY(head, val) \
554 _HASH_KEY((head).tabshift, val)
555 #define HASH_GROW_THRESHOLD(head) \
556 ((head).count >= HASH_SIZE(head))
557 #define HASH_SHRINK_THRESHOLD(head) \
558 ((head).count <= (HASH_SIZE(head) - 1) / 2)
560 extern void typesafe_hash_grow(struct thash_head
*head
);
561 extern void typesafe_hash_shrink(struct thash_head
*head
);
565 * PREDECL_HASH(namelist)
567 * struct namelist_item nlitem;
569 * DECLARE_HASH(namelist, struct name, nlitem, cmpfunc, hashfunc)
571 #define PREDECL_HASH(prefix) \
572 struct prefix ## _head { struct thash_head hh; }; \
573 struct prefix ## _item { struct thash_item hi; };
575 #define INIT_HASH(var) { }
577 #define DECLARE_HASH(prefix, type, field, cmpfn, hashfn) \
579 macro_inline void prefix ## _init(struct prefix##_head *h) \
581 memset(h, 0, sizeof(*h)); \
583 macro_inline void prefix ## _fini(struct prefix##_head *h) \
585 assert(h->hh.count == 0); \
586 h->hh.minshift = 0; \
587 typesafe_hash_shrink(&h->hh); \
588 memset(h, 0, sizeof(*h)); \
590 macro_inline type *prefix ## _add(struct prefix##_head *h, type *item) \
593 if (!h->hh.tabshift || HASH_GROW_THRESHOLD(h->hh)) \
594 typesafe_hash_grow(&h->hh); \
596 uint32_t hval = hashfn(item), hbits = HASH_KEY(h->hh, hval); \
597 item->field.hi.hashval = hval; \
598 struct thash_item **np = &h->hh.entries[hbits]; \
599 while (*np && (*np)->hashval < hval) \
601 if (*np && cmpfn(container_of(*np, type, field.hi), item) == 0) { \
603 return container_of(*np, type, field.hi); \
605 item->field.hi.next = *np; \
606 *np = &item->field.hi; \
609 macro_inline type *prefix ## _find(struct prefix##_head *h, const type *item) \
611 if (!h->hh.tabshift) \
613 uint32_t hval = hashfn(item), hbits = HASH_KEY(h->hh, hval); \
614 struct thash_item *hitem = h->hh.entries[hbits]; \
615 while (hitem && hitem->hashval < hval) \
616 hitem = hitem->next; \
617 while (hitem && hitem->hashval == hval) { \
618 if (!cmpfn(container_of(hitem, type, field.hi), item)) \
619 return container_of(hitem, type, field.hi); \
620 hitem = hitem->next; \
624 macro_inline type *prefix ## _del(struct prefix##_head *h, type *item) \
626 if (!h->hh.tabshift) \
628 uint32_t hval = item->field.hi.hashval, hbits = HASH_KEY(h->hh, hval); \
629 struct thash_item **np = &h->hh.entries[hbits]; \
630 while (*np && (*np)->hashval < hval) \
632 while (*np && *np != &item->field.hi && (*np)->hashval == hval) \
634 if (*np != &item->field.hi) \
636 *np = item->field.hi.next; \
637 item->field.hi.next = NULL; \
639 if (HASH_SHRINK_THRESHOLD(h->hh)) \
640 typesafe_hash_shrink(&h->hh); \
643 macro_inline type *prefix ## _pop(struct prefix##_head *h) \
646 for (i = 0; i < HASH_SIZE(h->hh); i++) \
647 if (h->hh.entries[i]) { \
648 struct thash_item *hitem = h->hh.entries[i]; \
649 h->hh.entries[i] = hitem->next; \
651 hitem->next = NULL; \
652 if (HASH_SHRINK_THRESHOLD(h->hh)) \
653 typesafe_hash_shrink(&h->hh); \
654 return container_of(hitem, type, field.hi); \
658 macro_pure type *prefix ## _first(struct prefix##_head *h) \
661 for (i = 0; i < HASH_SIZE(h->hh); i++) \
662 if (h->hh.entries[i]) \
663 return container_of(h->hh.entries[i], type, field.hi); \
666 macro_pure type *prefix ## _next(struct prefix##_head *h, type *item) \
668 struct thash_item *hitem = &item->field.hi; \
670 return container_of(hitem->next, type, field.hi); \
671 uint32_t i = HASH_KEY(h->hh, hitem->hashval) + 1; \
672 for (; i < HASH_SIZE(h->hh); i++) \
673 if (h->hh.entries[i]) \
674 return container_of(h->hh.entries[i], type, field.hi); \
677 macro_pure type *prefix ## _next_safe(struct prefix##_head *h, type *item) \
681 return prefix ## _next(h, item); \
683 macro_pure size_t prefix ## _count(const struct prefix##_head *h) \
685 return h->hh.count; \
690 * can be used as priority queue with add / pop
693 /* don't use these structs directly */
694 #define SKIPLIST_MAXDEPTH 16
695 #define SKIPLIST_EMBED 4
696 #define SKIPLIST_OVERFLOW (SKIPLIST_EMBED - 1)
699 struct sskip_item
*next
[SKIPLIST_EMBED
];
702 struct sskip_overflow
{
703 struct sskip_item
*next
[SKIPLIST_MAXDEPTH
- SKIPLIST_OVERFLOW
];
707 struct sskip_item hitem
;
708 struct sskip_item
*overflow
[SKIPLIST_MAXDEPTH
- SKIPLIST_OVERFLOW
];
714 * PREDECL_SKIPLIST(namelist)
716 * struct namelist_item nlitem;
718 * DECLARE_SKIPLIST(namelist, struct name, nlitem, cmpfunc)
720 #define _PREDECL_SKIPLIST(prefix) \
721 struct prefix ## _head { struct sskip_head sh; }; \
722 struct prefix ## _item { struct sskip_item si; };
724 #define INIT_SKIPLIST_UNIQ(var) { }
725 #define INIT_SKIPLIST_NONUNIQ(var) { }
727 #define _DECLARE_SKIPLIST(prefix, type, field, cmpfn_nuq, cmpfn_uq) \
729 macro_inline void prefix ## _init(struct prefix##_head *h) \
731 memset(h, 0, sizeof(*h)); \
732 h->sh.hitem.next[SKIPLIST_OVERFLOW] = (struct sskip_item *) \
733 ((uintptr_t)h->sh.overflow | 1); \
735 macro_inline void prefix ## _fini(struct prefix##_head *h) \
737 memset(h, 0, sizeof(*h)); \
739 macro_inline type *prefix ## _add(struct prefix##_head *h, type *item) \
741 struct sskip_item *si; \
742 si = typesafe_skiplist_add(&h->sh, &item->field.si, cmpfn_uq); \
743 return container_of_null(si, type, field.si); \
745 macro_inline type *prefix ## _find_gteq(struct prefix##_head *h, \
748 struct sskip_item *sitem = typesafe_skiplist_find_gteq(&h->sh, \
749 &item->field.si, cmpfn_nuq); \
750 return container_of_null(sitem, type, field.si); \
752 macro_inline type *prefix ## _find_lt(struct prefix##_head *h, \
755 struct sskip_item *sitem = typesafe_skiplist_find_lt(&h->sh, \
756 &item->field.si, cmpfn_nuq); \
757 return container_of_null(sitem, type, field.si); \
759 macro_inline type *prefix ## _del(struct prefix##_head *h, type *item) \
761 struct sskip_item *sitem = typesafe_skiplist_del(&h->sh, \
762 &item->field.si, cmpfn_uq); \
763 return container_of_null(sitem, type, field.si); \
765 macro_inline type *prefix ## _pop(struct prefix##_head *h) \
767 struct sskip_item *sitem = typesafe_skiplist_pop(&h->sh); \
768 return container_of_null(sitem, type, field.si); \
770 macro_pure type *prefix ## _first(struct prefix##_head *h) \
772 struct sskip_item *first = h->sh.hitem.next[0]; \
773 return container_of_null(first, type, field.si); \
775 macro_pure type *prefix ## _next(struct prefix##_head *h, type *item) \
777 struct sskip_item *next = item->field.si.next[0]; \
778 return container_of_null(next, type, field.si); \
780 macro_pure type *prefix ## _next_safe(struct prefix##_head *h, type *item) \
782 struct sskip_item *next; \
783 next = item ? item->field.si.next[0] : NULL; \
784 return container_of_null(next, type, field.si); \
786 macro_pure size_t prefix ## _count(const struct prefix##_head *h) \
788 return h->sh.count; \
792 #define PREDECL_SKIPLIST_UNIQ(prefix) \
793 _PREDECL_SKIPLIST(prefix)
794 #define DECLARE_SKIPLIST_UNIQ(prefix, type, field, cmpfn) \
796 macro_inline int prefix ## __cmp(const struct sskip_item *a, \
797 const struct sskip_item *b) \
799 return cmpfn(container_of(a, type, field.si), \
800 container_of(b, type, field.si)); \
802 macro_inline type *prefix ## _find(struct prefix##_head *h, const type *item) \
804 struct sskip_item *sitem = typesafe_skiplist_find(&h->sh, \
805 &item->field.si, &prefix ## __cmp); \
806 return container_of_null(sitem, type, field.si); \
809 _DECLARE_SKIPLIST(prefix, type, field, \
810 prefix ## __cmp, prefix ## __cmp) \
813 #define PREDECL_SKIPLIST_NONUNIQ(prefix) \
814 _PREDECL_SKIPLIST(prefix)
815 #define DECLARE_SKIPLIST_NONUNIQ(prefix, type, field, cmpfn) \
817 macro_inline int prefix ## __cmp(const struct sskip_item *a, \
818 const struct sskip_item *b) \
820 return cmpfn(container_of(a, type, field.si), \
821 container_of(b, type, field.si)); \
823 macro_inline int prefix ## __cmp_uq(const struct sskip_item *a, \
824 const struct sskip_item *b) \
826 int cmpval = cmpfn(container_of(a, type, field.si), \
827 container_of(b, type, field.si)); \
837 _DECLARE_SKIPLIST(prefix, type, field, \
838 prefix ## __cmp, prefix ## __cmp_uq) \
842 extern struct sskip_item
*typesafe_skiplist_add(struct sskip_head
*head
,
843 struct sskip_item
*item
, int (*cmpfn
)(
844 const struct sskip_item
*a
,
845 const struct sskip_item
*b
));
846 extern struct sskip_item
*typesafe_skiplist_find(struct sskip_head
*head
,
847 const struct sskip_item
*item
, int (*cmpfn
)(
848 const struct sskip_item
*a
,
849 const struct sskip_item
*b
));
850 extern struct sskip_item
*typesafe_skiplist_find_gteq(struct sskip_head
*head
,
851 const struct sskip_item
*item
, int (*cmpfn
)(
852 const struct sskip_item
*a
,
853 const struct sskip_item
*b
));
854 extern struct sskip_item
*typesafe_skiplist_find_lt(struct sskip_head
*head
,
855 const struct sskip_item
*item
, int (*cmpfn
)(
856 const struct sskip_item
*a
,
857 const struct sskip_item
*b
));
858 extern struct sskip_item
*typesafe_skiplist_del(
859 struct sskip_head
*head
, struct sskip_item
*item
, int (*cmpfn
)(
860 const struct sskip_item
*a
,
861 const struct sskip_item
*b
));
862 extern struct sskip_item
*typesafe_skiplist_pop(struct sskip_head
*head
);
868 /* this needs to stay at the end because both files include each other.
869 * the resolved order is typesafe.h before typerb.h
873 #endif /* _FRR_TYPESAFE_H */