diff options
author | Manish Badarkhe <badarkhe.manish@gmail.com> | 2014-01-29 09:57:27 -0500 |
---|---|---|
committer | Mark Brown <broonie@linaro.org> | 2014-02-11 11:34:32 -0500 |
commit | e31108cad3deabb1a63111d7aa699ca67753c01f (patch) | |
tree | 064b0ddbd24537c667b4d308286d980770f83ac5 | |
parent | 38dbfb59d1175ef458d006556061adeaa8751b72 (diff) |
devres: introduce API "devm_kstrdup"
This patch introduces "devm_kstrdup" API so that the
device's driver can allocate memory and copy string.
Signed-off-by: Manish Badarkhe <badarkhe.manish@gmail.com>
Signed-off-by: Mark Brown <broonie@linaro.org>
-rw-r--r-- | drivers/base/devres.c | 26 | ||||
-rw-r--r-- | include/linux/device.h | 1 |
2 files changed, 27 insertions, 0 deletions
diff --git a/drivers/base/devres.c b/drivers/base/devres.c index 545c4de412c3..db4e264eecb6 100644 --- a/drivers/base/devres.c +++ b/drivers/base/devres.c | |||
@@ -791,6 +791,32 @@ void * devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) | |||
791 | EXPORT_SYMBOL_GPL(devm_kmalloc); | 791 | EXPORT_SYMBOL_GPL(devm_kmalloc); |
792 | 792 | ||
793 | /** | 793 | /** |
794 | * devm_kstrdup - Allocate resource managed space and | ||
795 | * copy an existing string into that. | ||
796 | * @dev: Device to allocate memory for | ||
797 | * @s: the string to duplicate | ||
798 | * @gfp: the GFP mask used in the devm_kmalloc() call when | ||
799 | * allocating memory | ||
800 | * RETURNS: | ||
801 | * Pointer to allocated string on success, NULL on failure. | ||
802 | */ | ||
803 | char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) | ||
804 | { | ||
805 | size_t size; | ||
806 | char *buf; | ||
807 | |||
808 | if (!s) | ||
809 | return NULL; | ||
810 | |||
811 | size = strlen(s) + 1; | ||
812 | buf = devm_kmalloc(dev, size, gfp); | ||
813 | if (buf) | ||
814 | memcpy(buf, s, size); | ||
815 | return buf; | ||
816 | } | ||
817 | EXPORT_SYMBOL_GPL(devm_kstrdup); | ||
818 | |||
819 | /** | ||
794 | * devm_kfree - Resource-managed kfree | 820 | * devm_kfree - Resource-managed kfree |
795 | * @dev: Device this memory belongs to | 821 | * @dev: Device this memory belongs to |
796 | * @p: Memory to free | 822 | * @p: Memory to free |
diff --git a/include/linux/device.h b/include/linux/device.h index 952b01033c32..ec1b6e21f0ef 100644 --- a/include/linux/device.h +++ b/include/linux/device.h | |||
@@ -626,6 +626,7 @@ static inline void *devm_kcalloc(struct device *dev, | |||
626 | return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO); | 626 | return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO); |
627 | } | 627 | } |
628 | extern void devm_kfree(struct device *dev, void *p); | 628 | extern void devm_kfree(struct device *dev, void *p); |
629 | extern char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp); | ||
629 | 630 | ||
630 | void __iomem *devm_ioremap_resource(struct device *dev, struct resource *res); | 631 | void __iomem *devm_ioremap_resource(struct device *dev, struct resource *res); |
631 | void __iomem *devm_request_and_ioremap(struct device *dev, | 632 | void __iomem *devm_request_and_ioremap(struct device *dev, |