1997-03-22 23:50:21 +00:00
|
|
|
.\"
|
|
|
|
.\" Copyright (c) 1996 The NetBSD Foundation, Inc.
|
|
|
|
.\" All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
.\" by Paul Kranenburg.
|
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
|
|
|
.\" 3. All advertising materials mentioning features or use of this software
|
|
|
|
.\" must display the following acknowledgement:
|
|
|
|
.\" This product includes software developed by the NetBSD
|
|
|
|
.\" Foundation, Inc. and its contributors.
|
|
|
|
.\" 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
.\" contributors may be used to endorse or promote products derived
|
|
|
|
.\" from this software without specific prior written permission.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
|
|
|
|
.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
.\" POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
.\"
|
1999-09-29 16:49:18 +00:00
|
|
|
.\" $NetBSD: malloc.9,v 1.3 1996/11/11 00:05:11 lukem Exp $
|
1999-08-28 00:22:10 +00:00
|
|
|
.\" $FreeBSD$
|
1999-07-12 21:02:10 +00:00
|
|
|
.\"
|
1997-03-22 23:50:21 +00:00
|
|
|
.Dd June 16, 1996
|
|
|
|
.Dt MALLOC 9
|
2001-07-10 15:31:11 +00:00
|
|
|
.Os
|
1997-03-22 23:50:21 +00:00
|
|
|
.Sh NAME
|
|
|
|
.Nm malloc ,
|
|
|
|
.Nm MALLOC ,
|
|
|
|
.Nm free ,
|
|
|
|
.Nm FREE
|
|
|
|
.Nd kernel memory management routines
|
|
|
|
.Sh SYNOPSIS
|
2001-10-01 16:09:29 +00:00
|
|
|
.In sys/types.h
|
|
|
|
.In sys/malloc.h
|
1997-03-22 23:50:21 +00:00
|
|
|
.Ft void *
|
1997-10-11 08:11:31 +00:00
|
|
|
.Fn malloc "unsigned long size" "struct malloc_type *type" "int flags"
|
|
|
|
.Fn MALLOC "space" "cast" "unsigned long size" "struct malloc_type *type" "int flags"
|
1997-03-22 23:50:21 +00:00
|
|
|
.Ft void
|
1997-10-11 08:11:31 +00:00
|
|
|
.Fn free "void *addr" "struct malloc_type *type"
|
|
|
|
.Fn FREE "void *addr" "struct malloc_type *type"
|
2002-03-13 01:42:33 +00:00
|
|
|
.Ft void *
|
|
|
|
.Fn realloc "void *addr" "unsigned long size" "struct malloc_type *type" "int flags"
|
|
|
|
.Ft void *
|
|
|
|
.Fn reallocf "void *addr" "unsigned long size" "struct malloc_type *type" "int flags"
|
1997-03-22 23:50:21 +00:00
|
|
|
.Sh DESCRIPTION
|
|
|
|
The
|
|
|
|
.Fn malloc
|
|
|
|
function allocates uninitialized memory in kernel address space for an
|
|
|
|
object whose size is specified by
|
|
|
|
.Fa size .
|
2002-03-13 01:42:33 +00:00
|
|
|
.Pp
|
1997-03-22 23:50:21 +00:00
|
|
|
.Fn free
|
|
|
|
releases memory at address
|
|
|
|
.Fa addr
|
|
|
|
that was previously allocated by
|
|
|
|
.Fn malloc
|
2002-03-13 01:42:33 +00:00
|
|
|
for re-use.
|
|
|
|
The memory is not zeroed.
|
|
|
|
If
|
|
|
|
.Fa addr
|
|
|
|
is
|
|
|
|
.Dv NULL ,
|
|
|
|
then
|
|
|
|
.Fn free
|
|
|
|
does nothing.
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn realloc
|
|
|
|
function changes the size of the previously allocated memory referenced by
|
|
|
|
.Fa addr
|
|
|
|
to
|
|
|
|
.Fa size
|
|
|
|
bytes.
|
|
|
|
The contents of the memory are unchanged up to the lesser of the new and
|
|
|
|
old sizes.
|
|
|
|
Note that the returned value may differ from
|
|
|
|
.Fa addr .
|
|
|
|
If the requested memory cannot be allocated,
|
|
|
|
.Dv NULL
|
|
|
|
is returned and the memory referenced by
|
|
|
|
.Fa addr
|
|
|
|
is valid and unchanged.
|
|
|
|
If
|
|
|
|
.Fa addr
|
|
|
|
is
|
|
|
|
.Dv NULL ,
|
|
|
|
the
|
|
|
|
.Fn realloc
|
|
|
|
function behaves identically to
|
|
|
|
.Fn malloc
|
|
|
|
for the specified size.
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn reallocf
|
2002-03-18 10:52:09 +00:00
|
|
|
function is identical to
|
|
|
|
.Fn realloc
|
|
|
|
except that it
|
2002-03-13 01:42:33 +00:00
|
|
|
will free the passed pointer when the requested memory cannot be allocated.
|
|
|
|
.Pp
|
1997-03-22 23:50:21 +00:00
|
|
|
The
|
|
|
|
.Fn MALLOC
|
|
|
|
macro variant is functionally equivalent to
|
|
|
|
.Bd -literal -offset indent
|
|
|
|
(space) = (cast)malloc((u_long)(size), type, flags)
|
|
|
|
.Ed
|
|
|
|
.Pp
|
|
|
|
and the
|
|
|
|
.Fn FREE
|
|
|
|
macro variant is equivalent to
|
|
|
|
.Bd -literal -offset indent
|
|
|
|
free((addr), type)
|
|
|
|
.Ed
|
|
|
|
.Pp
|
|
|
|
Unlike its standard C library counterpart
|
|
|
|
.Pq Xr malloc 3 ,
|
|
|
|
the kernel version takes two more arguments. The
|
|
|
|
.Fa flags
|
|
|
|
argument further qualifies
|
2001-02-07 10:11:57 +00:00
|
|
|
.Fn malloc Ns 's
|
1997-03-22 23:50:21 +00:00
|
|
|
operational characteristics as follows:
|
1998-12-23 01:05:06 +00:00
|
|
|
.Bl -tag -width indent
|
2000-10-20 17:54:55 +00:00
|
|
|
.It Dv M_ZERO
|
|
|
|
Causes the allocated memory to be set to all zeros.
|
1997-03-22 23:50:21 +00:00
|
|
|
.It Dv M_NOWAIT
|
|
|
|
Causes
|
2002-03-13 01:42:33 +00:00
|
|
|
.Fn malloc ,
|
|
|
|
.Fn realloc ,
|
2002-03-18 10:52:09 +00:00
|
|
|
and
|
2002-03-13 01:42:33 +00:00
|
|
|
.Fn reallocf
|
1997-03-22 23:50:21 +00:00
|
|
|
to return
|
|
|
|
.Dv NULL
|
|
|
|
if the request cannot be immediately fulfilled due to resource shortage.
|
2002-03-13 01:42:33 +00:00
|
|
|
Otherwise, the current process may be put to sleep to wait for
|
|
|
|
resources to be released by other processes.
|
1998-12-23 01:05:06 +00:00
|
|
|
If this flag is set,
|
1997-03-22 23:50:21 +00:00
|
|
|
.Fn malloc
|
2001-07-14 19:41:16 +00:00
|
|
|
will return
|
1998-12-23 01:05:06 +00:00
|
|
|
.Dv NULL
|
2002-03-13 01:42:33 +00:00
|
|
|
rather then block.
|
|
|
|
Note that
|
1997-03-22 23:50:21 +00:00
|
|
|
.Dv M_WAITOK
|
1998-12-23 01:05:06 +00:00
|
|
|
is defined to be 0, meaning that blocking operation is the default.
|
2002-03-18 10:52:09 +00:00
|
|
|
Also note that
|
2002-03-13 01:42:33 +00:00
|
|
|
.Dv M_NOWAIT
|
|
|
|
is required when running in an interrupt context.
|
1998-12-23 01:05:06 +00:00
|
|
|
.It Dv M_WAITOK
|
1999-09-29 16:49:18 +00:00
|
|
|
Indicates that it is Ok to wait for resources. It is unconveniently
|
1998-12-23 01:05:06 +00:00
|
|
|
defined as 0 so care should be taken never to compare against this value
|
|
|
|
directly or try to AND it as a flag. The default operation is to block
|
2001-07-14 19:41:16 +00:00
|
|
|
until the memory allocation succeeds.
|
2002-03-13 01:42:33 +00:00
|
|
|
.Fn malloc ,
|
|
|
|
.Fn realloc ,
|
|
|
|
and
|
|
|
|
.Fn reallocf
|
1998-12-23 01:05:06 +00:00
|
|
|
can only return
|
|
|
|
.Dv NULL
|
2001-07-14 19:41:16 +00:00
|
|
|
if
|
|
|
|
.Dv M_NOWAIT
|
1998-12-23 01:05:06 +00:00
|
|
|
is specified.
|
1999-09-29 16:49:18 +00:00
|
|
|
.It Dv M_USE_RESERVE
|
|
|
|
Indicates that the system can dig into its reserve in order to obtain the
|
2001-07-14 19:41:16 +00:00
|
|
|
requested memory. This option used to be called M_KERNEL but has been
|
2001-08-23 23:41:53 +00:00
|
|
|
renamed to something more obvious. This option has been deprecated and is
|
1999-09-29 16:49:18 +00:00
|
|
|
slowly being removed from the kernel, and so should not be used with any new
|
|
|
|
programming.
|
1997-03-22 23:50:21 +00:00
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fa type
|
1997-10-11 08:11:31 +00:00
|
|
|
argument is used to perform statistics on memory usage, and for
|
|
|
|
basic sanity checks.
|
|
|
|
The statistics can be examined by
|
|
|
|
.Sq vmstat -m .
|
1997-03-22 23:50:21 +00:00
|
|
|
.Pp
|
1997-10-11 08:11:31 +00:00
|
|
|
A
|
1997-03-22 23:50:21 +00:00
|
|
|
.Fa type
|
1997-10-11 08:11:31 +00:00
|
|
|
is defined using the
|
|
|
|
.Va malloc_type_t
|
1999-09-29 16:49:18 +00:00
|
|
|
typedef via the
|
|
|
|
.Fn MALLOC_DECLARE
|
|
|
|
and
|
|
|
|
.Fn MALLOC_DEFINE
|
|
|
|
macros.
|
1997-10-11 08:11:31 +00:00
|
|
|
.Bd -literal -offset indent
|
|
|
|
/* sys/something/foo_extern.h */
|
|
|
|
|
1999-09-29 16:49:18 +00:00
|
|
|
MALLOC_DECLARE(M_FOOBUF);
|
1997-10-11 08:11:31 +00:00
|
|
|
|
|
|
|
/* sys/something/foo_main.c */
|
|
|
|
|
1999-09-29 16:49:18 +00:00
|
|
|
MALLOC_DEFINE(M_FOOBUF, "foobuffers", "Buffers to foo data into the ether");
|
1997-10-11 08:11:31 +00:00
|
|
|
|
|
|
|
/* sys/something/foo_subr.c */
|
|
|
|
|
2000-12-29 09:18:45 +00:00
|
|
|
\&...
|
1998-07-02 05:37:33 +00:00
|
|
|
MALLOC(buf, struct foo_buf *, sizeof *buf, M_FOOBUF, M_NOWAIT);
|
1997-10-11 08:11:31 +00:00
|
|
|
|
1998-12-23 01:05:06 +00:00
|
|
|
.Ed
|
1997-03-22 23:50:21 +00:00
|
|
|
.Sh RETURN VALUES
|
2002-03-13 01:42:33 +00:00
|
|
|
.Fn malloc ,
|
|
|
|
.Fn realloc ,
|
|
|
|
and
|
2002-03-18 10:52:09 +00:00
|
|
|
.Fn reallocf
|
2002-03-13 01:42:33 +00:00
|
|
|
return a kernel virtual address that is suitably aligned for storage of
|
1998-12-23 01:05:06 +00:00
|
|
|
any type of object, or
|
|
|
|
.Dv NULL
|
2002-03-13 01:42:33 +00:00
|
|
|
if the request could not be satisfied (implying that
|
2001-07-14 19:41:16 +00:00
|
|
|
.Dv M_NOWAIT
|
2002-03-13 01:42:33 +00:00
|
|
|
was set).
|
|
|
|
.Sh IMPLEMENTATION NOTES
|
|
|
|
The memory allocator allocates memory in chunks that have size a power
|
|
|
|
of two for requests up to the size of a page of memory.
|
|
|
|
For larger requests, one or more pages is allocated.
|
|
|
|
While it should not be relied upon, this information may be useful for
|
|
|
|
optimizing the efficiency of memory use.
|
1997-03-22 23:50:21 +00:00
|
|
|
.Sh SEE ALSO
|
|
|
|
.Xr vmstat 8
|
|
|
|
.Sh DIAGNOSTICS
|
|
|
|
A kernel compiled with the
|
|
|
|
.Dv DIAGNOSTIC
|
1999-02-12 02:12:08 +00:00
|
|
|
configuration option attempts to detect memory corruption caused by
|
1997-03-22 23:50:21 +00:00
|
|
|
such things as writing outside the allocated area and imbalanced calls to the
|
|
|
|
.Fn malloc
|
|
|
|
and
|
|
|
|
.Fn free
|
2000-03-03 14:05:08 +00:00
|
|
|
functions.
|
|
|
|
Failing consistency checks will cause a panic or a system console
|
1997-03-22 23:50:21 +00:00
|
|
|
message:
|
|
|
|
.Bl -bullet -offset indent -compact
|
|
|
|
.Pp
|
|
|
|
.It
|
|
|
|
panic:
|
|
|
|
.Dq malloc: bogus type
|
|
|
|
.It
|
|
|
|
panic:
|
|
|
|
.Dq malloc: allocation too large
|
|
|
|
.It
|
|
|
|
panic:
|
|
|
|
.Dq malloc: wrong bucket
|
|
|
|
.It
|
|
|
|
panic:
|
|
|
|
.Dq malloc: lost data
|
|
|
|
.It
|
|
|
|
panic:
|
|
|
|
.Dq free: address 0x%x out of range
|
|
|
|
.It
|
|
|
|
panic:
|
|
|
|
.Dq free: type %d out of range
|
|
|
|
.It
|
|
|
|
panic:
|
|
|
|
.Dq free: unaligned addr Aq description of object
|
|
|
|
.It
|
|
|
|
panic:
|
|
|
|
.Dq free: item modified
|
|
|
|
.It
|
|
|
|
panic:
|
|
|
|
.Dq free: multiple free[s]
|
|
|
|
.It
|
|
|
|
.Dq Data modified on freelist: Aq description of object
|
|
|
|
.El
|