Skip to content

Commit

Permalink
strlist: Introduce strlist__entry and strlist__nr_entries methods
Browse files Browse the repository at this point in the history
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 <[email protected]>
LKML-Reference: <[email protected]>
Signed-off-by: Ingo Molnar <[email protected]>
  • Loading branch information
acmel authored and Ingo Molnar committed Jul 11, 2009
1 parent 60c1baf commit 27d0fd4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
20 changes: 18 additions & 2 deletions tools/perf/util/strlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
11 changes: 9 additions & 2 deletions tools/perf/util/strlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ struct str_node {

struct strlist {
struct rb_root entries;
bool dupstr;
unsigned int nr_entries;
bool dupstr;
};

struct strlist *strlist__new(bool dupstr, const char *slist);
Expand All @@ -21,11 +22,17 @@ void strlist__remove(struct strlist *self, struct str_node *sn);
int strlist__load(struct strlist *self, const char *filename);
int strlist__add(struct strlist *self, const char *str);

struct str_node *strlist__entry(const struct strlist *self, unsigned int idx);
bool strlist__has_entry(struct strlist *self, const char *entry);

static inline bool strlist__empty(const struct strlist *self)
{
return rb_first(&self->entries) == NULL;
return self->nr_entries == 0;
}

static inline unsigned int strlist__nr_entries(const struct strlist *self)
{
return self->nr_entries;
}

int strlist__parse_list(struct strlist *self, const char *s);
Expand Down

0 comments on commit 27d0fd4

Please sign in to comment.