xref: /linux/tools/perf/util/intlist.h (revision d0034a7a4ac7fae708146ac0059b9c47a1543f0d)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
270b40c4aSDavid Ahern #ifndef __PERF_INTLIST_H
370b40c4aSDavid Ahern #define __PERF_INTLIST_H
470b40c4aSDavid Ahern 
570b40c4aSDavid Ahern #include <linux/rbtree.h>
670b40c4aSDavid Ahern #include <stdbool.h>
770b40c4aSDavid Ahern 
870b40c4aSDavid Ahern #include "rblist.h"
970b40c4aSDavid Ahern 
1070b40c4aSDavid Ahern struct int_node {
1170b40c4aSDavid Ahern 	struct rb_node rb_node;
12*94253393SJin Yao 	unsigned long i;
132969b129SDavid Ahern 	void *priv;
1470b40c4aSDavid Ahern };
1570b40c4aSDavid Ahern 
1670b40c4aSDavid Ahern struct intlist {
1770b40c4aSDavid Ahern 	struct rblist rblist;
1870b40c4aSDavid Ahern };
1970b40c4aSDavid Ahern 
20ffe0fb76SArnaldo Carvalho de Melo struct intlist *intlist__new(const char *slist);
2170b40c4aSDavid Ahern void intlist__delete(struct intlist *ilist);
2270b40c4aSDavid Ahern 
2370b40c4aSDavid Ahern void intlist__remove(struct intlist *ilist, struct int_node *in);
24*94253393SJin Yao int intlist__add(struct intlist *ilist, unsigned long i);
2570b40c4aSDavid Ahern 
2670b40c4aSDavid Ahern struct int_node *intlist__entry(const struct intlist *ilist, unsigned int idx);
27*94253393SJin Yao struct int_node *intlist__find(struct intlist *ilist, unsigned long i);
28*94253393SJin Yao struct int_node *intlist__findnew(struct intlist *ilist, unsigned long i);
2970b40c4aSDavid Ahern 
intlist__has_entry(struct intlist * ilist,unsigned long i)30*94253393SJin Yao static inline bool intlist__has_entry(struct intlist *ilist, unsigned long i)
3170b40c4aSDavid Ahern {
3270b40c4aSDavid Ahern 	return intlist__find(ilist, i) != NULL;
3370b40c4aSDavid Ahern }
3470b40c4aSDavid Ahern 
intlist__empty(const struct intlist * ilist)3570b40c4aSDavid Ahern static inline bool intlist__empty(const struct intlist *ilist)
3670b40c4aSDavid Ahern {
3770b40c4aSDavid Ahern 	return rblist__empty(&ilist->rblist);
3870b40c4aSDavid Ahern }
3970b40c4aSDavid Ahern 
intlist__nr_entries(const struct intlist * ilist)4070b40c4aSDavid Ahern static inline unsigned int intlist__nr_entries(const struct intlist *ilist)
4170b40c4aSDavid Ahern {
4270b40c4aSDavid Ahern 	return rblist__nr_entries(&ilist->rblist);
4370b40c4aSDavid Ahern }
4470b40c4aSDavid Ahern 
4570b40c4aSDavid Ahern /* For intlist iteration */
intlist__first(struct intlist * ilist)4670b40c4aSDavid Ahern static inline struct int_node *intlist__first(struct intlist *ilist)
4770b40c4aSDavid Ahern {
48ca227029SDavidlohr Bueso 	struct rb_node *rn = rb_first_cached(&ilist->rblist.entries);
4970b40c4aSDavid Ahern 	return rn ? rb_entry(rn, struct int_node, rb_node) : NULL;
5070b40c4aSDavid Ahern }
intlist__next(struct int_node * in)5170b40c4aSDavid Ahern static inline struct int_node *intlist__next(struct int_node *in)
5270b40c4aSDavid Ahern {
5370b40c4aSDavid Ahern 	struct rb_node *rn;
5470b40c4aSDavid Ahern 	if (!in)
5570b40c4aSDavid Ahern 		return NULL;
5670b40c4aSDavid Ahern 	rn = rb_next(&in->rb_node);
5770b40c4aSDavid Ahern 	return rn ? rb_entry(rn, struct int_node, rb_node) : NULL;
5870b40c4aSDavid Ahern }
5970b40c4aSDavid Ahern 
6070b40c4aSDavid Ahern /**
6110daf4d0SArnaldo Carvalho de Melo  * intlist__for_each_entry      - iterate over a intlist
6270b40c4aSDavid Ahern  * @pos:	the &struct int_node to use as a loop cursor.
6370b40c4aSDavid Ahern  * @ilist:	the &struct intlist for loop.
6470b40c4aSDavid Ahern  */
6510daf4d0SArnaldo Carvalho de Melo #define intlist__for_each_entry(pos, ilist)	\
6670b40c4aSDavid Ahern 	for (pos = intlist__first(ilist); pos; pos = intlist__next(pos))
6770b40c4aSDavid Ahern 
6870b40c4aSDavid Ahern /**
6910daf4d0SArnaldo Carvalho de Melo  * intlist__for_each_entry_safe - iterate over a intlist safe against removal of
7070b40c4aSDavid Ahern  *                         int_node
7170b40c4aSDavid Ahern  * @pos:	the &struct int_node to use as a loop cursor.
7270b40c4aSDavid Ahern  * @n:		another &struct int_node to use as temporary storage.
7370b40c4aSDavid Ahern  * @ilist:	the &struct intlist for loop.
7470b40c4aSDavid Ahern  */
7510daf4d0SArnaldo Carvalho de Melo #define intlist__for_each_entry_safe(pos, n, ilist)	\
7670b40c4aSDavid Ahern 	for (pos = intlist__first(ilist), n = intlist__next(pos); pos;\
7770b40c4aSDavid Ahern 	     pos = n, n = intlist__next(n))
7870b40c4aSDavid Ahern #endif /* __PERF_INTLIST_H */
79