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