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 realloc "void *addr" "size_t size" "struct malloc_type *type" "int flags"
55 .Fn reallocf "void *addr" "size_t size" "struct malloc_type *type" "int flags"
57 .Fn malloc_usable_size "const void *addr"
62 .Fa "struct malloc_type *type"
65 .Fn MALLOC_DECLARE type
69 .Fn MALLOC_DEFINE type shortdesc longdesc
73 .Fn malloc_domainset "size_t size" "struct malloc_type *type" "struct domainset *ds" "int flags"
75 .Fo malloc_domainset_aligned
78 .Fa "struct malloc_type *type"
79 .Fa "struct domainset *ds"
83 .Fn mallocarray_domainset "size_t nmemb" "size_t size" "struct malloc_type *type" "struct domainset *ds" "int flags"
85 .Fn free_domain "void *addr" "struct malloc_type *type"
89 function allocates uninitialized memory in kernel address space for an
90 object whose size is specified by
95 variant allocates memory from a specific
97 domain using the specified domain selection policy.
100 for some example policies.
101 Memory allocated with this function should be returned with
107 .Fn malloc_domainset_aligned
108 variants return allocations aligned as specified by
110 which must be non-zero, a power of two, and less than or equal to the page size.
114 function allocates uninitialized memory in kernel address space for an
117 entries whose size is specified by
121 .Fn mallocarray_domainset
122 variant allocates memory from a specific
124 domain using the specified domain selection policy.
127 for some example policies.
131 function releases memory at address
133 that was previously allocated by
136 The memory is not zeroed.
147 function changes the size of the previously allocated memory referenced by
152 The contents of the memory are unchanged up to the lesser of the new and
154 Note that the returned value may differ from
156 If the requested memory cannot be allocated,
158 is returned and the memory referenced by
160 is valid and unchanged.
167 function behaves identically to
169 for the specified size.
173 function is identical to
176 will free the passed pointer when the requested memory cannot be allocated.
179 .Fn malloc_usable_size
180 function returns the usable size of the allocation pointed to by
182 The return value may be larger than the size that was requested during
185 Unlike its standard C library counterpart
187 the kernel version takes two more arguments.
190 argument further qualifies
192 operational characteristics as follows:
193 .Bl -tag -width indent
195 Causes the allocated memory to be set to all zeros.
197 For allocations greater than page size, causes the allocated
198 memory to be excluded from kernel core dumps.
207 if the request cannot be immediately fulfilled due to resource shortage.
210 is required when running in an interrupt context.
212 Indicates that it is OK to wait for resources.
213 If the request cannot be immediately fulfilled, the current process is put
214 to sleep to wait for resources to be released by other processes.
221 functions cannot return
226 If the multiplication of
230 would cause an integer overflow, the
232 function induces a panic.
234 Indicates that the system can use its reserve of memory to satisfy the
236 This option should only be used in combination with
238 when an allocation failure cannot be tolerated by the caller without
239 catastrophic effects on the system.
241 Indicates that the system should allocate executable memory.
242 If this flag is not set, the system will not allocate executable memory.
243 Not all platforms enforce a distinction between executable and
244 non-executable memory.
247 Exactly one of either
255 argument is used to perform statistics on memory usage, and for
257 It can be used to identify multiple allocations.
258 The statistics can be examined by
264 .Vt "struct malloc_type"
270 .Bd -literal -offset indent
271 /* sys/something/foo_extern.h */
273 MALLOC_DECLARE(M_FOOBUF);
275 /* sys/something/foo_main.c */
277 MALLOC_DEFINE(M_FOOBUF, "foobuffers", "Buffers to foo data into the ether");
279 /* sys/something/foo_subr.c */
282 buf = malloc(sizeof(*buf), M_FOOBUF, M_NOWAIT);
299 may not be called from fast interrupts handlers.
300 When called from threaded interrupts,
309 may sleep when called with
319 may not be called in a critical section or while holding a spin lock.
329 interlock, will cause a LOR (Lock Order Reversal) due to the
330 intertwining of VM Objects and Vnodes.
331 .Sh IMPLEMENTATION NOTES
332 The memory allocator allocates memory in chunks that have size a power
333 of two for requests up to the size of a page of memory.
334 For larger requests, one or more pages is allocated.
335 While it should not be relied upon, this information may be useful for
336 optimizing the efficiency of memory use.
343 functions return a kernel virtual address that is suitably aligned for
344 storage of any type of object, or
346 if the request could not be satisfied (implying that
350 A kernel compiled with the
352 configuration option attempts to detect memory corruption caused by
353 such things as writing outside the allocated area and imbalanced calls to the
358 Failing consistency checks will cause a panic or a system console