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