comm.c 2.2 KB
Newer Older
1 2 3 4
#include "comm.h"
#include "util.h"
#include <stdlib.h>
#include <stdio.h>
5
#include <linux/atomic.h>
6 7 8 9

struct comm_str {
	char *str;
	struct rb_node rb_node;
10
	atomic_t refcnt;
11 12 13 14 15
};

/* Should perhaps be moved to struct machine */
static struct rb_root comm_str_root;

16
static struct comm_str *comm_str__get(struct comm_str *cs)
17
{
18 19 20
	if (cs)
		atomic_inc(&cs->refcnt);
	return cs;
21 22 23 24
}

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);
27
		zfree(&cs->str);
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
		free(cs);
	}
}

static struct comm_str *comm_str__alloc(const char *str)
{
	struct comm_str *cs;

	cs = zalloc(sizeof(*cs));
	if (!cs)
		return NULL;

	cs->str = strdup(str);
	if (!cs->str) {
		free(cs);
		return NULL;
	}

46 47
	atomic_set(&cs->refcnt, 0);

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
	return cs;
}

static struct comm_str *comm_str__findnew(const char *str, struct rb_root *root)
{
	struct rb_node **p = &root->rb_node;
	struct rb_node *parent = NULL;
	struct comm_str *iter, *new;
	int cmp;

	while (*p != NULL) {
		parent = *p;
		iter = rb_entry(parent, struct comm_str, rb_node);

		cmp = strcmp(str, iter->str);
		if (!cmp)
			return iter;

		if (cmp < 0)
			p = &(*p)->rb_left;
		else
			p = &(*p)->rb_right;
	}

	new = comm_str__alloc(str);
	if (!new)
		return NULL;

	rb_link_node(&new->rb_node, parent, p);
	rb_insert_color(&new->rb_node, root);

	return new;
}

82
struct comm *comm__new(const char *str, u64 timestamp, bool exec)
83 84 85 86 87 88 89
{
	struct comm *comm = zalloc(sizeof(*comm));

	if (!comm)
		return NULL;

	comm->start = timestamp;
90
	comm->exec = exec;
91 92 93 94 95 96 97 98 99 100 101 102

	comm->comm_str = comm_str__findnew(str, &comm_str_root);
	if (!comm->comm_str) {
		free(comm);
		return NULL;
	}

	comm_str__get(comm->comm_str);

	return comm;
}

103
int comm__override(struct comm *comm, const char *str, u64 timestamp, bool exec)
104
{
105
	struct comm_str *new, *old = comm->comm_str;
106

107 108 109
	new = comm_str__findnew(str, &comm_str_root);
	if (!new)
		return -ENOMEM;
110

111
	comm_str__get(new);
112
	comm_str__put(old);
113 114
	comm->comm_str = new;
	comm->start = timestamp;
115 116
	if (exec)
		comm->exec = true;
117 118

	return 0;
119 120
}

121 122 123 124 125 126 127 128 129 130
void comm__free(struct comm *comm)
{
	comm_str__put(comm->comm_str);
	free(comm);
}

const char *comm__str(const struct comm *comm)
{
	return comm->comm_str->str;
}