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.42 2005/02/22 17:20:20 brueffer Exp $
46 .Nm kmalloc_raise_limit ,
49 .Nd kernel memory management routines
54 .Fn kmalloc "unsigned long size" "struct malloc_type *type" "int flags"
56 .Fn kfree "void *addr" "struct malloc_type *type"
58 .Fn krealloc "void *addr" "unsigned long size" "struct malloc_type *type" "int flags"
60 .Fn kmalloc_raise_limit "struct malloc_type *type" "size_t bytes"
61 .Fn MALLOC_DECLARE type
65 .Fn MALLOC_DEFINE type shortdesc longdesc
69 function allocates uninitialized memory in kernel address space for an
70 object whose size is specified by
75 function releases memory at address
77 that was previously allocated by
80 The memory is not zeroed.
81 The kernel implementation of
90 function changes the size of the previously allocated memory referenced by
95 The contents of the memory are unchanged up to the lesser of the new and
97 Note that the returned value may differ from
99 If the requested memory cannot be allocated,
101 is returned and the memory referenced by
103 is valid and unchanged.
110 function behaves identically to
112 for the specified size.
114 .Fn kmalloc_raise_limit
115 is used to increase the internal pool limit to
117 Under most of the cases
118 the default internal pool limit should be more than enough,
119 so this function is currently rarely used and must be used with care.
121 Unlike its standard C library counterpart
123 the kernel version takes two more arguments.
126 argument further qualifies
128 operational characteristics as follows:
129 .Bl -tag -width indent
131 Causes the allocated memory to be set to all zeros.
139 if the request cannot be immediately fulfilled due to resource shortage.
142 is required when running in an interrupt context.
144 Indicates that it is OK to wait for resources.
145 If the request cannot be immediately fulfilled, the current process is put
146 to sleep to wait for resources to be released by other processes.
147 Before the internal pool limit is reached,
152 functions cannot return
157 If the internal pool limit is reached and
159 is not specified along with
161 the system will panic.
162 If the internal pool limit is reached and
164 is specified along with
172 instead of panicing the system.
176 to dig into the system's reserved free pages looking for enough room to
177 perform the allocation.
178 This is typically used in interrupts where you cannot afford
181 Before the internal pool limit is reached,
186 functions cannot return
191 If the internal pool limit is reached and
193 is not specified along with
195 the system will panic.
196 If the internal pool limit is reached and
198 is specified along with
206 instead of panicing the system.
208 Indicates that the system can dig into its reserve in order to obtain the
211 Rounds up the size to the nearest power of 2.
213 Aligns to the CPU cache line size.
215 This flag is usually specified along with
219 so when the interal pool limit is reached,
223 functions will not panic the system,
227 This flag is usually used on the kernel code path that is triggered by
228 user space programs' requests.
231 Exactly one of either
240 argument is used to perform statistics on memory usage, and for
242 It can be used to identify multiple allocations.
243 The statistics can be examined by
255 .Sh IMPLEMENTATION NOTES
256 The memory allocator allocates memory in chunks that have size a power
257 of two for requests up to the size of a page of memory.
258 For larger requests, one or more pages is allocated.
259 The allocated memory will be at least 8 bytes aligned.
260 While it should not be relied upon, this information may be useful for
261 optimizing the efficiency of memory use.
267 functions return a kernel virtual address that is suitably aligned for
268 storage of any type of object, or
270 if the request could not be satisfied (implying that
277 /* sys/something/foo_extern.h */
278 MALLOC_DECLARE(M_FOOBUF);
280 /* sys/something/foo_main.c */
281 MALLOC_DEFINE(M_FOOBUF, "foobuffers", "Foo data");
283 /* sys/something/foo_subr.c */
285 buf = kmalloc(sizeof *buf, M_FOOBUF, M_NOWAIT);
288 A kernel compiled with the
290 configuration option attempts to detect memory corruption caused by
291 such things as writing outside the allocated area and imbalanced calls to the
296 Failing consistency checks will cause a panic or a system console