Home | History | Annotate | Download | only in util
      1 #ifndef __PERF_STRLIST_H
      2 #define __PERF_STRLIST_H
      3 
      4 /* ANDROID_CHANGE_BEGIN */
      5 #if 0
      6 #include <linux/rbtree.h>
      7 #else
      8 #include "include/linux/kernel.h"
      9 #include "include/linux/rbtree.h"
     10 #endif
     11 /* ANDROID_CHANGE_END */
     12 #include <stdbool.h>
     13 
     14 struct str_node {
     15 	struct rb_node rb_node;
     16 	const char     *s;
     17 };
     18 
     19 struct strlist {
     20 	struct rb_root entries;
     21 	unsigned int   nr_entries;
     22 	bool	       dupstr;
     23 };
     24 
     25 struct strlist *strlist__new(bool dupstr, const char *slist);
     26 void strlist__delete(struct strlist *self);
     27 
     28 void strlist__remove(struct strlist *self, struct str_node *sn);
     29 int strlist__load(struct strlist *self, const char *filename);
     30 int strlist__add(struct strlist *self, const char *str);
     31 
     32 struct str_node *strlist__entry(const struct strlist *self, unsigned int idx);
     33 struct str_node *strlist__find(struct strlist *self, const char *entry);
     34 
     35 static inline bool strlist__has_entry(struct strlist *self, const char *entry)
     36 {
     37 	return strlist__find(self, entry) != NULL;
     38 }
     39 
     40 static inline bool strlist__empty(const struct strlist *self)
     41 {
     42 	return self->nr_entries == 0;
     43 }
     44 
     45 static inline unsigned int strlist__nr_entries(const struct strlist *self)
     46 {
     47 	return self->nr_entries;
     48 }
     49 
     50 /* For strlist iteration */
     51 static inline struct str_node *strlist__first(struct strlist *self)
     52 {
     53 	struct rb_node *rn = rb_first(&self->entries);
     54 	return rn ? rb_entry(rn, struct str_node, rb_node) : NULL;
     55 }
     56 static inline struct str_node *strlist__next(struct str_node *sn)
     57 {
     58 	struct rb_node *rn;
     59 	if (!sn)
     60 		return NULL;
     61 	rn = rb_next(&sn->rb_node);
     62 	return rn ? rb_entry(rn, struct str_node, rb_node) : NULL;
     63 }
     64 
     65 /**
     66  * strlist_for_each      - iterate over a strlist
     67  * @pos:	the &struct str_node to use as a loop cursor.
     68  * @self:	the &struct strlist for loop.
     69  */
     70 #define strlist__for_each(pos, self)	\
     71 	for (pos = strlist__first(self); pos; pos = strlist__next(pos))
     72 
     73 /**
     74  * strlist_for_each_safe - iterate over a strlist safe against removal of
     75  *                         str_node
     76  * @pos:	the &struct str_node to use as a loop cursor.
     77  * @n:		another &struct str_node to use as temporary storage.
     78  * @self:	the &struct strlist for loop.
     79  */
     80 #define strlist__for_each_safe(pos, n, self)	\
     81 	for (pos = strlist__first(self), n = strlist__next(pos); pos;\
     82 	     pos = n, n = strlist__next(n))
     83 
     84 int strlist__parse_list(struct strlist *self, const char *s);
     85 #endif /* __PERF_STRLIST_H */
     86