mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@barebox.org>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@barebox.org>
Subject: [PATCH 2/7] stddef: implement scoped_var for use in iterators
Date: Mon, 13 Apr 2026 12:09:37 +0200	[thread overview]
Message-ID: <20260413101118.1462119-2-a.fatoum@barebox.org> (raw)
In-Reply-To: <20260413101118.1462119-1-a.fatoum@barebox.org>

Our various iterator macros depend on a variable being declared
beforehand, which can be error prone as the value after the last
iteration completed may not be stable or even invoke undefined behavior
on access.

To allow restricting lifetime to only the iteration, implement
scoped_var, a macro for scoping a variable definitions to the statement
following it. The implementation is inspired by Linux __scoped_class.

Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
 include/linux/stddef.h | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/include/linux/stddef.h b/include/linux/stddef.h
index 88ff6f1733d2..e5e0421fe558 100644
--- a/include/linux/stddef.h
+++ b/include/linux/stddef.h
@@ -139,4 +139,33 @@ typedef unsigned short wchar_t;
 #define DECLARE_FLEX_ARRAY(TYPE, NAME) \
 	__DECLARE_FLEX_ARRAY(TYPE, NAME)
 
+#define __scoped_var(decl, _label)	 	\
+	for (decl; ; ({ goto _label; }))	\
+		if (0) {			\
+_label:						\
+			break;			\
+		} else
+
+/**
+ * scoped_var - declare a variable scoped to the following statement
+ * @type:       the full type (e.g. ``struct foo *`` or ``int``)
+ * @name:       the variable name to declare
+ *
+ * Declares @name as @type, initialized to 0, whose scope is limited
+ * to the immediately following statement. Works with both pointer and
+ * scalar types. Intended for use with existing loop macros to add scoped
+ * iterator variables without duplicating the loop body.
+ *
+ * Example with a list iterator:
+ *
+ * .. code-block:: c
+ *
+ *   scoped_var(struct foo *p)
+ *       list_for_each_entry(p, &head, member) {
+ *           // use p
+ *       }
+ *   // p is out of scope here
+ */
+#define scoped_var(decl) __scoped_var(decl, __UNIQUE_ID(label))
+
 #endif
-- 
2.47.3




  reply	other threads:[~2026-04-13 10:11 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-13 10:09 [PATCH 1/7] console: return characters written from console_putc Ahmad Fatoum
2026-04-13 10:09 ` Ahmad Fatoum [this message]
2026-04-13 10:09 ` [PATCH 3/7] console: have for_each_console declare the iterator internally Ahmad Fatoum
2026-04-13 10:09 ` [PATCH 4/7] console: make console_puts and friends accept a console_device Ahmad Fatoum
2026-04-13 10:09 ` [PATCH 5/7] console: implement console_putc in terms of console_putbin Ahmad Fatoum
2026-04-13 10:09 ` [PATCH 6/7] console: implement console_printf Ahmad Fatoum
2026-04-13 10:09 ` [PATCH 7/7] commands: dmesg: give log_print a console_device parameter Ahmad Fatoum
2026-04-13 12:28 ` [PATCH 1/7] console: return characters written from console_putc Sascha Hauer

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=20260413101118.1462119-2-a.fatoum@barebox.org \
    --to=a.fatoum@barebox.org \
    --cc=barebox@lists.infradead.org \
    /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