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