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