NetBSD/share/man/man9/malloc.9

349 lines
8.8 KiB
Groff
Raw Normal View History

.\" $NetBSD: malloc.9,v 1.44 2008/12/19 13:41:03 cegger Exp $
1996-06-17 03:20:53 +04:00
.\"
2003-02-01 09:27:23 +03:00
.\" Copyright (c) 1996, 2003 The NetBSD Foundation, Inc.
1996-06-17 03:20:53 +04:00
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
2003-02-01 09:27:23 +03:00
.\" by Paul Kranenburg, and by Jason R. Thorpe.
1996-06-17 03:20:53 +04:00
.\"
.\" 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.
.\"
.\" 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 FOUNDATION OR CONTRIBUTORS
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1996-06-17 03:20:53 +04:00
.\" 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.
.\"
.Dd December 19, 2008
1996-06-17 03:20:53 +04:00
.Dt MALLOC 9
.Os
1996-06-17 03:20:53 +04:00
.Sh NAME
1997-11-11 13:06:37 +03:00
.Nm malloc ,
2003-02-01 09:27:23 +03:00
.Nm MALLOC ,
.Nm realloc ,
.Nm free ,
.Nm FREE ,
.Nm malloc_roundup ,
.Nm malloc_type_attach ,
.Nm malloc_type_detach ,
.Nm malloc_type_setlimit ,
.Nm MALLOC_DEFINE_LIMIT ,
.Nm MALLOC_DEFINE ,
.Nm MALLOC_DECLARE
.Nd general-purpose kernel memory allocator
1996-06-17 03:20:53 +04:00
.Sh SYNOPSIS
.In sys/malloc.h
1996-06-17 03:20:53 +04:00
.Ft void *
2003-02-01 09:27:23 +03:00
.Fn malloc "unsigned long size" "struct malloc_type *type" "int flags"
.Ft void *
.Fn realloc "void *addr" "unsigned long newsize" "struct malloc_type *type" \
"int flags"
1996-06-17 03:20:53 +04:00
.Ft void
.Fn free "void *addr" "struct malloc_type *type"
2003-02-01 09:27:23 +03:00
.Ft unsigned long
.Fn malloc_roundup "unsigned long size"
.Ft void
.Fn malloc_type_attach "struct malloc_type *type"
.Ft void
.Fn malloc_type_detach "struct malloc_type *type"
.Ft void
.Fn malloc_type_setlimit "struct malloc_type *type" "unsigned long limit"
.In sys/mallocvar.h
2003-02-01 09:27:23 +03:00
.Fn MALLOC_DEFINE_LIMIT "type" "shortdesc" "longdesc" "limit"
.Fn MALLOC_JUSTDEFINE_LIMIT "type" "shortdesc" "longdesc" "limit"
2003-02-01 09:27:23 +03:00
.Fn MALLOC_DEFINE "type" "shortdesc" "longdesc"
.Fn MALLOC_JUSTDEFINE "type" "shortdesc" "longdesc"
2003-02-01 09:27:23 +03:00
.Fn MALLOC_DECLARE "type"
1996-06-17 03:20:53 +04:00
.Sh DESCRIPTION
2008-03-24 11:24:48 +03:00
.Bf -symbolic
These interfaces are being obsoleted and their new use is discouraged.
For new code, please consider to use
.Xr kmem_alloc 9
or
.Xr pool_cache 9
instead.
.Ef
.Pp
1996-06-17 03:20:53 +04:00
The
.Fn malloc
function allocates uninitialized memory in kernel address space for an
object whose size is specified by
.Fa size .
2001-12-05 15:44:24 +03:00
.Fn malloc_roundup
returns the actual size of the allocation unit for the given value.
1996-06-17 03:20:53 +04:00
.Fn free
releases memory at address
.Fa addr
that was previously allocated by
.Fn malloc
for re-use.
Unlike
.Xr free 3 ,
.Fn free
does not accept an
.Fa addr
argument that is
.Dv NULL .
2003-02-01 09:27:23 +03:00
.Pp
The
.Fn realloc
function changes the size of the previously allocated memory referenced
by
.Fa addr
to
.Fa size
and returns a pointer to the
.Pq possibly moved
2003-02-01 15:52:49 +03:00
object.
The memory contents are unchanged up to the lesser of the new
2003-02-01 09:27:23 +03:00
and old sizes.
If the new size is larger, the newly allocated memory is
uninitialized.
If the requested memory cannot be allocated,
.Dv NULL
is returned and the memory referenced by
.Fa addr
is unchanged.
If
.Fa addr
is
.Dv NULL ,
then
.Fn realloc
behaves exactly as
.Fn malloc .
If the new size is 0, then
.Fn realloc
behaves exactly as
.Fn free .
.Pp
1996-06-17 03:20:53 +04:00
Unlike its standard C library counterpart
.Pq Xr malloc 3 ,
the kernel version takes two more arguments.
.Pp
The
1996-06-17 03:20:53 +04:00
.Fa flags
argument further qualifies
2001-12-04 23:39:02 +03:00
.Fn malloc
operational characteristics as follows:
2001-12-04 23:39:02 +03:00
.Bl -tag -offset indent -width M_CANFAIL
1996-06-17 03:20:53 +04:00
.It Dv M_NOWAIT
Causes
1996-10-30 08:29:54 +03:00
.Fn malloc
to return
1996-06-17 03:20:53 +04:00
.Dv NULL
if the request cannot be immediately fulfilled due to resource shortage.
If this flag is not set
(see
.Dv M_WAITOK ) ,
1996-06-17 03:20:53 +04:00
.Fn malloc
will never return
.Dv NULL .
.It Dv M_WAITOK
By default,
.Fn malloc
may call
.Xr cv_wait 9
to wait for resources to be released by other processes, and this
flag represents this behaviour.
1996-06-17 03:20:53 +04:00
Note that
.Dv M_WAITOK
2001-12-04 23:39:02 +03:00
is conveniently defined to be 0, and hence may be or'ed into the
1996-06-17 03:20:53 +04:00
.Fa flags
2001-12-05 02:42:02 +03:00
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.
2001-12-04 23:39:02 +03:00
.It Dv M_CANFAIL
Changes behaviour for
.Dv M_WAITOK
case - if the requested memory size is bigger than
.Fn malloc
can ever allocate, return failure, rather than calling
2001-12-05 02:42:02 +03:00
.Xr panic 9 .
This is different to M_NOWAIT, since
2001-12-04 23:39:02 +03:00
the call can still wait for resources.
.Pp
Rather than depending on
.Dv M_CANFAIL ,
2002-10-14 17:43:14 +04:00
kernel code should do proper bound checking itself.
This flag should only be used in cases where this is not feasible.
Since it can hide real kernel bugs, its usage is
2001-12-04 23:39:02 +03:00
.Em strongly discouraged .
1996-06-17 03:20:53 +04:00
.El
.Pp
The
.Fa type
2003-02-01 09:27:23 +03:00
argument describes the subsystem and/or use within a subsystem for which
the allocated memory was needed, and is commonly used to maintain statistics
2003-02-01 15:52:49 +03:00
about kernel memory usage and, optionally, enforce limits on this usage for
2003-02-01 09:27:23 +03:00
certain memory types.
.Pp
In addition to some built-in generic types defined by the kernel
memory allocator, subsystems may define their own types.
.Pp
The
.Fn MALLOC_DEFINE_LIMIT
macro defines a malloc type named
.Fa type
with the short description
.Fa shortdesc ,
which must be a constant string; this description will be used for
kernel memory statistics reporting.
The
.Fa longdesc
argument, also a constant string, is intended as way to place a
comment in the actual type definition, and is not currently stored
in the type structure.
The
.Fa limit
argument specifies the maximum amount of memory, in bytes, that this
malloc type can consume.
.Pp
The
.Fn MALLOC_DEFINE
macro is equivalent to the
.Fn MALLOC_DEFINE_LIMIT
macro with a
.Fa limit
argument of 0.
If kernel memory statistics are being gathered, the system will
choose a reasonable default limit for the malloc type.
.Pp
The
.Fn MALLOC_DECLARE
macro is intended for use in header files which are included by
code which needs to use the malloc type, providing the necessary
extern declaration.
.Pp
Code which includes
\*[Lt]sys/malloc.h\*[Gt]
does not need to include
\*[Lt]sys/mallocvar.h\*[Gt]
to get these macro definitions.
The
\*[Lt]sys/mallocvar.h\*[Gt]
header file is intended for other header files which need to use the
.Fn MALLOC_DECLARE
macro.
.Pp
The
.Fn malloc_type_attach
function attaches the malloc type
.Fa type
to the kernel memory allocator.
.Pp
The
.Fn malloc_type_detach
function detaches the malloc type
.Fa type
previously attached with
.Fn malloc_type_attach .
.Pp
The
.Fn malloc_type_setlimit
function sets the memory limit of the malloc type
.Fa type
to
.Fa limit
bytes.
The type must already be registered with the kernel memory allocator.
.Pp
The following generic malloc types are currently defined:
1996-06-17 03:20:53 +04:00
.Pp
.Bl -tag -offset indent -width XXXXXXXXXXXXXX -compact
.It Dv M_DEVBUF
Device driver memory.
1998-07-30 02:07:14 +04:00
.It Dv M_DMAMAP
.Xr bus_dma 9
structures.
2003-02-01 09:27:23 +03:00
.It Dv M_FREE
Should be on free list.
.It Dv M_PCB
Protocol control block.
.It Dv M_SOFTINTR
Softinterrupt structures.
2003-02-01 09:27:23 +03:00
.It Dv M_TEMP
Misc temporary data buffers.
1996-06-17 03:20:53 +04:00
.El
.Pp
2003-02-01 09:27:23 +03:00
Other malloc types are defined by the corresponding subsystem; see the
documentation for that subsystem for information its available malloc
types.
.Pp
1996-06-17 03:20:53 +04:00
Statistics based on the
.Fa type
2003-02-01 09:27:23 +03:00
argument are maintained only if the kernel option
1996-06-17 03:20:53 +04:00
.Dv KMEMSTATS
is used when compiling the kernel
2000-12-24 05:52:53 +03:00
.Po
the default in current
1996-06-17 03:20:53 +04:00
.Nx
kernels
.Pc
and can be examined by using
.Sq vmstat -m .
.Sh RETURN VALUES
.Fn malloc
returns a kernel virtual address that is suitably aligned for storage of
any type of object.
.Sh DIAGNOSTICS
A kernel compiled with the
.Dv DIAGNOSTIC
1999-11-18 21:12:52 +03:00
configuration option attempts to detect memory corruption caused by
1996-06-17 03:20:53 +04:00
such things as writing outside the allocated area and imbalanced calls to the
.Fn malloc
and
.Fn free
functions.
Failing consistency checks will cause a panic or a system console message:
1996-06-17 03:20:53 +04:00
.Bl -bullet -offset indent -compact
.Pp
.It
panic:
.Dq malloc - bogus type
.It
panic:
.Dq malloc: out of space in kmem_map
.It
panic:
.Dq malloc: allocation too large
.It
panic:
.Dq malloc: wrong bucket
.It
panic:
.Dq malloc: lost data
.It
panic:
.Dq free: unaligned addr
.It
panic:
.Dq free: duplicated free
.It
panic:
.Dq free: multiple frees
.It
panic:
.Dq init: minbucket too small/struct freelist too big
.It
.Dq multiply freed item Aq addr
.It
.Dq Data modified on freelist: Aq data object description
.El
2001-11-19 02:00:10 +03:00
.Sh SEE ALSO
2007-02-17 11:50:04 +03:00
.Xr vmstat 1 ,
.Xr memoryallocators 9