x86/xen: resume timer irqs early
[linux/fpc-iii.git] / tools / perf / util / rblist.c
bloba16cdd2625ad2b4a0ff8ff39d6b91b331f3a3a1e
1 /*
2 * Based on strlist.c by:
3 * (c) 2009 Arnaldo Carvalho de Melo <acme@redhat.com>
5 * Licensed under the GPLv2.
6 */
8 #include <errno.h>
9 #include <stdio.h>
10 #include <stdlib.h>
12 #include "rblist.h"
14 int rblist__add_node(struct rblist *rblist, const void *new_entry)
16 struct rb_node **p = &rblist->entries.rb_node;
17 struct rb_node *parent = NULL, *new_node;
19 while (*p != NULL) {
20 int rc;
22 parent = *p;
24 rc = rblist->node_cmp(parent, new_entry);
25 if (rc > 0)
26 p = &(*p)->rb_left;
27 else if (rc < 0)
28 p = &(*p)->rb_right;
29 else
30 return -EEXIST;
33 new_node = rblist->node_new(rblist, new_entry);
34 if (new_node == NULL)
35 return -ENOMEM;
37 rb_link_node(new_node, parent, p);
38 rb_insert_color(new_node, &rblist->entries);
39 ++rblist->nr_entries;
41 return 0;
44 void rblist__remove_node(struct rblist *rblist, struct rb_node *rb_node)
46 rb_erase(rb_node, &rblist->entries);
47 --rblist->nr_entries;
48 rblist->node_delete(rblist, rb_node);
51 struct rb_node *rblist__find(struct rblist *rblist, const void *entry)
53 struct rb_node **p = &rblist->entries.rb_node;
54 struct rb_node *parent = NULL;
56 while (*p != NULL) {
57 int rc;
59 parent = *p;
61 rc = rblist->node_cmp(parent, entry);
62 if (rc > 0)
63 p = &(*p)->rb_left;
64 else if (rc < 0)
65 p = &(*p)->rb_right;
66 else
67 return parent;
70 return NULL;
73 void rblist__init(struct rblist *rblist)
75 if (rblist != NULL) {
76 rblist->entries = RB_ROOT;
77 rblist->nr_entries = 0;
80 return;
83 void rblist__delete(struct rblist *rblist)
85 if (rblist != NULL) {
86 struct rb_node *pos, *next = rb_first(&rblist->entries);
88 while (next) {
89 pos = next;
90 next = rb_next(pos);
91 rblist__remove_node(rblist, pos);
93 free(rblist);
97 struct rb_node *rblist__entry(const struct rblist *rblist, unsigned int idx)
99 struct rb_node *node;
101 for (node = rb_first(&rblist->entries); node; node = rb_next(node)) {
102 if (!idx--)
103 return node;
106 return NULL;