[BACK]Return to malloc.9 CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / share / man / man9

File: [cvs.NetBSD.org] / src / share / man / man9 / malloc.9 (download)

Revision 1.47, Mon Aug 3 19:57:40 2009 UTC (14 years, 8 months ago) by rmind
Branch: MAIN
CVS Tags: yamt-pagecache-tag8, yamt-pagecache-base9, yamt-pagecache-base8, yamt-pagecache-base7, yamt-pagecache-base6, yamt-pagecache-base5, yamt-pagecache-base4, yamt-pagecache-base3, yamt-pagecache-base2, yamt-pagecache-base, yamt-pagecache, uebayasi-xip-base7, uebayasi-xip-base6, uebayasi-xip-base5, uebayasi-xip-base4, uebayasi-xip-base3, uebayasi-xip-base2, uebayasi-xip-base1, uebayasi-xip, riastradh-xf86-video-intel-2-7-1-pre-2-21-15, riastradh-drm2-base3, riastradh-drm2-base2, riastradh-drm2-base1, riastradh-drm2-base, riastradh-drm2, netbsd-6-base, netbsd-6-1-RELEASE, netbsd-6-1-RC4, netbsd-6-1-RC3, netbsd-6-1-RC2, netbsd-6-1-RC1, netbsd-6-1-5-RELEASE, netbsd-6-1-4-RELEASE, netbsd-6-1-3-RELEASE, netbsd-6-1-2-RELEASE, netbsd-6-1-1-RELEASE, netbsd-6-1, netbsd-6-0-RELEASE, netbsd-6-0-RC2, netbsd-6-0-RC1, netbsd-6-0-6-RELEASE, netbsd-6-0-5-RELEASE, netbsd-6-0-4-RELEASE, netbsd-6-0-3-RELEASE, netbsd-6-0-2-RELEASE, netbsd-6-0-1-RELEASE, netbsd-6-0, netbsd-6, matt-premerge-20091211, matt-nb6-plus-nbase, matt-nb6-plus-base, matt-nb6-plus, matt-mips64-premerge-20101231, cherry-xenmp-base, cherry-xenmp, bouyer-quota2-nbase, bouyer-quota2-base, bouyer-quota2, agc-symver-base, agc-symver
Branch point for: tls-maxphys, tls-earlyentropy
Changes since 1.46: +2 -2 lines

Mention kmem(9).

.\"	$NetBSD: malloc.9,v 1.47 2009/08/03 19:57:40 rmind Exp $
.\"
.\" Copyright (c) 1996, 2003 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Paul Kranenburg, and by Jason R. Thorpe.
.\"
.\" 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
.\" 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 29, 2008
.Dt MALLOC 9
.Os
.Sh NAME
.Nm malloc ,
.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
.Sh SYNOPSIS
.In sys/malloc.h
.Ft void *
.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"
.Ft void
.Fn free "void *addr" "struct malloc_type *type"
.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
.Fn MALLOC_DEFINE_LIMIT "type" "shortdesc" "longdesc" "limit"
.Fn MALLOC_JUSTDEFINE_LIMIT "type" "shortdesc" "longdesc" "limit"
.Fn MALLOC_DEFINE "type" "shortdesc" "longdesc"
.Fn MALLOC_JUSTDEFINE "type" "shortdesc" "longdesc"
.Fn MALLOC_DECLARE "type"
.Sh DESCRIPTION
.Bf -symbolic
These interfaces are being obsoleted and their new use is discouraged.
For new code, use
.Xr kmem 9
or
.Xr pool_cache 9
instead.
.Ef
.Pp
The
.Fn malloc
function allocates uninitialized memory in kernel address space for an
object whose size is specified by
.Fa size .
.Fn malloc_roundup
returns the actual size of the allocation unit for the given value.
.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 .
.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
object.
The memory contents are unchanged up to the lesser of the new
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
Unlike its standard C library counterpart
.Pq Xr malloc 3 ,
the kernel version takes two more arguments.
.Pp
The
.Fa flags
argument further qualifies
.Fn malloc
operational characteristics as follows:
.Bl -tag -offset indent -width M_CANFAIL
.It Dv M_NOWAIT
Causes
.Fn malloc
to return
.Dv NULL
if the request cannot be immediately fulfilled due to resource shortage.
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 cv_wait 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 may be 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.
.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
.Xr panic 9 .
This is different to M_NOWAIT, since
the call can still wait for resources.
.Pp
Rather than depending on
.Dv M_CANFAIL ,
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
.Em strongly discouraged .
.El
.Pp
The
.Fa type
argument describes the subsystem and/or use within a subsystem for which
the allocated memory was needed, and is commonly used to maintain statistics
about kernel memory usage and, optionally, enforce limits on this usage for
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:
.Pp
.Bl -tag -offset indent -width XXXXXXXXXXXXXX -compact
.It Dv M_DEVBUF
Device driver memory.
.It Dv M_DMAMAP
.Xr bus_dma 9
structures.
.It Dv M_FREE
Should be on free list.
.It Dv M_PCB
Protocol control block.
.It Dv M_SOFTINTR
Softinterrupt structures.
.It Dv M_TEMP
Misc temporary data buffers.
.El
.Pp
Other malloc types are defined by the corresponding subsystem; see the
documentation for that subsystem for information its available malloc
types.
.Pp
Statistics based on the
.Fa type
argument are maintained only if the kernel option
.Dv KMEMSTATS
is used when compiling the kernel
.Po
the default in current
.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
configuration option attempts to detect memory corruption caused by
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:
.Pp
.Bl -bullet -offset indent -compact
.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
.Sh SEE ALSO
.Xr vmstat 1 ,
.Xr memoryallocators 9