diff options
author | Himangi Saraogi <himangi774@gmail.com> | 2014-07-16 16:57:52 -0400 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2014-07-17 21:26:44 -0400 |
commit | 75f2a4ead5d5890ada9c2663a70fb58613c0d9f2 (patch) | |
tree | a1eecedee8e357a88fb7a8ebdcf179946dbd5bc2 /drivers/base | |
parent | 871379dc0fd9748892bdf10536f2664751a7819c (diff) |
devres: Add devm_kasprintf and devm_kvasprintf API
devm_kasprintf() and devm_kvasprintf() are the managed counterparts
for kasprintf() and kvasprintf().
Signed-off-by: Himangi Saraogi <himangi774@gmail.com>
Acked-by: Julia Lawall <julia.lawall@lip6.fr>
Acked-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers/base')
-rw-r--r-- | drivers/base/devres.c | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/drivers/base/devres.c b/drivers/base/devres.c index 52302946770f..69d9b0c89a01 100644 --- a/drivers/base/devres.c +++ b/drivers/base/devres.c | |||
@@ -817,6 +817,61 @@ char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) | |||
817 | EXPORT_SYMBOL_GPL(devm_kstrdup); | 817 | EXPORT_SYMBOL_GPL(devm_kstrdup); |
818 | 818 | ||
819 | /** | 819 | /** |
820 | * devm_kvasprintf - Allocate resource managed space | ||
821 | * for the formatted string. | ||
822 | * @dev: Device to allocate memory for | ||
823 | * @gfp: the GFP mask used in the devm_kmalloc() call when | ||
824 | * allocating memory | ||
825 | * @fmt: the formatted string to duplicate | ||
826 | * @ap: the list of tokens to be placed in the formatted string | ||
827 | * RETURNS: | ||
828 | * Pointer to allocated string on success, NULL on failure. | ||
829 | */ | ||
830 | char *devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt, | ||
831 | va_list ap) | ||
832 | { | ||
833 | unsigned int len; | ||
834 | char *p; | ||
835 | va_list aq; | ||
836 | |||
837 | va_copy(aq, ap); | ||
838 | len = vsnprintf(NULL, 0, fmt, aq); | ||
839 | va_end(aq); | ||
840 | |||
841 | p = devm_kmalloc(dev, len+1, gfp); | ||
842 | if (!p) | ||
843 | return NULL; | ||
844 | |||
845 | vsnprintf(p, len+1, fmt, ap); | ||
846 | |||
847 | return p; | ||
848 | } | ||
849 | EXPORT_SYMBOL(devm_kvasprintf); | ||
850 | |||
851 | /** | ||
852 | * devm_kasprintf - Allocate resource managed space | ||
853 | * and copy an existing formatted string into that | ||
854 | * @dev: Device to allocate memory for | ||
855 | * @gfp: the GFP mask used in the devm_kmalloc() call when | ||
856 | * allocating memory | ||
857 | * @fmt: the string to duplicate | ||
858 | * RETURNS: | ||
859 | * Pointer to allocated string on success, NULL on failure. | ||
860 | */ | ||
861 | char *devm_kasprintf(struct device *dev, gfp_t gfp, const char *fmt, ...) | ||
862 | { | ||
863 | va_list ap; | ||
864 | char *p; | ||
865 | |||
866 | va_start(ap, fmt); | ||
867 | p = devm_kvasprintf(dev, gfp, fmt, ap); | ||
868 | va_end(ap); | ||
869 | |||
870 | return p; | ||
871 | } | ||
872 | EXPORT_SYMBOL_GPL(devm_kasprintf); | ||
873 | |||
874 | /** | ||
820 | * devm_kfree - Resource-managed kfree | 875 | * devm_kfree - Resource-managed kfree |
821 | * @dev: Device this memory belongs to | 876 | * @dev: Device this memory belongs to |
822 | * @p: Memory to free | 877 | * @p: Memory to free |