Merge patch series "membuff: Add tests and update to support a flag for empty/full"

Simon Glass <sjg@chromium.org> says:

The membuff implementation curently has no tests. It also assumes that
head and tail can never correspond unless the buffer is empty.

This series provides a compile-time flag to support a 'full' flag. It
also adds some tests of the main routines.

The data structure is also renamed to membuf which fits better with
U-Boot.

There may be some cases in the code which could be optimised a little,
but the implementation is functional.

Link: https://lore.kernel.org/r/20250318152059.1464369-1-sjg@chromium.org
This commit is contained in:
Tom Rini
2025-04-03 16:54:59 -06:00
12 changed files with 366 additions and 114 deletions

View File

@@ -23,7 +23,7 @@
#include <board_f.h>
#include <event_internal.h>
#include <fdtdec.h>
#include <membuff.h>
#include <membuf.h>
#include <linux/list.h>
#include <linux/build_bug.h>
#include <asm-offsets.h>
@@ -316,14 +316,14 @@ struct global_data {
*
* This buffer is used to collect output during console recording.
*/
struct membuff console_out;
struct membuf console_out;
/**
* @console_in: input buffer for console recording
*
* If console recording is activated, this buffer can be used to
* emulate input.
*/
struct membuff console_in;
struct membuf console_in;
#endif
#if CONFIG_IS_ENABLED(VIDEO)
/**

View File

@@ -6,11 +6,13 @@
* Copyright (c) 1992 Simon Glass
*/
#ifndef _MEMBUFF_H
#define _MEMBUFF_H
#ifndef _membuf_H
#define _membuf_H
#include <stdbool.h>
/**
* @struct membuff: holds the state of a membuff - it is used for input and
* @struct membuf: holds the state of a membuff - it is used for input and
* output buffers. The buffer extends from @start to (@start + @size - 1).
* Data in the buffer extends from @tail to @head: it is written in at
* @head and read out from @tail. The membuff is empty when @head == @tail
@@ -23,13 +25,13 @@
*
* .............xxxxxxxxxxxxxxxx.........................
* ^ ^
* tail head
* ^start tail head ^end
*
* xxxxxxxxxxxxx................xxxxxxxxxxxxxxxxxxxxxxxxx
* ^ ^
* head tail
*/
struct membuff {
struct membuf {
char *start; /** the start of the buffer */
char *end; /** the end of the buffer (start + length) */
char *head; /** current buffer head */
@@ -37,16 +39,16 @@ struct membuff {
};
/**
* membuff_purge() - reset a membuff to the empty state
* membuf_purge() - reset a membuff to the empty state
*
* Initialise head and tail pointers so that the membuff becomes empty.
*
* @mb: membuff to purge
*/
void membuff_purge(struct membuff *mb);
void membuf_purge(struct membuf *mb);
/**
* membuff_putraw() - find out where bytes can be written
* membuf_putraw() - find out where bytes can be written
*
* Work out where in the membuff some data could be written. Return a pointer
* to the address and the number of bytes which can be written there. If
@@ -64,10 +66,10 @@ void membuff_purge(struct membuff *mb);
* @data: the address data can be written to
* Return: number of bytes which can be written
*/
int membuff_putraw(struct membuff *mb, int maxlen, bool update, char **data);
int membuf_putraw(struct membuf *mb, int maxlen, bool update, char **data);
/**
* membuff_getraw() - find and return a pointer to available bytes
* membuf_getraw() - find and return a pointer to available bytes
*
* Returns a pointer to any valid input data in the given membuff and
* optionally marks it as read. Note that not all input data may not be
@@ -82,37 +84,37 @@ int membuff_putraw(struct membuff *mb, int maxlen, bool update, char **data);
* @data: returns address of data in input membuff
* Return: the number of bytes available at *@data
*/
int membuff_getraw(struct membuff *mb, int maxlen, bool update, char **data);
int membuf_getraw(struct membuf *mb, int maxlen, bool update, char **data);
/**
* membuff_putbyte() - Writes a byte to a membuff
* membuf_putbyte() - Writes a byte to a membuff
*
* @mb: membuff to adjust
* @ch: byte to write
* Return: true on success, false if membuff is full
*/
bool membuff_putbyte(struct membuff *mb, int ch);
bool membuf_putbyte(struct membuf *mb, int ch);
/**
* @mb: membuff to adjust
* membuff_getbyte() - Read a byte from the membuff
* membuf_getbyte() - Read a byte from the membuff
* Return: the byte read, or -1 if the membuff is empty
*/
int membuff_getbyte(struct membuff *mb);
int membuf_getbyte(struct membuf *mb);
/**
* membuff_peekbyte() - check the next available byte
* membuf_peekbyte() - check the next available byte
*
* Return the next byte which membuff_getbyte() would return, without
* Return the next byte which membuf_getbyte() would return, without
* removing it from the membuff.
*
* @mb: membuff to adjust
* Return: the byte peeked, or -1 if the membuff is empty
*/
int membuff_peekbyte(struct membuff *mb);
int membuf_peekbyte(struct membuf *mb);
/**
* membuff_get() - get data from a membuff
* membuf_get() - get data from a membuff
*
* Copies any available data (up to @maxlen bytes) to @buff and removes it
* from the membuff.
@@ -122,10 +124,10 @@ int membuff_peekbyte(struct membuff *mb);
* @maxlen: maximum number of bytes to read
* Return: the number of bytes read
*/
int membuff_get(struct membuff *mb, char *buff, int maxlen);
int membuf_get(struct membuf *mb, char *buff, int maxlen);
/**
* membuff_put() - write data to a membuff
* membuf_put() - write data to a membuff
*
* Writes some data to a membuff. Returns the number of bytes added. If this
* is less than @lnehgt, then the membuff got full
@@ -135,36 +137,36 @@ int membuff_get(struct membuff *mb, char *buff, int maxlen);
* @length: number of bytes to write from 'data'
* Return: the number of bytes added
*/
int membuff_put(struct membuff *mb, const char *buff, int length);
int membuf_put(struct membuf *mb, const char *buff, int length);
/**
* membuff_isempty() - check if a membuff is empty
* membuf_isempty() - check if a membuff is empty
*
* @mb: membuff to check
* Return: true if empty, else false
*/
bool membuff_isempty(struct membuff *mb);
bool membuf_isempty(struct membuf *mb);
/**
* membuff_avail() - check available data in a membuff
* membuf_avail() - check available data in a membuff
*
* @mb: membuff to check
* Return: number of bytes of data available
*/
int membuff_avail(struct membuff *mb);
int membuf_avail(struct membuf *mb);
/**
* membuff_size() - get the size of a membuff
* membuf_size() - get the size of a membuff
*
* Note that a membuff can only old data up to one byte less than its size.
*
* @mb: membuff to check
* Return: total size
*/
int membuff_size(struct membuff *mb);
int membuf_size(struct membuf *mb);
/**
* membuff_makecontig() - adjust all membuff data to be contiguous
* membuf_makecontig() - adjust all membuff data to be contiguous
*
* This places all data in a membuff into a single contiguous lump, if
* possible
@@ -172,18 +174,18 @@ int membuff_size(struct membuff *mb);
* @mb: membuff to adjust
* Return: true on success
*/
bool membuff_makecontig(struct membuff *mb);
bool membuf_makecontig(struct membuf *mb);
/**
* membuff_free() - find the number of bytes that can be written to a membuff
* membuf_free() - find the number of bytes that can be written to a membuff
*
* @mb: membuff to check
* Return: returns the number of bytes free in a membuff
*/
int membuff_free(struct membuff *mb);
int membuf_free(struct membuf *mb);
/**
* membuff_readline() - read a line of text from a membuff
* membuf_readline() - read a line of text from a membuff
*
* Reads a line of text of up to 'maxlen' characters from a membuff and puts
* it in @str. Any character less than @minch is assumed to be the end of
@@ -192,14 +194,16 @@ int membuff_free(struct membuff *mb);
* @mb: membuff to adjust
* @str: Place to put the line
* @maxlen: Maximum line length (excluding terminator)
* @minch: Minimum ASCII character to permit as part of the line (e.g. ' ')
* @must_fit: If true then str is empty if line doesn't fit
* Return: number of bytes read (including terminator) if a line has been
* read, 0 if nothing was there or line didn't fit when must_fit is set
*/
int membuff_readline(struct membuff *mb, char *str, int maxlen, int minch, bool must_fit);
int membuf_readline(struct membuf *mb, char *str, int maxlen, int minch,
bool must_fit);
/**
* membuff_extend_by() - expand a membuff
* membuf_extend_by() - expand a membuff
*
* Extends a membuff by the given number of bytes
*
@@ -209,38 +213,38 @@ int membuff_readline(struct membuff *mb, char *str, int maxlen, int minch, bool
* Return: 0 if the expand succeeded, -ENOMEM if not enough memory, -E2BIG
* if the the size would exceed @max
*/
int membuff_extend_by(struct membuff *mb, int by, int max);
int membuf_extend_by(struct membuf *mb, int by, int max);
/**
* membuff_init() - set up a new membuff using an existing membuff
* membuf_init() - set up a new membuff using an existing membuff
*
* @mb: membuff to set up
* @buff: Address of buffer
* @size: Size of buffer
*/
void membuff_init(struct membuff *mb, char *buff, int size);
void membuf_init(struct membuf *mb, char *buff, int size);
/**
* membuff_uninit() - clear a membuff so it can no longer be used
* membuf_uninit() - clear a membuff so it can no longer be used
*
* @mb: membuff to uninit
*/
void membuff_uninit(struct membuff *mb);
void membuf_uninit(struct membuf *mb);
/**
* membuff_new() - create a new membuff
* membuf_new() - create a new membuff
*
* @mb: membuff to init
* @size: size of membuff to create
* Return: 0 if OK, -ENOMEM if out of memory
*/
int membuff_new(struct membuff *mb, int size);
int membuf_new(struct membuf *mb, int size);
/**
* membuff_dispose() - free memory allocated to a membuff and uninit it
* membuf_dispose() - free memory allocated to a membuff and uninit it
*
* @mb: membuff to dispose
*/
void membuff_dispose(struct membuff *mb);
void membuf_dispose(struct membuf *mb);
#endif