License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[linux-2.6-block.git] / tools / perf / util / comm.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1902efe7
FW
2#include "comm.h"
3#include "util.h"
a43783ae 4#include <errno.h>
1902efe7
FW
5#include <stdlib.h>
6#include <stdio.h>
72f7c4d2 7#include <string.h>
6df74bc0 8#include <linux/refcount.h>
1902efe7
FW
9
10struct comm_str {
11 char *str;
12 struct rb_node rb_node;
6df74bc0 13 refcount_t refcnt;
1902efe7
FW
14};
15
16/* Should perhaps be moved to struct machine */
17static struct rb_root comm_str_root;
18
86c19525 19static struct comm_str *comm_str__get(struct comm_str *cs)
1902efe7 20{
86c19525 21 if (cs)
6df74bc0 22 refcount_inc(&cs->refcnt);
86c19525 23 return cs;
1902efe7
FW
24}
25
26static void comm_str__put(struct comm_str *cs)
27{
6df74bc0 28 if (cs && refcount_dec_and_test(&cs->refcnt)) {
1902efe7 29 rb_erase(&cs->rb_node, &comm_str_root);
74cf249d 30 zfree(&cs->str);
1902efe7
FW
31 free(cs);
32 }
33}
34
35static struct comm_str *comm_str__alloc(const char *str)
36{
37 struct comm_str *cs;
38
39 cs = zalloc(sizeof(*cs));
40 if (!cs)
41 return NULL;
42
43 cs->str = strdup(str);
44 if (!cs->str) {
45 free(cs);
46 return NULL;
47 }
48
6df74bc0 49 refcount_set(&cs->refcnt, 1);
86c19525 50
1902efe7
FW
51 return cs;
52}
53
54static struct comm_str *comm_str__findnew(const char *str, struct rb_root *root)
55{
56 struct rb_node **p = &root->rb_node;
57 struct rb_node *parent = NULL;
58 struct comm_str *iter, *new;
59 int cmp;
60
61 while (*p != NULL) {
62 parent = *p;
63 iter = rb_entry(parent, struct comm_str, rb_node);
64
65 cmp = strcmp(str, iter->str);
66 if (!cmp)
6df74bc0 67 return comm_str__get(iter);
1902efe7
FW
68
69 if (cmp < 0)
70 p = &(*p)->rb_left;
71 else
72 p = &(*p)->rb_right;
73 }
74
75 new = comm_str__alloc(str);
76 if (!new)
77 return NULL;
78
79 rb_link_node(&new->rb_node, parent, p);
80 rb_insert_color(&new->rb_node, root);
81
82 return new;
83}
84
65de51f9 85struct comm *comm__new(const char *str, u64 timestamp, bool exec)
1902efe7
FW
86{
87 struct comm *comm = zalloc(sizeof(*comm));
88
89 if (!comm)
90 return NULL;
91
92 comm->start = timestamp;
65de51f9 93 comm->exec = exec;
1902efe7
FW
94
95 comm->comm_str = comm_str__findnew(str, &comm_str_root);
96 if (!comm->comm_str) {
97 free(comm);
98 return NULL;
99 }
100
1902efe7
FW
101 return comm;
102}
103
65de51f9 104int comm__override(struct comm *comm, const char *str, u64 timestamp, bool exec)
4dfced35 105{
3178f58b 106 struct comm_str *new, *old = comm->comm_str;
4dfced35 107
3178f58b
FW
108 new = comm_str__findnew(str, &comm_str_root);
109 if (!new)
110 return -ENOMEM;
4dfced35 111
4dfced35 112 comm_str__put(old);
3178f58b
FW
113 comm->comm_str = new;
114 comm->start = timestamp;
65de51f9
AH
115 if (exec)
116 comm->exec = true;
3178f58b
FW
117
118 return 0;
4dfced35
NK
119}
120
1902efe7
FW
121void comm__free(struct comm *comm)
122{
123 comm_str__put(comm->comm_str);
124 free(comm);
125}
126
127const char *comm__str(const struct comm *comm)
128{
129 return comm->comm_str->str;
130}