1 .\" $NetBSD: malloc.9,v 1.46 2009/03/09 17:29:25 joerg Exp $
3 .\" Copyright (c) 1996, 2003 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
6 .\" This code is derived from software contributed to The NetBSD Foundation
7 .\" by Paul Kranenburg, and by Jason R. Thorpe.
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\" notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\" notice, this list of conditions and the following disclaimer in the
16 .\" documentation and/or other materials provided with the distribution.
18 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 .\" POSSIBILITY OF SUCH DAMAGE.
40 .Nm malloc_type_attach ,
41 .Nm malloc_type_detach ,
42 .Nm malloc_type_setlimit ,
43 .Nm MALLOC_DEFINE_LIMIT ,
46 .Nd general-purpose kernel memory allocator
50 .Fn malloc "unsigned long size" "struct malloc_type *type" "int flags"
52 .Fn realloc "void *addr" "unsigned long newsize" "struct malloc_type *type" \
55 .Fn free "void *addr" "struct malloc_type *type"
57 .Fn malloc_roundup "unsigned long size"
59 .Fn malloc_type_attach "struct malloc_type *type"
61 .Fn malloc_type_detach "struct malloc_type *type"
63 .Fn malloc_type_setlimit "struct malloc_type *type" "unsigned long limit"
65 .Fn MALLOC_DEFINE_LIMIT "type" "shortdesc" "longdesc" "limit"
66 .Fn MALLOC_JUSTDEFINE_LIMIT "type" "shortdesc" "longdesc" "limit"
67 .Fn MALLOC_DEFINE "type" "shortdesc" "longdesc"
68 .Fn MALLOC_JUSTDEFINE "type" "shortdesc" "longdesc"
69 .Fn MALLOC_DECLARE "type"
72 These interfaces are being obsoleted and their new use is discouraged.
82 function allocates uninitialized memory in kernel address space for an
83 object whose size is specified by
86 returns the actual size of the allocation unit for the given value.
88 releases memory at address
90 that was previously allocated by
103 function changes the size of the previously allocated memory referenced
108 and returns a pointer to the
111 The memory contents are unchanged up to the lesser of the new
113 If the new size is larger, the newly allocated memory is
115 If the requested memory cannot be allocated,
117 is returned and the memory referenced by
128 If the new size is 0, then
133 Unlike its standard C library counterpart
135 the kernel version takes two more arguments.
139 argument further qualifies
141 operational characteristics as follows:
142 .Bl -tag -offset indent -width M_CANFAIL
148 if the request cannot be immediately fulfilled due to resource shortage.
149 If this flag is not set
160 to wait for resources to be released by other processes, and this
161 flag represents this behaviour.
164 is conveniently defined to be 0, and hence may be or'ed into the
166 argument to indicate that it's ok to wait for resources.
168 Causes the allocated memory to be set to all zeros.
170 Changes behaviour for
172 case - if the requested memory size is bigger than
174 can ever allocate, return failure, rather than calling
176 This is different to M_NOWAIT, since
177 the call can still wait for resources.
179 Rather than depending on
181 kernel code should do proper bound checking itself.
182 This flag should only be used in cases where this is not feasible.
183 Since it can hide real kernel bugs, its usage is
184 .Em strongly discouraged .
189 argument describes the subsystem and/or use within a subsystem for which
190 the allocated memory was needed, and is commonly used to maintain statistics
191 about kernel memory usage and, optionally, enforce limits on this usage for
192 certain memory types.
194 In addition to some built-in generic types defined by the kernel
195 memory allocator, subsystems may define their own types.
198 .Fn MALLOC_DEFINE_LIMIT
199 macro defines a malloc type named
201 with the short description
203 which must be a constant string; this description will be used for
204 kernel memory statistics reporting.
207 argument, also a constant string, is intended as way to place a
208 comment in the actual type definition, and is not currently stored
209 in the type structure.
212 argument specifies the maximum amount of memory, in bytes, that this
213 malloc type can consume.
217 macro is equivalent to the
218 .Fn MALLOC_DEFINE_LIMIT
222 If kernel memory statistics are being gathered, the system will
223 choose a reasonable default limit for the malloc type.
227 macro is intended for use in header files which are included by
228 code which needs to use the malloc type, providing the necessary
232 \*[Lt]sys/malloc.h\*[Gt]
233 does not need to include
234 \*[Lt]sys/mallocvar.h\*[Gt]
235 to get these macro definitions.
237 \*[Lt]sys/mallocvar.h\*[Gt]
238 header file is intended for other header files which need to use the
243 .Fn malloc_type_attach
244 function attaches the malloc type
246 to the kernel memory allocator.
249 .Fn malloc_type_detach
250 function detaches the malloc type
252 previously attached with
253 .Fn malloc_type_attach .
256 .Fn malloc_type_setlimit
257 function sets the memory limit of the malloc type
262 The type must already be registered with the kernel memory allocator.
264 The following generic malloc types are currently defined:
266 .Bl -tag -offset indent -width XXXXXXXXXXXXXX -compact
268 Device driver memory.
273 Should be on free list.
275 Protocol control block.
277 Softinterrupt structures.
279 Misc temporary data buffers.
282 Other malloc types are defined by the corresponding subsystem; see the
283 documentation for that subsystem for information its available malloc
286 Statistics based on the
288 argument are maintained only if the kernel option
290 is used when compiling the kernel
292 the default in current
296 and can be examined by using
300 returns a kernel virtual address that is suitably aligned for storage of
303 A kernel compiled with the
305 configuration option attempts to detect memory corruption caused by
306 such things as writing outside the allocated area and imbalanced calls to the
311 Failing consistency checks will cause a panic or a system console message:
313 .Bl -bullet -offset indent -compact
316 .Dq malloc - bogus type
319 .Dq malloc: out of space in kmem_map
322 .Dq malloc: allocation too large
325 .Dq malloc: wrong bucket
328 .Dq malloc: lost data
331 .Dq free: unaligned addr
334 .Dq free: duplicated free
337 .Dq free: multiple frees
340 .Dq init: minbucket too small/struct freelist too big
342 .Dq multiply freed item Aq addr
344 .Dq Data modified on freelist: Aq data object description
348 .Xr memoryallocators 9