strlist: Introduce strlist__entry and strlist__nr_entries methods
The strlist__entry method allows accessing strlists like an array, will be used in the 'perf report' to access the first entry. We now keep the nr_entries so that we can check if we have just one entry, will be used in 'perf report' to improve the output by showing just at the top when we have just, say, one DSO. While at it use nr_entries to optimize strlist__is_empty by not using the far more costly rb_first based implementation. Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com> LKML-Reference: <1247325517-12272-2-git-send-email-acme@redhat.com> Signed-off-by: Ingo Molnar <mingo@elte.hu>
This commit is contained in:

committed by
Ingo Molnar

parent
60c1baf124
commit
27d0fd410c
@@ -64,6 +64,7 @@ int strlist__add(struct strlist *self, const char *new_entry)
|
||||
|
||||
rb_link_node(&sn->rb_node, parent, p);
|
||||
rb_insert_color(&sn->rb_node, &self->entries);
|
||||
++self->nr_entries;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -155,8 +156,9 @@ struct strlist *strlist__new(bool dupstr, const char *slist)
|
||||
struct strlist *self = malloc(sizeof(*self));
|
||||
|
||||
if (self != NULL) {
|
||||
self->entries = RB_ROOT;
|
||||
self->dupstr = dupstr;
|
||||
self->entries = RB_ROOT;
|
||||
self->dupstr = dupstr;
|
||||
self->nr_entries = 0;
|
||||
if (slist && strlist__parse_list(self, slist) != 0)
|
||||
goto out_error;
|
||||
}
|
||||
@@ -182,3 +184,17 @@ void strlist__delete(struct strlist *self)
|
||||
free(self);
|
||||
}
|
||||
}
|
||||
|
||||
struct str_node *strlist__entry(const struct strlist *self, unsigned int idx)
|
||||
{
|
||||
struct rb_node *nd;
|
||||
|
||||
for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
|
||||
struct str_node *pos = rb_entry(nd, struct str_node, rb_node);
|
||||
|
||||
if (!idx--)
|
||||
return pos;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
Reference in New Issue
Block a user