xref: /openbsd-src/share/man/man9/malloc.9 (revision 62a742911104f98b9185b2c6b6007d9b1c36396c)
1.\"	$NetBSD: malloc.9,v 1.2 1996/10/30 05:29:54 lukem Exp $
2.\"
3.\" Copyright (c) 1996 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.
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 REGENTS OR CONTRIBUTORS BE
29.\" 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 Jun 16, 1996
38.Dt MALLOC 9
39.Os
40.Sh NAME
41.Nm malloc
42.Nd kernel memory allocator
43.Sh SYNOPSIS
44.Ft void *
45.Fn malloc "unsigned long size" "int type" "int flags"
46.Fn MALLOC "space" "cast" "unsigned long size" "int type" "int flags"
47.Ft void
48.Fn free "void *addr" "int type"
49.Fn FREE "void *addr" "int type"
50.Sh DESCRIPTION
51The
52.Fn malloc
53function allocates uninitialized memory in kernel address space for an
54object whose size is specified by
55.Fa size .
56.Fn free
57releases memory at address
58.Fa addr
59that was previously allocated by
60.Fn malloc
61for re-use.
62The
63.Fn MALLOC
64macro variant is functionally equivalent to
65.Bd -literal -offset indent
66(space) = (cast)malloc((u_long)(size), type, flags)
67.Ed
68.Pp
69and the
70.Fn FREE
71macro variant is equivalent to
72.Bd -literal -offset indent
73free((caddr_t)(addr), type)
74.Ed
75.Pp
76Unlike its standard C library counterpart
77.Pq Xr malloc 3 ,
78the kernel version takes two more arguments.  The
79.Fa flags
80argument further qualifies
81.Fn malloc No Ns 's
82operational charateristics as follows:
83.Bl -tag -offset indent
84.It Dv M_NOWAIT
85Causes
86.Fn malloc
87to return
88.Dv NULL
89if the request cannot be immediately fulfilled due to resource shortage.
90Otherwise,
91.Fn malloc
92may call sleep to wait for resources to be released by other processes.
93If this flag is not set,
94.Fn malloc
95will never return
96.Dv NULL .
97Note that
98.Dv M_WAITOK
99is conveniently defined to be 0, and hence maybe or'ed into the
100.Fa flags
101argument to indicate that it's Ok to wait for resources.
102.El
103.Pp
104Currently, only one flag is defined.
105.Pp
106The
107.Fa type
108argument broadly identifies the kernel subsystem for which the allocated
109memory was needed, and is commonly used to maintain statistics about
110kernel memory usage. The following types are currently defined:
111.Pp
112.Bl -tag -offset indent -width XXXXXXXXXXXXXX -compact
113.It Dv M_FREE
114Should be on free list.
115.It Dv M_MBUF
116Mbuf  memory.
117.It Dv M_DEVBUF
118Device driver memory.
119.It Dv M_SOCKET
120Socket structure.
121.It Dv M_PCB
122Protocol control block.
123.It Dv M_RTABLE
124Routing tables.
125.It Dv M_HTABLE
126IMP host tables.
127.It Dv M_FTABLE
128Fragment reassembly header.
129.It Dv M_ZOMBIE
130Zombie proc status
131.It Dv M_IFADDR
132Interface address.
133.It Dv M_SOOPTS
134Socket options.
135.It Dv M_SONAME
136Socket name.
137.It Dv M_NAMEI
138Namei path name buffer.
139.It Dv M_GPROF
140Kernel profiling buffer.
141.It Dv M_IOCTLOPS
142Ioctl data buffer.
143.It Dv M_MAPMEM
144Mapped memory descriptors.
145.It Dv M_CRED
146Credentials.
147.It Dv M_PGRP
148Process group header.
149.It Dv M_SESSION
150Session header.
151.It Dv M_IOV
152Large iov's.
153.It Dv M_MOUNT
154Vfs mount struct.
155.It Dv M_FHANDLE
156Network file handle.
157.It Dv M_NFSREQ
158NFS request header.
159.It Dv M_NFSMNT
160NFS mount structure.
161.It Dv M_NFSNODE
162NFS vnode private part.
163.It Dv M_VNODE
164Dynamically allocated vnodes.
165.It Dv M_CACHE
166Dynamically allocated cache entries.
167.It Dv M_DQUOT
168UFS quota entries.
169.It Dv M_UFSMNT
170UFS mount structure.
171.It Dv M_SHM
172SVID compatible shared memory segments.
173.It Dv M_VMMAP
174VM map structures.
175.It Dv M_VMMAPENT
176VM map entry structures.
177.It Dv M_VMOBJ
178VM object structure.
179.It Dv M_VMOBJHASH
180VM object hash structure.
181.It Dv M_VMPMAP
182VM pmap.
183.It Dv M_VMPVENT
184VM phys-virt mapping entry.
185.It Dv M_VMPAGER
186XXX: VM pager struct.
187.It Dv M_VMPGDATA
188XXX: VM pager private data.
189.It Dv M_FILE
190Open file structure.
191.It Dv M_FILEDESC
192Open file descriptor table.
193.It Dv M_LOCKF
194Byte-range locking structures.
195.It Dv M_PROC
196Proc structures.
197.It Dv M_SUBPROC
198Proc sub-structures.
199.It Dv M_SEGMENT
200Segment for LFS.
201.It Dv M_LFSNODE
202LFS vnode private part.
203.It Dv M_FFSNODE
204FFS vnode private part.
205.It Dv M_MFSNODE
206MFS vnode private part.
207.It Dv M_NQLEASE
208Nqnfs lease.
209.It Dv M_NQMHOST
210Nqnfs host address table.
211.It Dv M_NETADDR
212Export host address structure.
213.It Dv M_NFSSVC
214Nfs server structure.
215.It Dv M_NFSUID
216Nfs uid mapping structure.
217.It Dv M_NFSD
218Nfs server daemon structure.
219.It Dv M_IPMOPTS
220Internet multicast options.
221.It Dv M_IPMADDR
222Internet multicast address.
223.It Dv M_IFMADDR
224Link-level multicast address.
225.It Dv M_MRTABLE
226Multicast routing tables.
227.It Dv M_ISOFSMNT
228ISOFS mount structure.
229.It Dv M_ISOFSNODE
230ISOFS vnode private part.
231.It Dv M_MSDOSFSMNT
232MSDOS FS mount structure.
233.It Dv M_MSDOSFSFAT
234MSDOS FS fat table.
235.It Dv M_MSDOSFSNODE
236MSDOS FS vnode private part.
237.It Dv M_TTYS
238Allocated tty structures.
239.It Dv M_EXEC
240Argument lists & other mem used by exec.
241.It Dv M_MISCFSMNT
242Miscfs mount structures.
243.It Dv M_MISCFSNODE
244Miscfs vnode private part.
245.It Dv M_ADOSFSMNT
246Adosfs mount structures.
247.It Dv M_ADOSFSNODE
248ADosfs vnode private part.
249.It Dv M_ANODE
250Adosfs anode structures and tables.
251.It Dv M_IPQ
252IP packet queue entry.
253.It Dv M_AFS
254Andrew File System.
255.It Dv M_ADOSFSBITMAP
256Adosfs bitmap.
257.It Dv M_TEMP
258Misc temporary data buffers.
259.El
260.Pp
261Statistics based on the
262.Fa type
263argument is maintained only if the kernel option
264.Dv KMEMSTATS
265is used when compiling the kernel
266.Po the default in current
267.Nx
268kernels
269.Pc
270and can be examined by using
271.Sq vmstat -m .
272.Sh RETURN VALUES
273.Fn malloc
274returns a kernel virtual address that is suitably aligned for storage of
275any type of object.
276.Sh SEE ALSO
277.Xr vmstat 8
278.Sh DIAGNOSTICS
279A kernel compiled with the
280.Dv DIAGNOSTIC
281configuration option attempts to detect detect memory corruption caused by
282such things as writing outside the allocated area and imbalanced calls to the
283.Fn malloc
284and
285.Fn free
286functions. Failing consistency checks will cause a panic or a system console
287message:
288.Bl -bullet -offset indent -compact
289.Pp
290.It
291panic:
292.Dq malloc - bogus type
293.It
294panic:
295.Dq malloc: out of space in kmem_map
296.It
297panic:
298.Dq malloc: allocation too large
299.It
300panic:
301.Dq malloc: wrong bucket
302.It
303panic:
304.Dq malloc: lost data
305.It
306panic:
307.Dq free: unaligned addr
308.It
309panic:
310.Dq free: duplicated free
311.It
312panic:
313.Dq free: multiple frees
314.It
315panic:
316.Dq init: minbucket too small/struct freelist too big
317.It
318.Dq multiply freed item Aq addr
319.It
320.Dq Data modified on freelist: Aq data object description
321.El
322