diff mbox series

[v1,01/19] lib/string_helpers: Introduce kasprintf_strarray()

Message ID 20211105124242.27288-1-andriy.shevchenko@linux.intel.com (mailing list archive)
State New, archived
Headers show
Series [v1,01/19] lib/string_helpers: Introduce kasprintf_strarray() | expand

Commit Message

Andy Shevchenko Nov. 5, 2021, 12:42 p.m. UTC
We have a few users already that basically want to have array of
sequential strings to be allocated and filled.

Provide a helper for them (basically adjusted version from gpio-mockup.c).

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 include/linux/string_helpers.h |  1 +
 lib/string_helpers.c           | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+)

Comments

Joe Perches Nov. 6, 2021, 7:34 a.m. UTC | #1
On Fri, 2021-11-05 at 14:42 +0200, Andy Shevchenko wrote:
> We have a few users already that basically want to have array of
> sequential strings to be allocated and filled.
> 
> Provide a helper for them (basically adjusted version from gpio-mockup.c).

I think this is overkill and unnecessary bloat for the number of
actual or possible in-tree uses.  The devm_ variant too.

And it'd be useful to have an 0/n patch cover letter describing
why the patchset is useful so I could reply to that instead of the
1/n.

> diff --git a/include/linux/string_helpers.h b/include/linux/string_helpers.h
[]
> @@ -100,6 +100,7 @@ char *kstrdup_quotable(const char *src, gfp_t gfp);
>  char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp);
>  char *kstrdup_quotable_file(struct file *file, gfp_t gfp);
>  
> +char **kasprintf_strarray(gfp_t gfp, const char *prefix, size_t n);
>  void kfree_strarray(char **array, size_t n);
>  
>  #endif
> diff --git a/lib/string_helpers.c b/lib/string_helpers.c
[]
> @@ -674,6 +674,39 @@ char *kstrdup_quotable_file(struct file *file, gfp_t gfp)
>  }
>  EXPORT_SYMBOL_GPL(kstrdup_quotable_file);
>  
> +/**
> + * kasprintf_strarray - allocate and fill array of sequential strings
> + * @gfp: flags for the slab allocator
> + * @prefix: prefix to be used
> + * @n: amount of lines to be allocated and filled
> + *
> + * Allocates and fills @n strings using pattern "%s-%zu", where prefix
> + * is provided by caller. The caller is responsible to free them with
> + * kfree_strarray() after use.
> + *
> + * Returns array of strings or NULL when memory can't be allocated.
> + */
> +char **kasprintf_strarray(gfp_t gfp, const char *prefix, size_t n)
> +{
> +	char **names;
> +	size_t i;
> +
> +	names = kcalloc(n + 1, sizeof(char *), gfp);
> +	if (!names)
> +		return NULL;
> +
> +	for (i = 0; i < n; i++) {
> +		names[i] = kasprintf(gfp, "%s-%zu", prefix, i);
> +		if (!names[i]) {
> +			kfree_strarray(names, i);
> +			return NULL;
> +		}
> +	}
> +
> +	return names;
> +}
> +EXPORT_SYMBOL_GPL(kasprintf_strarray);
> +
>  /**
>   * kfree_strarray - free a number of dynamically allocated strings contained
>   *                  in an array and the array itself
Linus Walleij Nov. 9, 2021, 11:42 a.m. UTC | #2
On Fri, Nov 5, 2021 at 1:43 PM Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:

> We have a few users already that basically want to have array of
> sequential strings to be allocated and filled.
>
> Provide a helper for them (basically adjusted version from gpio-mockup.c).
>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

Fulfils Rusty Russell's API design hierarchy requirements
and help people to make less mistakes so:
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>

Yours,
Linus Walleij
Andy Shevchenko Nov. 15, 2021, 11:23 a.m. UTC | #3
On Tue, Nov 09, 2021 at 12:42:00PM +0100, Linus Walleij wrote:
> On Fri, Nov 5, 2021 at 1:43 PM Andy Shevchenko
> <andriy.shevchenko@linux.intel.com> wrote:
> 
> > We have a few users already that basically want to have array of
> > sequential strings to be allocated and filled.
> >
> > Provide a helper for them (basically adjusted version from gpio-mockup.c).
> >
> > Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> 
> Fulfils Rusty Russell's API design hierarchy requirements
> and help people to make less mistakes so:
> Reviewed-by: Linus Walleij <linus.walleij@linaro.org>

Thanks!

Should I resend, take into mine PR, or you just apply it as is?

As I answered previously the series doesn't require additional work
from my perspective.
Andy Shevchenko Nov. 18, 2021, 4:56 p.m. UTC | #4
On Mon, Nov 15, 2021 at 01:23:02PM +0200, Andy Shevchenko wrote:
> On Tue, Nov 09, 2021 at 12:42:00PM +0100, Linus Walleij wrote:
> > On Fri, Nov 5, 2021 at 1:43 PM Andy Shevchenko
> > <andriy.shevchenko@linux.intel.com> wrote:
> > 
> > > We have a few users already that basically want to have array of
> > > sequential strings to be allocated and filled.
> > >
> > > Provide a helper for them (basically adjusted version from gpio-mockup.c).
> > >
> > > Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> > 
> > Fulfils Rusty Russell's API design hierarchy requirements
> > and help people to make less mistakes so:
> > Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
> 
> Thanks!
> 
> Should I resend, take into mine PR, or you just apply it as is?
> 
> As I answered previously the series doesn't require additional work
> from my perspective.

Okay, I'm about to send a PR to you and Bart for this.
Does it sound good?
Bartosz Golaszewski Nov. 18, 2021, 8:30 p.m. UTC | #5
On Thu, Nov 18, 2021 at 5:56 PM Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:
>
> On Mon, Nov 15, 2021 at 01:23:02PM +0200, Andy Shevchenko wrote:
> > On Tue, Nov 09, 2021 at 12:42:00PM +0100, Linus Walleij wrote:
> > > On Fri, Nov 5, 2021 at 1:43 PM Andy Shevchenko
> > > <andriy.shevchenko@linux.intel.com> wrote:
> > >
> > > > We have a few users already that basically want to have array of
> > > > sequential strings to be allocated and filled.
> > > >
> > > > Provide a helper for them (basically adjusted version from gpio-mockup.c).
> > > >
> > > > Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> > >
> > > Fulfils Rusty Russell's API design hierarchy requirements
> > > and help people to make less mistakes so:
> > > Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
> >
> > Thanks!
> >
> > Should I resend, take into mine PR, or you just apply it as is?
> >
> > As I answered previously the series doesn't require additional work
> > from my perspective.
>
> Okay, I'm about to send a PR to you and Bart for this.
> Does it sound good?
>

Sounds good to me.

Bart
diff mbox series

Patch

diff --git a/include/linux/string_helpers.h b/include/linux/string_helpers.h
index 4ba39e1403b2..f67a94013c87 100644
--- a/include/linux/string_helpers.h
+++ b/include/linux/string_helpers.h
@@ -100,6 +100,7 @@  char *kstrdup_quotable(const char *src, gfp_t gfp);
 char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp);
 char *kstrdup_quotable_file(struct file *file, gfp_t gfp);
 
+char **kasprintf_strarray(gfp_t gfp, const char *prefix, size_t n);
 void kfree_strarray(char **array, size_t n);
 
 #endif
diff --git a/lib/string_helpers.c b/lib/string_helpers.c
index d5d008f5b1d9..9758997c465e 100644
--- a/lib/string_helpers.c
+++ b/lib/string_helpers.c
@@ -674,6 +674,39 @@  char *kstrdup_quotable_file(struct file *file, gfp_t gfp)
 }
 EXPORT_SYMBOL_GPL(kstrdup_quotable_file);
 
+/**
+ * kasprintf_strarray - allocate and fill array of sequential strings
+ * @gfp: flags for the slab allocator
+ * @prefix: prefix to be used
+ * @n: amount of lines to be allocated and filled
+ *
+ * Allocates and fills @n strings using pattern "%s-%zu", where prefix
+ * is provided by caller. The caller is responsible to free them with
+ * kfree_strarray() after use.
+ *
+ * Returns array of strings or NULL when memory can't be allocated.
+ */
+char **kasprintf_strarray(gfp_t gfp, const char *prefix, size_t n)
+{
+	char **names;
+	size_t i;
+
+	names = kcalloc(n + 1, sizeof(char *), gfp);
+	if (!names)
+		return NULL;
+
+	for (i = 0; i < n; i++) {
+		names[i] = kasprintf(gfp, "%s-%zu", prefix, i);
+		if (!names[i]) {
+			kfree_strarray(names, i);
+			return NULL;
+		}
+	}
+
+	return names;
+}
+EXPORT_SYMBOL_GPL(kasprintf_strarray);
+
 /**
  * kfree_strarray - free a number of dynamically allocated strings contained
  *                  in an array and the array itself