Module Name:    src
Committed By:   thorpej
Date:           Sun Jan 24 17:29:11 UTC 2021

Modified Files:
        src/distrib/sets/lists/comp: mi
        src/share/man/man9: Makefile kmem.9
        src/sys/kern: subr_kmem.c
        src/sys/sys: kmem.h

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.2372 -r1.2373 src/distrib/sets/lists/comp/mi
cvs rdiff -u -r1.454 -r1.455 src/share/man/man9/Makefile
cvs rdiff -u -r1.26 -r1.27 src/share/man/man9/kmem.9
cvs rdiff -u -r1.80 -r1.81 src/sys/kern/subr_kmem.c
cvs rdiff -u -r1.11 -r1.12 src/sys/sys/kmem.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/distrib/sets/lists/comp/mi
diff -u src/distrib/sets/lists/comp/mi:1.2372 src/distrib/sets/lists/comp/mi:1.2373
--- src/distrib/sets/lists/comp/mi:1.2372	Thu Jan 21 15:53:15 2021
+++ src/distrib/sets/lists/comp/mi	Sun Jan 24 17:29:11 2021
@@ -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

Index: src/share/man/man9/Makefile
diff -u src/share/man/man9/Makefile:1.454 src/share/man/man9/Makefile:1.455
--- src/share/man/man9/Makefile:1.454	Thu Jan 21 15:53:16 2021
+++ src/share/man/man9/Makefile	Sun Jan 24 17:29:11 2021
@@ -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 @@ MLINKS+=kmem.9 kmem_alloc.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 \

Index: src/share/man/man9/kmem.9
diff -u src/share/man/man9/kmem.9:1.26 src/share/man/man9/kmem.9:1.27
--- src/share/man/man9/kmem.9:1.26	Fri Jun 19 07:25:20 2020
+++ src/share/man/man9/kmem.9	Sun Jan 24 17:29:11 2021
@@ -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 @@ and adding one for the
 .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

Index: src/sys/kern/subr_kmem.c
diff -u src/sys/kern/subr_kmem.c:1.80 src/sys/kern/subr_kmem.c:1.81
--- src/sys/kern/subr_kmem.c:1.80	Thu May 14 17:01:34 2020
+++ src/sys/kern/subr_kmem.c	Sun Jan 24 17:29:11 2021
@@ -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"
@@ -476,6 +476,29 @@ kmem_strfree(char *str)
 	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 --------------------------- */
 
 #if defined(KMEM_SIZE)

Index: src/sys/sys/kmem.h
diff -u src/sys/sys/kmem.h:1.11 src/sys/sys/kmem.h:1.12
--- src/sys/sys/kmem.h:1.11	Tue Jan  9 01:53:55 2018
+++ src/sys/sys/kmem.h	Sun Jan 24 17:29:11 2021
@@ -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,
@@ -51,6 +51,9 @@ char *	kmem_strdupsize(const char *, siz
 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:
  */

Reply via email to