[PATCH 1/4] loadable: add loadable_from_buf()
Steffen Trumtrar
s.trumtrar at pengutronix.de
Tue Oct 27 06:23:53 PDT 2026
loadable_from_mem() leaves the memory to the caller, which then has to
keep it until the loadable is released, wherever that happens. Add a
variant that takes ownership of a malloc()ed buffer and frees it on
release, for data generated at boot time.
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Steffen Trumtrar <s.trumtrar at pengutronix.de>
---
include/loadable.h | 3 +++
lib/loadable-mem.c | 24 ++++++++++++++++++++++++
2 files changed, 27 insertions(+)
diff --git a/include/loadable.h b/include/loadable.h
index 9cc89ea458..6ab1692c5d 100644
--- a/include/loadable.h
+++ b/include/loadable.h
@@ -254,6 +254,9 @@ void loadable_release(struct loadable **l);
__returns_nonnull struct loadable *
loadable_from_mem(const void *mem, size_t size, enum loadable_type type);
+__returns_nonnull struct loadable *
+loadable_from_buf(void *buf, size_t size, enum loadable_type type);
+
__returns_nonnull struct loadable *
loadable_from_file(const char *path, enum loadable_type type);
diff --git a/lib/loadable-mem.c b/lib/loadable-mem.c
index 4e697acc0b..a7f7bca8db 100644
--- a/lib/loadable-mem.c
+++ b/lib/loadable-mem.c
@@ -8,6 +8,7 @@
struct mem_loadable_priv {
const void *start;
size_t size;
+ void *owned;
};
static int mem_loadable_get_info(struct loadable *l, struct loadable_info *info)
@@ -80,6 +81,7 @@ static void mem_loadable_release(struct loadable *l)
{
struct mem_loadable_priv *priv = l->priv;
+ free(priv->owned);
free(priv);
}
@@ -125,3 +127,25 @@ struct loadable *loadable_from_mem(const void *mem, size_t size,
return l;
}
+
+/**
+ * loadable_from_buf - create a loadable owning a buffer
+ * @buf: buffer allocated with malloc()
+ * @size: size of the buffer in bytes
+ * @type: type of loadable (LOADABLE_KERNEL, LOADABLE_INITRD, etc.)
+ *
+ * Like loadable_from_mem(), but the loadable takes ownership of @buf and
+ * frees it in loadable_release().
+ *
+ * Return: pointer to allocated loadable (never fails due to xzalloc)
+ */
+struct loadable *loadable_from_buf(void *buf, size_t size,
+ enum loadable_type type)
+{
+ struct loadable *l = loadable_from_mem(buf, size, type);
+ struct mem_loadable_priv *priv = l->priv;
+
+ priv->owned = buf;
+
+ return l;
+}
--
2.56.0
More information about the barebox
mailing list