[PATCH 4/6] membuf: Add an easy way to set up a buffer with data
Simon Glass
sjg at chromium.org
Wed Mar 19 12:59:06 CET 2025
In some cases it is useful to set up a buffer with some data to read,
e.g. when reading lines from a text file. Add a helper for this.
Tidy up the comment for membuf_init() while we are here.
Signed-off-by: Simon Glass <sjg at chromium.org>
---
include/membuf.h | 15 ++++++++++++++-
lib/membuf.c | 8 ++++++++
2 files changed, 22 insertions(+), 1 deletion(-)
diff --git a/include/membuf.h b/include/membuf.h
index b495a72652b..f5c474cf730 100644
--- a/include/membuf.h
+++ b/include/membuf.h
@@ -222,7 +222,9 @@ int membuf_readline(struct membuf *mb, char *str, int maxlen, int minch,
int membuf_extend_by(struct membuf *mb, int by, int max);
/**
- * membuf_init() - set up a new membuff using an existing membuff
+ * membuf_init() - set up a new membuff using an existing buffer
+ *
+ * The buffer is initially empty
*
* @mb: membuff to set up
* @buff: Address of buffer
@@ -230,6 +232,17 @@ int membuf_extend_by(struct membuf *mb, int by, int max);
*/
void membuf_init(struct membuf *mb, char *buff, int size);
+/**
+ * membuf_init_with_data() - set up a new membuff using existing data
+ *
+ * The buffer is set up to contain the provided data
+ *
+ * @mb: membuff to set up
+ * @buff: Address of buffer
+ * @size: Size of buffer
+ */
+void membuf_init_with_data(struct membuf *mb, char *buff, int size);
+
/**
* membuf_uninit() - clear a membuff so it can no longer be used
*
diff --git a/lib/membuf.c b/lib/membuf.c
index 016430ae988..347c92e3326 100644
--- a/lib/membuf.c
+++ b/lib/membuf.c
@@ -405,6 +405,14 @@ void membuf_init(struct membuf *mb, char *buff, int size)
membuf_purge(mb);
}
+void membuf_init_with_data(struct membuf *mb, char *buff, int size)
+{
+ char *data;
+
+ membuf_init(mb, buff, size);
+ membuf_putraw(mb, size, true, &data);
+}
+
int membuf_new(struct membuf *mb, int size)
{
mb->start = malloc(size);
--
2.43.0
More information about the U-Boot
mailing list