Linux Audio

Check our new training course

Loading...
v5.4
 1/* SPDX-License-Identifier: GPL-2.0 */
 2#ifndef __PERF_STRLIST_H
 3#define __PERF_STRLIST_H
 4
 5#include <linux/rbtree.h>
 6#include <stdbool.h>
 7
 8#include "rblist.h"
 9
10struct str_node {
11	struct rb_node rb_node;
12	const char     *s;
13};
14
15struct strlist {
16	struct rblist rblist;
17	bool	      dupstr;
18	bool	      file_only;
19};
20
21/*
22 * @file_only: When dirname is present, only consider entries as filenames,
23 *             that should not be added to the list if dirname/entry is not
24 *             found
25 */
26struct strlist_config {
27	bool dont_dupstr;
28	bool file_only;
29	const char *dirname;
30};
31
32struct strlist *strlist__new(const char *slist, const struct strlist_config *config);
33void strlist__delete(struct strlist *slist);
 
34
35void strlist__remove(struct strlist *slist, struct str_node *sn);
36int strlist__load(struct strlist *slist, const char *filename);
37int strlist__add(struct strlist *slist, const char *str);
38
39struct str_node *strlist__entry(const struct strlist *slist, unsigned int idx);
40struct str_node *strlist__find(struct strlist *slist, const char *entry);
41
42static inline bool strlist__has_entry(struct strlist *slist, const char *entry)
43{
44	return strlist__find(slist, entry) != NULL;
45}
46
47static inline bool strlist__empty(const struct strlist *slist)
48{
49	return rblist__empty(&slist->rblist);
50}
51
52static inline unsigned int strlist__nr_entries(const struct strlist *slist)
53{
54	return rblist__nr_entries(&slist->rblist);
55}
56
57/* For strlist iteration */
58static inline struct str_node *strlist__first(struct strlist *slist)
59{
60	struct rb_node *rn = rb_first_cached(&slist->rblist.entries);
61	return rn ? rb_entry(rn, struct str_node, rb_node) : NULL;
62}
63static inline struct str_node *strlist__next(struct str_node *sn)
64{
65	struct rb_node *rn;
66	if (!sn)
67		return NULL;
68	rn = rb_next(&sn->rb_node);
69	return rn ? rb_entry(rn, struct str_node, rb_node) : NULL;
70}
71
72/**
73 * strlist_for_each      - iterate over a strlist
74 * @pos:	the &struct str_node to use as a loop cursor.
75 * @slist:	the &struct strlist for loop.
76 */
77#define strlist__for_each_entry(pos, slist)	\
78	for (pos = strlist__first(slist); pos; pos = strlist__next(pos))
79
80/**
81 * strlist_for_each_safe - iterate over a strlist safe against removal of
82 *                         str_node
83 * @pos:	the &struct str_node to use as a loop cursor.
84 * @n:		another &struct str_node to use as temporary storage.
85 * @slist:	the &struct strlist for loop.
86 */
87#define strlist__for_each_entry_safe(pos, n, slist)	\
88	for (pos = strlist__first(slist), n = strlist__next(pos); pos;\
89	     pos = n, n = strlist__next(n))
 
 
90#endif /* __PERF_STRLIST_H */
v3.5.6
 
 1#ifndef __PERF_STRLIST_H
 2#define __PERF_STRLIST_H
 3
 4#include <linux/rbtree.h>
 5#include <stdbool.h>
 6
 
 
 7struct str_node {
 8	struct rb_node rb_node;
 9	const char     *s;
10};
11
12struct strlist {
13	struct rb_root entries;
14	unsigned int   nr_entries;
15	bool	       dupstr;
16};
17
18struct strlist *strlist__new(bool dupstr, const char *slist);
19void strlist__delete(struct strlist *self);
 
 
 
 
 
 
 
 
20
21void strlist__remove(struct strlist *self, struct str_node *sn);
22int strlist__load(struct strlist *self, const char *filename);
23int strlist__add(struct strlist *self, const char *str);
24
25struct str_node *strlist__entry(const struct strlist *self, unsigned int idx);
26struct str_node *strlist__find(struct strlist *self, const char *entry);
 
27
28static inline bool strlist__has_entry(struct strlist *self, const char *entry)
 
 
 
29{
30	return strlist__find(self, entry) != NULL;
31}
32
33static inline bool strlist__empty(const struct strlist *self)
34{
35	return self->nr_entries == 0;
36}
37
38static inline unsigned int strlist__nr_entries(const struct strlist *self)
39{
40	return self->nr_entries;
41}
42
43/* For strlist iteration */
44static inline struct str_node *strlist__first(struct strlist *self)
45{
46	struct rb_node *rn = rb_first(&self->entries);
47	return rn ? rb_entry(rn, struct str_node, rb_node) : NULL;
48}
49static inline struct str_node *strlist__next(struct str_node *sn)
50{
51	struct rb_node *rn;
52	if (!sn)
53		return NULL;
54	rn = rb_next(&sn->rb_node);
55	return rn ? rb_entry(rn, struct str_node, rb_node) : NULL;
56}
57
58/**
59 * strlist_for_each      - iterate over a strlist
60 * @pos:	the &struct str_node to use as a loop cursor.
61 * @self:	the &struct strlist for loop.
62 */
63#define strlist__for_each(pos, self)	\
64	for (pos = strlist__first(self); pos; pos = strlist__next(pos))
65
66/**
67 * strlist_for_each_safe - iterate over a strlist safe against removal of
68 *                         str_node
69 * @pos:	the &struct str_node to use as a loop cursor.
70 * @n:		another &struct str_node to use as temporary storage.
71 * @self:	the &struct strlist for loop.
72 */
73#define strlist__for_each_safe(pos, n, self)	\
74	for (pos = strlist__first(self), n = strlist__next(pos); pos;\
75	     pos = n, n = strlist__next(n))
76
77int strlist__parse_list(struct strlist *self, const char *s);
78#endif /* __PERF_STRLIST_H */