summaryrefslogtreecommitdiff
path: root/drivers/core
diff options
context:
space:
mode:
authorMasahiro Yamada <yamada.masahiro@socionext.com>2015-07-25 21:52:36 +0900
committerSimon Glass <sjg@chromium.org>2015-08-06 07:44:29 -0600
commit2b07f6859ad17b74ce490f371f4878add6ae5a11 (patch)
tree506866b0d88d2ff24d87cecf26e8eb3da844e811 /drivers/core
parent608f26c51bebc68db7f2edc7590ee513d2bc5465 (diff)
devres: add devm_kmalloc() and friends (managed memory allocators)
devm_kmalloc() is identical to kmalloc() except that the memory allocated with it is managed and will be automatically released when the device is removed/unbound. Likewise for the other variants. Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com> Acked-by: Simon Glass <sjg@chromium.org>
Diffstat (limited to 'drivers/core')
-rw-r--r--drivers/core/devres.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/drivers/core/devres.c b/drivers/core/devres.c
index 49c270c57c8..f235c1bcfdd 100644
--- a/drivers/core/devres.c
+++ b/drivers/core/devres.c
@@ -194,3 +194,37 @@ void devres_release_all(struct udevice *dev)
{
release_nodes(dev, &dev->devres_head, false);
}
+
+/*
+ * Managed kmalloc/kfree
+ */
+static void devm_kmalloc_release(struct udevice *dev, void *res)
+{
+ /* noop */
+}
+
+static int devm_kmalloc_match(struct udevice *dev, void *res, void *data)
+{
+ return res == data;
+}
+
+void *devm_kmalloc(struct udevice *dev, size_t size, gfp_t gfp)
+{
+ void *data;
+
+ data = _devres_alloc(devm_kmalloc_release, size, gfp);
+ if (unlikely(!data))
+ return NULL;
+
+ devres_add(dev, data);
+
+ return data;
+}
+
+void devm_kfree(struct udevice *dev, void *p)
+{
+ int rc;
+
+ rc = devres_destroy(dev, devm_kmalloc_release, devm_kmalloc_match, p);
+ WARN_ON(rc);
+}