]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
regmap: define cleanup helper for regmap_field
authorSander Vanheule <sander@svanheule.net>
Fri, 20 Feb 2026 16:01:12 +0000 (17:01 +0100)
committerMark Brown <broonie@kernel.org>
Tue, 24 Feb 2026 15:53:51 +0000 (15:53 +0000)
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>
Link: https://patch.msgid.link/20260220160112.543391-2-sander@svanheule.net
Signed-off-by: Mark Brown <broonie@kernel.org>
include/linux/regmap.h

index c8a6a05bdba1141361730511740de186bf45d3a4..f1c5cb63c171a7725eea5991dd52bc696f16833a 100644 (file)
@@ -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);