Skip to content

Commit

Permalink
devres: Add devm_kasprintf and devm_kvasprintf API
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Himangi Saraogi authored and Greg Kroah-Hartman committed Jul 18, 2014
1 parent 871379d commit 75f2a4e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
55 changes: 55 additions & 0 deletions drivers/base/devres.c
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,61 @@ char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp)
}
EXPORT_SYMBOL_GPL(devm_kstrdup);

/**
* devm_kvasprintf - Allocate resource managed space
* for the formatted string.
* @dev: Device to allocate memory for
* @gfp: the GFP mask used in the devm_kmalloc() call when
* allocating memory
* @fmt: the formatted string to duplicate
* @ap: the list of tokens to be placed in the formatted string
* RETURNS:
* Pointer to allocated string on success, NULL on failure.
*/
char *devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt,
va_list ap)
{
unsigned int len;
char *p;
va_list aq;

va_copy(aq, ap);
len = vsnprintf(NULL, 0, fmt, aq);
va_end(aq);

p = devm_kmalloc(dev, len+1, gfp);
if (!p)
return NULL;

vsnprintf(p, len+1, fmt, ap);

return p;
}
EXPORT_SYMBOL(devm_kvasprintf);

/**
* devm_kasprintf - Allocate resource managed space
* and copy an existing formatted string into that
* @dev: Device to allocate memory for
* @gfp: the GFP mask used in the devm_kmalloc() call when
* allocating memory
* @fmt: the string to duplicate
* RETURNS:
* Pointer to allocated string on success, NULL on failure.
*/
char *devm_kasprintf(struct device *dev, gfp_t gfp, const char *fmt, ...)
{
va_list ap;
char *p;

va_start(ap, fmt);
p = devm_kvasprintf(dev, gfp, fmt, ap);
va_end(ap);

return p;
}
EXPORT_SYMBOL_GPL(devm_kasprintf);

/**
* devm_kfree - Resource-managed kfree
* @dev: Device this memory belongs to
Expand Down
4 changes: 4 additions & 0 deletions include/linux/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,10 @@ extern int devres_release_group(struct device *dev, void *id);

/* managed devm_k.alloc/kfree for device drivers */
extern void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp);
extern char *devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt,
va_list ap);
extern char *devm_kasprintf(struct device *dev, gfp_t gfp,
const char *fmt, ...);
static inline void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)
{
return devm_kmalloc(dev, size, gfp | __GFP_ZERO);
Expand Down

0 comments on commit 75f2a4e

Please sign in to comment.