Source-Changes-HG archive

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

[src/trunk]: src Add new malloc(9) flag M_ZERO - zeros memory before returning.



details:   https://anonhg.NetBSD.org/src/rev/7ddef2b023b9
branches:  trunk
changeset: 517688:7ddef2b023b9
user:      lukem <lukem%NetBSD.org@localhost>
date:      Sat Nov 17 03:50:27 2001 +0000

description:
Add new malloc(9) flag M_ZERO - zeros memory before returning.
>From Poul-Henning Kamp's equivalent enhancement in FreeBSD.

diffstat:

 share/man/man9/malloc.9      |  28 ++++++++++++++++++----------
 sys/kern/kern_malloc.c       |   6 ++++--
 sys/kern/kern_malloc_debug.c |   6 ++++--
 sys/sys/malloc.h             |   3 ++-
 4 files changed, 28 insertions(+), 15 deletions(-)

diffs (140 lines):

diff -r ce83895932f2 -r 7ddef2b023b9 share/man/man9/malloc.9
--- a/share/man/man9/malloc.9   Sat Nov 17 02:06:47 2001 +0000
+++ b/share/man/man9/malloc.9   Sat Nov 17 03:50:27 2001 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: malloc.9,v 1.15 2001/05/06 23:48:33 wiz Exp $
+.\"    $NetBSD: malloc.9,v 1.16 2001/11/17 03:50:29 lukem Exp $
 .\"
 .\" Copyright (c) 1996 The NetBSD Foundation, Inc.
 .\" All rights reserved.
@@ -34,7 +34,7 @@
 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 .\" POSSIBILITY OF SUCH DAMAGE.
 .\"
-.Dd June 16, 1996
+.Dd November 17, 2001
 .Dt MALLOC 9
 .Os
 .Sh NAME
@@ -76,34 +76,42 @@
 .Pp
 Unlike its standard C library counterpart
 .Pq Xr malloc 3 ,
-the kernel version takes two more arguments.  The
+the kernel version takes two more arguments.
+.Pp
+The
 .Fa flags
 argument further qualifies
 .Fn malloc No Ns 's
 operational characteristics as follows:
-.Bl -tag -offset indent
+.Bl -tag -offset indent -width M_NOWAIT
 .It Dv M_NOWAIT
 Causes
 .Fn malloc
 to return
 .Dv NULL
 if the request cannot be immediately fulfilled due to resource shortage.
-Otherwise,
-.Fn malloc
-may call sleep to wait for resources to be released by other processes.
-If this flag is not set,
+If this flag is not set
+(see
+.Dv M_WAITOK ) ,
 .Fn malloc
 will never return
 .Dv NULL .
+.It Dv M_WAITOK
+By default,
+.Fn malloc
+may call
+.Xr sleep 9
+to wait for resources to be released by other processes, and this
+flag represents this behaviour.
 Note that
 .Dv M_WAITOK
 is conveniently defined to be 0, and hence maybe or'ed into the
 .Fa flags
 argument to indicate that it's Ok to wait for resources.
+.It Dv M_ZERO
+Causes the allocated memory to be set to all zeros.
 .El
 .Pp
-Currently, only one flag is defined.
-.Pp
 The
 .Fa type
 argument broadly identifies the kernel subsystem for which the allocated
diff -r ce83895932f2 -r 7ddef2b023b9 sys/kern/kern_malloc.c
--- a/sys/kern/kern_malloc.c    Sat Nov 17 02:06:47 2001 +0000
+++ b/sys/kern/kern_malloc.c    Sat Nov 17 03:50:27 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_malloc.c,v 1.64 2001/11/12 15:25:12 lukem Exp $   */
+/*     $NetBSD: kern_malloc.c,v 1.65 2001/11/17 03:50:28 lukem Exp $   */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_malloc.c,v 1.64 2001/11/12 15:25:12 lukem Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_malloc.c,v 1.65 2001/11/17 03:50:28 lukem Exp $");
 
 #include "opt_lockdebug.h"
 
@@ -410,6 +410,8 @@
        domlog(va, size, type, 1, file, line);
 #endif
        splx(s);
+       if (va != NULL && (flags & M_ZERO))
+               memset(va, 0, size);
        return ((void *) va);
 }
 
diff -r ce83895932f2 -r 7ddef2b023b9 sys/kern/kern_malloc_debug.c
--- a/sys/kern/kern_malloc_debug.c      Sat Nov 17 02:06:47 2001 +0000
+++ b/sys/kern/kern_malloc_debug.c      Sat Nov 17 03:50:27 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_malloc_debug.c,v 1.4 2001/11/12 15:25:12 lukem Exp $      */
+/*     $NetBSD: kern_malloc_debug.c,v 1.5 2001/11/17 03:50:28 lukem Exp $      */
 
 /*
  * Copyright (c) 1999, 2000 Artur Grabowski <art%openbsd.org@localhost>
@@ -56,7 +56,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_malloc_debug.c,v 1.4 2001/11/12 15:25:12 lukem Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_malloc_debug.c,v 1.5 2001/11/17 03:50:28 lukem Exp $");
 
 #include <sys/param.h>
 #include <sys/proc.h>
@@ -154,6 +154,8 @@
         * ends. roundup to get decent alignment.
         */
        *addr = (void *)(md->md_va + PAGE_SIZE - roundup(size, sizeof(long)));
+       if (*addr != NULL && (flags & M_ZERO))
+               memset(*addr, 0, size);
        return (1);
 }
 
diff -r ce83895932f2 -r 7ddef2b023b9 sys/sys/malloc.h
--- a/sys/sys/malloc.h  Sat Nov 17 02:06:47 2001 +0000
+++ b/sys/sys/malloc.h  Sat Nov 17 03:50:27 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: malloc.h,v 1.67 2001/10/04 19:00:44 eeh Exp $  */
+/*     $NetBSD: malloc.h,v 1.68 2001/11/17 03:50:27 lukem Exp $        */
 
 /*
  * Copyright (c) 1987, 1993
@@ -50,6 +50,7 @@
  */
 #define        M_WAITOK        0x0000
 #define        M_NOWAIT        0x0001
+#define        M_ZERO          0x0002  /* zero the allocation */
 
 /*
  * Types of memory to be allocated



Home | Main Index | Thread Index | Old Index