xref: /netbsd-src/share/man/man9/malloc.9 (revision cac8e449158efc7261bebc8657cbb0125a2cfdde)
1.\"	$NetBSD: malloc.9,v 1.42 2008/04/30 13:10:58 martin 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.\"
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.
29.\"
30.Dd March 24, 2008
31.Dt MALLOC 9
32.Os
33.Sh NAME
34.Nm malloc ,
35.Nm MALLOC ,
36.Nm realloc ,
37.Nm free ,
38.Nm FREE ,
39.Nm malloc_roundup ,
40.Nm malloc_type_attach ,
41.Nm malloc_type_detach ,
42.Nm malloc_type_setlimit ,
43.Nm MALLOC_DEFINE_LIMIT ,
44.Nm MALLOC_DEFINE ,
45.Nm MALLOC_DECLARE
46.Nd general-purpose kernel memory allocator
47.Sh SYNOPSIS
48.In sys/malloc.h
49.Ft void *
50.Fn malloc "unsigned long size" "struct malloc_type *type" "int flags"
51.Fn MALLOC "space" "cast" "unsigned long size" "struct malloc_type *type" \
52    "int flags"
53.Ft void *
54.Fn realloc "void *addr" "unsigned long newsize" "struct malloc_type *type" \
55    "int flags"
56.Ft void
57.Fn free "void *addr" "struct malloc_type *type"
58.Fn FREE "void *addr" "struct malloc_type *type"
59.Ft unsigned long
60.Fn malloc_roundup "unsigned long size"
61.Ft void
62.Fn malloc_type_attach "struct malloc_type *type"
63.Ft void
64.Fn malloc_type_detach "struct malloc_type *type"
65.Ft void
66.Fn malloc_type_setlimit "struct malloc_type *type" "unsigned long limit"
67.In sys/mallocvar.h
68.Fn MALLOC_DEFINE_LIMIT "type" "shortdesc" "longdesc" "limit"
69.Fn MALLOC_JUSTDEFINE_LIMIT "type" "shortdesc" "longdesc" "limit"
70.Fn MALLOC_DEFINE "type" "shortdesc" "longdesc"
71.Fn MALLOC_JUSTDEFINE "type" "shortdesc" "longdesc"
72.Fn MALLOC_DECLARE "type"
73.Sh DESCRIPTION
74.Bf -symbolic
75These interfaces are being obsoleted and their new use is discouraged.
76For new code, please consider to use
77.Xr kmem_alloc 9
78or
79.Xr pool_cache 9
80instead.
81.Ef
82.Pp
83The
84.Fn malloc
85function allocates uninitialized memory in kernel address space for an
86object whose size is specified by
87.Fa size .
88.Fn malloc_roundup
89returns the actual size of the allocation unit for the given value.
90.Fn free
91releases memory at address
92.Fa addr
93that was previously allocated by
94.Fn malloc
95for re-use.
96Unlike
97.Xr free 3 ,
98.Fn free
99does not accept an
100.Fa addr
101argument that is
102.Dv NULL .
103.Pp
104The
105.Fn realloc
106function changes the size of the previously allocated memory referenced
107by
108.Fa addr
109to
110.Fa size
111and returns a pointer to the
112.Pq possibly moved
113object.
114The memory contents are unchanged up to the lesser of the new
115and old sizes.
116If the new size is larger, the newly allocated memory is
117uninitialized.
118If the requested memory cannot be allocated,
119.Dv NULL
120is returned and the memory referenced by
121.Fa addr
122is unchanged.
123If
124.Fa addr
125is
126.Dv NULL ,
127then
128.Fn realloc
129behaves exactly as
130.Fn malloc .
131If the new size is 0, then
132.Fn realloc
133behaves exactly as
134.Fn free .
135.Pp
136The
137.Fn MALLOC
138macro variant is functionally equivalent to
139.Bd -literal -offset indent
140(space) = (cast)malloc((u_long)(size), type, flags)
141.Ed
142.Pp
143and the
144.Fn FREE
145macro variant is equivalent to
146.Bd -literal -offset indent
147free((void *)(addr), type)
148.Ed
149.Pp
150The
151.Fn MALLOC
152macro is intended to be used with a compile-time constant
153.Fa size
154so that the compiler can do constant folding.
155In the comparison to
156.Fn malloc
157and
158.Fn free
159functions, the
160.Fn MALLOC
161and
162.Fn FREE
163macros may be faster, at the cost of increased code size.
164There is no difference between the memory allocated with MALLOC and malloc.
165i.e., no matter which MALLOC or malloc is used to allocate the memory,
166either FREE or free can be used to free it.
167.Pp
168Unlike its standard C library counterpart
169.Pq Xr malloc 3 ,
170the kernel version takes two more arguments.
171.Pp
172The
173.Fa flags
174argument further qualifies
175.Fn malloc
176operational characteristics as follows:
177.Bl -tag -offset indent -width M_CANFAIL
178.It Dv M_NOWAIT
179Causes
180.Fn malloc
181to return
182.Dv NULL
183if the request cannot be immediately fulfilled due to resource shortage.
184If this flag is not set
185(see
186.Dv M_WAITOK ) ,
187.Fn malloc
188will never return
189.Dv NULL .
190.It Dv M_WAITOK
191By default,
192.Fn malloc
193may call
194.Xr cv_wait 9
195to wait for resources to be released by other processes, and this
196flag represents this behaviour.
197Note that
198.Dv M_WAITOK
199is conveniently defined to be 0, and hence may be or'ed into the
200.Fa flags
201argument to indicate that it's ok to wait for resources.
202.It Dv M_ZERO
203Causes the allocated memory to be set to all zeros.
204.It Dv M_CANFAIL
205Changes behaviour for
206.Dv M_WAITOK
207case - if the requested memory size is bigger than
208.Fn malloc
209can ever allocate, return failure, rather than calling
210.Xr panic 9 .
211This is different to M_NOWAIT, since
212the call can still wait for resources.
213.Pp
214Rather than depending on
215.Dv M_CANFAIL ,
216kernel code should do proper bound checking itself.
217This flag should only be used in cases where this is not feasible.
218Since it can hide real kernel bugs, its usage is
219.Em strongly discouraged .
220.El
221.Pp
222The
223.Fa type
224argument describes the subsystem and/or use within a subsystem for which
225the allocated memory was needed, and is commonly used to maintain statistics
226about kernel memory usage and, optionally, enforce limits on this usage for
227certain memory types.
228.Pp
229In addition to some built-in generic types defined by the kernel
230memory allocator, subsystems may define their own types.
231.Pp
232The
233.Fn MALLOC_DEFINE_LIMIT
234macro defines a malloc type named
235.Fa type
236with the short description
237.Fa shortdesc ,
238which must be a constant string; this description will be used for
239kernel memory statistics reporting.
240The
241.Fa longdesc
242argument, also a constant string, is intended as way to place a
243comment in the actual type definition, and is not currently stored
244in the type structure.
245The
246.Fa limit
247argument specifies the maximum amount of memory, in bytes, that this
248malloc type can consume.
249.Pp
250The
251.Fn MALLOC_DEFINE
252macro is equivalent to the
253.Fn MALLOC_DEFINE_LIMIT
254macro with a
255.Fa limit
256argument of 0.
257If kernel memory statistics are being gathered, the system will
258choose a reasonable default limit for the malloc type.
259.Pp
260The
261.Fn MALLOC_DECLARE
262macro is intended for use in header files which are included by
263code which needs to use the malloc type, providing the necessary
264extern declaration.
265.Pp
266Code which includes
267\*[Lt]sys/malloc.h\*[Gt]
268does not need to include
269\*[Lt]sys/mallocvar.h\*[Gt]
270to get these macro definitions.
271The
272\*[Lt]sys/mallocvar.h\*[Gt]
273header file is intended for other header files which need to use the
274.Fn MALLOC_DECLARE
275macro.
276.Pp
277The
278.Fn malloc_type_attach
279function attaches the malloc type
280.Fa type
281to the kernel memory allocator.
282This is intended for use by LKMs; malloc types included in modules
283statically-linked into the kernel are automatically registered with
284the kernel memory allocator.
285However, it is possible to define malloc types without automatically
286registering them using
287.Fn MALLOC_JUSTDEFINE
288or
289.Fn MALLOC_JUSTDEFINE_LIMIT .
290Apart from not automatically registering to the kernel a boot time,
291these functions are equivalent to their counterparts.
292They can be used when a separate LKM codepath for initialization is
293not desired.
294.Pp
295The
296.Fn malloc_type_detach
297function detaches the malloc type
298.Fa type
299previously attached with
300.Fn malloc_type_attach .
301.Pp
302The
303.Fn malloc_type_setlimit
304function sets the memory limit of the malloc type
305.Fa type
306to
307.Fa limit
308bytes.
309The type must already be registered with the kernel memory allocator.
310.Pp
311The following generic malloc types are currently defined:
312.Pp
313.Bl -tag -offset indent -width XXXXXXXXXXXXXX -compact
314.It Dv M_DEVBUF
315Device driver memory.
316.It Dv M_DMAMAP
317.Xr bus_dma 9
318structures.
319.It Dv M_FREE
320Should be on free list.
321.It Dv M_PCB
322Protocol control block.
323.It Dv M_SOFTINTR
324Softinterrupt structures.
325.It Dv M_TEMP
326Misc temporary data buffers.
327.El
328.Pp
329Other malloc types are defined by the corresponding subsystem; see the
330documentation for that subsystem for information its available malloc
331types.
332.Pp
333Statistics based on the
334.Fa type
335argument are maintained only if the kernel option
336.Dv KMEMSTATS
337is used when compiling the kernel
338.Po
339the default in current
340.Nx
341kernels
342.Pc
343and can be examined by using
344.Sq vmstat -m .
345.Sh RETURN VALUES
346.Fn malloc
347returns a kernel virtual address that is suitably aligned for storage of
348any type of object.
349.Sh DIAGNOSTICS
350A kernel compiled with the
351.Dv DIAGNOSTIC
352configuration option attempts to detect memory corruption caused by
353such things as writing outside the allocated area and imbalanced calls to the
354.Fn malloc
355and
356.Fn free
357functions.
358Failing consistency checks will cause a panic or a system console message:
359.Bl -bullet -offset indent -compact
360.Pp
361.It
362panic:
363.Dq malloc - bogus type
364.It
365panic:
366.Dq malloc: out of space in kmem_map
367.It
368panic:
369.Dq malloc: allocation too large
370.It
371panic:
372.Dq malloc: wrong bucket
373.It
374panic:
375.Dq malloc: lost data
376.It
377panic:
378.Dq free: unaligned addr
379.It
380panic:
381.Dq free: duplicated free
382.It
383panic:
384.Dq free: multiple frees
385.It
386panic:
387.Dq init: minbucket too small/struct freelist too big
388.It
389.Dq multiply freed item Aq addr
390.It
391.Dq Data modified on freelist: Aq data object description
392.El
393.Sh SEE ALSO
394.Xr vmstat 1 ,
395.Xr memoryallocators 9
396