2 .\" Copyright (c) 1996 The NetBSD Foundation, Inc.
3 .\" All rights reserved.
5 .\" This code is derived from software contributed to The NetBSD Foundation
6 .\" by Paul Kranenburg.
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
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.
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.
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.13.2.6 2002/03/16 02:20:28 archie Exp $
38 .\" $DragonFly: src/share/man/man9/Attic/malloc.9,v 1.2 2003/06/17 04:37:01 dillon Exp $
48 .Nd kernel memory management routines
53 .Fn malloc "unsigned long size" "struct malloc_type *type" "int flags"
54 .Fn MALLOC "space" "cast" "unsigned long size" "struct malloc_type *type" "int flags"
56 .Fn free "void *addr" "struct malloc_type *type"
57 .Fn FREE "void *addr" "struct malloc_type *type"
59 .Fn realloc "void *addr" "unsigned long size" "struct malloc_type *type" "int flags"
61 .Fn reallocf "void *addr" "unsigned long size" "struct malloc_type *type" "int flags"
65 function allocates uninitialized memory in kernel address space for an
66 object whose size is specified by
70 releases memory at address
72 that was previously allocated by
75 The memory is not zeroed.
86 function changes the size of the previously allocated memory referenced by
91 The contents of the memory are unchanged up to the lesser of the new and
93 Note that the returned value may differ from
95 If the requested memory cannot be allocated,
97 is returned and the memory referenced by
99 is valid and unchanged.
106 function behaves identically to
108 for the specified size.
112 function call is identical to the realloc function call, except that it
113 will free the passed pointer when the requested memory cannot be allocated.
117 macro variant is functionally equivalent to
118 .Bd -literal -offset indent
119 (space) = (cast)malloc((u_long)(size), type, flags)
124 macro variant is equivalent to
125 .Bd -literal -offset indent
129 Unlike its standard C library counterpart
131 the kernel version takes two more arguments. The
133 argument further qualifies
135 operational characteristics as follows:
136 .Bl -tag -width indent
145 if the request cannot be immediately fulfilled due to resource shortage.
146 Otherwise, the current process may be put to sleep to wait for
147 resources to be released by other processes.
155 is defined to be 0, meaning that blocking operation is the default.
158 is required when running in an interrupt context.
167 if the request cannot be immediately fulfilled due to a resource shortage.
168 M_ASLEEP is not useful alone and should always be or'd with M_NOWAIT to allow
173 immediately. It is expected that the caller will at some point call
175 and then retry the allocation. Depending on the routine in question, the
176 caller may decide to propagate the temporary failure up the call chain
177 and actually have some other higher level routine block on the async wait
178 that the function queued.
180 Indicates that it is Ok to wait for resources. It is unconveniently
181 defined as 0 so care should be taken never to compare against this value
182 directly or try to AND it as a flag. The default operation is to block
183 until the memory allocation succeeds.
194 Indicates that the system can dig into its reserve in order to obtain the
195 requested memory. This option used to be called M_KERNEL but has been
196 renamed to something more obvious. This option has been deprecated and is
197 slowly being removed from the kernel, and so should not be used with any new
203 argument is used to perform statistics on memory usage, and for
205 The statistics can be examined by
217 .Bd -literal -offset indent
218 /* sys/something/foo_extern.h */
220 MALLOC_DECLARE(M_FOOBUF);
222 /* sys/something/foo_main.c */
224 MALLOC_DEFINE(M_FOOBUF, "foobuffers", "Buffers to foo data into the ether");
226 /* sys/something/foo_subr.c */
229 MALLOC(buf, struct foo_buf *, sizeof *buf, M_FOOBUF, M_NOWAIT);
237 return a kernel virtual address that is suitably aligned for storage of
238 any type of object, or
240 if the request could not be satisfied (implying that
245 was set and the function returns
250 .Sh IMPLEMENTATION NOTES
251 The memory allocator allocates memory in chunks that have size a power
252 of two for requests up to the size of a page of memory.
253 For larger requests, one or more pages is allocated.
254 While it should not be relied upon, this information may be useful for
255 optimizing the efficiency of memory use.
259 A kernel compiled with the
261 configuration option attempts to detect memory corruption caused by
262 such things as writing outside the allocated area and imbalanced calls to the
267 Failing consistency checks will cause a panic or a system console
269 .Bl -bullet -offset indent -compact
273 .Dq malloc: bogus type
276 .Dq malloc: allocation too large
279 .Dq malloc: wrong bucket
282 .Dq malloc: lost data
285 .Dq free: address 0x%x out of range
288 .Dq free: type %d out of range
291 .Dq free: unaligned addr Aq description of object
294 .Dq free: item modified
297 .Dq free: multiple free[s]
299 .Dq Data modified on freelist: Aq description of object