summaryrefslogtreecommitdiffstats
path: root/lib/string_helpers.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/string_helpers.c')
-rw-r--r--lib/string_helpers.c33
1 files changed, 33 insertions, 0 deletions
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
@@ -675,6 +675,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
*