Sun Jan 24 17:29:11 2021 UTC ()
Add kmem_tmpbuf_alloc(), a utility function for allocating memory for
temporary use where allocation on the stack is desirable, but only up to
a certain size.  If the requested size fits within the specified stack
buffer, the stack buffer is returned.  Otherwise, memory is allocated with
kmem_alloc().  Add a corresponding kmem_tmpbuf_free() function that frees
the memory using kmem_free() if it is not the tempory stack buffer location.


(thorpej)
diff -r1.2372 -r1.2373 src/distrib/sets/lists/comp/mi
diff -r1.454 -r1.455 src/share/man/man9/Makefile
diff -r1.26 -r1.27 src/share/man/man9/kmem.9
diff -r1.80 -r1.81 src/sys/kern/subr_kmem.c
diff -r1.11 -r1.12 src/sys/sys/kmem.h

cvs diff -r1.2372 -r1.2373 src/distrib/sets/lists/comp/mi (expand / switch to context diff)
--- src/distrib/sets/lists/comp/mi 2021/01/21 15:53:15 1.2372
+++ src/distrib/sets/lists/comp/mi 2021/01/24 17:29:11 1.2373
@@ -1,4 +1,4 @@
-#	$NetBSD: mi,v 1.2372 2021/01/21 15:53:15 thorpej Exp $
+#	$NetBSD: mi,v 1.2373 2021/01/24 17:29:11 thorpej Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 ./etc/mtree/set.comp				comp-sys-root
@@ -11650,6 +11650,8 @@
 ./usr/share/man/cat9/kmem_intr_zalloc.0		comp-sys-catman		.cat
 ./usr/share/man/cat9/kmem_strdupsize.0		comp-sys-catman		.cat
 ./usr/share/man/cat9/kmem_strfree.0		comp-sys-catman		.cat
+./usr/share/man/cat9/kmem_tmpbuf_alloc.0	comp-sys-catman		.cat
+./usr/share/man/cat9/kmem_tmpbuf_free.0		comp-sys-catman		.cat
 ./usr/share/man/cat9/kmem_zalloc.0		comp-sys-catman		.cat
 ./usr/share/man/cat9/knote.0			comp-sys-catman		.cat
 ./usr/share/man/cat9/kpause.0			comp-sys-catman		.cat
@@ -19815,6 +19817,8 @@
 ./usr/share/man/html9/kmem_intr_zalloc.html	comp-sys-htmlman	html
 ./usr/share/man/html9/kmem_strdupsize.html	comp-sys-htmlman	html
 ./usr/share/man/html9/kmem_strfree.html		comp-sys-htmlman	html
+./usr/share/man/html9/kmem_tmpbuf_alloc.html	comp-sys-htmlman	html
+./usr/share/man/html9/kmem_tmpbuf_free.html	comp-sys-htmlman	html
 ./usr/share/man/html9/kmem_zalloc.html		comp-sys-htmlman	html
 ./usr/share/man/html9/knote.html		comp-sys-htmlman	html
 ./usr/share/man/html9/kpause.html		comp-sys-htmlman	html
@@ -28132,6 +28136,8 @@
 ./usr/share/man/man9/kmem_intr_zalloc.9		comp-sys-man		.man
 ./usr/share/man/man9/kmem_strdupsize.9		comp-sys-man		.man
 ./usr/share/man/man9/kmem_strfree.9		comp-sys-man		.man
+./usr/share/man/man9/kmem_tmpbuf_alloc.9	comp-sys-man		.man
+./usr/share/man/man9/kmem_tmpbuf_free.9		comp-sys-man		.man
 ./usr/share/man/man9/kmem_zalloc.9		comp-sys-man		.man
 ./usr/share/man/man9/knote.9			comp-sys-man		.man
 ./usr/share/man/man9/kpause.9			comp-sys-man		.man

cvs diff -r1.454 -r1.455 src/share/man/man9/Makefile (expand / switch to context diff)
--- src/share/man/man9/Makefile 2021/01/21 15:53:16 1.454
+++ src/share/man/man9/Makefile 2021/01/24 17:29:11 1.455
@@ -1,4 +1,4 @@
-#       $NetBSD: Makefile,v 1.454 2021/01/21 15:53:16 thorpej Exp $
+#       $NetBSD: Makefile,v 1.455 2021/01/24 17:29:11 thorpej Exp $
 
 #	Makefile for section 9 (kernel function and variable) manual pages.
 
@@ -456,7 +456,9 @@
 	kmem.9 kmem_intr_zalloc.9 \
 	kmem.9 kmem_asprintf.9 \
 	kmem.9 kmem_strdupsize.9 \
-	kmem.9 kmem_strfree.9
+	kmem.9 kmem_strfree.9 \
+	kmem.9 kmem_tmpbuf_alloc.9 \
+	kmem.9 kmem_tmpbuf_free.9
 MAN+=	kpreempt.9
 MLINKS+=kpreempt.9 kpreempt_disable.9 \
 	kpreempt.9 kpreempt_disabled.9 \

