xref: /netbsd-src/share/man/man9/malloc.9 (revision 1ffa7b76c40339c17a0fb2a09fac93f287cfc046)
1.\"	$NetBSD: malloc.9,v 1.31 2003/04/16 13:35:30 wiz Exp $
2.\"
3.\" Copyright (c) 1996, 2003 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Paul Kranenburg, and by Jason R. Thorpe.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
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.
17.\" 3. All advertising materials mentioning features or use of this software
18.\"    must display the following acknowledgement:
19.\"        This product includes software developed by the NetBSD
20.\"        Foundation, Inc. and its contributors.
21.\" 4. Neither the name of The NetBSD Foundation nor the names of its
22.\"    contributors may be used to endorse or promote products derived
23.\"    from this software without specific prior written permission.
24.\"
25.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35.\" POSSIBILITY OF SUCH DAMAGE.
36.\"
37.Dd January 31, 2003
38.Dt MALLOC 9
39.Os
40.Sh NAME
41.Nm malloc ,
42.Nm MALLOC ,
43.Nm realloc ,
44.Nm free ,
45.Nm FREE ,
46.Nm malloc_roundup ,
47.Nm malloc_type_attach ,
48.Nm malloc_type_detach ,
49.Nm malloc_type_setlimit ,
50.Nm MALLOC_DEFINE_LIMIT ,
51.Nm MALLOC_DEFINE ,
52.Nm MALLOC_DECLARE
53.Nd general-purpose kernel memory allocator
54.Sh SYNOPSIS
55.In sys/malloc.h
56.Ft void *
57.Fn malloc "unsigned long size" "struct malloc_type *type" "int flags"
58.Fn MALLOC "space" "cast" "unsigned long size" "struct malloc_type *type" \
59    "int flags"
60.Ft void *
61.Fn realloc "void *addr" "unsigned long newsize" "struct malloc_type *type" \
62    "int flags"
63.Ft void
64.Fn free "void *addr" "int type"
65.Fn FREE "void *addr" "int type"
66.Ft unsigned long
67.Fn malloc_roundup "unsigned long size"
68.Ft void
69.Fn malloc_type_attach "struct malloc_type *type"
70.Ft void
71.Fn malloc_type_detach "struct malloc_type *type"
72.Ft void
73.Fn malloc_type_setlimit "struct malloc_type *type" "unsigned long limit"
74.In sys/mallocvar.h
75.Fn MALLOC_DEFINE_LIMIT "type" "shortdesc" "longdesc" "limit"
76.Fn MALLOC_DEFINE "type" "shortdesc" "longdesc"
77.Fn MALLOC_DECLARE "type"
78.Sh DESCRIPTION
79The
80.Fn malloc
81function allocates uninitialized memory in kernel address space for an
82object whose size is specified by
83.Fa size .
84.Fn malloc_roundup
85returns the actual size of the allocation unit for the given value.
86.Fn free
87releases memory at address
88.Fa addr
89that was previously allocated by
90.Fn malloc
91for re-use.
92.Pp
93The
94.Fn realloc
95function changes the size of the previously allocated memory referenced
96by
97.Fa addr
98to
99.Fa size
100and returns a pointer to the
101.Pq possibly moved
102object.
103The memory contents are unchanged up to the lesser of the new
104and old sizes.
105If the new size is larger, the newly allocated memory is
106uninitialized.
107If the requested memory cannot be allocated,
108.Dv NULL
109is returned and the memory referenced by
110.Fa addr
111is unchanged.
112If
113.Fa addr
114is
115.Dv NULL ,
116then
117.Fn realloc
118behaves exactly as
119.Fn malloc .
120If the new size is 0, then
121.Fn realloc
122behaves exactly as
123.Fn free .
124.Pp
125The
126.Fn MALLOC
127macro variant is functionally equivalent to
128.Bd -literal -offset indent
129(space) = (cast)malloc((u_long)(size), type, flags)
130.Ed
131.Pp
132and the
133.Fn FREE
134macro variant is equivalent to
135.Bd -literal -offset indent
136free((caddr_t)(addr), type)
137.Ed
138.Pp
139The
140.Fn MALLOC
141macro is intended to be used with a compile-time constant
142.Fa size
143so that the compiler can do constant folding.
144The
145.Fn MALLOC
146and
147.Fn FREE
148macros may be faster for some fixed-size allocations, at the cost of
149increased code size.
150.Pp
151Unlike its standard C library counterpart
152.Pq Xr malloc 3 ,
153the kernel version takes two more arguments.
154.Pp
155The
156.Fa flags
157argument further qualifies
158.Fn malloc
159operational characteristics as follows:
160.Bl -tag -offset indent -width M_CANFAIL
161.It Dv M_NOWAIT
162Causes
163.Fn malloc
164to return
165.Dv NULL
166if the request cannot be immediately fulfilled due to resource shortage.
167If this flag is not set
168(see
169.Dv M_WAITOK ) ,
170.Fn malloc
171will never return
172.Dv NULL .
173.It Dv M_WAITOK
174By default,
175.Fn malloc
176may call
177.Xr tsleep 9
178to wait for resources to be released by other processes, and this
179flag represents this behaviour.
180Note that
181.Dv M_WAITOK
182is conveniently defined to be 0, and hence may be or'ed into the
183.Fa flags
184argument to indicate that it's ok to wait for resources.
185.It Dv M_ZERO
186Causes the allocated memory to be set to all zeros.
187.It Dv M_CANFAIL
188Changes behaviour for
189.Dv M_WAITOK
190case - if the requested memory size is bigger than
191.Fn malloc
192can ever allocate, return failure, rather than calling
193.Xr panic 9 .
194This is different to M_NOWAIT, since
195the call can still wait for resources.
196.Pp
197Rather than depending on
198.Dv M_CANFAIL ,
199kernel code should do proper bound checking itself.
200This flag should only be used in cases where this is not feasible.
201Since it can hide real kernel bugs, its usage is
202.Em strongly discouraged .
203.El
204.Pp
205The
206.Fa type
207argument describes the subsystem and/or use within a subsystem for which
208the allocated memory was needed, and is commonly used to maintain statistics
209about kernel memory usage and, optionally, enforce limits on this usage for
210certain memory types.
211.Pp
212In addition to some built-in generic types defined by the kernel
213memory allocator, subsystems may define their own types.
214.Pp
215The
216.Fn MALLOC_DEFINE_LIMIT
217macro defines a malloc type named
218.Fa type
219with the short description
220.Fa shortdesc ,
221which must be a constant string; this description will be used for
222kernel memory statistics reporting.
223The
224.Fa longdesc
225argument, also a constant string, is intended as way to place a
226comment in the actual type definition, and is not currently stored
227in the type structure.
228The
229.Fa limit
230argument specifies the maximum amount of memory, in bytes, that this
231malloc type can consume.
232.Pp
233The
234.Fn MALLOC_DEFINE
235macro is equivalent to the
236.Fn MALLOC_DEFINE_LIMIT
237macro with a
238.Fa limit
239argument of 0.
240If kernel memory statistics are being gathered, the system will
241choose a reasonable default limit for the malloc type.
242.Pp
243The
244.Fn MALLOC_DECLARE
245macro is intended for use in header files which are included by
246code which needs to use the malloc type, providing the necessary
247extern declaration.
248.Pp
249Code which includes
250\*[Lt]sys/malloc.h\*[Gt]
251does not need to include
252\*[Lt]sys/mallocvar.h\*[Gt]
253to get these macro definitions.
254The
255\*[Lt]sys/mallocvar.h\*[Gt]
256header file is intended for other header files which need to use the
257.Fn MALLOC_DECLARE
258macro.
259.Pp
260The
261.Fn malloc_type_attach
262function attaches the malloc type
263.Fa type
264to the kernel memory allocator.
265This is intended for use by LKMs; malloc types included in modules
266statically-linked into the kernel are automatically registered with
267the kernel memory allocator.
268.Pp
269The
270.Fn malloc_type_detach
271function detaches the malloc type
272.Fa type
273previously attached with
274.Fn malloc_type_attach .
275.Pp
276The
277.Fn malloc_type_setlimit
278function sets the memory limit of the malloc type
279.Fa type
280to
281.Fa limit
282bytes.
283The type must already be registered with the kernel memory allocator.
284.Pp
285The following generic malloc types are currently defined:
286.Pp
287.Bl -tag -offset indent -width XXXXXXXXXXXXXX -compact
288.It Dv M_DEVBUF
289Device driver memory.
290.It Dv M_DMAMAP
291.Xr bus_dma 9
292structures.
293.It Dv M_FREE
294Should be on free list.
295.It Dv M_PCB
296Protocol control block.
297.It Dv M_SOFTINTR
298Softinterrupt structures.
299.It Dv M_TEMP
300Misc temporary data buffers.
301.El
302.Pp
303Other malloc types are defined by the corresponding subsystem; see the
304documentation for that subsystem for information its available malloc
305types.
306.Pp
307Statistics based on the
308.Fa type
309argument are maintained only if the kernel option
310.Dv KMEMSTATS
311is used when compiling the kernel
312.Po
313the default in current
314.Nx
315kernels
316.Pc
317and can be examined by using
318.Sq vmstat -m .
319.Sh RETURN VALUES
320.Fn malloc
321returns a kernel virtual address that is suitably aligned for storage of
322any type of object.
323.Sh DIAGNOSTICS
324A kernel compiled with the
325.Dv DIAGNOSTIC
326configuration option attempts to detect memory corruption caused by
327such things as writing outside the allocated area and imbalanced calls to the
328.Fn malloc
329and
330.Fn free
331functions.
332Failing consistency checks will cause a panic or a system console message:
333.Bl -bullet -offset indent -compact
334.Pp
335.It
336panic:
337.Dq malloc - bogus type
338.It
339panic:
340.Dq malloc: out of space in kmem_map
341.It
342panic:
343.Dq malloc: allocation too large
344.It
345panic:
346.Dq malloc: wrong bucket
347.It
348panic:
349.Dq malloc: lost data
350.It
351panic:
352.Dq free: unaligned addr
353.It
354panic:
355.Dq free: duplicated free
356.It
357panic:
358.Dq free: multiple frees
359.It
360panic:
361.Dq init: minbucket too small/struct freelist too big
362.It
363.Dq multiply freed item Aq addr
364.It
365.Dq Data modified on freelist: Aq data object description
366.El
367.Sh SEE ALSO
368.Xr vmstat 1
369