7 #include <linux/refcount.h>
11 struct rb_node rb_node;
15 /* Should perhaps be moved to struct machine */
16 static struct rb_root comm_str_root;
18 static struct comm_str *comm_str__get(struct comm_str *cs)
21 refcount_inc(&cs->refcnt);
25 static void comm_str__put(struct comm_str *cs)
27 if (cs && refcount_dec_and_test(&cs->refcnt)) {
28 rb_erase(&cs->rb_node, &comm_str_root);
34 static struct comm_str *comm_str__alloc(const char *str)
38 cs = zalloc(sizeof(*cs));
42 cs->str = strdup(str);
48 refcount_set(&cs->refcnt, 1);
53 static struct comm_str *comm_str__findnew(const char *str, struct rb_root *root)
55 struct rb_node **p = &root->rb_node;
56 struct rb_node *parent = NULL;
57 struct comm_str *iter, *new;
62 iter = rb_entry(parent, struct comm_str, rb_node);
64 cmp = strcmp(str, iter->str);
66 return comm_str__get(iter);
74 new = comm_str__alloc(str);
78 rb_link_node(&new->rb_node, parent, p);
79 rb_insert_color(&new->rb_node, root);
84 struct comm *comm__new(const char *str, u64 timestamp, bool exec)
86 struct comm *comm = zalloc(sizeof(*comm));
91 comm->start = timestamp;
94 comm->comm_str = comm_str__findnew(str, &comm_str_root);
95 if (!comm->comm_str) {
103 int comm__override(struct comm *comm, const char *str, u64 timestamp, bool exec)
105 struct comm_str *new, *old = comm->comm_str;
107 new = comm_str__findnew(str, &comm_str_root);
112 comm->comm_str = new;
113 comm->start = timestamp;
120 void comm__free(struct comm *comm)
122 comm_str__put(comm->comm_str);
126 const char *comm__str(const struct comm *comm)
128 return comm->comm_str->str;