cvs diff -r1.26 -r1.27 src/share/man/man9/kmem.9 (expand / switch to context diff)
--- src/share/man/man9/kmem.9 2020/06/19 07:25:20 1.26
+++ src/share/man/man9/kmem.9 2021/01/24 17:29:11 1.27
@@ -1,4 +1,4 @@
-.\"	$NetBSD: kmem.9,v 1.26 2020/06/19 07:25:20 wiz Exp $
+.\"	$NetBSD: kmem.9,v 1.27 2021/01/24 17:29:11 thorpej Exp $
 .\"
 .\" Copyright (c)2006 YAMAMOTO Takashi,
 .\" All rights reserved.
@@ -25,7 +25,7 @@
 .\" SUCH DAMAGE.
 .\"
 .\" ------------------------------------------------------------
-.Dd June 19, 2020
+.Dd January 24, 2021
 .Dt KMEM 9
 .Os
 .\" ------------------------------------------------------------
@@ -73,6 +73,13 @@
 .Fn kmem_strfree \
 "char *str"
 .\" ------------------------------------------------------------
+.Ft void *
+.Fn kmem_tmpbuf_alloc \
+"size_t size" "void *stackbuf" "size_t stackbufsize" "km_flag_t kmflags"
+.Ft void
+.Fn kmem_tmpbuf_free \
+"void *p" "size_t size" "void *stsackbuf"
+.\" ------------------------------------------------------------
 .Pp
 .Cd "options KMEM_SIZE"
 .Sh DESCRIPTION
@@ -215,6 +222,23 @@
 .Dv NUL
 and then using
 .Fn kmem_free .
+.Pp
+The
+.Fn kmem_tmpbuf_alloc
+function is a utility function for allocating memory for temporary
+use, where allocation on the stack is desirable, but only up to a
+certain size.
+If the requested size fits within the specified stack buffer, the
+stack buffer is returned.
+Otherwise, memory is allocated with
+.Fn kmem_alloc .
+The
+.Fn kmem_tmpbuf_free
+function compares the result of a previous call to
+.Fn kmem_tmpbuf_alloc
+and frees the memory using
+.Fn kmem_free
+if it is not the specified stack buffer.
 .\" ------------------------------------------------------------
 .Sh NOTES
 Making

cvs diff -r1.80 -r1.81 src/sys/kern/subr_kmem.c (expand / switch to context diff)
--- src/sys/kern/subr_kmem.c 2020/05/14 17:01:34 1.80
+++ src/sys/kern/subr_kmem.c 2021/01/24 17:29:11 1.81
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_kmem.c,v 1.80 2020/05/14 17:01:34 maxv Exp $	*/
+/*	$NetBSD: subr_kmem.c,v 1.81 2021/01/24 17:29:11 thorpej Exp $	*/
 
 /*
  * Copyright (c) 2009-2020 The NetBSD Foundation, Inc.
@@ -78,7 +78,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.80 2020/05/14 17:01:34 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.81 2021/01/24 17:29:11 thorpej Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_kmem.h"
@@ -474,6 +474,29 @@
 		return;
 
 	kmem_free(str, strlen(str) + 1);
+}
+
+/*
+ * Utility routine to maybe-allocate a temporary buffer if the size
+ * is larger than we're willing to put on the stack.
+ */
+void *
+kmem_tmpbuf_alloc(size_t size, void *stackbuf, size_t stackbufsize,
+    km_flag_t flags)
+{
+	if (size <= stackbufsize) {
+		return stackbuf;
+	}
+
+	return kmem_alloc(size, flags);
+}
+
+void
+kmem_tmpbuf_free(void *buf, size_t size, void *stackbuf)
+{
+	if (buf != stackbuf) {
+		kmem_free(buf, size);
+	}
 }
 
 /* --------------------------- DEBUG / DIAGNOSTIC --------------------------- */

cvs diff -r1.11 -r1.12 src/sys/sys/kmem.h (expand / switch to context diff)
--- src/sys/sys/kmem.h 2018/01/09 01:53:55 1.11
+++ src/sys/sys/kmem.h 2021/01/24 17:29:11 1.12
@@ -1,4 +1,4 @@
-/*	$NetBSD: kmem.h,v 1.11 2018/01/09 01:53:55 christos Exp $	*/
+/*	$NetBSD: kmem.h,v 1.12 2021/01/24 17:29:11 thorpej Exp $	*/
 
 /*-
  * Copyright (c)2006 YAMAMOTO Takashi,
@@ -50,6 +50,9 @@
 #define kmem_strdup(s, f)	kmem_strdupsize((s), NULL, (f))
 char *	kmem_strndup(const char *, size_t, km_flag_t);
 void	kmem_strfree(char *);
+
+void *	kmem_tmpbuf_alloc(size_t, void *, size_t, km_flag_t);
+void	kmem_tmpbuf_free(void *, size_t, void *);
 
 /*
  * km_flag_t values: