Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __PERF_STRLIST_H
0003 #define __PERF_STRLIST_H
0004 
0005 #include <linux/rbtree.h>
0006 #include <stdbool.h>
0007 
0008 #include "rblist.h"
0009 
0010 struct str_node {
0011     struct rb_node rb_node;
0012     const char     *s;
0013 };
0014 
0015 struct strlist {
0016     struct rblist rblist;
0017     bool          dupstr;
0018     bool          file_only;
0019 };
0020 
0021 /*
0022  * @file_only: When dirname is present, only consider entries as filenames,
0023  *             that should not be added to the list if dirname/entry is not
0024  *             found
0025  */
0026 struct strlist_config {
0027     bool dont_dupstr;
0028     bool file_only;
0029     const char *dirname;
0030 };
0031 
0032 struct strlist *strlist__new(const char *slist, const struct strlist_config *config);
0033 void strlist__delete(struct strlist *slist);
0034 
0035 void strlist__remove(struct strlist *slist, struct str_node *sn);
0036 int strlist__load(struct strlist *slist, const char *filename);
0037 int strlist__add(struct strlist *slist, const char *str);
0038 
0039 struct str_node *strlist__entry(const struct strlist *slist, unsigned int idx);
0040 struct str_node *strlist__find(struct strlist *slist, const char *entry);
0041 
0042 static inline bool strlist__has_entry(struct strlist *slist, const char *entry)
0043 {
0044     return strlist__find(slist, entry) != NULL;
0045 }
0046 
0047 static inline bool strlist__empty(const struct strlist *slist)
0048 {
0049     return rblist__empty(&slist->rblist);
0050 }
0051 
0052 static inline unsigned int strlist__nr_entries(const struct strlist *slist)
0053 {
0054     return rblist__nr_entries(&slist->rblist);
0055 }
0056 
0057 /* For strlist iteration */
0058 static inline struct str_node *strlist__first(struct strlist *slist)
0059 {
0060     struct rb_node *rn = rb_first_cached(&slist->rblist.entries);
0061     return rn ? rb_entry(rn, struct str_node, rb_node) : NULL;
0062 }
0063 static inline struct str_node *strlist__next(struct str_node *sn)
0064 {
0065     struct rb_node *rn;
0066     if (!sn)
0067         return NULL;
0068     rn = rb_next(&sn->rb_node);
0069     return rn ? rb_entry(rn, struct str_node, rb_node) : NULL;
0070 }
0071 
0072 /**
0073  * strlist_for_each      - iterate over a strlist
0074  * @pos:    the &struct str_node to use as a loop cursor.
0075  * @slist:  the &struct strlist for loop.
0076  */
0077 #define strlist__for_each_entry(pos, slist) \
0078     for (pos = strlist__first(slist); pos; pos = strlist__next(pos))
0079 
0080 /**
0081  * strlist_for_each_safe - iterate over a strlist safe against removal of
0082  *                         str_node
0083  * @pos:    the &struct str_node to use as a loop cursor.
0084  * @n:      another &struct str_node to use as temporary storage.
0085  * @slist:  the &struct strlist for loop.
0086  */
0087 #define strlist__for_each_entry_safe(pos, n, slist) \
0088     for (pos = strlist__first(slist), n = strlist__next(pos); pos;\
0089          pos = n, n = strlist__next(n))
0090 #endif /* __PERF_STRLIST_H */