5 #include <linux/atomic.h>
9 struct rb_node rb_node;
13 /* Should perhaps be moved to struct machine */
14 static struct rb_root comm_str_root;
16 static struct comm_str *comm_str__get(struct comm_str *cs)
19 atomic_inc(&cs->refcnt);
23 static void comm_str__put(struct comm_str *cs)
25 if (cs && atomic_dec_and_test(&cs->refcnt)) {
26 rb_erase(&cs->rb_node, &comm_str_root);
32 static struct comm_str *comm_str__alloc(const char *str)
36 cs = zalloc(sizeof(*cs));
40 cs->str = strdup(str);
46 atomic_set(&cs->refcnt, 0);
51 static struct comm_str *comm_str__findnew(const char *str, struct rb_root *root)
53 struct rb_node **p = &root->rb_node;
54 struct rb_node *parent = NULL;
55 struct comm_str *iter, *new;
60 iter = rb_entry(parent, struct comm_str, rb_node);
62 cmp = strcmp(str, iter->str);
72 new = comm_str__alloc(str);
76 rb_link_node(&new->rb_node, parent, p);
77 rb_insert_color(&new->rb_node, root);
82 struct comm *comm__new(const char *str, u64 timestamp, bool exec)
84 struct comm *comm = zalloc(sizeof(*comm));
89 comm->start = timestamp;
92 comm->comm_str = comm_str__findnew(str, &comm_str_root);
93 if (!comm->comm_str) {
98 comm_str__get(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);
113 comm->comm_str = new;
114 comm->start = timestamp;
121 void comm__free(struct comm *comm)
123 comm_str__put(comm->comm_str);
127 const char *comm__str(const struct comm *comm)
129 return comm->comm_str->str;