xref: /freebsd/share/man/man9/malloc.9 (revision 6990ffd8a95caaba6858ad44ff1b3157d1efba8f)
1.\"
2.\" Copyright (c) 1996 The NetBSD Foundation, Inc.
3.\" All rights reserved.
4.\"
5.\" This code is derived from software contributed to The NetBSD Foundation
6.\" by Paul Kranenburg.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. All advertising materials mentioning features or use of this software
17.\"    must display the following acknowledgement:
18.\"        This product includes software developed by the NetBSD
19.\"        Foundation, Inc. and its contributors.
20.\" 4. Neither the name of The NetBSD Foundation nor the names of its
21.\"    contributors may be used to endorse or promote products derived
22.\"    from this software without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
28.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34.\" POSSIBILITY OF SUCH DAMAGE.
35.\"
36.\" $NetBSD: malloc.9,v 1.3 1996/11/11 00:05:11 lukem Exp $
37.\" $FreeBSD$
38.\"
39.Dd June 16, 1996
40.Dt MALLOC 9
41.Os
42.Sh NAME
43.Nm malloc ,
44.Nm MALLOC ,
45.Nm free ,
46.Nm FREE
47.Nd kernel memory management routines
48.Sh SYNOPSIS
49.Fd #include <sys/types.h>
50.Fd #include <sys/malloc.h>
51.Ft void *
52.Fn malloc "unsigned long size" "struct malloc_type *type" "int flags"
53.Fn MALLOC "space" "cast" "unsigned long size" "struct malloc_type  *type" "int flags"
54.Ft void
55.Fn free "void *addr" "struct malloc_type *type"
56.Fn FREE "void *addr" "struct malloc_type *type"
57.Sh DESCRIPTION
58The
59.Fn malloc
60function allocates uninitialized memory in kernel address space for an
61object whose size is specified by
62.Fa size .
63.Fn free
64releases memory at address
65.Fa addr
66that was previously allocated by
67.Fn malloc
68for re-use.  The memory is not zeroed.
69The
70.Fn MALLOC
71macro variant is functionally equivalent to
72.Bd -literal -offset indent
73(space) = (cast)malloc((u_long)(size), type, flags)
74.Ed
75.Pp
76and the
77.Fn FREE
78macro variant is equivalent to
79.Bd -literal -offset indent
80free((addr), type)
81.Ed
82.Pp
83Unlike its standard C library counterpart
84.Pq Xr malloc 3 ,
85the kernel version takes two more arguments.  The
86.Fa flags
87argument further qualifies
88.Fn malloc Ns 's
89operational characteristics as follows:
90.Bl -tag -width indent
91.It Dv M_ZERO
92Causes the allocated memory to be set to all zeros.
93.It Dv M_NOWAIT
94Causes
95.Fn malloc
96to return
97.Dv NULL
98if the request cannot be immediately fulfilled due to resource shortage.
99Otherwise,
100.Fn malloc
101may call sleep to wait for resources to be released by other processes.
102If this flag is set,
103.Fn malloc
104will return
105.Dv NULL
106rather then block.  Note that
107.Dv M_WAITOK
108is defined to be 0, meaning that blocking operation is the default.
109.It Dv M_WAITOK
110Indicates that it is Ok to wait for resources.  It is unconveniently
111defined as 0 so care should be taken never to compare against this value
112directly or try to AND it as a flag.  The default operation is to block
113until the memory allocation succeeds.
114.Fn malloc
115can only return
116.Dv NULL
117if
118.Dv M_NOWAIT
119is specified.
120.It Dv M_USE_RESERVE
121Indicates that the system can dig into its reserve in order to obtain the
122requested memory.  This option used to be called M_KERNEL but has been
123renamed to something more obvious.  This option has been deprecated and is
124slowly being removed from the kernel, and so should not be used with any new
125programming.
126.El
127.Pp
128The
129.Fa type
130argument is used to perform statistics on memory usage, and for
131basic sanity checks.
132The statistics can be examined by
133.Sq vmstat -m .
134.Pp
135A
136.Fa type
137is defined using the
138.Va malloc_type_t
139typedef via the
140.Fn MALLOC_DECLARE
141and
142.Fn MALLOC_DEFINE
143macros.
144.Bd -literal -offset indent
145/* sys/something/foo_extern.h */
146
147MALLOC_DECLARE(M_FOOBUF);
148
149/* sys/something/foo_main.c */
150
151MALLOC_DEFINE(M_FOOBUF, "foobuffers", "Buffers to foo data into the ether");
152
153/* sys/something/foo_subr.c */
154
155\&...
156MALLOC(buf, struct foo_buf *, sizeof *buf, M_FOOBUF, M_NOWAIT);
157
158.Ed
159.Sh RETURN VALUES
160.Fn malloc
161returns a kernel virtual address that is suitably aligned for storage of
162any type of object, or
163.Dv NULL
164if the request could not be satisfied and
165.Dv M_NOWAIT
166was set.
167.Sh SEE ALSO
168.Xr vmstat 8
169.Sh DIAGNOSTICS
170A kernel compiled with the
171.Dv DIAGNOSTIC
172configuration option attempts to detect memory corruption caused by
173such things as writing outside the allocated area and imbalanced calls to the
174.Fn malloc
175and
176.Fn free
177functions.
178Failing consistency checks will cause a panic or a system console
179message:
180.Bl -bullet -offset indent -compact
181.Pp
182.It
183panic:
184.Dq malloc: bogus type
185.It
186panic:
187.Dq malloc: allocation too large
188.It
189panic:
190.Dq malloc: wrong bucket
191.It
192panic:
193.Dq malloc: lost data
194.It
195panic:
196.Dq free: address 0x%x out of range
197.It
198panic:
199.Dq free: type %d out of range
200.It
201panic:
202.Dq free: unaligned addr Aq description of object
203.It
204panic:
205.Dq free: item modified
206.It
207panic:
208.Dq free: multiple free[s]
209.It
210.Dq Data modified on freelist: Aq description of object
211.El
212