xref: /freebsd/share/man/man9/malloc.9 (revision ee2ea5ceafed78a5bd9810beb9e3ca927180c226)
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.In sys/types.h
50.In 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.Ft void *
58.Fn realloc "void *addr" "unsigned long size" "struct malloc_type *type" "int flags"
59.Ft void *
60.Fn reallocf "void *addr" "unsigned long size" "struct malloc_type *type" "int flags"
61.Sh DESCRIPTION
62The
63.Fn malloc
64function allocates uninitialized memory in kernel address space for an
65object whose size is specified by
66.Fa size .
67.Pp
68.Fn free
69releases memory at address
70.Fa addr
71that was previously allocated by
72.Fn malloc
73for re-use.
74The memory is not zeroed.
75If
76.Fa addr
77is
78.Dv NULL ,
79then
80.Fn free
81does nothing.
82.Pp
83The
84.Fn realloc
85function changes the size of the previously allocated memory referenced by
86.Fa addr
87to
88.Fa size
89bytes.
90The contents of the memory are unchanged up to the lesser of the new and
91old sizes.
92Note that the returned value may differ from
93.Fa addr .
94If the requested memory cannot be allocated,
95.Dv NULL
96is returned and the memory referenced by
97.Fa addr
98is valid and unchanged.
99If
100.Fa addr
101is
102.Dv NULL ,
103the
104.Fn realloc
105function behaves identically to
106.Fn malloc
107for the specified size.
108.Pp
109The
110.Fn reallocf
111function is identical to
112.Fn realloc
113except that it
114will free the passed pointer when the requested memory cannot be allocated.
115.Pp
116The
117.Fn MALLOC
118macro variant is functionally equivalent to
119.Bd -literal -offset indent
120(space) = (cast)malloc((u_long)(size), type, flags)
121.Ed
122.Pp
123and the
124.Fn FREE
125macro variant is equivalent to
126.Bd -literal -offset indent
127free((addr), type)
128.Ed
129.Pp
130Unlike its standard C library counterpart
131.Pq Xr malloc 3 ,
132the kernel version takes two more arguments.  The
133.Fa flags
134argument further qualifies
135.Fn malloc Ns 's
136operational characteristics as follows:
137.Bl -tag -width indent
138.It Dv M_ZERO
139Causes the allocated memory to be set to all zeros.
140.It Dv M_NOWAIT
141Causes
142.Fn malloc ,
143.Fn realloc ,
144and
145.Fn reallocf
146to return
147.Dv NULL
148if the request cannot be immediately fulfilled due to resource shortage.
149Otherwise, the current process may be put to sleep to wait for
150resources to be released by other processes.
151If this flag is set,
152.Fn malloc
153will return
154.Dv NULL
155rather then block.
156Note that
157.Dv M_WAITOK
158is defined to be 0, meaning that blocking operation is the default.
159Also note that
160.Dv M_NOWAIT
161is required when running in an interrupt context.
162.It Dv M_WAITOK
163Indicates that it is Ok to wait for resources.  It is unconveniently
164defined as 0 so care should be taken never to compare against this value
165directly or try to AND it as a flag.  The default operation is to block
166until the memory allocation succeeds.
167.Fn malloc ,
168.Fn realloc ,
169and
170.Fn reallocf
171can only return
172.Dv NULL
173if
174.Dv M_NOWAIT
175is specified.
176.It Dv M_USE_RESERVE
177Indicates that the system can dig into its reserve in order to obtain the
178requested memory.  This option used to be called M_KERNEL but has been
179renamed to something more obvious.  This option has been deprecated and is
180slowly being removed from the kernel, and so should not be used with any new
181programming.
182.El
183.Pp
184The
185.Fa type
186argument is used to perform statistics on memory usage, and for
187basic sanity checks.
188The statistics can be examined by
189.Sq vmstat -m .
190.Pp
191A
192.Fa type
193is defined using the
194.Va malloc_type_t
195typedef via the
196.Fn MALLOC_DECLARE
197and
198.Fn MALLOC_DEFINE
199macros.
200.Bd -literal -offset indent
201/* sys/something/foo_extern.h */
202
203MALLOC_DECLARE(M_FOOBUF);
204
205/* sys/something/foo_main.c */
206
207MALLOC_DEFINE(M_FOOBUF, "foobuffers", "Buffers to foo data into the ether");
208
209/* sys/something/foo_subr.c */
210
211\&...
212MALLOC(buf, struct foo_buf *, sizeof *buf, M_FOOBUF, M_NOWAIT);
213
214.Ed
215.Sh RETURN VALUES
216.Fn malloc ,
217.Fn realloc ,
218and
219.Fn reallocf
220return a kernel virtual address that is suitably aligned for storage of
221any type of object, or
222.Dv NULL
223if the request could not be satisfied (implying that
224.Dv M_NOWAIT
225was set).
226.Sh IMPLEMENTATION NOTES
227The memory allocator allocates memory in chunks that have size a power
228of two for requests up to the size of a page of memory.
229For larger requests, one or more pages is allocated.
230While it should not be relied upon, this information may be useful for
231optimizing the efficiency of memory use.
232.Sh SEE ALSO
233.Xr vmstat 8
234.Sh DIAGNOSTICS
235A kernel compiled with the
236.Dv DIAGNOSTIC
237configuration option attempts to detect memory corruption caused by
238such things as writing outside the allocated area and imbalanced calls to the
239.Fn malloc
240and
241.Fn free
242functions.
243Failing consistency checks will cause a panic or a system console
244message:
245.Bl -bullet -offset indent -compact
246.Pp
247.It
248panic:
249.Dq malloc: bogus type
250.It
251panic:
252.Dq malloc: allocation too large
253.It
254panic:
255.Dq malloc: wrong bucket
256.It
257panic:
258.Dq malloc: lost data
259.It
260panic:
261.Dq free: address 0x%x out of range
262.It
263panic:
264.Dq free: type %d out of range
265.It
266panic:
267.Dq free: unaligned addr Aq description of object
268.It
269panic:
270.Dq free: item modified
271.It
272panic:
273.Dq free: multiple free[s]
274.It
275.Dq Data modified on freelist: Aq description of object
276.El
277