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 required when running in an interrupt context. 160The 161.Fn malloc , 162.Fn realloc , 163and 164.Fn reallocf 165functions can only return 166.Dv NULL 167if 168.Dv M_NOWAIT 169is specified. 170.It Dv M_USE_RESERVE 171Indicates that the system can dig into its reserve in order to obtain the 172requested memory. This option used to be called M_KERNEL but has been 173renamed to something more obvious. This option has been deprecated and is 174slowly being removed from the kernel, and so should not be used with any new 175programming. 176.El 177.Pp 178The 179.Fa type 180argument is used to perform statistics on memory usage, and for 181basic sanity checks. 182The statistics can be examined by 183.Sq vmstat -m . 184.Pp 185A 186.Fa type 187is defined using the 188.Va malloc_type_t 189typedef via the 190.Fn MALLOC_DECLARE 191and 192.Fn MALLOC_DEFINE 193macros. 194.Bd -literal -offset indent 195/* sys/something/foo_extern.h */ 196 197MALLOC_DECLARE(M_FOOBUF); 198 199/* sys/something/foo_main.c */ 200 201MALLOC_DEFINE(M_FOOBUF, "foobuffers", "Buffers to foo data into the ether"); 202 203/* sys/something/foo_subr.c */ 204 205\&... 206MALLOC(buf, struct foo_buf *, sizeof *buf, M_FOOBUF, M_NOWAIT); 207 208.Ed 209.Sh RETURN VALUES 210The 211.Fn malloc , 212.Fn realloc , 213and 214.Fn reallocf 215functions return a kernel virtual address that is suitably aligned for 216storage of any type of object, or 217.Dv NULL 218if the request could not be satisfied (implying that 219.Dv M_NOWAIT 220was set). 221.Sh IMPLEMENTATION NOTES 222The memory allocator allocates memory in chunks that have size a power 223of two for requests up to the size of a page of memory. 224For larger requests, one or more pages is allocated. 225While it should not be relied upon, this information may be useful for 226optimizing the efficiency of memory use. 227.Pp 228Malloc flags documented above should 229.Em NOT 230be used with 231.Xr mbuf 9 232routines as it will cause undesired results. 233.Pp 234Any calls to 235.Fn malloc 236or 237.Fn free 238when holding a 239.Xr vnode 9 240interlock, will cause a LOR (Lock Order Reversal) due to the 241interwining of VM Objects and Vnodes. 242.Sh SEE ALSO 243.Xr vmstat 8 , 244.Xr vnode 9 245.Sh DIAGNOSTICS 246A kernel compiled with the 247.Dv DIAGNOSTIC 248configuration option attempts to detect memory corruption caused by 249such things as writing outside the allocated area and imbalanced calls to the 250.Fn malloc 251and 252.Fn free 253functions. 254Failing consistency checks will cause a panic or a system console 255message: 256.Bl -bullet -offset indent -compact 257.Pp 258.It 259panic: 260.Dq malloc: bogus type 261.It 262panic: 263.Dq malloc: allocation too large 264.It 265panic: 266.Dq malloc: wrong bucket 267.It 268panic: 269.Dq malloc: lost data 270.It 271panic: 272.Dq free: address 0x%x out of range 273.It 274panic: 275.Dq free: type %d out of range 276.It 277panic: 278.Dq free: unaligned addr Aq description of object 279.It 280panic: 281.Dq free: item modified 282.It 283panic: 284.Dq free: multiple free[s] 285.It 286.Dq Data modified on freelist: Aq description of object 287.El 288