linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1 01/19] lib/string_helpers: Introduce kasprintf_strarray()
@ 2021-11-05 12:42 Andy Shevchenko
  2021-11-05 12:42 ` [PATCH v1 02/19] lib/string_helpers: Introduce managed variant of kasprintf_strarray() Andy Shevchenko
                   ` (19 more replies)
  0 siblings, 20 replies; 48+ messages in thread
From: Andy Shevchenko @ 2021-11-05 12:42 UTC (permalink / raw)
  To: Andy Shevchenko, Bartosz Golaszewski, Jianqun Xu, Linus Walleij,
	Sai Krishna Potthuri, Andrew Morton, linux-gpio, linux-kernel,
	linux-arm-kernel, linux-rockchip
  Cc: Bamvor Jian Zhang, Andrew Lunn, Gregory Clement,
	Sebastian Hesselbarth, Heiko Stuebner, Patrice Chotard,
	Michal Simek, Andy Shevchenko

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(+)

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
-- 
2.33.0


^ permalink raw reply related	[flat|nested] 48+ messages in thread

end of thread, other threads:[~2021-11-18 20:30 UTC | newest]

Thread overview: 48+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-05 12:42 [PATCH v1 01/19] lib/string_helpers: Introduce kasprintf_strarray() Andy Shevchenko
2021-11-05 12:42 ` [PATCH v1 02/19] lib/string_helpers: Introduce managed variant of kasprintf_strarray() Andy Shevchenko
2021-11-05 12:42 ` [PATCH v1 03/19] pinctrl/rockchip: Drop wrong kernel doc annotation Andy Shevchenko
2021-11-06 16:32   ` Heiko Stübner
2021-11-05 12:42 ` [PATCH v1 04/19] pinctrl/rockchip: Use temporary variable for struct device Andy Shevchenko
2021-11-08 12:46   ` Heiko Stübner
2021-11-05 12:42 ` [PATCH v1 05/19] pinctrl/rockchip: Make use of the devm_platform_get_and_ioremap_resource() Andy Shevchenko
2021-11-08 12:48   ` Heiko Stübner
2021-11-05 12:42 ` [PATCH v1 06/19] pinctrl/rockchip: Convert to use dev_err_probe() Andy Shevchenko
2021-11-08 12:49   ` Heiko Stübner
2021-11-05 12:42 ` [PATCH v1 07/19] pinctrl/rockchip: Switch to use devm_kasprintf_strarray() Andy Shevchenko
2021-11-08 12:51   ` Heiko Stübner
2021-11-05 12:42 ` [PATCH v1 08/19] pinctrl: armada-37xx: Fix function name in the kernel doc Andy Shevchenko
2021-11-08 10:37   ` Gregory CLEMENT
2021-11-05 12:42 ` [PATCH v1 09/19] pinctrl: armada-37xx: Use temporary variable for struct device Andy Shevchenko
2021-11-08 10:49   ` Gregory CLEMENT
2021-11-05 12:42 ` [PATCH v1 10/19] pinctrl: armada-37xx: Make use of the devm_platform_ioremap_resource() Andy Shevchenko
2021-11-08 10:56   ` Gregory CLEMENT
2021-11-05 12:42 ` [PATCH v1 11/19] pinctrl: armada-37xx: Convert to use dev_err_probe() Andy Shevchenko
2021-11-08 10:59   ` Gregory CLEMENT
2021-11-05 12:42 ` [PATCH v1 12/19] pinctrl: armada-37xx: Switch to use devm_kasprintf_strarray() Andy Shevchenko
2021-11-08 11:09   ` Gregory CLEMENT
2021-11-05 12:42 ` [PATCH v1 13/19] pinctrl: st: Drop wrong kernel doc annotations Andy Shevchenko
2021-11-09 11:32   ` Linus Walleij
2021-11-09 12:04     ` Andy Shevchenko
2021-11-05 12:42 ` [PATCH v1 14/19] pinctrl: st: Use temporary variable for struct device Andy Shevchenko
2021-11-06  7:50   ` Joe Perches
     [not found]     ` <CAHp75Ve0Bv9VsWFFZxL9wYk=Z_Mm7nat-vf7g8HHTiROi7EY=Q@mail.gmail.com>
2021-11-06  8:28       ` Joe Perches
2021-11-08  9:26         ` Andy Shevchenko
2021-11-09 11:55           ` Linus Walleij
2021-11-09 12:07             ` Andy Shevchenko
2021-11-09 12:09             ` Andy Shevchenko
2021-11-05 12:42 ` [PATCH v1 15/19] pinctrl: st: Make use of the devm_platform_ioremap_resource_byname() Andy Shevchenko
2021-11-09 11:33   ` Linus Walleij
2021-11-05 12:42 ` [PATCH v1 16/19] pinctrl: st: Convert to use dev_err_probe() Andy Shevchenko
2021-11-09 11:34   ` Linus Walleij
2021-11-05 12:42 ` [PATCH v1 17/19] pinctrl: st: Switch to use devm_kasprintf_strarray() Andy Shevchenko
2021-11-09 11:34   ` Linus Walleij
2021-11-05 12:42 ` [PATCH v1 18/19] pinctrl: zynqmp: Unify pin naming Andy Shevchenko
2021-11-09 11:34   ` Linus Walleij
2021-11-05 12:42 ` [PATCH v1 19/19] gpio: mockup: Switch to use kasprintf_strarray() Andy Shevchenko
2021-11-09 11:35   ` Linus Walleij
2021-11-16  9:12   ` Bartosz Golaszewski
2021-11-06  7:34 ` [PATCH v1 01/19] lib/string_helpers: Introduce kasprintf_strarray() Joe Perches
2021-11-09 11:42 ` Linus Walleij
2021-11-15 11:23   ` Andy Shevchenko
2021-11-18 16:56     ` Andy Shevchenko
2021-11-18 20:30       ` Bartosz Golaszewski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).