* [PATCH] Implement kasprintf
@ 2006-06-20 23:57 Jeremy Fitzhardinge
2006-06-21 0:10 ` Randy.Dunlap
2006-06-21 3:04 ` Kyle McMartin
0 siblings, 2 replies; 9+ messages in thread
From: Jeremy Fitzhardinge @ 2006-06-20 23:57 UTC (permalink / raw)
To: Linux Kernel Mailing List; +Cc: Andrew Morton, Christian Limpach, Chris Wright
From: Jeremy Fitzhardinge <jeremy@xensource.com>
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.
Signed-off-by: Jeremy Fitzhardinge <jeremy@xensource.com>
Signed-off-by: Chris Wright <chrisw@sous-sol.org>
---
include/linux/kernel.h | 2 +
lib/Makefile | 2 -
lib/kasprintf.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 57 insertions(+), 1 deletion(-)
diff -r c175fd50e604 include/linux/kernel.h
--- a/include/linux/kernel.h Tue Jun 20 16:47:53 2006 -0700
+++ b/include/linux/kernel.h Tue Jun 20 16:53:19 2006 -0700
@@ -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
--- a/lib/Makefile Tue Jun 20 16:47:53 2006 -0700
+++ b/lib/Makefile Tue Jun 20 16:53:19 2006 -0700
@@ -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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/kasprintf.c Tue Jun 20 16:53:19 2006 -0700
@@ -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);
+
+ 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);
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH] Implement kasprintf
2006-06-20 23:57 [PATCH] Implement kasprintf Jeremy Fitzhardinge
@ 2006-06-21 0:10 ` Randy.Dunlap
2006-06-21 0:14 ` Jeremy Fitzhardinge
2006-06-21 3:04 ` Kyle McMartin
1 sibling, 1 reply; 9+ messages in thread
From: Randy.Dunlap @ 2006-06-21 0:10 UTC (permalink / raw)
To: Jeremy Fitzhardinge; +Cc: linux-kernel, akpm, Christian.Limpach, chrisw
On Tue, 20 Jun 2006 16:57:16 -0700 Jeremy Fitzhardinge wrote:
> From: Jeremy Fitzhardinge <jeremy@xensource.com>
>
> 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.
>
> Signed-off-by: Jeremy Fitzhardinge <jeremy@xensource.com>
> Signed-off-by: Chris Wright <chrisw@sous-sol.org>
>
> ---
> 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?
> 3 files changed, 57 insertions(+), 1 deletion(-)
>
>
> diff -r c175fd50e604 include/linux/kernel.h
> --- a/include/linux/kernel.h Tue Jun 20 16:47:53 2006 -0700
> +++ b/include/linux/kernel.h Tue Jun 20 16:53:19 2006 -0700
> @@ -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
> --- a/lib/Makefile Tue Jun 20 16:47:53 2006 -0700
> +++ b/lib/Makefile Tue Jun 20 16:53:19 2006 -0700
> @@ -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
> --- /dev/null Thu Jan 01 00:00:00 1970 +0000
> +++ b/lib/kasprintf.c Tue Jun 20 16:53:19 2006 -0700
> @@ -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);
> +
> + 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
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH] Implement kasprintf
2006-06-21 0:10 ` Randy.Dunlap
@ 2006-06-21 0:14 ` Jeremy Fitzhardinge
2006-06-21 0:26 ` Randy.Dunlap
0 siblings, 1 reply; 9+ messages in thread
From: Jeremy Fitzhardinge @ 2006-06-21 0:14 UTC (permalink / raw)
To: Randy.Dunlap; +Cc: linux-kernel, akpm, Christian.Limpach, chrisw
Randy.Dunlap wrote:
> 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
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH] Implement kasprintf
2006-06-21 0:14 ` Jeremy Fitzhardinge
@ 2006-06-21 0:26 ` Randy.Dunlap
2006-06-21 0:31 ` Jeremy Fitzhardinge
0 siblings, 1 reply; 9+ messages in thread
From: Randy.Dunlap @ 2006-06-21 0:26 UTC (permalink / raw)
To: Jeremy Fitzhardinge; +Cc: linux-kernel, akpm, Christian.Limpach, chrisw
On Tue, 20 Jun 2006 17:14:55 -0700 Jeremy Fitzhardinge wrote:
> Randy.Dunlap wrote:
> > 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? ::
diff -r c175fd50e604 lib/Makefile
--- a/lib/Makefile Tue Jun 20 16:47:53 2006 -0700
+++ b/lib/Makefile Tue Jun 20 16:53:19 2006 -0700
@@ -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
> I'm happy to fold it into vsprintf.c though.
That makes sense to me.
Thanks,
---
~Randy
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] Implement kasprintf
2006-06-21 0:26 ` Randy.Dunlap
@ 2006-06-21 0:31 ` Jeremy Fitzhardinge
0 siblings, 0 replies; 9+ messages in thread
From: Jeremy Fitzhardinge @ 2006-06-21 0:31 UTC (permalink / raw)
To: Randy.Dunlap; +Cc: linux-kernel, akpm, Christian.Limpach, chrisw
Randy.Dunlap wrote:
> 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 <jeremy@xensource.com>
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.
Signed-off-by: Jeremy Fitzhardinge <jeremy@xensource.com>
Signed-off-by: Chris Wright <chrisw@sous-sol.org>
---
include/linux/kernel.h | 2 ++
lib/vsprintf.c | 23 +++++++++++++++++++++++
2 files changed, 25 insertions(+)
diff -r cd45ea4bb813 include/linux/kernel.h
--- a/include/linux/kernel.h Tue Jun 20 17:28:16 2006 -0700
+++ b/include/linux/kernel.h Tue Jun 20 17:29:18 2006 -0700
@@ -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 cd45ea4bb813 lib/vsprintf.c
--- a/lib/vsprintf.c Tue Jun 20 17:28:16 2006 -0700
+++ b/lib/vsprintf.c Tue Jun 20 17:29:18 2006 -0700
@@ -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);
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] Implement kasprintf
2006-06-20 23:57 [PATCH] Implement kasprintf Jeremy Fitzhardinge
2006-06-21 0:10 ` Randy.Dunlap
@ 2006-06-21 3:04 ` Kyle McMartin
2006-06-21 3:26 ` Andrew Morton
1 sibling, 1 reply; 9+ messages in thread
From: Kyle McMartin @ 2006-06-21 3:04 UTC (permalink / raw)
To: Jeremy Fitzhardinge
Cc: Linux Kernel Mailing List, Andrew Morton, Christian Limpach,
Chris Wright
On Tue, Jun 20, 2006 at 04:57:16PM -0700, Jeremy Fitzhardinge wrote:
> +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.
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH] Implement kasprintf
2006-06-21 3:04 ` Kyle McMartin
@ 2006-06-21 3:26 ` Andrew Morton
2006-06-21 4:30 ` H. Peter Anvin
2006-06-21 11:58 ` Kyle McMartin
0 siblings, 2 replies; 9+ messages in thread
From: Andrew Morton @ 2006-06-21 3:26 UTC (permalink / raw)
To: Kyle McMartin; +Cc: jeremy, linux-kernel, Christian.Limpach, chrisw
On Tue, 20 Jun 2006 23:04:44 -0400
Kyle McMartin <kyle@parisc-linux.org> wrote:
> On Tue, Jun 20, 2006 at 04:57:16PM -0700, Jeremy Fitzhardinge wrote:
> > +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.
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH] Implement kasprintf
2006-06-21 3:26 ` Andrew Morton
@ 2006-06-21 4:30 ` H. Peter Anvin
2006-06-21 11:58 ` Kyle McMartin
1 sibling, 0 replies; 9+ messages in thread
From: H. Peter Anvin @ 2006-06-21 4:30 UTC (permalink / raw)
To: Andrew Morton
Cc: Kyle McMartin, jeremy, linux-kernel, Christian.Limpach, chrisw
Andrew Morton wrote:
> On Tue, 20 Jun 2006 23:04:44 -0400
> Kyle McMartin <kyle@parisc-linux.org> wrote:
>
>> On Tue, Jun 20, 2006 at 04:57:16PM -0700, Jeremy Fitzhardinge wrote:
>>> +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
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH] Implement kasprintf
2006-06-21 3:26 ` Andrew Morton
2006-06-21 4:30 ` H. Peter Anvin
@ 2006-06-21 11:58 ` Kyle McMartin
1 sibling, 0 replies; 9+ messages in thread
From: Kyle McMartin @ 2006-06-21 11:58 UTC (permalink / raw)
To: Andrew Morton
Cc: Kyle McMartin, jeremy, linux-kernel, Christian.Limpach, chrisw
On Tue, Jun 20, 2006 at 08:26:17PM -0700, Andrew Morton wrote:
>
> asprintf() doesn't take a gfp_t arg.
>
Good point. :)
Cheers,
Kyle
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2006-06-21 11:58 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-06-20 23:57 [PATCH] Implement kasprintf Jeremy Fitzhardinge
2006-06-21 0:10 ` Randy.Dunlap
2006-06-21 0:14 ` Jeremy Fitzhardinge
2006-06-21 0:26 ` Randy.Dunlap
2006-06-21 0:31 ` Jeremy Fitzhardinge
2006-06-21 3:04 ` Kyle McMartin
2006-06-21 3:26 ` Andrew Morton
2006-06-21 4:30 ` H. Peter Anvin
2006-06-21 11:58 ` Kyle McMartin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox