mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Steffen Trumtrar <s.trumtrar@pengutronix.de>
To: barebox@lists.infradead.org, Sascha Hauer <s.hauer@pengutronix.de>
Cc: Steffen Trumtrar <s.trumtrar@pengutronix.de>
Subject: [PATCH 1/4] loadable: add loadable_from_buf()
Date: Tue, 27 Oct 2026 14:23:53 +0100	[thread overview]
Message-ID: <20261027-v2026-10-0-topic-bootconfig-v1-1-2758d3d9a165@pengutronix.de> (raw)
In-Reply-To: <20261027-v2026-10-0-topic-bootconfig-v1-0-2758d3d9a165@pengutronix.de>

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@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




  reply	other threads:[~2026-10-27 13:25 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-27 13:23 [PATCH 0/4] Linux bootconfig support Steffen Trumtrar
2026-10-27 13:23 ` Steffen Trumtrar [this message]
2026-10-27 13:23 ` [PATCH 2/4] bootm: append a Linux bootconfig to the initrd Steffen Trumtrar
2026-10-27 13:23 ` [PATCH 3/4] boot: add bootm.bootconfig override Steffen Trumtrar
2026-10-27 13:23 ` [PATCH 4/4] defaultenv: devboot: add devboot_bootconfig Steffen Trumtrar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261027-v2026-10-0-topic-bootconfig-v1-1-2758d3d9a165@pengutronix.de \
    --to=s.trumtrar@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=s.hauer@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox