lkml.org 
[lkml]   [2019]   [Dec]   [14]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
From
Subject[PATCH] drivers: devres: add devres_add_auto_ptr()
Date
Adding a helper for allocating and adding a devres in one shot,
in order to minimize boilerplate. In case of failure, it frees
the resource and returns error.

Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
---
drivers/base/devres.c | 29 +++++++++++++++++++++++++++++
include/linux/device.h | 2 ++
2 files changed, 31 insertions(+)

diff --git a/drivers/base/devres.c b/drivers/base/devres.c
index 0bbb328bd17f..6631d3748ebe 100644
--- a/drivers/base/devres.c
+++ b/drivers/base/devres.c
@@ -237,6 +237,35 @@ void devres_add(struct device *dev, void *res)
}
EXPORT_SYMBOL_GPL(devres_add);

+/**
+ * devres_add_auto_ptr - Reigster pointer device resource
+ * @dev: Device to add resource to
+ * @release: release callback function
+ * @ptr: pointer to register
+ *
+ * Allocate a devres entry and register pointer @ptr to @dev. On driver
+ * detach, the associated release function will be invokied and devres
+ * will be freed automatically.
+ *
+ * The devres will be allocated w/ GFP_KERNEL
+ *
+ * In case of failure, the resource will be released automatically !
+ */
+int devres_add_auto_ptr(struct device *dev, dr_release_t release, void *ptr)
+{
+ void **dr;
+ dr = devres_alloc(release, sizeof(ptr), GFP_KERNEL);
+ if (!dr) {
+ pr_err("devm: out of memory!\n");
+ release(dev, ptr);
+ return -ENOMEM;
+ }
+
+ devres_add(dev, dr);
+ return 0;
+}
+EXPORT_SYMBOL_GPL(devres_add_auto_ptr);
+
static struct devres *find_dr(struct device *dev, dr_release_t release,
dr_match_t match, void *match_data)
{
diff --git a/include/linux/device.h b/include/linux/device.h
index e226030c1df3..b8d8eef89fdf 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -905,6 +905,8 @@ extern void devres_for_each_res(struct device *dev, dr_release_t release,
void *data);
extern void devres_free(void *res);
extern void devres_add(struct device *dev, void *res);
+extern int devres_add_auto_ptr(struct device *dev, dr_release_t release,
+ void *ptr);
extern void *devres_find(struct device *dev, dr_release_t release,
dr_match_t match, void *match_data);
extern void *devres_get(struct device *dev, void *new_res,
--
2.11.0
\
 
 \ /
  Last update: 2019-12-14 15:42    [W:0.022 / U:1.000 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site