1.\" 2.\" Copyright (c) 1996 The NetBSD Foundation, Inc. 3.\" All rights reserved. 4.\" 5.\" This code is derived from software contributed to The NetBSD Foundation 6.\" by Paul Kranenburg. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" notice, this list of conditions and the following disclaimer in the 15.\" documentation and/or other materials provided with the distribution. 16.\" 3. All advertising materials mentioning features or use of this software 17.\" must display the following acknowledgement: 18.\" This product includes software developed by the NetBSD 19.\" Foundation, Inc. and its contributors. 20.\" 4. Neither the name of The NetBSD Foundation nor the names of its 21.\" contributors may be used to endorse or promote products derived 22.\" from this software without specific prior written permission. 23.\" 24.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 25.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 26.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 27.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE 28.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 29.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 32.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34.\" POSSIBILITY OF SUCH DAMAGE. 35.\" 36.\" $NetBSD: malloc.9,v 1.3 1996/11/11 00:05:11 lukem Exp $ 37.\" $FreeBSD$ 38.\" 39.Dd June 16, 1996 40.Dt MALLOC 9 41.Os 42.Sh NAME 43.Nm malloc , 44.Nm MALLOC , 45.Nm free , 46.Nm FREE 47.Nd kernel memory management routines 48.Sh SYNOPSIS 49.In sys/types.h 50.In sys/malloc.h 51.Ft void * 52.Fn malloc "unsigned long size" "struct malloc_type *type" "int flags" 53.Fn MALLOC "space" "cast" "unsigned long size" "struct malloc_type *type" "int flags" 54.Ft void 55.Fn free "void *addr" "struct malloc_type *type" 56.Fn FREE "void *addr" "struct malloc_type *type" 57.Ft void * 58.Fn realloc "void *addr" "unsigned long size" "struct malloc_type *type" "int flags" 59.Ft void * 60.Fn reallocf "void *addr" "unsigned long size" "struct malloc_type *type" "int flags" 61.Sh DESCRIPTION 62The 63.Fn malloc 64function allocates uninitialized memory in kernel address space for an 65object whose size is specified by 66.Fa size . 67.Pp 68The 69.Fn free 70function releases memory at address 71.Fa addr 72that was previously allocated by 73.Fn malloc 74for re-use. 75The memory is not zeroed. 76If 77.Fa addr 78is 79.Dv NULL , 80then 81.Fn free 82does nothing. 83.Pp 84The 85.Fn realloc 86function changes the size of the previously allocated memory referenced by 87.Fa addr 88to 89.Fa size 90bytes. 91The contents of the memory are unchanged up to the lesser of the new and 92old sizes. 93Note that the returned value may differ from 94.Fa addr . 95If the requested memory cannot be allocated, 96.Dv NULL 97is returned and the memory referenced by 98.Fa addr 99is valid and unchanged. 100If 101.Fa addr 102is 103.Dv NULL , 104the 105.Fn realloc 106function behaves identically to 107.Fn malloc 108for the specified size. 109.Pp 110The 111.Fn reallocf 112function is identical to 113.Fn realloc 114except that it 115will free the passed pointer when the requested memory cannot be allocated. 116.Pp 117The 118.Fn MALLOC 119macro variant is functionally equivalent to 120.Bd -literal -offset indent 121(space) = (cast)malloc((u_long)(size), type, flags) 122.Ed 123.Pp 124and the 125.Fn FREE 126macro variant is equivalent to 127.Bd -literal -offset indent 128free((addr), type) 129.Ed 130.Pp 131Unlike its standard C library counterpart 132.Pq Xr malloc 3 , 133the kernel version takes two more arguments. The 134.Fa flags 135argument further qualifies 136.Fn malloc Ns 's 137operational characteristics as follows: 138.Bl -tag -width indent 139.It Dv M_ZERO 140Causes the allocated memory to be set to all zeros. 141.It Dv M_NOWAIT 142Causes 143.Fn malloc , 144.Fn realloc , 145and 146.Fn reallocf 147to return 148.Dv NULL 149if the request cannot be immediately fulfilled due to resource shortage. 150Otherwise, the current process may be put to sleep to wait for 151resources to be released by other processes. 152If this flag is set, 153.Fn malloc 154will return 155.Dv NULL 156rather than block. 157Note that 158.Dv M_NOWAIT 159is defined to be 0, meaning that blocking operation is the default. 160Also note that 161.Dv M_NOWAIT 162is required when running in an interrupt context. 163.Pp 164Programmers should be careful not to confuse 165.Dv M_NOWAIT , 166the 167.Fn malloc 168flag, with 169.Dv M_DONTWAIT , 170an 171.Xr mbuf 9 172allocation flag, which is not a valid argument to 173.Fn malloc . 174.It Dv M_WAITOK 175Indicates that it is Ok to wait for resources. It is unconveniently 176defined as 0 so care should be taken never to compare against this value 177directly or try to AND it as a flag. The default operation is to block 178until the memory allocation succeeds. 179The 180.Fn malloc , 181.Fn realloc , 182and 183.Fn reallocf 184functions can only return 185.Dv NULL 186if 187.Dv M_NOWAIT 188is specified. 189.It Dv M_USE_RESERVE 190Indicates that the system can dig into its reserve in order to obtain the 191requested memory. This option used to be called M_KERNEL but has been 192renamed to something more obvious. This option has been deprecated and is 193slowly being removed from the kernel, and so should not be used with any new 194programming. 195.El 196.Pp 197The 198.Fa type 199argument is used to perform statistics on memory usage, and for 200basic sanity checks. 201The statistics can be examined by 202.Sq vmstat -m . 203.Pp 204A 205.Fa type 206is defined using the 207.Va malloc_type_t 208typedef via the 209.Fn MALLOC_DECLARE 210and 211.Fn MALLOC_DEFINE 212macros. 213.Bd -literal -offset indent 214/* sys/something/foo_extern.h */ 215 216MALLOC_DECLARE(M_FOOBUF); 217 218/* sys/something/foo_main.c */ 219 220MALLOC_DEFINE(M_FOOBUF, "foobuffers", "Buffers to foo data into the ether"); 221 222/* sys/something/foo_subr.c */ 223 224\&... 225MALLOC(buf, struct foo_buf *, sizeof *buf, M_FOOBUF, M_NOWAIT); 226 227.Ed 228.Sh RETURN VALUES 229The 230.Fn malloc , 231.Fn realloc , 232and 233.Fn reallocf 234functions return a kernel virtual address that is suitably aligned for 235storage of any type of object, or 236.Dv NULL 237if the request could not be satisfied (implying that 238.Dv M_NOWAIT 239was set). 240.Sh IMPLEMENTATION NOTES 241The memory allocator allocates memory in chunks that have size a power 242of two for requests up to the size of a page of memory. 243For larger requests, one or more pages is allocated. 244While it should not be relied upon, this information may be useful for 245optimizing the efficiency of memory use. 246.Pp 247Malloc flags documented above should 248.Em NOT 249be used with 250.Xr mbuf 9 251routines as it will cause undesired results. 252.Pp 253Any calls to 254.Fn malloc 255or 256.Fn free 257when holding a 258.Xr vnode 9 259interlock, will cause a LOR (Lock Order Reversal) due to the 260interwining of VM Objects and Vnodes. 261.Sh SEE ALSO 262.Xr vmstat 8 , 263.Xr vnode 9 264.Sh DIAGNOSTICS 265A kernel compiled with the 266.Dv DIAGNOSTIC 267configuration option attempts to detect memory corruption caused by 268such things as writing outside the allocated area and imbalanced calls to the 269.Fn malloc 270and 271.Fn free 272functions. 273Failing consistency checks will cause a panic or a system console 274message: 275.Bl -bullet -offset indent -compact 276.Pp 277.It 278panic: 279.Dq malloc: bogus type 280.It 281panic: 282.Dq malloc: allocation too large 283.It 284panic: 285.Dq malloc: wrong bucket 286.It 287panic: 288.Dq malloc: lost data 289.It 290panic: 291.Dq free: address 0x%x out of range 292.It 293panic: 294.Dq free: type %d out of range 295.It 296panic: 297.Dq free: unaligned addr Aq description of object 298.It 299panic: 300.Dq free: item modified 301.It 302panic: 303.Dq free: multiple free[s] 304.It 305.Dq Data modified on freelist: Aq description of object 306.El 307