lib/kunit/attributes.c

Source file repositories/reference/linux-study-clean/lib/kunit/attributes.c

File Facts

System
Linux kernel
Corpus path
lib/kunit/attributes.c
Extension
.c
Size
11650 bytes
Lines
475
Domain
Kernel Services
Bucket
lib
Inferred role
Kernel Services: implementation source
Status
source implementation candidate

Why This File Exists

Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.

Dependency Surface

Detected Declarations

Annotated Snippet

struct kunit_attr {
	const char *name;
	void *(*get_attr)(void *test_or_suite, bool is_test);
	const char *(*to_string)(void *attr, bool *to_free);
	int (*filter)(void *attr, const char *input, int *err);
	void *attr_default;
	enum print_ops print;
};

/* String Lists for enum Attributes */

static const char * const speed_str_list[] = {"unset", "very_slow", "slow", "normal"};

/* To String Methods */

static const char *attr_enum_to_string(void *attr, const char * const str_list[], bool *to_free)
{
	long val = (long)attr;

	*to_free = false;
	if (!val)
		return NULL;
	return str_list[val];
}

static const char *attr_bool_to_string(void *attr, bool *to_free)
{
	bool val = (bool)attr;

	*to_free = false;
	if (val)
		return "true";
	return "false";
}

static const char *attr_speed_to_string(void *attr, bool *to_free)
{
	return attr_enum_to_string(attr, speed_str_list, to_free);
}

static const char *attr_string_to_string(void *attr, bool *to_free)
{
	*to_free = false;
	return (char *) attr;
}

/* Filter Methods */

static const char op_list[] = "<>!=";

/*
 * Returns whether the inputted integer value matches the filter given
 * by the operation string and inputted integer.
 */
static int int_filter(long val, const char *op, int input, int *err)
{
	if (!strncmp(op, "<=", 2))
		return (val <= input);
	else if (!strncmp(op, ">=", 2))
		return (val >= input);
	else if (!strncmp(op, "!=", 2))
		return (val != input);
	else if (!strncmp(op, ">", 1))
		return (val > input);
	else if (!strncmp(op, "<", 1))
		return (val < input);
	else if (!strncmp(op, "=", 1))
		return (val == input);
	*err = -EINVAL;
	pr_err("kunit executor: invalid filter operation: %s\n", op);
	return false;
}

/*
 * Returns whether the inputted enum value "attr" matches the filter given
 * by the input string. Note: the str_list includes the corresponding string
 * list to the enum values.
 */
static int attr_enum_filter(void *attr, const char *input, int *err,
		const char * const str_list[], int max)
{
	int i, j, input_int = -1;
	long test_val = (long)attr;
	const char *input_val = NULL;

	for (i = 0; input[i]; i++) {
		if (!strchr(op_list, input[i])) {
			input_val = input + i;
			break;
		}

Annotation

Implementation Notes