tools/perf/util/comm.c

Source file repositories/reference/linux-study-clean/tools/perf/util/comm.c

File Facts

System
Linux kernel
Corpus path
tools/perf/util/comm.c
Extension
.c
Size
5302 bytes
Lines
238
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

if (comm_strs->num_strs == comm_strs->capacity) {
			struct comm_str **tmp;

			tmp = reallocarray(comm_strs->strs,
					   comm_strs->capacity + 16,
					   sizeof(*comm_strs->strs));
			if (!tmp) {
				up_write(&comm_strs->lock);
				return NULL;
			}
			comm_strs->strs = tmp;
			comm_strs->capacity += 16;
		}
		result = comm_str__new(str);
		if (result) {
			int low = 0, high = comm_strs->num_strs - 1;
			int insert = comm_strs->num_strs; /* Default to inserting at the end. */

			while (low <= high) {
				int mid = low + (high - low) / 2;
				int cmp = strcmp(comm_str__str(comm_strs->strs[mid]), str);

				if (cmp < 0) {
					low = mid + 1;
				} else {
					high = mid - 1;
					insert = mid;
				}
			}
			memmove(&comm_strs->strs[insert + 1], &comm_strs->strs[insert],
				(comm_strs->num_strs - insert) * sizeof(struct comm_str *));
			comm_strs->num_strs++;
			comm_strs->strs[insert] = result;
		}
	}
	up_write(&comm_strs->lock);
	return comm_str__get(result);
}

struct comm *comm__new(const char *str, u64 timestamp, bool exec)
{
	struct comm *comm = zalloc(sizeof(*comm));

	if (!comm)
		return NULL;

	comm->start = timestamp;
	comm->exec = exec;

	comm->comm_str = comm_strs__findnew(str);
	if (!comm->comm_str) {
		free(comm);
		return NULL;
	}

	return comm;
}

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

	new = comm_strs__findnew(str);
	if (!new)
		return -ENOMEM;

	comm_str__put(old);
	comm->comm_str = new;
	comm->start = timestamp;
	if (exec)
		comm->exec = true;

	return 0;
}

void comm__free(struct comm *comm)
{
	comm_str__put(comm->comm_str);
	free(comm);
}

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

Annotation

Implementation Notes