malloc.9 (32eef9aeb1f39a1623cea55da147c89abbd5b9a5) | malloc.9 (44a8ff315e2a51614155fcbb86ab0577bb1f1152) |
---|---|
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 --- 40 unchanged lines hidden (view full) --- 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" | 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 --- 40 unchanged lines hidden (view full) --- 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" |
|
57.Sh DESCRIPTION 58The 59.Fn malloc 60function allocates uninitialized memory in kernel address space for an 61object whose size is specified by 62.Fa size . | 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 |
|
63.Fn free 64releases memory at address 65.Fa addr 66that was previously allocated by 67.Fn malloc | 68.Fn free 69releases memory at address 70.Fa addr 71that was previously allocated by 72.Fn malloc |
68for re-use. The memory is not zeroed. | 73for re-use. 74The memory is not zeroed. 75If 76.Fa addr 77is 78.Dv NULL , 79then 80.Fn free 81does nothing. 82.Pp |
69The | 83The |
84.Fn realloc 85function changes the size of the previously allocated memory referenced by 86.Fa addr 87to 88.Fa size 89bytes. 90The contents of the memory are unchanged up to the lesser of the new and 91old sizes. 92Note that the returned value may differ from 93.Fa addr . 94If the requested memory cannot be allocated, 95.Dv NULL 96is returned and the memory referenced by 97.Fa addr 98is valid and unchanged. 99If 100.Fa addr 101is 102.Dv NULL , 103the 104.Fn realloc 105function behaves identically to 106.Fn malloc 107for the specified size. 108.Pp 109The 110.Fn reallocf 111function call is identical to the realloc function call, except that it 112will free the passed pointer when the requested memory cannot be allocated. 113.Pp 114The |
|
70.Fn MALLOC 71macro variant is functionally equivalent to 72.Bd -literal -offset indent 73(space) = (cast)malloc((u_long)(size), type, flags) 74.Ed 75.Pp 76and the 77.Fn FREE --- 9 unchanged lines hidden (view full) --- 87argument further qualifies 88.Fn malloc Ns 's 89operational characteristics as follows: 90.Bl -tag -width indent 91.It Dv M_ZERO 92Causes the allocated memory to be set to all zeros. 93.It Dv M_NOWAIT 94Causes | 115.Fn MALLOC 116macro variant is functionally equivalent to 117.Bd -literal -offset indent 118(space) = (cast)malloc((u_long)(size), type, flags) 119.Ed 120.Pp 121and the 122.Fn FREE --- 9 unchanged lines hidden (view full) --- 132argument further qualifies 133.Fn malloc Ns 's 134operational characteristics as follows: 135.Bl -tag -width indent 136.It Dv M_ZERO 137Causes the allocated memory to be set to all zeros. 138.It Dv M_NOWAIT 139Causes |
95.Fn malloc | 140.Fn malloc , 141.Fn realloc , 142or 143.Fn reallocf |
96to return 97.Dv NULL 98if the request cannot be immediately fulfilled due to resource shortage. | 144to return 145.Dv NULL 146if the request cannot be immediately fulfilled due to resource shortage. |
99Otherwise, 100.Fn malloc 101may call sleep to wait for resources to be released by other processes. | 147Otherwise, the current process may be put to sleep to wait for 148resources to be released by other processes. |
102If this flag is set, 103.Fn malloc 104will return 105.Dv NULL | 149If this flag is set, 150.Fn malloc 151will return 152.Dv NULL |
106rather then block. Note that | 153rather then block. 154Note that |
107.Dv M_WAITOK 108is defined to be 0, meaning that blocking operation is the default. | 155.Dv M_WAITOK 156is defined to be 0, meaning that blocking operation is the default. |
157Also note that 158.Dv M_NOWAIT 159is required when running in an interrupt context. |
|
109.It Dv M_WAITOK 110Indicates that it is Ok to wait for resources. It is unconveniently 111defined as 0 so care should be taken never to compare against this value 112directly or try to AND it as a flag. The default operation is to block 113until the memory allocation succeeds. | 160.It Dv M_WAITOK 161Indicates that it is Ok to wait for resources. It is unconveniently 162defined as 0 so care should be taken never to compare against this value 163directly or try to AND it as a flag. The default operation is to block 164until the memory allocation succeeds. |
114.Fn malloc | 165.Fn malloc , 166.Fn realloc , 167and 168.Fn reallocf |
115can only return 116.Dv NULL 117if 118.Dv M_NOWAIT 119is specified. 120.It Dv M_USE_RESERVE 121Indicates that the system can dig into its reserve in order to obtain the 122requested memory. This option used to be called M_KERNEL but has been --- 29 unchanged lines hidden (view full) --- 152 153/* sys/something/foo_subr.c */ 154 155\&... 156MALLOC(buf, struct foo_buf *, sizeof *buf, M_FOOBUF, M_NOWAIT); 157 158.Ed 159.Sh RETURN VALUES | 169can only return 170.Dv NULL 171if 172.Dv M_NOWAIT 173is specified. 174.It Dv M_USE_RESERVE 175Indicates that the system can dig into its reserve in order to obtain the 176requested memory. This option used to be called M_KERNEL but has been --- 29 unchanged lines hidden (view full) --- 206 207/* sys/something/foo_subr.c */ 208 209\&... 210MALLOC(buf, struct foo_buf *, sizeof *buf, M_FOOBUF, M_NOWAIT); 211 212.Ed 213.Sh RETURN VALUES |
160.Fn malloc 161returns a kernel virtual address that is suitably aligned for storage of | 214.Fn malloc , 215.Fn realloc , 216and 217.Fn reallocf 218return a kernel virtual address that is suitably aligned for storage of |
162any type of object, or 163.Dv NULL | 219any type of object, or 220.Dv NULL |
164if the request could not be satisfied and | 221if the request could not be satisfied (implying that |
165.Dv M_NOWAIT | 222.Dv M_NOWAIT |
166was set. | 223was set). 224.Sh IMPLEMENTATION NOTES 225The memory allocator allocates memory in chunks that have size a power 226of two for requests up to the size of a page of memory. 227For larger requests, one or more pages is allocated. 228While it should not be relied upon, this information may be useful for 229optimizing the efficiency of memory use. |
167.Sh SEE ALSO 168.Xr vmstat 8 169.Sh DIAGNOSTICS 170A kernel compiled with the 171.Dv DIAGNOSTIC 172configuration option attempts to detect memory corruption caused by 173such things as writing outside the allocated area and imbalanced calls to the 174.Fn malloc --- 37 unchanged lines hidden --- | 230.Sh SEE ALSO 231.Xr vmstat 8 232.Sh DIAGNOSTICS 233A kernel compiled with the 234.Dv DIAGNOSTIC 235configuration option attempts to detect memory corruption caused by 236such things as writing outside the allocated area and imbalanced calls to the 237.Fn malloc --- 37 unchanged lines hidden --- |