aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorDavid Ahern <dsahern@gmail.com>2012-07-31 00:31:33 -0400
committerArnaldo Carvalho de Melo <acme@redhat.com>2012-08-03 09:39:51 -0400
commitee8dd3ca43f151d9fbe1edeef68fb8a77eb9f047 (patch)
tree051af5be993ec33515d575580d408a89ee072f86 /tools
parent37bbd3fff1480a1f5d57abb9e9e56f468954c1b1 (diff)
perf tools: Change strlist to use the new rblist
Replaces the direct use of rbtree code with the rblist API. In the end the patch is a no-op on strlist functionality; the API for strlist is not changed, only its implementaton. Signed-off-by: David Ahern <dsahern@gmail.com> Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Ingo Molnar <mingo@kernel.org> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Link: http://lkml.kernel.org/r/1343709095-7089-3-git-send-email-dsahern@gmail.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools')
-rw-r--r--tools/perf/util/strlist.c130
-rw-r--r--tools/perf/util/strlist.h11
2 files changed, 57 insertions, 84 deletions
diff --git a/tools/perf/util/strlist.c b/tools/perf/util/strlist.c
index 6783a2043555..95856ff3dda4 100644
--- a/tools/perf/util/strlist.c
+++ b/tools/perf/util/strlist.c
@@ -10,23 +10,28 @@
10#include <stdlib.h> 10#include <stdlib.h>
11#include <string.h> 11#include <string.h>
12 12
13static struct str_node *str_node__new(const char *s, bool dupstr) 13static
14struct rb_node *strlist__node_new(struct rblist *rblist, const void *entry)
14{ 15{
15 struct str_node *self = malloc(sizeof(*self)); 16 const char *s = entry;
17 struct rb_node *rc = NULL;
18 struct strlist *strlist = container_of(rblist, struct strlist, rblist);
19 struct str_node *snode = malloc(sizeof(*snode));
16 20
17 if (self != NULL) { 21 if (snode != NULL) {
18 if (dupstr) { 22 if (strlist->dupstr) {
19 s = strdup(s); 23 s = strdup(s);
20 if (s == NULL) 24 if (s == NULL)
21 goto out_delete; 25 goto out_delete;
22 } 26 }
23 self->s = s; 27 snode->s = s;
28 rc = &snode->rb_node;
24 } 29 }
25 30
26 return self; 31 return rc;
27 32
28out_delete: 33out_delete:
29 free(self); 34 free(snode);
30 return NULL; 35 return NULL;
31} 36}
32 37
@@ -37,36 +42,26 @@ static void str_node__delete(struct str_node *self, bool dupstr)
37 free(self); 42 free(self);
38} 43}
39 44
40int strlist__add(struct strlist *self, const char *new_entry) 45static
46void strlist__node_delete(struct rblist *rblist, struct rb_node *rb_node)
41{ 47{
42 struct rb_node **p = &self->entries.rb_node; 48 struct strlist *slist = container_of(rblist, struct strlist, rblist);
43 struct rb_node *parent = NULL; 49 struct str_node *snode = container_of(rb_node, struct str_node, rb_node);
44 struct str_node *sn;
45
46 while (*p != NULL) {
47 int rc;
48
49 parent = *p;
50 sn = rb_entry(parent, struct str_node, rb_node);
51 rc = strcmp(sn->s, new_entry);
52
53 if (rc > 0)
54 p = &(*p)->rb_left;
55 else if (rc < 0)
56 p = &(*p)->rb_right;
57 else
58 return -EEXIST;
59 }
60 50
61 sn = str_node__new(new_entry, self->dupstr); 51 str_node__delete(snode, slist->dupstr);
62 if (sn == NULL) 52}
63 return -ENOMEM;
64 53
65 rb_link_node(&sn->rb_node, parent, p); 54static int strlist__node_cmp(struct rb_node *rb_node, const void *entry)
66 rb_insert_color(&sn->rb_node, &self->entries); 55{
67 ++self->nr_entries; 56 const char *str = entry;
57 struct str_node *snode = container_of(rb_node, struct str_node, rb_node);
58
59 return strcmp(snode->s, str);
60}
68 61
69 return 0; 62int strlist__add(struct strlist *self, const char *new_entry)
63{
64 return rblist__add_node(&self->rblist, new_entry);
70} 65}
71 66
72int strlist__load(struct strlist *self, const char *filename) 67int strlist__load(struct strlist *self, const char *filename)
@@ -96,34 +91,20 @@ out:
96 return err; 91 return err;
97} 92}
98 93
99void strlist__remove(struct strlist *self, struct str_node *sn) 94void strlist__remove(struct strlist *slist, struct str_node *snode)
100{ 95{
101 rb_erase(&sn->rb_node, &self->entries); 96 str_node__delete(snode, slist->dupstr);
102 str_node__delete(sn, self->dupstr);
103} 97}
104 98
105struct str_node *strlist__find(struct strlist *self, const char *entry) 99struct str_node *strlist__find(struct strlist *slist, const char *entry)
106{ 100{
107 struct rb_node **p = &self->entries.rb_node; 101 struct str_node *snode = NULL;
108 struct rb_node *parent = NULL; 102 struct rb_node *rb_node = rblist__find(&slist->rblist, entry);
109
110 while (*p != NULL) {
111 struct str_node *sn;
112 int rc;
113
114 parent = *p;
115 sn = rb_entry(parent, struct str_node, rb_node);
116 rc = strcmp(sn->s, entry);
117
118 if (rc > 0)
119 p = &(*p)->rb_left;
120 else if (rc < 0)
121 p = &(*p)->rb_right;
122 else
123 return sn;
124 }
125 103
126 return NULL; 104 if (rb_node)
105 snode = container_of(rb_node, struct str_node, rb_node);
106
107 return snode;
127} 108}
128 109
129static int strlist__parse_list_entry(struct strlist *self, const char *s) 110static int strlist__parse_list_entry(struct strlist *self, const char *s)
@@ -156,9 +137,12 @@ struct strlist *strlist__new(bool dupstr, const char *slist)
156 struct strlist *self = malloc(sizeof(*self)); 137 struct strlist *self = malloc(sizeof(*self));
157 138
158 if (self != NULL) { 139 if (self != NULL) {
159 self->entries = RB_ROOT; 140 rblist__init(&self->rblist);
141 self->rblist.node_cmp = strlist__node_cmp;
142 self->rblist.node_new = strlist__node_new;
143 self->rblist.node_delete = strlist__node_delete;
144
160 self->dupstr = dupstr; 145 self->dupstr = dupstr;
161 self->nr_entries = 0;
162 if (slist && strlist__parse_list(self, slist) != 0) 146 if (slist && strlist__parse_list(self, slist) != 0)
163 goto out_error; 147 goto out_error;
164 } 148 }
@@ -171,30 +155,18 @@ out_error:
171 155
172void strlist__delete(struct strlist *self) 156void strlist__delete(struct strlist *self)
173{ 157{
174 if (self != NULL) { 158 if (self != NULL)
175 struct str_node *pos; 159 rblist__delete(&self->rblist);
176 struct rb_node *next = rb_first(&self->entries);
177
178 while (next) {
179 pos = rb_entry(next, struct str_node, rb_node);
180 next = rb_next(&pos->rb_node);
181 strlist__remove(self, pos);
182 }
183 self->entries = RB_ROOT;
184 free(self);
185 }
186} 160}
187 161
188struct str_node *strlist__entry(const struct strlist *self, unsigned int idx) 162struct str_node *strlist__entry(const struct strlist *slist, unsigned int idx)
189{ 163{
190 struct rb_node *nd; 164 struct str_node *snode = NULL;
165 struct rb_node *rb_node;
191 166
192 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) { 167 rb_node = rblist__entry(&slist->rblist, idx);
193 struct str_node *pos = rb_entry(nd, struct str_node, rb_node); 168 if (rb_node)
169 snode = container_of(rb_node, struct str_node, rb_node);
194 170
195 if (!idx--) 171 return snode;
196 return pos;
197 }
198
199 return NULL;
200} 172}
diff --git a/tools/perf/util/strlist.h b/tools/perf/util/strlist.h
index 3ba839007d2c..dd9f922ec67c 100644
--- a/tools/perf/util/strlist.h
+++ b/tools/perf/util/strlist.h
@@ -4,14 +4,15 @@
4#include <linux/rbtree.h> 4#include <linux/rbtree.h>
5#include <stdbool.h> 5#include <stdbool.h>
6 6
7#include "rblist.h"
8
7struct str_node { 9struct str_node {
8 struct rb_node rb_node; 10 struct rb_node rb_node;
9 const char *s; 11 const char *s;
10}; 12};
11 13
12struct strlist { 14struct strlist {
13 struct rb_root entries; 15 struct rblist rblist;
14 unsigned int nr_entries;
15 bool dupstr; 16 bool dupstr;
16}; 17};
17 18
@@ -32,18 +33,18 @@ static inline bool strlist__has_entry(struct strlist *self, const char *entry)
32 33
33static inline bool strlist__empty(const struct strlist *self) 34static inline bool strlist__empty(const struct strlist *self)
34{ 35{
35 return self->nr_entries == 0; 36 return rblist__empty(&self->rblist);
36} 37}
37 38
38static inline unsigned int strlist__nr_entries(const struct strlist *self) 39static inline unsigned int strlist__nr_entries(const struct strlist *self)
39{ 40{
40 return self->nr_entries; 41 return rblist__nr_entries(&self->rblist);
41} 42}
42 43
43/* For strlist iteration */ 44/* For strlist iteration */
44static inline struct str_node *strlist__first(struct strlist *self) 45static inline struct str_node *strlist__first(struct strlist *self)
45{ 46{
46 struct rb_node *rn = rb_first(&self->entries); 47 struct rb_node *rn = rb_first(&self->rblist.entries);
47 return rn ? rb_entry(rn, struct str_node, rb_node) : NULL; 48 return rn ? rb_entry(rn, struct str_node, rb_node) : NULL;
48} 49}
49static inline struct str_node *strlist__next(struct str_node *sn) 50static inline struct str_node *strlist__next(struct str_node *sn)