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