[PATCH 2/9] lib: string_helpers: implement a CLASS() for string arrays

From: Bartosz Golaszewski

Date: Wed Nov 04 2026 - 09:10:06 EST


Provide a CLASS() for managed arrays of strings. It can be used for
automated release of temporary string arrays when they go out of scope.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxxxxxxxx>
---
include/linux/string_helpers.h | 12 ++++++++++++
1 file changed, 12 insertions(+)

diff --git a/include/linux/string_helpers.h b/include/linux/string_helpers.h
index 3fb88a1e989833ad5ea1a46858efa16e3971bf08..3122be1359a36272190264d1c3e4c84dce785e6e 100644
--- a/include/linux/string_helpers.h
+++ b/include/linux/string_helpers.h
@@ -3,6 +3,7 @@
#define _LINUX_STRING_HELPERS_H_

#include <linux/bits.h>
+#include <linux/cleanup.h>
#include <linux/ctype.h>
#include <linux/string_choices.h>
#include <linux/string.h>
@@ -119,6 +120,17 @@ char *kstrdup_and_replace(const char *src, char old, char new, gfp_t gfp);
char **kasprintf_strarray(gfp_t gfp, const char *prefix, size_t n);
void kfree_strarray(char **array, size_t n);

+struct kstrarray {
+ char **array;
+ size_t size;
+};
+
+DEFINE_CLASS(strarray,
+ struct kstrarray,
+ kfree_strarray(_T.array, _T.size),
+ ((struct kstrarray){ .array = array, .size = size }),
+ char **array, size_t size);
+
char **devm_kasprintf_strarray(struct device *dev, const char *prefix, size_t n);

#endif

--
2.47.3