Skip to contentSkip to conversation

Implement kasprintf

PATCH In review Jeremy Fitzhardinge 5 participants 9 messages Jun 20, 2006 – Jun 21, 2006

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Implement kasprintf

From: Jeremy Fitzhardinge <[email protected]>

Implement kasprintf, a kernel version of asprintf. This allocates the memory required for the formatted string, including the trailing '\0'. Returns NULL on allocation failure.

Requires vsnprintf to accept a NULL buffer when the buffer size is 0.

---

include/linux/kernel.h |    2 +
lib/Makefile           |    2 -
lib/kasprintf.c        |   54 ++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 57 insertions(+), 1 deletion(-)
include/linux/kernel.h +0 -0
diff -r c175fd50e604 include/linux/kernel.h
@@ -114,6 +114,8 @@ extern int scnprintf(char * buf, size_t 
	__attribute__ ((format (printf, 3, 4)));
extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
	__attribute__ ((format (printf, 3, 0)));

+extern char *kasprintf(gfp_t gfp, const char *fmt, ...)
+ __attribute__ ((format (printf, 2, 3)));

extern int sscanf(const char *, const char *, ...)

	__attribute__ ((format (scanf, 2, 3)));
lib/Makefile +0 -0
diff -r c175fd50e604 lib/Makefile
@@ -5,7 +5,7 @@ lib-y := errno.o ctype.o string.o vsprin
lib-y := errno.o ctype.o string.o vsprintf.o cmdline.o \
	 bust_spinlocks.o rbtree.o radix-tree.o dump_stack.o \
	 idr.o div64.o int_sqrt.o bitmap.o extable.o prio_tree.o \

- sha1.o
+ sha1.o kasprintf.o

lib-$(CONFIG_SMP) += cpumask.o

lib/kasprintf.c new file +50 -0

50 lines not on this page · raw message

#1 lore raw
Quoted 8 lines · On Tue, 20 Jun 2006 16:57:16 -0700 Jeremy Fitzhardinge wrote:

From: Jeremy Fitzhardinge <[email protected]>

Implement kasprintf, a kernel version of asprintf. This allocates the memory required for the formatted string, including the trailing '\0'. Returns NULL on allocation failure.

Requires vsnprintf to accept a NULL buffer when the buffer size is 0.

Quoted 7 lines
---
 include/linux/kernel.h |    2 +
 lib/Makefile           |    2 -
 lib/kasprintf.c        |   54 ++++++++++++++++++++++++++++++++++++++++++++++++

Hi,
<nit>

Why do we want a separate source file for this one function?

Quoted 75 lines
 3 files changed, 57 insertions(+), 1 deletion(-)

diff -r c175fd50e604 include/linux/kernel.h

include/linux/kernel.h +2 -0
@@ -114,6 +114,8 @@ extern int scnprintf(char * buf, size_t 
 	__attribute__ ((format (printf, 3, 4)));
 extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
 	__attribute__ ((format (printf, 3, 0)));
+extern char *kasprintf(gfp_t gfp, const char *fmt, ...)
+	__attribute__ ((format (printf, 2, 3)));
 
 extern int sscanf(const char *, const char *, ...)
 	__attribute__ ((format (scanf, 2, 3)));

diff -r c175fd50e604 lib/Makefile

lib/Makefile +1 -1
@@ -5,7 +5,7 @@ lib-y := errno.o ctype.o string.o vsprin
 lib-y := errno.o ctype.o string.o vsprintf.o cmdline.o \
 	 bust_spinlocks.o rbtree.o radix-tree.o dump_stack.o \
 	 idr.o div64.o int_sqrt.o bitmap.o extable.o prio_tree.o \
-	 sha1.o
+	 sha1.o kasprintf.o
 
 lib-$(CONFIG_SMP) += cpumask.o
 

diff -r c175fd50e604 lib/kasprintf.c

lib/kasprintf.c new file +44 -0
@@ -0,0 +1,54 @@
+/******************************************************************************
+ * Simplified asprintf.
+ *
+ * Copyright (C) 2006 XenSource Ltd
+ * 
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation; or, when distributed
+ * separately from the Linux kernel or incorporated into other
+ * software packages, subject to the following license:
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this source file (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy, modify,
+ * merge, publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+
+/* Simplified asprintf. */
+char *kasprintf(gfp_t gfp, const char *fmt, ...)
+{
+	va_list ap;
+	unsigned int len;
+	char *p;
+
+	va_start(ap, fmt);
+	len = vsnprintf(NULL, 0, fmt, ap);
+	va_end(ap);

lib/kasprintf.c line 54

Quoted 10 lines
lib/kasprintf.c new file +10 -0
+
+	p = kmalloc(len+1, gfp);
+	if (!p)
+		return NULL;
+	va_start(ap, fmt);
+	vsnprintf(p, len+1, fmt, ap);
+	va_end(ap);
+	return p;
+}
+EXPORT_SYMBOL(kasprintf);

---
~Randy

#2 lore raw
Quoted 2 lines · Randy.Dunlap

Why do we want a separate source file for this one function?

Because if it shared a file with something else, someone would complain about it bloating code which doesn't use it... At the moment there are no in-tree users (though I'm sure there's something out there with an open-coded version of this), but we'll be needing it for Xen.

I'm happy to fold it into vsprintf.c though.

J

#3 lore raw
Quoted 7 lines · On Tue, 20 Jun 2006 17:14:55 -0700 Jeremy Fitzhardinge wrote:
Quoted 2 lines · Randy.Dunlap

Why do we want a separate source file for this one function?

Because if it shared a file with something else, someone would complain about it bloating code which doesn't use it... At the moment there are no in-tree users (though I'm sure there's something out there with an open-coded version of this), but we'll be needing it for Xen.

Doesn't this already add the bloat/code? ::

lib/Makefile +1 -1
diff -r c175fd50e604 lib/Makefile
@@ -5,7 +5,7 @@ lib-y := errno.o ctype.o string.o vsprin
 lib-y := errno.o ctype.o string.o vsprintf.o cmdline.o \
 	 bust_spinlocks.o rbtree.o radix-tree.o dump_stack.o \
 	 idr.o div64.o int_sqrt.o bitmap.o extable.o prio_tree.o \
-	 sha1.o
+	 sha1.o kasprintf.o
 
Quoted 1 line

I'm happy to fold it into vsprintf.c though.

That makes sense to me.

Thanks,
---
~Randy

#4 lore raw
Quoted 2 lines · Randy.Dunlap

Doesn't this already add the bloat/code? ::

Yup. My back-of-the-mind unexamined assumption was that this was going into a .a, which it isn't.

Updated patch below.

J

--

From: Jeremy Fitzhardinge <[email protected]>

Implement kasprintf, a kernel version of asprintf. This allocates the memory required for the formatted string, including the trailing '\0'. Returns NULL on allocation failure.

---
include/linux/kernel.h | 2 ++
lib/vsprintf.c | 23 +++++++++++++++++++++++

2 files changed, 25 insertions(+)
include/linux/kernel.h +0 -0
diff -r cd45ea4bb813 include/linux/kernel.h
@@ -114,6 +114,8 @@ extern int scnprintf(char * buf, size_t 
	__attribute__ ((format (printf, 3, 4)));
extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
	__attribute__ ((format (printf, 3, 0)));

+extern char *kasprintf(gfp_t gfp, const char *fmt, ...)
+ __attribute__ ((format (printf, 2, 3)));

extern int sscanf(const char *, const char *, ...)

	__attribute__ ((format (scanf, 2, 3)));
lib/vsprintf.c +0 -0
diff -r cd45ea4bb813 lib/vsprintf.c
@@ -849,3 +849,26 @@ int sscanf(const char * buf, const char }
 

EXPORT_SYMBOL(sscanf);
+
+
+/* Simplified asprintf. */
+char *kasprintf(gfp_t gfp, const char *fmt, ...)
+{
+ va_list ap;
+ unsigned int len;
+ char *p;
+
+ va_start(ap, fmt);
+ len = vsnprintf(NULL, 0, fmt, ap);
+ va_end(ap);
+
+ p = kmalloc(len+1, gfp);
+ if (!p)
+ return NULL;
+ va_start(ap, fmt);
+ vsnprintf(p, len+1, fmt, ap);
+ va_end(ap);
+ return p;
+}
+
+EXPORT_SYMBOL(kasprintf);

#5 lore raw
In reply to Jeremy Fitzhardinge

lib/kasprintf.c line 33

Quoted 2 lines · Jeremy Fitzhardinge
lib/kasprintf.c new file +2 -0
+char *kasprintf(gfp_t gfp, const char *fmt, ...)
+{

Why not just asprintf? We don't have ksprintf...

% grep EXPORT_SYMBOL lib/vsprintf.c
EXPORT_SYMBOL(simple_strtoul);
EXPORT_SYMBOL(simple_strtol);
EXPORT_SYMBOL(simple_strtoull);
EXPORT_SYMBOL(vsnprintf);
EXPORT_SYMBOL(vscnprintf);
EXPORT_SYMBOL(snprintf);
EXPORT_SYMBOL(scnprintf);
EXPORT_SYMBOL(vsprintf);
EXPORT_SYMBOL(sprintf);
EXPORT_SYMBOL(vsscanf);
EXPORT_SYMBOL(sscanf);

?

Cheers,
Kyle M.

#6 lore raw
Quoted 5 lines · On Tue, 20 Jun 2006 23:04:44 -0400 Kyle McMartin <[email protected]> wrote:

lib/kasprintf.c line 33

Quoted 2 lines · Jeremy Fitzhardinge
lib/kasprintf.c new file +2 -0
+char *kasprintf(gfp_t gfp, const char *fmt, ...)
+{

Why not just asprintf? We don't have ksprintf...

asprintf() doesn't take a gfp_t arg.

#7 lore raw
Quoted 9 lines · Andrew Morton
Quoted 4 lines · On Tue, 20 Jun 2006 23:04:44 -0400 Kyle McMartin <[email protected]> wrote:

lib/kasprintf.c line 33

Quoted 2 lines · Jeremy Fitzhardinge
lib/kasprintf.c new file +2 -0
+char *kasprintf(gfp_t gfp, const char *fmt, ...)
+{

Why not just asprintf? We don't have ksprintf...

asprintf() doesn't take a gfp_t arg.

Or, more generally: if we change the API, change the name. kmalloc() is different from malloc() as it is invoked differently, as is kasprintf().

printk() was arguably a mistake, although it's semi-justified a posteori since it takes the priority pseudo-argument now.

-hpa