Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | .\" Copyright (c) 2006 Apple Computer, Inc. All rights reserved. .\" .\" @APPLE_LICENSE_HEADER_START@ .\" .\" The contents of this file constitute Original Code as defined in and .\" are subject to the Apple Public Source License Version 1.1 (the .\" "License"). You may not use this file except in compliance with the .\" License. Please obtain a copy of the License at .\" http://www.apple.com/publicsource and read it before using this file. .\" .\" This Original Code and all software distributed under the License are .\" distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER .\" EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, .\" INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, .\" FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the .\" License for the specific language governing rights and limitations .\" under the License. .\" .\" @APPLE_LICENSE_HEADER_END@ .\" .Dd May 23, 2006 .Dt MALLOC 3 .Os .Sh NAME .Nm calloc , .Nm free , .Nm malloc , .Nm realloc , .Nm reallocf , .Nm valloc .Nd memory allocation .Sh SYNOPSIS .In stdlib.h .Ft void * .Fo calloc .Fa "size_t count" .Fa "size_t size" .Fc .Ft void .Fo free .Fa "void *ptr" .Fc .Ft void * .Fo malloc .Fa "size_t size" .Fc .Ft void * .Fo realloc .Fa "void *ptr" .Fa "size_t size" .Fc .Ft void * .Fo reallocf .Fa "void *ptr" .Fa "size_t size" .Fc .Ft void * .Fo valloc .Fa "size_t size" .Fc .Sh DESCRIPTION The .Fn malloc , .Fn calloc , .Fn valloc , .Fn realloc , and .Fn reallocf functions allocate memory. The allocated memory is aligned such that it can be used for any data type, including AltiVec- and SSE-related types. The .Fn free function frees allocations that were created via the preceding allocation functions. .Pp The .Fn malloc function allocates .Fa size bytes of memory and returns a pointer to the allocated memory. .Pp The .Fn calloc function contiguously allocates enough space for .Fa count objects that are .Fa size bytes of memory each and returns a pointer to the allocated memory. The allocated memory is filled with bytes of value zero. .Pp The .Fn valloc function allocates .Fa size bytes of memory and returns a pointer to the allocated memory. The allocated memory is aligned on a page boundary. .Pp The .Fn realloc function tries to change the size of the allocation pointed to by .Fa ptr to .Fa size , and returns .Fa ptr . If there is not enough room to enlarge the memory allocation pointed to by .Fa ptr , .Fn realloc creates a new allocation, copies as much of the old data pointed to by .Fa ptr as will fit to the new allocation, frees the old allocation, and returns a pointer to the allocated memory. If .Fa ptr is .Dv NULL , .Fn realloc is identical to a call to .Fn malloc for .Fa size bytes. If .Fa size is zero and .Fa ptr is not .Dv NULL , a new, minimum sized object is allocated and the original object is freed. .Pp The .Fn reallocf function is identical to the .Fn realloc function, except that it will free the passed pointer when the requested memory cannot be allocated. This is a .Fx specific API designed to ease the problems with traditional coding styles for realloc causing memory leaks in libraries. .Pp The .Fn free function deallocates the memory allocation pointed to by .Fa ptr . .Sh RETURN VALUES If successful, .Fn calloc , .Fn malloc , .Fn realloc , .Fn reallocf , and .Fn valloc functions return a pointer to allocated memory. If there is an error, they return a .Dv NULL pointer and set .Va errno to .Er ENOMEM . .Pp For .Fn realloc , the input pointer is still valid if reallocation failed. For .Fn reallocf , the input pointer will have been freed if reallocation failed. .Pp The .Fn free function does not return a value. .Sh DEBUGGING ALLOCATION ERRORS A number of facilities are provided to aid in debugging allocation errors in applications. These facilities are primarily controlled via environment variables. The recognized environment variables and their meanings are documented below. .Sh ENVIRONMENT The following environment variables change the behavior of the allocation-related functions. .Bl -tag -width ".Ev MallocStackLoggingNoCompact" .It Ev MallocLogFile <f> Create/append messages to the given file path .Fa <f> instead of writing to the standard error. .It Ev MallocGuardEdges If set, add a guard page before and after each large block. .It Ev MallocDoNotProtectPrelude If set, do not add a guard page before large blocks, even if the .Ev MallocGuardEdges environment variable is set. .It Ev MallocDoNotProtectPostlude If set, do not add a guard page after large blocks, even if the .Ev MallocGuardEdges environment variable is set. .It Ev MallocStackLogging If set, record all stacks, so that tools like .Nm leaks can be used. .It Ev MallocStackLoggingNoCompact If set, record all stacks in a manner that is compatible with the .Nm malloc_history program. .It Ev MallocPreScribble If set, fill memory that has been allocated with 0xaa bytes. This increases the likelihood that a program making assumptions about the contents of freshly allocated memory will fail. .It Ev MallocScribble If set, fill memory that has been deallocated with 0x55 bytes. This increases the likelihood that a program will fail due to accessing memory that is no longer allocated. .It Ev MallocCheckHeapStart <s> If set, specifies the number of allocations .Fa <s> to wait before begining periodic heap checks every .Fa <n> as specified by .Ev MallocCheckHeapEach . If .Ev MallocCheckHeapStart is set but .Ev MallocCheckHeapEach is not specified, the default check repetition is 1000. .It Ev MallocCheckHeapEach <n> If set, run a consistency check on the heap every .Fa <n> operations. .Ev MallocCheckHeapEach is only meaningful if .Ev MallocCheckHeapStart is also set. .It Ev MallocCheckHeapSleep <t> Sets the number of seconds to sleep (waiting for a debugger to attach) when .Ev MallocCheckHeapStart is set and a heap corruption is detected. The default is 100 seconds. Setting this to zero means not to sleep at all. Setting this to a negative number means to sleep (for the positive number of seconds) only the very first time a heap corruption is detected. .It Ev MallocCheckHeapAbort <b> When .Ev MallocCheckHeapStart is set and this is set to a non-zero value, causes .Xr abort 3 to be called if a heap corruption is detected, instead of any sleeping. .It Ev MallocErrorAbort If set, causes .Xr abort 3 to be called if an error was encountered in .Xr malloc 3 or .Xr free 3 , such as a calling .Xr free 3 on a pointer previously freed. .It Ev MallocHelp If set, print a list of environment variables that are paid heed to by the allocation-related functions, along with short descriptions. The list should correspond to this documentation. .El .Sh DIAGNOSTIC MESSAGES .Sh SEE ALSO .Xr leaks 1 , .Xr malloc_history 1 , .Xr abort 3 , .Xr malloc_size 3 .Pa /Developer/ADC Reference Library/releasenotes/DeveloperTools/MallocOptions.html |