Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src Add kmem_asprintf rotuine which allocates string accordingly...
details: https://anonhg.NetBSD.org/src/rev/3fa85313a89a
branches: trunk
changeset: 752042:3fa85313a89a
user: haad <haad%NetBSD.org@localhost>
date: Thu Feb 11 23:13:46 2010 +0000
description:
Add kmem_asprintf rotuine which allocates string accordingly to format
string from kmem pool. Allocated string is string length + 1 char for ending
zero.
Ok: ad@.
diffstat:
share/man/man9/kmem.9 | 15 ++++++++++++++-
sys/kern/subr_kmem.c | 31 +++++++++++++++++++++++++++++--
sys/sys/kmem.h | 3 ++-
3 files changed, 45 insertions(+), 4 deletions(-)
diffs (109 lines):
diff -r 9be820b2c926 -r 3fa85313a89a share/man/man9/kmem.9
--- a/share/man/man9/kmem.9 Thu Feb 11 21:28:16 2010 +0000
+++ b/share/man/man9/kmem.9 Thu Feb 11 23:13:46 2010 +0000
@@ -1,4 +1,4 @@
-.\" $NetBSD: kmem.9,v 1.4 2010/01/23 00:54:43 rmind Exp $
+.\" $NetBSD: kmem.9,v 1.5 2010/02/11 23:13:46 haad Exp $
.\"
.\" Copyright (c)2006 YAMAMOTO Takashi,
.\" All rights reserved.
@@ -45,6 +45,9 @@
.Ft void
.Fn kmem_free \
"void *p" "size_t size"
+.Ft char *
+.Fn kmem_asprintf \
+"const char *fmt" "..."
.\" ------------------------------------------------------------
.Pp
.Cd "options DEBUG"
@@ -93,6 +96,16 @@
except that it initializes the memory to zero.
.Pp
.\" ------------------------------------------------------------
+.Fn kmem_asprintf
+functions as the well known
+.Fn asprintf
+function, but allocates memory using
+.Fn kmem_alloc .
+This routine can sleep during allocation.
+The size of the allocated area is the length of the returned character string, plus one (for the NUL terminator).
+This must be taken into consideration when freeing the returned area with
+.Fn kmem_free .
+.\" ------------------------------------------------------------
.Fn kmem_free
frees kernel wired memory allocated by
.Fn kmem_alloc
diff -r 9be820b2c926 -r 3fa85313a89a sys/kern/subr_kmem.c
--- a/sys/kern/subr_kmem.c Thu Feb 11 21:28:16 2010 +0000
+++ b/sys/kern/subr_kmem.c Thu Feb 11 23:13:46 2010 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: subr_kmem.c,v 1.32 2010/01/31 11:54:32 skrll Exp $ */
+/* $NetBSD: subr_kmem.c,v 1.33 2010/02/11 23:13:46 haad Exp $ */
/*-
* Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -63,7 +63,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.32 2010/01/31 11:54:32 skrll Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.33 2010/02/11 23:13:46 haad Exp $");
#include <sys/param.h>
#include <sys/callback.h>
@@ -79,6 +79,8 @@
#include <lib/libkern/libkern.h>
+#include <machine/stdarg.h>
+
#define KMEM_QUANTUM_SIZE (ALIGNBYTES + 1)
#define KMEM_QCACHE_MAX (KMEM_QUANTUM_SIZE * 32)
#define KMEM_CACHE_COUNT 16
@@ -456,3 +458,28 @@
}
}
#endif /* defined(KMEM_SIZE) */
+
+/*
+ * Used to dynamically allocate string with kmem accordingly to format.
+ */
+char *
+kmem_asprintf(const char *fmt, ...)
+{
+ int size, str_len;
+ va_list va;
+ char *str;
+ char buf[1];
+
+ va_start(va, fmt);
+ str_len = vsnprintf(buf, sizeof(buf), fmt, va) + 1;
+ va_end(va);
+
+ str = kmem_alloc(str_len, KM_SLEEP);
+
+ if ((size = vsnprintf(str, str_len, fmt, va)) == -1) {
+ kmem_free(str, str_len);
+ return NULL;
+ }
+
+ return str;
+}
diff -r 9be820b2c926 -r 3fa85313a89a sys/sys/kmem.h
--- a/sys/sys/kmem.h Thu Feb 11 21:28:16 2010 +0000
+++ b/sys/sys/kmem.h Thu Feb 11 23:13:46 2010 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: kmem.h,v 1.3 2007/12/24 16:45:16 ad Exp $ */
+/* $NetBSD: kmem.h,v 1.4 2010/02/11 23:13:46 haad Exp $ */
/*-
* Copyright (c)2006 YAMAMOTO Takashi,
@@ -39,6 +39,7 @@
void kmem_init(void);
size_t kmem_roundup_size(size_t);
+char *kmem_asprintf(const char *, ...);
/*
* km_flag_t
*
Home |
Main Index |
Thread Index |
Old Index