xref: /freebsd/share/man/man9/malloc.9 (revision 77b7cdf1999ee965ad494fddd184b18f532ac91a)
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 April 9, 2003
40.Dt MALLOC 9
41.Os
42.Sh NAME
43.Nm malloc ,
44.Nm MALLOC ,
45.Nm free ,
46.Nm FREE ,
47.Nm realloc ,
48.Nm reallocf ,
49.Nm MALLOC_DEFINE ,
50.Nm MALLOC_DECLARE
51.Nd kernel memory management routines
52.Sh SYNOPSIS
53.In sys/types.h
54.In sys/malloc.h
55.Ft void *
56.Fn malloc "unsigned long size" "struct malloc_type *type" "int flags"
57.Fn MALLOC "space" "cast" "unsigned long size" "struct malloc_type  *type" "int flags"
58.Ft void
59.Fn free "void *addr" "struct malloc_type *type"
60.Fn FREE "void *addr" "struct malloc_type *type"
61.Ft void *
62.Fn realloc "void *addr" "unsigned long size" "struct malloc_type *type" "int flags"
63.Ft void *
64.Fn reallocf "void *addr" "unsigned long size" "struct malloc_type *type" "int flags"
65.Fn MALLOC_DECLARE "type"
66.In sys/param.h
67.In sys/malloc.h
68.In sys/kernel.h
69.Fn MALLOC_DEFINE "type" "shortdesc" "longdesc"
70.Sh DESCRIPTION
71The
72.Fn malloc
73function allocates uninitialized memory in kernel address space for an
74object whose size is specified by
75.Fa size .
76.Pp
77The
78.Fn free
79function releases memory at address
80.Fa addr
81that was previously allocated by
82.Fn malloc
83for re-use.
84The memory is not zeroed.
85If
86.Fa addr
87is
88.Dv NULL ,
89then
90.Fn free
91does nothing.
92.Pp
93The
94.Fn realloc
95function changes the size of the previously allocated memory referenced by
96.Fa addr
97to
98.Fa size
99bytes.
100The contents of the memory are unchanged up to the lesser of the new and
101old sizes.
102Note that the returned value may differ from
103.Fa addr .
104If the requested memory cannot be allocated,
105.Dv NULL
106is returned and the memory referenced by
107.Fa addr
108is valid and unchanged.
109If
110.Fa addr
111is
112.Dv NULL ,
113the
114.Fn realloc
115function behaves identically to
116.Fn malloc
117for the specified size.
118.Pp
119The
120.Fn reallocf
121function is identical to
122.Fn realloc
123except that it
124will free the passed pointer when the requested memory cannot be allocated.
125.Pp
126The
127.Fn MALLOC
128macro variant is functionally equivalent to
129.Bd -literal -offset indent
130(space) = (cast)malloc((u_long)(size), type, flags)
131.Ed
132.Pp
133and the
134.Fn FREE
135macro variant is equivalent to
136.Bd -literal -offset indent
137free((addr), type)
138.Ed
139.Pp
140Unlike its standard C library counterpart
141.Pq Xr malloc 3 ,
142the kernel version takes two more arguments.  The
143.Fa flags
144argument further qualifies
145.Fn malloc Ns 's
146operational characteristics as follows:
147.Bl -tag -width indent
148.It Dv M_ZERO
149Causes the allocated memory to be set to all zeros.
150.It Dv M_NOWAIT
151Causes
152.Fn malloc ,
153.Fn realloc ,
154and
155.Fn reallocf
156to return
157.Dv NULL
158if the request cannot be immediately fulfilled due to resource shortage.
159Note 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.
164If the request cannot be immediately fulfilled the current process is put
165to sleep to wait for resources to be released by other processes.
166The
167.Fn malloc ,
168.Fn realloc ,
169and
170.Fn reallocf
171functions cannot return
172.Dv NULL
173if
174.Dv M_WAITOK
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
184Exactly one of either
185.Dv M_WAITOK
186or
187.Dv M_NOWAIT
188must be specified.
189.Pp
190The
191.Fa type
192argument is used to perform statistics on memory usage, and for
193basic sanity checks.
194The statistics can be examined by
195.Sq vmstat -m .
196.Pp
197A
198.Fa type
199is defined using
200.Va struct malloc_type
201via the
202.Fn MALLOC_DECLARE
203and
204.Fn MALLOC_DEFINE
205macros.
206.Bd -literal -offset indent
207/* sys/something/foo_extern.h */
208
209MALLOC_DECLARE(M_FOOBUF);
210
211/* sys/something/foo_main.c */
212
213MALLOC_DEFINE(M_FOOBUF, "foobuffers", "Buffers to foo data into the ether");
214
215/* sys/something/foo_subr.c */
216
217\&...
218MALLOC(buf, struct foo_buf *, sizeof *buf, M_FOOBUF, M_NOWAIT);
219
220.Ed
221.Pp
222In order to use
223.Fn MALLOC_DEFINE
224one must include
225.Aq sys/param.h
226(instead of
227.Aq sys/types.h )
228and
229.Aq sys/kernel.h .
230.Sh RETURN VALUES
231The
232.Fn malloc ,
233.Fn realloc ,
234and
235.Fn reallocf
236functions return a kernel virtual address that is suitably aligned for
237storage of any type of object, or
238.Dv NULL
239if the request could not be satisfied (implying that
240.Dv M_NOWAIT
241was set).
242.Sh IMPLEMENTATION NOTES
243The memory allocator allocates memory in chunks that have size a power
244of two for requests up to the size of a page of memory.
245For larger requests, one or more pages is allocated.
246While it should not be relied upon, this information may be useful for
247optimizing the efficiency of memory use.
248.Pp
249Programmers should be careful not to confuse the malloc flags
250.Dv M_NOWAIT
251and
252.Dv M_WAITOK
253with the
254.Xr mbuf 9
255flags
256.Dv M_DONTWAIT
257and
258.Dv M_TRYWAIT .
259.Sh LOCKING CONSIDERATIONS
260.Fn malloc ,
261.Fn realloc
262and
263.Fn reallocf
264may not be called from fast interrupts handlers.
265When called from threaded interrupts
266.Ar flag
267must contain
268.Dv M_NOWAIT .
269.Pp
270.Fn malloc ,
271.Fn realloc
272and
273.Fn reallocf
274must not be called with
275.Dv M_WAITOK
276while a mutex other than Giant is held.
277Giant may or may not be held when
278.Fn free
279is called.
280.Pp
281Any calls to
282.Fn malloc
283(even with
284.Dv M_NOWAIT )
285or
286.Fn free
287when holding a
288.Xr vnode 9
289interlock, will cause a LOR (Lock Order Reversal) due to the
290interwining of VM Objects and Vnodes.
291.Sh SEE ALSO
292.Xr vmstat 8 ,
293.Xr vnode 9
294.Sh DIAGNOSTICS
295A kernel compiled with the
296.Dv INVARIANTS
297configuration option attempts to detect memory corruption caused by
298such things as writing outside the allocated area and imbalanced calls to the
299.Fn malloc
300and
301.Fn free
302functions.
303Failing consistency checks will cause a panic or a system console
304message.
305