xref: /dragonfly/share/man/man9/kmalloc.9 (revision 1847e88f)
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: src/share/man/man9/malloc.9,v 1.42 2005/02/22 17:20:20 brueffer Exp $
38.\" $DragonFly: src/share/man/man9/kmalloc.9,v 1.5 2005/12/27 22:45:23 swildner Exp $
39.\"
40.Dd June 12, 2003
41.Dt MALLOC 9
42.Os
43.Sh NAME
44.Nm malloc ,
45.Nm MALLOC ,
46.Nm free ,
47.Nm FREE ,
48.Nm realloc ,
49.Nm reallocf ,
50.Nm MALLOC_DEFINE ,
51.Nm MALLOC_DECLARE
52.Nd kernel memory management routines
53.Sh SYNOPSIS
54.In sys/types.h
55.In sys/malloc.h
56.Ft void *
57.Fn malloc "unsigned long size" "struct malloc_type *type" "int flags"
58.Fn MALLOC space cast "unsigned long size" "struct malloc_type *type" "int flags"
59.Ft void
60.Fn free "void *addr" "struct malloc_type *type"
61.Fn FREE "void *addr" "struct malloc_type *type"
62.Ft void *
63.Fn realloc "void *addr" "unsigned long size" "struct malloc_type *type" "int flags"
64.Ft void *
65.Fn reallocf "void *addr" "unsigned long size" "struct malloc_type *type" "int flags"
66.Fn MALLOC_DECLARE type
67.In sys/param.h
68.In sys/malloc.h
69.In sys/kernel.h
70.Fn MALLOC_DEFINE type shortdesc longdesc
71.Sh DESCRIPTION
72The
73.Fn malloc
74function allocates uninitialized memory in kernel address space for an
75object whose size is specified by
76.Fa size .
77.Pp
78The
79.Fn free
80function releases memory at address
81.Fa addr
82that was previously allocated by
83.Fn malloc
84for re-use.
85The memory is not zeroed.
86The kernel implementation of
87.Fn free
88does not allow
89.Fa addr
90to be
91.Dv NULL .
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.
143The
144.Fa flags
145argument further qualifies
146.Fn malloc Ns 's
147operational characteristics as follows:
148.Bl -tag -width indent
149.It Dv M_ZERO
150Causes the allocated memory to be set to all zeros.
151.It Dv M_NOWAIT
152Causes
153.Fn malloc ,
154.Fn realloc ,
155and
156.Fn reallocf
157to return
158.Dv NULL
159if the request cannot be immediately fulfilled due to resource shortage.
160Note that
161.Dv M_NOWAIT
162is required when running in an interrupt context.
163.It Dv M_WAITOK
164Indicates that it is OK to wait for resources.
165If the request cannot be immediately fulfilled, the current process is put
166to sleep to wait for resources to be released by other processes.
167The
168.Fn malloc ,
169.Fn realloc ,
170and
171.Fn reallocf
172functions cannot return
173.Dv NULL
174if
175.Dv M_WAITOK
176is specified.
177.It Dv M_USE_RESERVE
178Indicates that the system can dig into its reserve in order to obtain the
179requested memory.
180This option used to be called
181.Dv M_KERNEL
182but has been renamed to something more obvious.
183This option has been deprecated and is slowly being removed from the kernel,
184and so should not be used with any new programming.
185.El
186.Pp
187Exactly one of either
188.Dv M_WAITOK
189or
190.Dv M_NOWAIT
191must be specified.
192.Pp
193The
194.Fa type
195argument is used to perform statistics on memory usage, and for
196basic sanity checks.
197It can be used to identify multiple allocations.
198The statistics can be examined by
199.Sq vmstat -m .
200.Pp
201A
202.Fa type
203is defined using the
204.Va malloc_type_t
205typedef via the
206.Fn MALLOC_DECLARE
207and
208.Fn MALLOC_DEFINE
209macros.
210.Bd -literal -offset indent
211/* sys/something/foo_extern.h */
212
213MALLOC_DECLARE(M_FOOBUF);
214
215/* sys/something/foo_main.c */
216
217MALLOC_DEFINE(M_FOOBUF, "foobuffers", "Buffers to foo data into the ether");
218
219/* sys/something/foo_subr.c */
220
221\&...
222MALLOC(buf, struct foo_buf *, sizeof *buf, M_FOOBUF, M_NOWAIT);
223
224.Ed
225.Sh IMPLEMENTATION NOTES
226The memory allocator allocates memory in chunks that have size a power
227of two for requests up to the size of a page of memory.
228For larger requests, one or more pages is allocated.
229While it should not be relied upon, this information may be useful for
230optimizing the efficiency of memory use.
231.Sh RETURN VALUES
232The
233.Fn malloc ,
234.Fn realloc ,
235and
236.Fn reallocf
237functions return a kernel virtual address that is suitably aligned for
238storage of any type of object, or
239.Dv NULL
240if the request could not be satisfied (implying that
241.Dv M_NOWAIT
242was set).
243.Sh DIAGNOSTICS
244A kernel compiled with the
245.Dv INVARIANTS
246configuration option attempts to detect memory corruption caused by
247such things as writing outside the allocated area and imbalanced calls to the
248.Fn malloc
249and
250.Fn free
251functions.
252Failing consistency checks will cause a panic or a system console
253message.
254.Sh SEE ALSO
255.Xr vmstat 8 ,
256.Xr vnode 9
257