perf util: Add findnew method to intlist
authorDavid Ahern <dsahern@gmail.com>
Wed, 9 Oct 2013 03:26:52 +0000 (21:26 -0600)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 14 Oct 2013 13:28:48 +0000 (10:28 -0300)
Similar to other findnew based methods if the requested object is not
found, add it to the list.

v2: followed format of other findnew methods per acme's request

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.kim@lge.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/r/1381289214-24885-2-git-send-email-dsahern@gmail.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/intlist.c
tools/perf/util/intlist.h
tools/perf/util/rblist.c
tools/perf/util/rblist.h

index 826d7b38c9a08abb96e1ed381f0e4c2b8e60c2cf..89715b64a31578e2f26fc98a4e9c778bb72710d5 100644 (file)
@@ -58,22 +58,36 @@ void intlist__remove(struct intlist *ilist, struct int_node *node)
        rblist__remove_node(&ilist->rblist, &node->rb_node);
 }
 
-struct int_node *intlist__find(struct intlist *ilist, int i)
+static struct int_node *__intlist__findnew(struct intlist *ilist,
+                                          int i, bool create)
 {
-       struct int_node *node;
+       struct int_node *node = NULL;
        struct rb_node *rb_node;
 
        if (ilist == NULL)
                return NULL;
 
-       node = NULL;
-       rb_node = rblist__find(&ilist->rblist, (void *)((long)i));
+       if (create)
+               rb_node = rblist__findnew(&ilist->rblist, (void *)((long)i));
+       else
+               rb_node = rblist__find(&ilist->rblist, (void *)((long)i));
+
        if (rb_node)
                node = container_of(rb_node, struct int_node, rb_node);
 
        return node;
 }
 
+struct int_node *intlist__find(struct intlist *ilist, int i)
+{
+       return __intlist__findnew(ilist, i, false);
+}
+
+struct int_node *intlist__findnew(struct intlist *ilist, int i)
+{
+       return __intlist__findnew(ilist, i, true);
+}
+
 static int intlist__parse_list(struct intlist *ilist, const char *s)
 {
        char *sep;
index 0eb00ac39e011e53137dffdbaca39768644c0b3d..aa6877d36858288c9027d83ab2390b82a2370fc2 100644 (file)
@@ -24,6 +24,7 @@ int intlist__add(struct intlist *ilist, int i);
 
 struct int_node *intlist__entry(const struct intlist *ilist, unsigned int idx);
 struct int_node *intlist__find(struct intlist *ilist, int i);
+struct int_node *intlist__findnew(struct intlist *ilist, int i);
 
 static inline bool intlist__has_entry(struct intlist *ilist, int i)
 {
index a16cdd2625ad2b4a0ff8ff39d6b91b331f3a3a1e..0dfe27d99458c845991e490283c067d0d0706221 100644 (file)
@@ -48,10 +48,12 @@ void rblist__remove_node(struct rblist *rblist, struct rb_node *rb_node)
        rblist->node_delete(rblist, rb_node);
 }
 
-struct rb_node *rblist__find(struct rblist *rblist, const void *entry)
+static struct rb_node *__rblist__findnew(struct rblist *rblist,
+                                        const void *entry,
+                                        bool create)
 {
        struct rb_node **p = &rblist->entries.rb_node;
-       struct rb_node *parent = NULL;
+       struct rb_node *parent = NULL, *new_node = NULL;
 
        while (*p != NULL) {
                int rc;
@@ -67,7 +69,26 @@ struct rb_node *rblist__find(struct rblist *rblist, const void *entry)
                        return parent;
        }
 
-       return NULL;
+       if (create) {
+               new_node = rblist->node_new(rblist, entry);
+               if (new_node) {
+                       rb_link_node(new_node, parent, p);
+                       rb_insert_color(new_node, &rblist->entries);
+                       ++rblist->nr_entries;
+               }
+       }
+
+       return new_node;
+}
+
+struct rb_node *rblist__find(struct rblist *rblist, const void *entry)
+{
+       return __rblist__findnew(rblist, entry, false);
+}
+
+struct rb_node *rblist__findnew(struct rblist *rblist, const void *entry)
+{
+       return __rblist__findnew(rblist, entry, true);
 }
 
 void rblist__init(struct rblist *rblist)
index 6d0cae5ae83d5e848b56a9518575ad3c7a114444..ff9913b994c26126203e303f694021f9d24e739a 100644 (file)
@@ -32,6 +32,7 @@ void rblist__delete(struct rblist *rblist);
 int rblist__add_node(struct rblist *rblist, const void *new_entry);
 void rblist__remove_node(struct rblist *rblist, struct rb_node *rb_node);
 struct rb_node *rblist__find(struct rblist *rblist, const void *entry);
+struct rb_node *rblist__findnew(struct rblist *rblist, const void *entry);
 struct rb_node *rblist__entry(const struct rblist *rblist, unsigned int idx);
 
 static inline bool rblist__empty(const struct rblist *rblist)