diff options
author | David Ahern <dsahern@gmail.com> | 2012-07-31 00:31:34 -0400 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2012-08-03 09:42:25 -0400 |
commit | 70b40c4a43ddfcf7a06dad4be32ff1dd2b62be09 (patch) | |
tree | 76910778151d86cff5a42f2458b79d64eea0c803 /tools | |
parent | ee8dd3ca43f151d9fbe1edeef68fb8a77eb9f047 (diff) |
perf tools: Introduce intlist
Built on rblist - like strlist. Used in the next patch.
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-4-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/Makefile | 2 | ||||
-rw-r--r-- | tools/perf/util/intlist.c | 101 | ||||
-rw-r--r-- | tools/perf/util/intlist.h | 75 |
3 files changed, 178 insertions, 0 deletions
diff --git a/tools/perf/Makefile b/tools/perf/Makefile index 285f700a7836..32912af430f0 100644 --- a/tools/perf/Makefile +++ b/tools/perf/Makefile | |||
@@ -320,6 +320,7 @@ LIB_H += util/cgroup.h | |||
320 | LIB_H += $(TRACE_EVENT_DIR)event-parse.h | 320 | LIB_H += $(TRACE_EVENT_DIR)event-parse.h |
321 | LIB_H += util/target.h | 321 | LIB_H += util/target.h |
322 | LIB_H += util/rblist.h | 322 | LIB_H += util/rblist.h |
323 | LIB_H += util/intlist.h | ||
323 | 324 | ||
324 | LIB_OBJS += $(OUTPUT)util/abspath.o | 325 | LIB_OBJS += $(OUTPUT)util/abspath.o |
325 | LIB_OBJS += $(OUTPUT)util/alias.o | 326 | LIB_OBJS += $(OUTPUT)util/alias.o |
@@ -385,6 +386,7 @@ LIB_OBJS += $(OUTPUT)util/cpumap.o | |||
385 | LIB_OBJS += $(OUTPUT)util/cgroup.o | 386 | LIB_OBJS += $(OUTPUT)util/cgroup.o |
386 | LIB_OBJS += $(OUTPUT)util/target.o | 387 | LIB_OBJS += $(OUTPUT)util/target.o |
387 | LIB_OBJS += $(OUTPUT)util/rblist.o | 388 | LIB_OBJS += $(OUTPUT)util/rblist.o |
389 | LIB_OBJS += $(OUTPUT)util/intlist.o | ||
388 | 390 | ||
389 | BUILTIN_OBJS += $(OUTPUT)builtin-annotate.o | 391 | BUILTIN_OBJS += $(OUTPUT)builtin-annotate.o |
390 | 392 | ||
diff --git a/tools/perf/util/intlist.c b/tools/perf/util/intlist.c new file mode 100644 index 000000000000..fd530dced9cb --- /dev/null +++ b/tools/perf/util/intlist.c | |||
@@ -0,0 +1,101 @@ | |||
1 | /* | ||
2 | * Based on intlist.c by: | ||
3 | * (c) 2009 Arnaldo Carvalho de Melo <acme@redhat.com> | ||
4 | * | ||
5 | * Licensed under the GPLv2. | ||
6 | */ | ||
7 | |||
8 | #include <errno.h> | ||
9 | #include <stdlib.h> | ||
10 | #include <linux/compiler.h> | ||
11 | |||
12 | #include "intlist.h" | ||
13 | |||
14 | static struct rb_node *intlist__node_new(struct rblist *rblist __used, | ||
15 | const void *entry) | ||
16 | { | ||
17 | int i = (int)((long)entry); | ||
18 | struct rb_node *rc = NULL; | ||
19 | struct int_node *node = malloc(sizeof(*node)); | ||
20 | |||
21 | if (node != NULL) { | ||
22 | node->i = i; | ||
23 | rc = &node->rb_node; | ||
24 | } | ||
25 | |||
26 | return rc; | ||
27 | } | ||
28 | |||
29 | static void int_node__delete(struct int_node *ilist) | ||
30 | { | ||
31 | free(ilist); | ||
32 | } | ||
33 | |||
34 | static void intlist__node_delete(struct rblist *rblist __used, | ||
35 | struct rb_node *rb_node) | ||
36 | { | ||
37 | struct int_node *node = container_of(rb_node, struct int_node, rb_node); | ||
38 | |||
39 | int_node__delete(node); | ||
40 | } | ||
41 | |||
42 | static int intlist__node_cmp(struct rb_node *rb_node, const void *entry) | ||
43 | { | ||
44 | int i = (int)((long)entry); | ||
45 | struct int_node *node = container_of(rb_node, struct int_node, rb_node); | ||
46 | |||
47 | return node->i - i; | ||
48 | } | ||
49 | |||
50 | int intlist__add(struct intlist *ilist, int i) | ||
51 | { | ||
52 | return rblist__add_node(&ilist->rblist, (void *)((long)i)); | ||
53 | } | ||
54 | |||
55 | void intlist__remove(struct intlist *ilist __used, struct int_node *node) | ||
56 | { | ||
57 | int_node__delete(node); | ||
58 | } | ||
59 | |||
60 | struct int_node *intlist__find(struct intlist *ilist, int i) | ||
61 | { | ||
62 | struct int_node *node = NULL; | ||
63 | struct rb_node *rb_node = rblist__find(&ilist->rblist, (void *)((long)i)); | ||
64 | |||
65 | if (rb_node) | ||
66 | node = container_of(rb_node, struct int_node, rb_node); | ||
67 | |||
68 | return node; | ||
69 | } | ||
70 | |||
71 | struct intlist *intlist__new(void) | ||
72 | { | ||
73 | struct intlist *ilist = malloc(sizeof(*ilist)); | ||
74 | |||
75 | if (ilist != NULL) { | ||
76 | rblist__init(&ilist->rblist); | ||
77 | ilist->rblist.node_cmp = intlist__node_cmp; | ||
78 | ilist->rblist.node_new = intlist__node_new; | ||
79 | ilist->rblist.node_delete = intlist__node_delete; | ||
80 | } | ||
81 | |||
82 | return ilist; | ||
83 | } | ||
84 | |||
85 | void intlist__delete(struct intlist *ilist) | ||
86 | { | ||
87 | if (ilist != NULL) | ||
88 | rblist__delete(&ilist->rblist); | ||
89 | } | ||
90 | |||
91 | struct int_node *intlist__entry(const struct intlist *ilist, unsigned int idx) | ||
92 | { | ||
93 | struct int_node *node = NULL; | ||
94 | struct rb_node *rb_node; | ||
95 | |||
96 | rb_node = rblist__entry(&ilist->rblist, idx); | ||
97 | if (rb_node) | ||
98 | node = container_of(rb_node, struct int_node, rb_node); | ||
99 | |||
100 | return node; | ||
101 | } | ||
diff --git a/tools/perf/util/intlist.h b/tools/perf/util/intlist.h new file mode 100644 index 000000000000..6d63ab90db50 --- /dev/null +++ b/tools/perf/util/intlist.h | |||
@@ -0,0 +1,75 @@ | |||
1 | #ifndef __PERF_INTLIST_H | ||
2 | #define __PERF_INTLIST_H | ||
3 | |||
4 | #include <linux/rbtree.h> | ||
5 | #include <stdbool.h> | ||
6 | |||
7 | #include "rblist.h" | ||
8 | |||
9 | struct int_node { | ||
10 | struct rb_node rb_node; | ||
11 | int i; | ||
12 | }; | ||
13 | |||
14 | struct intlist { | ||
15 | struct rblist rblist; | ||
16 | }; | ||
17 | |||
18 | struct intlist *intlist__new(void); | ||
19 | void intlist__delete(struct intlist *ilist); | ||
20 | |||
21 | void intlist__remove(struct intlist *ilist, struct int_node *in); | ||
22 | int intlist__add(struct intlist *ilist, int i); | ||
23 | |||
24 | struct int_node *intlist__entry(const struct intlist *ilist, unsigned int idx); | ||
25 | struct int_node *intlist__find(struct intlist *ilist, int i); | ||
26 | |||
27 | static inline bool intlist__has_entry(struct intlist *ilist, int i) | ||
28 | { | ||
29 | return intlist__find(ilist, i) != NULL; | ||
30 | } | ||
31 | |||
32 | static inline bool intlist__empty(const struct intlist *ilist) | ||
33 | { | ||
34 | return rblist__empty(&ilist->rblist); | ||
35 | } | ||
36 | |||
37 | static inline unsigned int intlist__nr_entries(const struct intlist *ilist) | ||
38 | { | ||
39 | return rblist__nr_entries(&ilist->rblist); | ||
40 | } | ||
41 | |||
42 | /* For intlist iteration */ | ||
43 | static inline struct int_node *intlist__first(struct intlist *ilist) | ||
44 | { | ||
45 | struct rb_node *rn = rb_first(&ilist->rblist.entries); | ||
46 | return rn ? rb_entry(rn, struct int_node, rb_node) : NULL; | ||
47 | } | ||
48 | static inline struct int_node *intlist__next(struct int_node *in) | ||
49 | { | ||
50 | struct rb_node *rn; | ||
51 | if (!in) | ||
52 | return NULL; | ||
53 | rn = rb_next(&in->rb_node); | ||
54 | return rn ? rb_entry(rn, struct int_node, rb_node) : NULL; | ||
55 | } | ||
56 | |||
57 | /** | ||
58 | * intlist_for_each - iterate over a intlist | ||
59 | * @pos: the &struct int_node to use as a loop cursor. | ||
60 | * @ilist: the &struct intlist for loop. | ||
61 | */ | ||
62 | #define intlist__for_each(pos, ilist) \ | ||
63 | for (pos = intlist__first(ilist); pos; pos = intlist__next(pos)) | ||
64 | |||
65 | /** | ||
66 | * intlist_for_each_safe - iterate over a intlist safe against removal of | ||
67 | * int_node | ||
68 | * @pos: the &struct int_node to use as a loop cursor. | ||
69 | * @n: another &struct int_node to use as temporary storage. | ||
70 | * @ilist: the &struct intlist for loop. | ||
71 | */ | ||
72 | #define intlist__for_each_safe(pos, n, ilist) \ | ||
73 | for (pos = intlist__first(ilist), n = intlist__next(pos); pos;\ | ||
74 | pos = n, n = intlist__next(n)) | ||
75 | #endif /* __PERF_INTLIST_H */ | ||