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.
17 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
21 .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 .\" POSSIBILITY OF SUCH DAMAGE.
29 .\" $NetBSD: malloc.9,v 1.3 1996/11/11 00:05:11 lukem Exp $
42 .Nd kernel memory management routines
47 .Fn malloc "size_t size" "struct malloc_type *type" "int flags"
49 .Fn mallocarray "size_t nmemb" "size_t size" "struct malloc_type *type" "int flags"
51 .Fn free "void *addr" "struct malloc_type *type"
53 .Fn zfree "void *addr" "struct malloc_type *type"
55 .Fn realloc "void *addr" "size_t size" "struct malloc_type *type" "int flags"
57 .Fn reallocf "void *addr" "size_t size" "struct malloc_type *type" "int flags"
59 .Fn malloc_usable_size "const void *addr"
64 .Fa "struct malloc_type *type"
68 .Fn malloc_exec "size_t size" "struct malloc_type *type" "int flags"
69 .Fn MALLOC_DECLARE type
73 .Fn MALLOC_DEFINE type shortdesc longdesc
77 .Fn malloc_domainset "size_t size" "struct malloc_type *type" "struct domainset *ds" "int flags"
79 .Fo malloc_domainset_aligned
82 .Fa "struct malloc_type *type"
83 .Fa "struct domainset *ds"
87 .Fn malloc_domainset_exec "size_t size" "struct malloc_type *type" "struct domainset *ds" "int flags"
89 .Fn mallocarray_domainset "size_t nmemb" "size_t size" "struct malloc_type *type" "struct domainset *ds" "int flags"
93 function allocates uninitialized memory in kernel address space for an
94 object whose size is specified by
99 variant allocates memory from a specific
101 domain using the specified domain selection policy.
104 for some example policies.
109 .Fn malloc_domainset_aligned
110 variants return allocations aligned as specified by
112 which must be non-zero, a power of two, and less than or equal to the page size.
117 .Fn malloc_domainset_exec
118 can be used to return executable memory.
119 Not all platforms enforce a distinction between executable and non-executable memory.
123 function allocates uninitialized memory in kernel address space for an
126 entries whose size is specified by
130 .Fn mallocarray_domainset
131 variant allocates memory from a specific
133 domain using the specified domain selection policy.
136 for some example policies.
140 function releases memory at address
142 that was previously allocated by
145 The memory is not zeroed.
158 function releases memory at address
160 that was previously allocated by
165 will zero the memory before it is released.
169 function changes the size of the previously allocated memory referenced by
174 The contents of the memory are unchanged up to the lesser of the new and
176 Note that the returned value may differ from
178 If the requested memory cannot be allocated,
180 is returned and the memory referenced by
182 is valid and unchanged.
189 function behaves identically to
191 for the specified size.
195 function is identical to
198 will free the passed pointer when the requested memory cannot be allocated.
201 .Fn malloc_usable_size
202 function returns the usable size of the allocation pointed to by
204 The return value may be larger than the size that was requested during
207 Unlike its standard C library counterpart
209 the kernel version takes two more arguments.
212 argument further qualifies
214 operational characteristics as follows:
215 .Bl -tag -width indent
217 Causes the allocated memory to be set to all zeros.
219 For allocations greater than page size, causes the allocated
220 memory to be excluded from kernel core dumps.
229 if the request cannot be immediately fulfilled due to resource shortage.
232 is required when running in an interrupt context.
234 Indicates that it is OK to wait for resources.
235 If the request cannot be immediately fulfilled, the current process is put
236 to sleep to wait for resources to be released by other processes.
243 functions cannot return
248 If the multiplication of
252 would cause an integer overflow, the
254 function induces a panic.
256 Indicates that the system can use its reserve of memory to satisfy the
258 This option should only be used in combination with
260 when an allocation failure cannot be tolerated by the caller without
261 catastrophic effects on the system.
264 Exactly one of either
272 argument is used to perform statistics on memory usage, and for
274 It can be used to identify multiple allocations.
275 The statistics can be examined by
281 .Vt "struct malloc_type"
287 .Bd -literal -offset indent
288 /* sys/something/foo_extern.h */
290 MALLOC_DECLARE(M_FOOBUF);
292 /* sys/something/foo_main.c */
294 MALLOC_DEFINE(M_FOOBUF, "foobuffers", "Buffers to foo data into the ether");
296 /* sys/something/foo_subr.c */
299 buf = malloc(sizeof(*buf), M_FOOBUF, M_NOWAIT);
316 may not be called from fast interrupts handlers.
317 When called from threaded interrupts,
326 may sleep when called with
336 may not be called in a critical section or while holding a spin lock.
346 interlock, will cause a LOR (Lock Order Reversal) due to the
347 intertwining of VM Objects and Vnodes.
348 .Sh IMPLEMENTATION NOTES
349 The memory allocator allocates memory in chunks that have size a power
350 of two for requests up to the size of a page of memory.
351 For larger requests, one or more pages is allocated.
352 While it should not be relied upon, this information may be useful for
353 optimizing the efficiency of memory use.
360 functions return a kernel virtual address that is suitably aligned for
361 storage of any type of object, or
363 if the request could not be satisfied (implying that
367 A kernel compiled with the
369 configuration option attempts to detect memory corruption caused by
370 such things as writing outside the allocated area and imbalanced calls to the
375 Failing consistency checks will cause a panic or a system console