Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/sys Add kmem_tmpbuf_alloc(), a utility function for allo...



details:   https://anonhg.NetBSD.org/src/rev/48fcb2b673da
branches:  trunk
changeset: 958911:48fcb2b673da
user:      thorpej <thorpej%NetBSD.org@localhost>
date:      Sun Jan 24 17:29:11 2021 +0000

description:
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.

diffstat:

 distrib/sets/lists/comp/mi |   8 +++++++-
 share/man/man9/Makefile    |   6 ++++--
 share/man/man9/kmem.9      |  28 ++++++++++++++++++++++++++--
 sys/kern/subr_kmem.c       |  27 +++++++++++++++++++++++++--
 sys/sys/kmem.h             |   5 ++++-
 5 files changed, 66 insertions(+), 8 deletions(-)

diffs (179 lines):

diff -r 3e5dc6fea7ea -r 48fcb2b673da distrib/sets/lists/comp/mi
--- a/distrib/sets/lists/comp/mi        Sun Jan 24 16:55:09 2021 +0000
+++ b/distrib/sets/lists/comp/mi        Sun Jan 24 17:29:11 2021 +0000
@@ -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
diff -r 3e5dc6fea7ea -r 48fcb2b673da share/man/man9/Makefile
--- a/share/man/man9/Makefile   Sun Jan 24 16:55:09 2021 +0000
+++ b/share/man/man9/Makefile   Sun Jan 24 17:29:11 2021 +0000
@@ -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 \
diff -r 3e5dc6fea7ea -r 48fcb2b673da share/man/man9/kmem.9
--- a/share/man/man9/kmem.9     Sun Jan 24 16:55:09 2021 +0000
+++ b/share/man/man9/kmem.9     Sun Jan 24 17:29:11 2021 +0000
@@ -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
diff -r 3e5dc6fea7ea -r 48fcb2b673da sys/kern/subr_kmem.c
--- a/sys/kern/subr_kmem.c      Sun Jan 24 16:55:09 2021 +0000
+++ b/sys/kern/subr_kmem.c      Sun Jan 24 17:29:11 2021 +0000
@@ -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_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)
diff -r 3e5dc6fea7ea -r 48fcb2b673da sys/sys/kmem.h
--- a/sys/sys/kmem.h    Sun Jan 24 16:55:09 2021 +0000
+++ b/sys/sys/kmem.h    Sun Jan 24 17:29:11 2021 +0000
@@ -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_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:
  */



Home | Main Index | Thread Index | Old Index