Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757332Ab0GAP54 (ORCPT ); Thu, 1 Jul 2010 11:57:56 -0400 Received: from s15228384.onlinehome-server.info ([87.106.30.177]:47062 "EHLO mail.x86-64.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757030Ab0GAPyG (ORCPT ); Thu, 1 Jul 2010 11:54:06 -0400 From: Borislav Petkov To: Subject: [PATCH 10/21] perf: rewire generic library stuff, p6 Date: Thu, 1 Jul 2010 17:55:52 +0200 Message-Id: <1277999763-20357-11-git-send-email-bp@amd64.org> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1277999763-20357-1-git-send-email-bp@amd64.org> References: <1277999763-20357-1-git-send-email-bp@amd64.org> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 19904 Lines: 796 From: Borislav Petkov Carve out strlist.[ch] and rbtree.c and make them generic. Signed-off-by: Borislav Petkov --- tools/lib/Makefile | 6 + tools/lib/lk/strlist.c | 200 ++++++++++++++++++++++++++++++++++++ tools/lib/lk/strlist.h | 78 ++++++++++++++ tools/perf/Makefile | 6 - tools/perf/builtin-buildid-cache.c | 2 +- tools/perf/builtin-probe.c | 2 +- tools/perf/builtin-report.c | 2 +- tools/perf/builtin-timechart.c | 2 +- tools/perf/util/event.c | 2 +- tools/perf/util/probe-event.c | 2 +- tools/perf/util/probe-event.h | 2 +- tools/perf/util/sort.h | 2 +- tools/perf/util/strlist.c | 200 ------------------------------------ tools/perf/util/strlist.h | 78 -------------- tools/perf/util/symbol.c | 2 +- 15 files changed, 293 insertions(+), 293 deletions(-) create mode 100644 tools/lib/lk/strlist.c create mode 100644 tools/lib/lk/strlist.h delete mode 100644 tools/perf/util/strlist.c delete mode 100644 tools/perf/util/strlist.h diff --git a/tools/lib/Makefile b/tools/lib/Makefile index df60156..818be02 100644 --- a/tools/lib/Makefile +++ b/tools/lib/Makefile @@ -12,6 +12,7 @@ LIB_H += lk/pstack.h LIB_H += lk/strbuf.h LIB_H += lk/color.h LIB_H += lk/debug.h +LIB_H += lk/strlist.h LIB_OBJS += $(OUTPUT)lk/bitmap.o LIB_OBJS += $(OUTPUT)lk/cpumap.o @@ -26,6 +27,8 @@ LIB_OBJS += $(OUTPUT)lk/color.o LIB_OBJS += $(OUTPUT)lk/config.o LIB_OBJS += $(OUTPUT)lk/debug.o LIB_OBJS += $(OUTPUT)lk/string.o +LIB_OBJS += $(OUTPUT)lk/rbtree.o +LIB_OBJS += $(OUTPUT)lk/strlist.o LIBFILE = lklib.a @@ -42,6 +45,9 @@ $(LIBFILE): $(LIB_OBJS) $(LIB_OBJS): $(LIB_H) +$(OUTPUT)lk/rbtree.o: ../../lib/rbtree.c + $(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) $< + $(OUTPUT)%.o: %.c $(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) $< $(OUTPUT)%.s: %.c diff --git a/tools/lib/lk/strlist.c b/tools/lib/lk/strlist.c new file mode 100644 index 0000000..6783a20 --- /dev/null +++ b/tools/lib/lk/strlist.c @@ -0,0 +1,200 @@ +/* + * (c) 2009 Arnaldo Carvalho de Melo + * + * Licensed under the GPLv2. + */ + +#include "strlist.h" +#include +#include +#include +#include + +static struct str_node *str_node__new(const char *s, bool dupstr) +{ + struct str_node *self = malloc(sizeof(*self)); + + if (self != NULL) { + if (dupstr) { + s = strdup(s); + if (s == NULL) + goto out_delete; + } + self->s = s; + } + + return self; + +out_delete: + free(self); + return NULL; +} + +static void str_node__delete(struct str_node *self, bool dupstr) +{ + if (dupstr) + free((void *)self->s); + free(self); +} + +int strlist__add(struct strlist *self, const char *new_entry) +{ + struct rb_node **p = &self->entries.rb_node; + struct rb_node *parent = NULL; + struct str_node *sn; + + while (*p != NULL) { + int rc; + + parent = *p; + sn = rb_entry(parent, struct str_node, rb_node); + rc = strcmp(sn->s, new_entry); + + if (rc > 0) + p = &(*p)->rb_left; + else if (rc < 0) + p = &(*p)->rb_right; + else + return -EEXIST; + } + + sn = str_node__new(new_entry, self->dupstr); + if (sn == NULL) + return -ENOMEM; + + rb_link_node(&sn->rb_node, parent, p); + rb_insert_color(&sn->rb_node, &self->entries); + ++self->nr_entries; + + return 0; +} + +int strlist__load(struct strlist *self, const char *filename) +{ + char entry[1024]; + int err; + FILE *fp = fopen(filename, "r"); + + if (fp == NULL) + return errno; + + while (fgets(entry, sizeof(entry), fp) != NULL) { + const size_t len = strlen(entry); + + if (len == 0) + continue; + entry[len - 1] = '\0'; + + err = strlist__add(self, entry); + if (err != 0) + goto out; + } + + err = 0; +out: + fclose(fp); + return err; +} + +void strlist__remove(struct strlist *self, struct str_node *sn) +{ + rb_erase(&sn->rb_node, &self->entries); + str_node__delete(sn, self->dupstr); +} + +struct str_node *strlist__find(struct strlist *self, const char *entry) +{ + struct rb_node **p = &self->entries.rb_node; + struct rb_node *parent = NULL; + + while (*p != NULL) { + struct str_node *sn; + int rc; + + parent = *p; + sn = rb_entry(parent, struct str_node, rb_node); + rc = strcmp(sn->s, entry); + + if (rc > 0) + p = &(*p)->rb_left; + else if (rc < 0) + p = &(*p)->rb_right; + else + return sn; + } + + return NULL; +} + +static int strlist__parse_list_entry(struct strlist *self, const char *s) +{ + if (strncmp(s, "file://", 7) == 0) + return strlist__load(self, s + 7); + + return strlist__add(self, s); +} + +int strlist__parse_list(struct strlist *self, const char *s) +{ + char *sep; + int err; + + while ((sep = strchr(s, ',')) != NULL) { + *sep = '\0'; + err = strlist__parse_list_entry(self, s); + *sep = ','; + if (err != 0) + return err; + s = sep + 1; + } + + return *s ? strlist__parse_list_entry(self, s) : 0; +} + +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->nr_entries = 0; + if (slist && strlist__parse_list(self, slist) != 0) + goto out_error; + } + + return self; +out_error: + free(self); + return NULL; +} + +void strlist__delete(struct strlist *self) +{ + if (self != NULL) { + struct str_node *pos; + struct rb_node *next = rb_first(&self->entries); + + while (next) { + pos = rb_entry(next, struct str_node, rb_node); + next = rb_next(&pos->rb_node); + strlist__remove(self, pos); + } + self->entries = RB_ROOT; + 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; +} diff --git a/tools/lib/lk/strlist.h b/tools/lib/lk/strlist.h new file mode 100644 index 0000000..562eed6 --- /dev/null +++ b/tools/lib/lk/strlist.h @@ -0,0 +1,78 @@ +#ifndef __LK_STRLIST_H +#define __LK_STRLIST_H + +#include +#include + +struct str_node { + struct rb_node rb_node; + const char *s; +}; + +struct strlist { + struct rb_root entries; + unsigned int nr_entries; + bool dupstr; +}; + +struct strlist *strlist__new(bool dupstr, const char *slist); +void strlist__delete(struct strlist *self); + +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); +struct str_node *strlist__find(struct strlist *self, const char *entry); + +static inline bool strlist__has_entry(struct strlist *self, const char *entry) +{ + return strlist__find(self, entry) != NULL; +} + +static inline bool strlist__empty(const struct strlist *self) +{ + return self->nr_entries == 0; +} + +static inline unsigned int strlist__nr_entries(const struct strlist *self) +{ + return self->nr_entries; +} + +/* For strlist iteration */ +static inline struct str_node *strlist__first(struct strlist *self) +{ + struct rb_node *rn = rb_first(&self->entries); + return rn ? rb_entry(rn, struct str_node, rb_node) : NULL; +} +static inline struct str_node *strlist__next(struct str_node *sn) +{ + struct rb_node *rn; + if (!sn) + return NULL; + rn = rb_next(&sn->rb_node); + return rn ? rb_entry(rn, struct str_node, rb_node) : NULL; +} + +/** + * strlist_for_each - iterate over a strlist + * @pos: the &struct str_node to use as a loop cursor. + * @self: the &struct strlist for loop. + */ +#define strlist__for_each(pos, self) \ + for (pos = strlist__first(self); pos; pos = strlist__next(pos)) + +/** + * strlist_for_each_safe - iterate over a strlist safe against removal of + * str_node + * @pos: the &struct str_node to use as a loop cursor. + * @n: another &struct str_node to use as temporary storage. + * @self: the &struct strlist for loop. + */ +#define strlist__for_each_safe(pos, n, self) \ + for (pos = strlist__first(self), n = strlist__next(pos); pos;\ + pos = n, n = strlist__next(n)) + +int strlist__parse_list(struct strlist *self, const char *s); +#endif /* __LK_STRLIST_H */ diff --git a/tools/perf/Makefile b/tools/perf/Makefile index 8770ff4..65a8a7b 100644 --- a/tools/perf/Makefile +++ b/tools/perf/Makefile @@ -365,7 +365,6 @@ LIB_H += util/quote.h LIB_H += util/header.h LIB_H += util/help.h LIB_H += util/session.h -LIB_H += util/strlist.h LIB_H += util/svghelper.h LIB_H += util/run-command.h LIB_H += util/sigchain.h @@ -390,10 +389,8 @@ LIB_OBJS += $(OUTPUT)util/levenshtein.o LIB_OBJS += $(OUTPUT)util/parse-options.o LIB_OBJS += $(OUTPUT)util/parse-events.o LIB_OBJS += $(OUTPUT)util/path.o -LIB_OBJS += $(OUTPUT)util/rbtree.o LIB_OBJS += $(OUTPUT)util/run-command.o LIB_OBJS += $(OUTPUT)util/quote.o -LIB_OBJS += $(OUTPUT)util/strlist.o LIB_OBJS += $(OUTPUT)util/wrapper.o LIB_OBJS += $(OUTPUT)util/sigchain.o LIB_OBJS += $(OUTPUT)util/symbol.o @@ -921,9 +918,6 @@ $(OUTPUT)util/config.o: util/config.c $(OUTPUT)PERF-CFLAGS $(OUTPUT)util/newt.o: util/newt.c $(OUTPUT)PERF-CFLAGS $(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) -DENABLE_SLFUTURE_CONST $< -$(OUTPUT)util/rbtree.o: ../../lib/rbtree.c $(OUTPUT)PERF-CFLAGS - $(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) -DETC_PERFCONFIG='"$(ETC_PERFCONFIG_SQ)"' $< - $(OUTPUT)util/scripting-engines/trace-event-perl.o: util/scripting-engines/trace-event-perl.c $(OUTPUT)PERF-CFLAGS $(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) $(PERL_EMBED_CCOPTS) -Wno-redundant-decls -Wno-strict-prototypes -Wno-unused-parameter -Wno-shadow $< diff --git a/tools/perf/builtin-buildid-cache.c b/tools/perf/builtin-buildid-cache.c index 5621017..3cac2d6 100644 --- a/tools/perf/builtin-buildid-cache.c +++ b/tools/perf/builtin-buildid-cache.c @@ -12,7 +12,7 @@ #include #include "util/header.h" #include "util/parse-options.h" -#include "util/strlist.h" +#include #include "util/symbol.h" static char const *add_name_list_str, *remove_name_list_str; diff --git a/tools/perf/builtin-probe.c b/tools/perf/builtin-probe.c index 8679663..f53edba 100644 --- a/tools/perf/builtin-probe.c +++ b/tools/perf/builtin-probe.c @@ -35,7 +35,7 @@ #include "perf.h" #include "builtin.h" #include -#include "util/strlist.h" +#include #include "util/symbol.h" #include #include diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c index e87180f..57fe707 100644 --- a/tools/perf/builtin-report.c +++ b/tools/perf/builtin-report.c @@ -15,7 +15,7 @@ #include #include "util/symbol.h" #include "util/callchain.h" -#include "util/strlist.h" +#include #include "util/values.h" #include "perf.h" diff --git a/tools/perf/builtin-timechart.c b/tools/perf/builtin-timechart.c index c90a68f..6e2dd8f 100644 --- a/tools/perf/builtin-timechart.c +++ b/tools/perf/builtin-timechart.c @@ -22,7 +22,7 @@ #include #include "util/symbol.h" #include "util/callchain.h" -#include "util/strlist.h" +#include #include "perf.h" #include "util/header.h" diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c index 928d3de..fda3406 100644 --- a/tools/perf/util/event.c +++ b/tools/perf/util/event.c @@ -4,7 +4,7 @@ #include "session.h" #include "sort.h" #include "string.h" -#include "strlist.h" +#include #include "thread.h" const char *event__name[] = { diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c index c33e17d..26f29ca 100644 --- a/tools/perf/util/probe-event.c +++ b/tools/perf/util/probe-event.c @@ -36,7 +36,7 @@ #include #include "event.h" #include "string.h" -#include "strlist.h" +#include #include #include "cache.h" #include diff --git a/tools/perf/util/probe-event.h b/tools/perf/util/probe-event.h index e9db1a2..22a6f17 100644 --- a/tools/perf/util/probe-event.h +++ b/tools/perf/util/probe-event.h @@ -2,7 +2,7 @@ #define _PROBE_EVENT_H #include -#include "strlist.h" +#include extern bool probe_event_dry_run; diff --git a/tools/perf/util/sort.h b/tools/perf/util/sort.h index 174b9c1..7a266b8 100644 --- a/tools/perf/util/sort.h +++ b/tools/perf/util/sort.h @@ -11,7 +11,7 @@ #include "symbol.h" #include "string.h" #include "callchain.h" -#include "strlist.h" +#include #include "values.h" #include "../perf.h" diff --git a/tools/perf/util/strlist.c b/tools/perf/util/strlist.c deleted file mode 100644 index 6783a20..0000000 --- a/tools/perf/util/strlist.c +++ /dev/null @@ -1,200 +0,0 @@ -/* - * (c) 2009 Arnaldo Carvalho de Melo - * - * Licensed under the GPLv2. - */ - -#include "strlist.h" -#include -#include -#include -#include - -static struct str_node *str_node__new(const char *s, bool dupstr) -{ - struct str_node *self = malloc(sizeof(*self)); - - if (self != NULL) { - if (dupstr) { - s = strdup(s); - if (s == NULL) - goto out_delete; - } - self->s = s; - } - - return self; - -out_delete: - free(self); - return NULL; -} - -static void str_node__delete(struct str_node *self, bool dupstr) -{ - if (dupstr) - free((void *)self->s); - free(self); -} - -int strlist__add(struct strlist *self, const char *new_entry) -{ - struct rb_node **p = &self->entries.rb_node; - struct rb_node *parent = NULL; - struct str_node *sn; - - while (*p != NULL) { - int rc; - - parent = *p; - sn = rb_entry(parent, struct str_node, rb_node); - rc = strcmp(sn->s, new_entry); - - if (rc > 0) - p = &(*p)->rb_left; - else if (rc < 0) - p = &(*p)->rb_right; - else - return -EEXIST; - } - - sn = str_node__new(new_entry, self->dupstr); - if (sn == NULL) - return -ENOMEM; - - rb_link_node(&sn->rb_node, parent, p); - rb_insert_color(&sn->rb_node, &self->entries); - ++self->nr_entries; - - return 0; -} - -int strlist__load(struct strlist *self, const char *filename) -{ - char entry[1024]; - int err; - FILE *fp = fopen(filename, "r"); - - if (fp == NULL) - return errno; - - while (fgets(entry, sizeof(entry), fp) != NULL) { - const size_t len = strlen(entry); - - if (len == 0) - continue; - entry[len - 1] = '\0'; - - err = strlist__add(self, entry); - if (err != 0) - goto out; - } - - err = 0; -out: - fclose(fp); - return err; -} - -void strlist__remove(struct strlist *self, struct str_node *sn) -{ - rb_erase(&sn->rb_node, &self->entries); - str_node__delete(sn, self->dupstr); -} - -struct str_node *strlist__find(struct strlist *self, const char *entry) -{ - struct rb_node **p = &self->entries.rb_node; - struct rb_node *parent = NULL; - - while (*p != NULL) { - struct str_node *sn; - int rc; - - parent = *p; - sn = rb_entry(parent, struct str_node, rb_node); - rc = strcmp(sn->s, entry); - - if (rc > 0) - p = &(*p)->rb_left; - else if (rc < 0) - p = &(*p)->rb_right; - else - return sn; - } - - return NULL; -} - -static int strlist__parse_list_entry(struct strlist *self, const char *s) -{ - if (strncmp(s, "file://", 7) == 0) - return strlist__load(self, s + 7); - - return strlist__add(self, s); -} - -int strlist__parse_list(struct strlist *self, const char *s) -{ - char *sep; - int err; - - while ((sep = strchr(s, ',')) != NULL) { - *sep = '\0'; - err = strlist__parse_list_entry(self, s); - *sep = ','; - if (err != 0) - return err; - s = sep + 1; - } - - return *s ? strlist__parse_list_entry(self, s) : 0; -} - -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->nr_entries = 0; - if (slist && strlist__parse_list(self, slist) != 0) - goto out_error; - } - - return self; -out_error: - free(self); - return NULL; -} - -void strlist__delete(struct strlist *self) -{ - if (self != NULL) { - struct str_node *pos; - struct rb_node *next = rb_first(&self->entries); - - while (next) { - pos = rb_entry(next, struct str_node, rb_node); - next = rb_next(&pos->rb_node); - strlist__remove(self, pos); - } - self->entries = RB_ROOT; - 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; -} diff --git a/tools/perf/util/strlist.h b/tools/perf/util/strlist.h deleted file mode 100644 index 3ba8390..0000000 --- a/tools/perf/util/strlist.h +++ /dev/null @@ -1,78 +0,0 @@ -#ifndef __PERF_STRLIST_H -#define __PERF_STRLIST_H - -#include -#include - -struct str_node { - struct rb_node rb_node; - const char *s; -}; - -struct strlist { - struct rb_root entries; - unsigned int nr_entries; - bool dupstr; -}; - -struct strlist *strlist__new(bool dupstr, const char *slist); -void strlist__delete(struct strlist *self); - -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); -struct str_node *strlist__find(struct strlist *self, const char *entry); - -static inline bool strlist__has_entry(struct strlist *self, const char *entry) -{ - return strlist__find(self, entry) != NULL; -} - -static inline bool strlist__empty(const struct strlist *self) -{ - return self->nr_entries == 0; -} - -static inline unsigned int strlist__nr_entries(const struct strlist *self) -{ - return self->nr_entries; -} - -/* For strlist iteration */ -static inline struct str_node *strlist__first(struct strlist *self) -{ - struct rb_node *rn = rb_first(&self->entries); - return rn ? rb_entry(rn, struct str_node, rb_node) : NULL; -} -static inline struct str_node *strlist__next(struct str_node *sn) -{ - struct rb_node *rn; - if (!sn) - return NULL; - rn = rb_next(&sn->rb_node); - return rn ? rb_entry(rn, struct str_node, rb_node) : NULL; -} - -/** - * strlist_for_each - iterate over a strlist - * @pos: the &struct str_node to use as a loop cursor. - * @self: the &struct strlist for loop. - */ -#define strlist__for_each(pos, self) \ - for (pos = strlist__first(self); pos; pos = strlist__next(pos)) - -/** - * strlist_for_each_safe - iterate over a strlist safe against removal of - * str_node - * @pos: the &struct str_node to use as a loop cursor. - * @n: another &struct str_node to use as temporary storage. - * @self: the &struct strlist for loop. - */ -#define strlist__for_each_safe(pos, n, self) \ - for (pos = strlist__first(self), n = strlist__next(pos); pos;\ - pos = n, n = strlist__next(n)) - -int strlist__parse_list(struct strlist *self, const char *s); -#endif /* __PERF_STRLIST_H */ diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 7012560..edd8202 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -13,7 +13,7 @@ #include #include "build-id.h" #include "symbol.h" -#include "strlist.h" +#include #include #include -- 1.7.1 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/