[Concept,01/26] lib: abuf: Add abuf_init_addr() helper

Message ID 20260110202906.187370-2-sjg@u-boot.org
State New
Headers
Series boot: pxe: Add three-phase API and fix memory leaks |

Commit Message

Simon Glass Jan. 10, 2026, 8:28 p.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

Add a helper function that combines abuf_init() and abuf_map_sysmem()
into a single call. This simplifies code that needs to set up an abuf
from a memory address.

Co-developed-by: Claude Opus 4.5 <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

 include/abuf.h  | 12 ++++++++++++
 lib/abuf.c      |  6 ++++++
 test/lib/abuf.c | 23 +++++++++++++++++++++++
 3 files changed, 41 insertions(+)
  

Patch

diff --git a/include/abuf.h b/include/abuf.h
index 804ab0e3e57..42a80819d82 100644
--- a/include/abuf.h
+++ b/include/abuf.h
@@ -214,6 +214,18 @@  void abuf_init_const(struct abuf *abuf, const void *data, size_t size);
  */
 void abuf_init_const_addr(struct abuf *abuf, ulong addr, size_t size);
 
+/**
+ * abuf_init_addr() - Set up a new buffer at a given address
+ *
+ * This is similar to abuf_init_const_addr() but uses abuf_map_sysmem()
+ * internally. Note that the abuf is unallocated.
+ *
+ * @abuf: abuf to set up
+ * @addr: Address to use
+ * @size: Size of buffer
+ */
+void abuf_init_addr(struct abuf *abuf, ulong addr, size_t size);
+
 /**
  * abuf_init_size() - Set up an allocated abuf
  *
diff --git a/lib/abuf.c b/lib/abuf.c
index 440f6a40023..f283f8e6e69 100644
--- a/lib/abuf.c
+++ b/lib/abuf.c
@@ -188,6 +188,12 @@  void abuf_init_const_addr(struct abuf *abuf, ulong addr, size_t size)
 {
 	return abuf_init_const(abuf, map_sysmem(addr, size), size);
 }
+
+void abuf_init_addr(struct abuf *abuf, ulong addr, size_t size)
+{
+	abuf_init(abuf);
+	abuf_map_sysmem(abuf, addr, size);
+}
 #endif
 
 void abuf_init_move(struct abuf *abuf, void *data, size_t size)
diff --git a/test/lib/abuf.c b/test/lib/abuf.c
index e97bb8b66bc..e6032998e94 100644
--- a/test/lib/abuf.c
+++ b/test/lib/abuf.c
@@ -91,6 +91,29 @@  static int lib_test_abuf_init_const_addr(struct unit_test_state *uts)
 }
 LIB_TEST(lib_test_abuf_init_const_addr, 0);
 
+/* Test abuf_init_addr() */
+static int lib_test_abuf_init_addr(struct unit_test_state *uts)
+{
+	struct abuf buf;
+	ulong start;
+	void *ptr;
+
+	start = ut_check_free();
+
+	ptr = map_sysmem(0x100, 0);
+
+	abuf_init_addr(&buf, 0x100, 10);
+	ut_asserteq_ptr(ptr, buf.data);
+	ut_asserteq(10, buf.size);
+	ut_asserteq(false, buf.alloced);
+
+	/* No memory should have been allocated */
+	ut_assertok(ut_check_delta(start));
+
+	return 0;
+}
+LIB_TEST(lib_test_abuf_init_addr, 0);
+
 /* Test abuf_map_sysmem() and abuf_addr() */
 static int lib_test_abuf_map_sysmem(struct unit_test_state *uts)
 {