lib/seq_buf.c

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

File Facts

System
Linux kernel
Corpus path
lib/seq_buf.c
Extension
.c
Size
11242 bytes
Lines
440
Domain
Kernel Services
Bucket
lib
Inferred role
Kernel Services: exported/initcall integration point
Status
integration 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

if (s->len + len < s->size) {
			s->len += len;
			return 0;
		}
	}
	seq_buf_set_overflow(s);
	return -1;
}

/**
 * seq_buf_printf - sequence printing of information
 * @s: seq_buf descriptor
 * @fmt: printf format string
 *
 * Writes a printf() format into the sequence buffer.
 *
 * Returns: zero on success, -1 on overflow.
 */
int seq_buf_printf(struct seq_buf *s, const char *fmt, ...)
{
	va_list ap;
	int ret;

	va_start(ap, fmt);
	ret = seq_buf_vprintf(s, fmt, ap);
	va_end(ap);

	return ret;
}
EXPORT_SYMBOL_GPL(seq_buf_printf);

/**
 * seq_buf_do_printk - printk() seq_buf line by line
 * @s: seq_buf descriptor
 * @lvl: printk level
 *
 * printk()-s a multi-line sequential buffer line by line. The function
 * makes sure that the buffer in @s is NUL-terminated and safe to read
 * as a string.
 */
void seq_buf_do_printk(struct seq_buf *s, const char *lvl)
{
	const char *start, *lf;

	if (s->size == 0 || s->len == 0)
		return;

	start = seq_buf_str(s);
	while ((lf = strchr(start, '\n'))) {
		int len = lf - start + 1;

		printk("%s%.*s", lvl, len, start);
		start = ++lf;
	}

	/* No trailing LF */
	if (start < s->buffer + s->len)
		printk("%s%s\n", lvl, start);
}
EXPORT_SYMBOL_GPL(seq_buf_do_printk);

#ifdef CONFIG_BINARY_PRINTF
/**
 * seq_buf_bprintf - Write the printf string from binary arguments
 * @s: seq_buf descriptor
 * @fmt: The format string for the @binary arguments
 * @binary: The binary arguments for @fmt.
 *
 * When recording in a fast path, a printf may be recorded with just
 * saving the format and the arguments as they were passed to the
 * function, instead of wasting cycles converting the arguments into
 * ASCII characters. Instead, the arguments are saved in a 32 bit
 * word array that is defined by the format string constraints.
 *
 * This function will take the format and the binary array and finish
 * the conversion into the ASCII string within the buffer.
 *
 * Returns: zero on success, -1 on overflow.
 */
int seq_buf_bprintf(struct seq_buf *s, const char *fmt, const u32 *binary)
{
	unsigned int len = seq_buf_buffer_left(s);
	int ret;

	WARN_ON(s->size == 0);

	if (s->len < s->size) {
		ret = bstr_printf(s->buffer + s->len, len, fmt, binary);
		if (s->len + ret < s->size) {
			s->len += ret;

Annotation

Implementation Notes