summaryrefslogtreecommitdiffstats
path: root/kernel/lib/kasprintf.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/lib/kasprintf.c')
-rw-r--r--kernel/lib/kasprintf.c16
1 files changed, 16 insertions, 0 deletions
diff --git a/kernel/lib/kasprintf.c b/kernel/lib/kasprintf.c
index 32f12150f..f194e6e59 100644
--- a/kernel/lib/kasprintf.c
+++ b/kernel/lib/kasprintf.c
@@ -31,6 +31,22 @@ char *kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
}
EXPORT_SYMBOL(kvasprintf);
+/*
+ * If fmt contains no % (or is exactly %s), use kstrdup_const. If fmt
+ * (or the sole vararg) points to rodata, we will then save a memory
+ * allocation and string copy. In any case, the return value should be
+ * freed using kfree_const().
+ */
+const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list ap)
+{
+ if (!strchr(fmt, '%'))
+ return kstrdup_const(fmt, gfp);
+ if (!strcmp(fmt, "%s"))
+ return kstrdup_const(va_arg(ap, const char*), gfp);
+ return kvasprintf(gfp, fmt, ap);
+}
+EXPORT_SYMBOL(kvasprintf_const);
+
char *kasprintf(gfp_t gfp, const char *fmt, ...)
{
va_list ap;