Add new malloc(9) flag M_ZERO - zeros memory before returning.

From Poul-Henning Kamp's equivalent enhancement in FreeBSD.
This commit is contained in:
lukem 2001-11-17 03:50:27 +00:00
parent b4109e020f
commit 576eed5512
4 changed files with 28 additions and 15 deletions

View File

@ -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 @@ free((caddr_t)(addr), type)
.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

View File

@ -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 @@ out:
domlog(va, size, type, 1, file, line);
#endif
splx(s);
if (va != NULL && (flags & M_ZERO))
memset(va, 0, size);
return ((void *) va);
}

View File

@ -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>
@ -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 @@ debug_malloc(unsigned long size, int type, int flags, void **addr)
* 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);
}

View File

@ -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