summaryrefslogtreecommitdiff
path: root/lib/string_helpers.c
diff options
context:
space:
mode:
authorLinus Walleij <linus.walleij@linaro.org>2021-11-20 03:44:07 +0100
committerLinus Walleij <linus.walleij@linaro.org>2021-11-20 03:44:07 +0100
commitcc4dac3f5e3eff8d042b4d8712bed6f7ea75454c (patch)
treefe3f2d3a9f5c20f90769748406ea69fc093f652b /lib/string_helpers.c
parentfa55b7dcdc43c1aa1ba12bca9d2dd4318c2a0dbf (diff)
parentf7c151d86487eec720f52843133bce270b07fecc (diff)
Merge tag 'intel-pinctrl-v5.17-2' of gitolite.kernel.org:pub/scm/linux/kernel/git/pinctrl/intel into devel
intel-pinctrl for v5.17-2 * Introduce new generic kasprintf_strarray() API * Clean up and convert existing drivers to use the above The API will be needed in the future for new comers, including Intel ones. The following is an automated git shortlog grouped by driver: armada-37xx: - Switch to use devm_kasprintf_strarray() - Convert to use dev_err_probe() - Make use of the devm_platform_ioremap_resource() - Use temporary variable for struct device - Fix function name in the kernel doc gpio: - mockup: Switch to use kasprintf_strarray() lib/string_helpers: - Introduce managed variant of kasprintf_strarray() - Introduce kasprintf_strarray() pinctrl/rockchip: - Switch to use devm_kasprintf_strarray() - Convert to use dev_err_probe() - Make use of the devm_platform_get_and_ioremap_resource() - Use temporary variable for struct device - Drop wrong kernel doc annotation st: - Switch to use devm_kasprintf_strarray() - Convert to use dev_err_probe() - Make use of the devm_platform_ioremap_resource_byname() - Use temporary variable for struct device - Drop wrong kernel doc annotations zynqmp: - Unify pin naming
Diffstat (limited to 'lib/string_helpers.c')
-rw-r--r--lib/string_helpers.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/lib/string_helpers.c b/lib/string_helpers.c
index d5d008f5b1d9..90f9f1b7afec 100644
--- a/lib/string_helpers.c
+++ b/lib/string_helpers.c
@@ -10,6 +10,7 @@
#include <linux/math64.h>
#include <linux/export.h>
#include <linux/ctype.h>
+#include <linux/device.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/limits.h>
@@ -675,6 +676,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
*
@@ -697,6 +731,36 @@ void kfree_strarray(char **array, size_t n)
}
EXPORT_SYMBOL_GPL(kfree_strarray);
+struct strarray {
+ char **array;
+ size_t n;
+};
+
+static void devm_kfree_strarray(struct device *dev, void *res)
+{
+ struct strarray *array = res;
+
+ kfree_strarray(array->array, array->n);
+}
+
+char **devm_kasprintf_strarray(struct device *dev, const char *prefix, size_t n)
+{
+ struct strarray *ptr;
+
+ ptr = devres_alloc(devm_kfree_strarray, sizeof(*ptr), GFP_KERNEL);
+ if (!ptr)
+ return ERR_PTR(-ENOMEM);
+
+ ptr->array = kasprintf_strarray(GFP_KERNEL, prefix, n);
+ if (!ptr->array) {
+ devres_free(ptr);
+ return ERR_PTR(-ENOMEM);
+ }
+
+ return ptr->array;
+}
+EXPORT_SYMBOL_GPL(devm_kasprintf_strarray);
+
/**
* strscpy_pad() - Copy a C-string into a sized buffer
* @dest: Where to copy the string to