1.\" $NetBSD: malloc.9,v 1.51 2014/12/27 20:45:08 wiz 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.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28.\" POSSIBILITY OF SUCH DAMAGE. 29.\" 30.Dd December 27, 2014 31.Dt MALLOC 9 32.Os 33.Sh NAME 34.Nm malloc , 35.Nm MALLOC , 36.Nm realloc , 37.Nm free , 38.Nm FREE , 39.Nm malloc_type_attach , 40.Nm malloc_type_detach , 41.Nm MALLOC_DEFINE , 42.Nm MALLOC_DECLARE 43.Nd general-purpose kernel memory allocator 44.Sh SYNOPSIS 45.In sys/malloc.h 46.Ft void * 47.Fn malloc "unsigned long size" "struct malloc_type *type" "int flags" 48.Ft void * 49.Fn realloc "void *addr" "unsigned long newsize" "struct malloc_type *type" \ 50 "int flags" 51.Ft void 52.Fn free "void *addr" "struct malloc_type *type" 53.Ft void 54.Fn malloc_type_attach "struct malloc_type *type" 55.Ft void 56.Fn malloc_type_detach "struct malloc_type *type" 57.In sys/mallocvar.h 58.Fn MALLOC_DEFINE "type" "shortdesc" "longdesc" 59.Fn MALLOC_JUSTDEFINE "type" "shortdesc" "longdesc" 60.Fn MALLOC_DECLARE "type" 61.Sh DESCRIPTION 62.Bf -symbolic 63These interfaces are being obsoleted and their new use is discouraged. 64For new code, use 65.Xr kmem 9 66for variable-sized or one-time allocations and 67.Xr pool_cache 9 68for frequent fixed-size allocations instead. 69.Ef 70.Pp 71The 72.Fn malloc 73function allocates uninitialized memory in kernel address space for an 74object whose size is specified by 75.Fa size . 76.Fn free 77releases memory at address 78.Fa addr 79that was previously allocated by 80.Fn malloc 81for re-use. 82Unlike 83.Xr free 3 , 84.Fn free 85does not accept an 86.Fa addr 87argument that is 88.Dv NULL . 89.Pp 90The 91.Fn realloc 92function changes the size of the previously allocated memory referenced 93by 94.Fa addr 95to 96.Fa size 97and returns a pointer to the 98.Pq possibly moved 99object. 100The memory contents are unchanged up to the lesser of the new 101and old sizes. 102If the new size is larger, the newly allocated memory is 103uninitialized. 104If the requested memory cannot be allocated, 105.Dv NULL 106is returned and the memory referenced by 107.Fa addr 108is unchanged. 109If 110.Fa addr 111is 112.Dv NULL , 113then 114.Fn realloc 115behaves exactly as 116.Fn malloc . 117If the new size is 0, then 118.Fn realloc 119behaves exactly as 120.Fn free . 121.Pp 122Unlike its standard C library counterpart 123.Pq Xr malloc 3 , 124the kernel version takes two more arguments. 125.Pp 126The 127.Fa flags 128argument further qualifies 129.Fn malloc 130operational characteristics as follows: 131.Bl -tag -offset indent -width M_CANFAIL 132.It Dv M_NOWAIT 133Causes 134.Fn malloc 135to return 136.Dv NULL 137if the request cannot be immediately fulfilled due to resource shortage. 138If this flag is not set 139(see 140.Dv M_WAITOK ) , 141.Fn malloc 142will never return 143.Dv NULL . 144.It Dv M_WAITOK 145By default, 146.Fn malloc 147may call 148.Xr cv_wait 9 149to wait for resources to be released by other processes, and this 150flag represents this behaviour. 151Note that 152.Dv M_WAITOK 153is conveniently defined to be 0, and hence may be or'ed into the 154.Fa flags 155argument to indicate that it's ok to wait for resources. 156.It Dv M_ZERO 157Causes the allocated memory to be set to all zeros. 158.It Dv M_CANFAIL 159Changes behaviour for 160.Dv M_WAITOK 161case - if the requested memory size is bigger than 162.Fn malloc 163can ever allocate, return failure, rather than calling 164.Xr panic 9 . 165This is different to M_NOWAIT, since 166the call can still wait for resources. 167.Pp 168Rather than depending on 169.Dv M_CANFAIL , 170kernel code should do proper bound checking itself. 171This flag should only be used in cases where this is not feasible. 172Since it can hide real kernel bugs, its usage is 173.Em strongly discouraged . 174.El 175.Pp 176The 177.Fa type 178argument describes the subsystem and/or use within a subsystem for which 179the allocated memory was needed, and is commonly used to maintain statistics 180about kernel memory usage and, optionally, enforce limits on this usage for 181certain memory types. 182.Pp 183In addition to some built-in generic types defined by the kernel 184memory allocator, subsystems may define their own types. 185.Pp 186The 187.Fn MALLOC_DEFINE 188macro defines a malloc type named 189.Fa type 190with the short description 191.Fa shortdesc , 192which must be a constant string; this description will be used for 193kernel memory statistics reporting. 194The 195.Fa longdesc 196argument, also a constant string, is intended as way to place a 197comment in the actual type definition, and is not currently stored 198in the type structure. 199If kernel memory statistics are being 200gathered, the system will choose a reasonable default limit for 201the malloc type. 202.Pp 203The 204.Fn MALLOC_DECLARE 205macro is intended for use in header files which are included by 206code which needs to use the malloc type, providing the necessary 207extern declaration. 208.Pp 209Code which includes 210\*[Lt]sys/malloc.h\*[Gt] 211does not need to include 212\*[Lt]sys/mallocvar.h\*[Gt] 213to get these macro definitions. 214The 215\*[Lt]sys/mallocvar.h\*[Gt] 216header file is intended for other header files which need to use the 217.Fn MALLOC_DECLARE 218macro. 219.Pp 220The 221.Fn malloc_type_attach 222function attaches the malloc type 223.Fa type 224to the kernel memory allocator. 225.Pp 226The 227.Fn malloc_type_detach 228function detaches the malloc type 229.Fa type 230previously attached with 231.Fn malloc_type_attach . 232.Pp 233The following generic malloc types are currently defined: 234.Pp 235.Bl -tag -offset indent -width XXXXXXXXXXXXXX -compact 236.It Dv M_DEVBUF 237Device driver memory. 238.It Dv M_DMAMAP 239.Xr bus_dma 9 240structures. 241.It Dv M_FREE 242Should be on free list. 243.It Dv M_PCB 244Protocol control block. 245.It Dv M_SOFTINTR 246Softinterrupt structures. 247.It Dv M_TEMP 248Misc temporary data buffers. 249.El 250.Pp 251Other malloc types are defined by the corresponding subsystem; see the 252documentation for that subsystem for information its available malloc 253types. 254.Pp 255Statistics based on the 256.Fa type 257argument are maintained only if the kernel option 258.Dv KMEMSTATS 259is used when compiling the kernel 260.Po 261the default in current 262.Nx 263kernels 264.Pc 265and can be examined by using 266.Sq vmstat -m . 267.Sh RETURN VALUES 268.Fn malloc 269returns a kernel virtual address that is suitably aligned for storage of 270any type of object. 271.Sh DIAGNOSTICS 272A kernel compiled with the 273.Dv DIAGNOSTIC 274configuration option attempts to detect memory corruption caused by 275such things as writing outside the allocated area and imbalanced calls to the 276.Fn malloc 277and 278.Fn free 279functions. 280Failing consistency checks will cause a panic or a system console message: 281.Pp 282.Bl -bullet -offset indent -compact 283.It 284panic: 285.Dq malloc - bogus type 286.It 287panic: 288.Dq malloc: out of space in kmem_map 289.It 290panic: 291.Dq malloc: allocation too large 292.It 293panic: 294.Dq malloc: wrong bucket 295.It 296panic: 297.Dq malloc: lost data 298.It 299panic: 300.Dq free: unaligned addr 301.It 302panic: 303.Dq free: duplicated free 304.It 305panic: 306.Dq free: multiple frees 307.It 308panic: 309.Dq init: minbucket too small/struct freelist too big 310.It 311.Dq multiply freed item Aq addr 312.It 313.Dq Data modified on freelist: Aq data object description 314.El 315.Sh SEE ALSO 316.Xr vmstat 1 , 317.Xr memoryallocators 9 318