mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sander Vanheule <sander@svanheule.net>
To: Mark Brown <broonie@kernel.org>
Cc: linux-kernel@vger.kernel.org, Sander Vanheule <sander@svanheule.net>
Subject: [PATCH v2 2/2] regmap: define cleanup helper for regmap_field
Date: Fri, 20 Feb 2026 17:01:12 +0100	[thread overview]
Message-ID: <20260220160112.543391-2-sander@svanheule.net> (raw)
In-Reply-To: <20260220160112.543391-1-sander@svanheule.net>

For temporary field allocation, the user has to perform manual cleanup,
or rely on devm_regmap_field_alloc() to (eventually) clean up the
allocated resources when an error occurs.

Add a cleanup helper that takes care of freeing the allocated
regmap_field whenever it goes out of scope.

This can simplify this example:

    struct regmap_field *field = regmap_field_alloc(...);
    if (IS_ERR(field))
        return PTR_ERR(field);

    int err = regmap_field_read(...);
    if (err)
        goto out;

    /* some logic that may also error */

    err = regmap_field_write(...);

  out:
    regmap_field_free(field);

    return err;

into the shorter:

    struct regmap_field *field __free(regmap_field) = regmap_field_alloc(...);
    if (IS_ERR(field))
        return PTR_ERR(field);

    int err = regmap_field_read(...);
    if (err)
        return err;

    /* some logic that may also error */

    return regmap_field_write(...);

Signed-off-by: Sander Vanheule <sander@svanheule.net>
---
Changes since v1:
- Fix error handling of regmap_field_alloc() in example
- Make example less trivial
---
 include/linux/regmap.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/include/linux/regmap.h b/include/linux/regmap.h
index c8a6a05bdba1..f1c5cb63c171 100644
--- a/include/linux/regmap.h
+++ b/include/linux/regmap.h
@@ -11,6 +11,7 @@
  */
 
 #include <linux/bug.h>
+#include <linux/cleanup.h>
 #include <linux/delay.h>
 #include <linux/err.h>
 #include <linux/fwnode.h>
@@ -1460,6 +1461,8 @@ struct regmap_field *regmap_field_alloc(struct regmap *regmap,
 		struct reg_field reg_field);
 void regmap_field_free(struct regmap_field *field);
 
+DEFINE_FREE(regmap_field, struct regmap_field *, if (_T) regmap_field_free(_T))
+
 struct regmap_field *devm_regmap_field_alloc(struct device *dev,
 		struct regmap *regmap, struct reg_field reg_field);
 void devm_regmap_field_free(struct device *dev,	struct regmap_field *field);
-- 
2.53.0


  reply	other threads:[~2026-02-20 16:01 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-20 16:01 [PATCH v2 1/2] regmap: sort header includes Sander Vanheule
2026-02-20 16:01 ` Sander Vanheule [this message]
2026-02-25 19:07 ` Mark Brown

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=20260220160112.543391-2-sander@svanheule.net \
    --to=sander@svanheule.net \
    --cc=broonie@kernel.org \
    --cc=linux-kernel@vger.kernel.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

Powered by JetHome