diff options
Diffstat (limited to 'src/lib/libc/stdlib/malloc.3')
| -rw-r--r-- | src/lib/libc/stdlib/malloc.3 | 399 |
1 files changed, 370 insertions, 29 deletions
diff --git a/src/lib/libc/stdlib/malloc.3 b/src/lib/libc/stdlib/malloc.3 index 3bbf2bf65e..1154537385 100644 --- a/src/lib/libc/stdlib/malloc.3 +++ b/src/lib/libc/stdlib/malloc.3 | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | .\" Copyright (c) 1980, 1991 Regents of the University of California. | 1 | .\" |
| 2 | .\" All rights reserved. | 2 | .\" Copyright (c) 1980, 1991, 1993 |
| 3 | .\" The Regents of the University of California. All rights reserved. | ||
| 3 | .\" | 4 | .\" |
| 4 | .\" This code is derived from software contributed to Berkeley by | 5 | .\" This code is derived from software contributed to Berkeley by |
| 5 | .\" the American National Standards Committee X3, on Information | 6 | .\" the American National Standards Committee X3, on Information |
| @@ -13,11 +14,7 @@ | |||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | 14 | .\" 2. Redistributions in binary form must reproduce the above copyright |
| 14 | .\" notice, this list of conditions and the following disclaimer in the | 15 | .\" notice, this list of conditions and the following disclaimer in the |
| 15 | .\" documentation and/or other materials provided with the distribution. | 16 | .\" documentation and/or other materials provided with the distribution. |
| 16 | .\" 3. All advertising materials mentioning features or use of this software | 17 | .\" 3. Neither the name of the University nor the names of its contributors |
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | 18 | .\" may be used to endorse or promote products derived from this software |
| 22 | .\" without specific prior written permission. | 19 | .\" without specific prior written permission. |
| 23 | .\" | 20 | .\" |
| @@ -33,19 +30,32 @@ | |||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 30 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 34 | .\" SUCH DAMAGE. | 31 | .\" SUCH DAMAGE. |
| 35 | .\" | 32 | .\" |
| 36 | .\" from: @(#)malloc.3 6.7 (Berkeley) 6/29/91 | 33 | .\" $OpenBSD: malloc.3,v 1.36 2004/02/09 13:07:42 espie Exp $ |
| 37 | .\" $Id: malloc.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $ | ||
| 38 | .\" | 34 | .\" |
| 39 | .Dd June 29, 1991 | 35 | .Dd August 27, 1996 |
| 40 | .Dt MALLOC 3 | 36 | .Dt MALLOC 3 |
| 41 | .Os BSD 4 | 37 | .Os |
| 42 | .Sh NAME | 38 | .Sh NAME |
| 43 | .Nm malloc | 39 | .Nm malloc , |
| 44 | .Nd general memory allocation function | 40 | .Nm calloc , |
| 41 | .Nm realloc , | ||
| 42 | .Nm free , | ||
| 43 | .Nm cfree | ||
| 44 | .Nd memory allocation and deallocation | ||
| 45 | .Sh SYNOPSIS | 45 | .Sh SYNOPSIS |
| 46 | .Fd #include <stdlib.h> | 46 | .Fd #include <stdlib.h> |
| 47 | .Ft void * | 47 | .Ft void * |
| 48 | .Fn malloc "size_t size" | 48 | .Fn malloc "size_t size" |
| 49 | .Ft void * | ||
| 50 | .Fn calloc "size_t nmemb" "size_t size" | ||
| 51 | .Ft void * | ||
| 52 | .Fn realloc "void *ptr" "size_t size" | ||
| 53 | .Ft void | ||
| 54 | .Fn free "void *ptr" | ||
| 55 | .Ft void | ||
| 56 | .Fn cfree "void *ptr" | ||
| 57 | .Ft char * | ||
| 58 | .Va malloc_options ; | ||
| 49 | .Sh DESCRIPTION | 59 | .Sh DESCRIPTION |
| 50 | The | 60 | The |
| 51 | .Fn malloc | 61 | .Fn malloc |
| @@ -59,33 +69,364 @@ space from the appropriate list. | |||
| 59 | .Pp | 69 | .Pp |
| 60 | The allocated space is | 70 | The allocated space is |
| 61 | suitably aligned (after possible pointer | 71 | suitably aligned (after possible pointer |
| 62 | coercion) for storage of any type of object. If the space is of | 72 | coercion) for storage of any type of object. |
| 73 | If the space is of | ||
| 63 | .Em pagesize | 74 | .Em pagesize |
| 64 | or larger, the memory returned will be page-aligned. | 75 | or larger, the memory returned will be page-aligned. |
| 76 | .Pp | ||
| 77 | Allocation of a zero size object returns a pointer to a zero size object. | ||
| 78 | This zero size object is access protected, so any access to it will | ||
| 79 | generate an exception (SIGSEGV). | ||
| 80 | Many zero-sized objects can be placed consecutively in shared | ||
| 81 | protected pages. | ||
| 82 | The minimum size of the protection on each object is suitably aligned and | ||
| 83 | sized as previously stated, but the protection may extend further depending | ||
| 84 | on where in a protected zone the object lands. | ||
| 85 | .Pp | ||
| 86 | The | ||
| 87 | .Fn calloc | ||
| 88 | function allocates space for an array of | ||
| 89 | .Fa nmemb | ||
| 90 | objects, each of whose size is | ||
| 91 | .Fa size . | ||
| 92 | The space is initialized to all bits zero. | ||
| 93 | .Pp | ||
| 94 | The | ||
| 95 | .Fn free | ||
| 96 | function causes the space pointed to by | ||
| 97 | .Fa ptr | ||
| 98 | to be deallocated, that is, at least made available for further allocation, | ||
| 99 | but if possible, it will passed back to the kernel with | ||
| 100 | .Xr sbrk 2 . | ||
| 101 | If | ||
| 102 | .Fa ptr | ||
| 103 | is a null pointer, no action occurs. | ||
| 104 | .Pp | ||
| 105 | A | ||
| 106 | .Fn cfree | ||
| 107 | function is also provided for compatibility with old systems and other | ||
| 108 | .Nm malloc | ||
| 109 | libraries; it is simply an alias for | ||
| 110 | .Fn free . | ||
| 111 | .Pp | ||
| 112 | The | ||
| 113 | .Fn realloc | ||
| 114 | function changes the size of the object pointed to by | ||
| 115 | .Fa ptr | ||
| 116 | to | ||
| 117 | .Fa size | ||
| 118 | bytes and returns a pointer to the (possibly moved) object. | ||
| 119 | The contents of the object are unchanged up to the lesser | ||
| 120 | of the new and old sizes. | ||
| 121 | If the new size is larger, the value of the newly allocated portion | ||
| 122 | of the object is indeterminate and uninitialized. | ||
| 123 | If | ||
| 124 | .Fa ptr | ||
| 125 | is a null pointer, the | ||
| 126 | .Fn realloc | ||
| 127 | function behaves like the | ||
| 128 | .Fn malloc | ||
| 129 | function for the specified size. | ||
| 130 | If the space cannot be allocated, the object | ||
| 131 | pointed to by | ||
| 132 | .Fa ptr | ||
| 133 | is unchanged. | ||
| 134 | If | ||
| 135 | .Fa size | ||
| 136 | is zero and | ||
| 137 | .Fa ptr | ||
| 138 | is not a null pointer, the object it points to is freed and a new zero size | ||
| 139 | object is returned. | ||
| 140 | .Pp | ||
| 141 | When using | ||
| 142 | .Fn realloc | ||
| 143 | one must be careful to avoid the following idiom: | ||
| 144 | .Bd -literal -offset indent | ||
| 145 | size += 50; | ||
| 146 | if ((p = realloc(p, size)) == NULL) | ||
| 147 | return (NULL); | ||
| 148 | .Ed | ||
| 149 | .Pp | ||
| 150 | Do not adjust the variable describing how much memory has been allocated | ||
| 151 | until one knows the allocation has been successful. | ||
| 152 | This can cause aberrant program behavior if the incorrect size value is used. | ||
| 153 | In most cases, the above sample will also result in a leak of memory. | ||
| 154 | As stated earlier, a return value of | ||
| 155 | .Dv NULL | ||
| 156 | indicates that the old object still remains allocated. | ||
| 157 | Better code looks like this: | ||
| 158 | .Bd -literal -offset indent | ||
| 159 | newsize = size + 50; | ||
| 160 | if ((newp = realloc(p, newsize)) == NULL) { | ||
| 161 | free(p); | ||
| 162 | p = NULL; | ||
| 163 | size = 0; | ||
| 164 | return (NULL); | ||
| 165 | } | ||
| 166 | p = newp; | ||
| 167 | size = newsize; | ||
| 168 | .Ed | ||
| 169 | .Pp | ||
| 170 | Malloc will first look for a symbolic link called | ||
| 171 | .Pa /etc/malloc.conf | ||
| 172 | and next check the environment for a variable called | ||
| 173 | .Ev MALLOC_OPTIONS | ||
| 174 | and finally for the global variable | ||
| 175 | .Va malloc_options | ||
| 176 | and scan them for flags in that order. | ||
| 177 | Flags are single letters, uppercase means on, lowercase means off. | ||
| 178 | .Bl -tag -width indent | ||
| 179 | .It Cm A | ||
| 180 | .Dq Abort . | ||
| 181 | .Fn malloc | ||
| 182 | will coredump the process, rather than tolerate failure. | ||
| 183 | This is a very handy debugging aid, since the core file will represent the | ||
| 184 | time of failure, rather than when the null pointer was accessed. | ||
| 185 | .Pp | ||
| 186 | .It Cm D | ||
| 187 | .Dq Dump . | ||
| 188 | .Fn malloc | ||
| 189 | will dump statistics in a file called | ||
| 190 | .Pa malloc.out | ||
| 191 | at exit. | ||
| 192 | This option requires the library to have been compiled with -DMALLOC_STATS in | ||
| 193 | order to have any effect. | ||
| 194 | .Pp | ||
| 195 | .It Cm F | ||
| 196 | .Dq Freeguard . | ||
| 197 | Enable use after free protection. | ||
| 198 | Unused pages on the freelist are read and write protected to | ||
| 199 | cause a segmentation fault upon access. | ||
| 200 | .Pp | ||
| 201 | .It Cm G | ||
| 202 | .Dq Guard . | ||
| 203 | Enable guard pages and chunk randomization. | ||
| 204 | Each page size or larger allocation is followed by a guard page that will | ||
| 205 | cause a segmentation fault upon any access. | ||
| 206 | Smaller than page size chunks are returned in a random order. | ||
| 207 | .Pp | ||
| 208 | .It Cm H | ||
| 209 | .Dq Hint . | ||
| 210 | Pass a hint to the kernel about pages we don't use. | ||
| 211 | If the machine is paging a lot this may help a bit. | ||
| 212 | .Pp | ||
| 213 | .It Cm J | ||
| 214 | .Dq Junk . | ||
| 215 | Fill some junk into the area allocated. | ||
| 216 | Currently junk is bytes of 0xd0; this is pronounced | ||
| 217 | .Dq Duh . | ||
| 218 | \&:-) | ||
| 219 | .Pp | ||
| 220 | .It Cm N | ||
| 221 | Do not output warning messages when encountering possible corruption | ||
| 222 | or bad pointers. | ||
| 223 | .Pp | ||
| 224 | .It Cm R | ||
| 225 | .Dq realloc . | ||
| 226 | Always reallocate when | ||
| 227 | .Fn realloc | ||
| 228 | is called, even if the initial allocation was big enough. | ||
| 229 | This can substantially aid in compacting memory. | ||
| 230 | .\".Pp | ||
| 231 | .\".It Cm U | ||
| 232 | .\".Dq utrace . | ||
| 233 | .\"Generate entries for | ||
| 234 | .\".Xr ktrace 1 | ||
| 235 | .\"for all operations. | ||
| 236 | .\"Consult the source for this one. | ||
| 237 | .Pp | ||
| 238 | .It Cm X | ||
| 239 | .Dq xmalloc . | ||
| 240 | Rather than return failure, | ||
| 241 | .Xr abort 3 | ||
| 242 | the program with a diagnostic message on stderr. | ||
| 243 | It is the intention that this option be set at compile time by | ||
| 244 | including in the source: | ||
| 245 | .Bd -literal -offset indent | ||
| 246 | extern char *malloc_options; | ||
| 247 | malloc_options = "X"; | ||
| 248 | .Ed | ||
| 249 | .Pp | ||
| 250 | .It Cm Z | ||
| 251 | .Dq Zero . | ||
| 252 | Fill some junk into the area allocated (see | ||
| 253 | .Cm J ) , | ||
| 254 | except for the exact length the user asked for, which is zeroed. | ||
| 255 | .Pp | ||
| 256 | .It Cm < | ||
| 257 | .Dq Half the cache size . | ||
| 258 | Reduce the size of the cache by a factor of two. | ||
| 259 | .Pp | ||
| 260 | .It Cm > | ||
| 261 | .Dq Double the cache size . | ||
| 262 | Double the size of the cache by a factor of two. | ||
| 263 | .El | ||
| 264 | .Pp | ||
| 265 | So to set a systemwide reduction of cache size and coredumps on problems | ||
| 266 | one would: | ||
| 267 | .Li ln -s 'A<' /etc/malloc.conf | ||
| 268 | .Pp | ||
| 269 | The | ||
| 270 | .Cm J | ||
| 271 | and | ||
| 272 | .Cm Z | ||
| 273 | flags are mostly for testing and debugging. | ||
| 274 | If a program changes behavior if either of these options are used, | ||
| 275 | it is buggy. | ||
| 276 | .Pp | ||
| 277 | The default cache size is 16 pages. | ||
| 65 | .Sh RETURN VALUES | 278 | .Sh RETURN VALUES |
| 66 | The | 279 | The |
| 67 | .Fn malloc | 280 | .Fn malloc |
| 68 | function returns | 281 | and |
| 69 | a pointer to the allocated space if successful; otherwise | 282 | .Fn calloc |
| 70 | a null pointer is returned. | 283 | functions return a pointer to the allocated space if successful; otherwise, |
| 284 | a null pointer is returned and | ||
| 285 | .Va errno | ||
| 286 | is set to | ||
| 287 | .Er ENOMEM . | ||
| 288 | .Pp | ||
| 289 | The | ||
| 290 | .Fn free | ||
| 291 | and | ||
| 292 | .Fn cfree | ||
| 293 | functions return no value. | ||
| 294 | .Pp | ||
| 295 | The | ||
| 296 | .Fn realloc | ||
| 297 | function returns a pointer to the (possibly moved) allocated space | ||
| 298 | if successful; otherwise, a null pointer is returned and | ||
| 299 | .Va errno | ||
| 300 | is set to | ||
| 301 | .Er ENOMEM . | ||
| 302 | .Sh ENVIRONMENT | ||
| 303 | See above. | ||
| 304 | .Sh FILES | ||
| 305 | .Bl -tag -width "/etc/malloc.conf" | ||
| 306 | .It Pa /etc/malloc.conf | ||
| 307 | symbolic link to filename containing option flags | ||
| 308 | .El | ||
| 309 | .Sh DIAGNOSTICS | ||
| 310 | If | ||
| 311 | .Fn malloc , | ||
| 312 | .Fn calloc , | ||
| 313 | .Fn realloc , | ||
| 314 | or | ||
| 315 | .Fn free | ||
| 316 | detect an error or warning condition, | ||
| 317 | a message will be printed to file descriptor | ||
| 318 | 2 (not using stdio). | ||
| 319 | Errors will always result in the process being | ||
| 320 | .Xr abort 3 'ed. | ||
| 321 | If the | ||
| 322 | .Cm A | ||
| 323 | option has been specified, warnings will also | ||
| 324 | .Xr abort 3 | ||
| 325 | the process. | ||
| 326 | .Pp | ||
| 327 | Here is a brief description of the error messages and what they mean: | ||
| 328 | .Bl -tag -width Fl | ||
| 329 | .It Dq (ES): mumble mumble mumble | ||
| 330 | .Fn malloc | ||
| 331 | has been compiled with | ||
| 332 | .Dv \&-DEXTRA_SANITY | ||
| 333 | and something looks fishy in there. | ||
| 334 | Consult sources and/or wizards. | ||
| 335 | .It Dq allocation failed | ||
| 336 | If the | ||
| 337 | .Cm A | ||
| 338 | option is specified it is an error for | ||
| 339 | .Fn malloc , | ||
| 340 | .Fn calloc , | ||
| 341 | or | ||
| 342 | .Fn realloc | ||
| 343 | to return | ||
| 344 | .Dv NULL . | ||
| 345 | .It Dq mmap(2) failed, check limits. | ||
| 346 | This is a rather weird condition that is most likely to indicate a | ||
| 347 | seriously overloaded system or a | ||
| 348 | .Xr ulimit 1 | ||
| 349 | restriction. | ||
| 350 | .It Dq freelist is destroyed. | ||
| 351 | .Fn malloc Ns 's | ||
| 352 | internal freelist has been stomped on. | ||
| 353 | .El | ||
| 354 | .Pp | ||
| 355 | Here is a brief description of the warning messages and what they mean: | ||
| 356 | .Bl -tag -width Fl | ||
| 357 | .It Dq chunk/page is already free. | ||
| 358 | There was an attempt to free a chunk that had already been freed. | ||
| 359 | .It Dq junk pointer, too high to make sense. | ||
| 360 | The pointer doesn't make sense. | ||
| 361 | It's above the area of memory that | ||
| 362 | .Fn malloc | ||
| 363 | knows something about. | ||
| 364 | This could be a pointer from some | ||
| 365 | .Xr mmap 2 'ed | ||
| 366 | memory. | ||
| 367 | .It Dq junk pointer, too low to make sense. | ||
| 368 | The pointer doesn't make sense. | ||
| 369 | It's below the area of memory that | ||
| 370 | .Fn malloc | ||
| 371 | knows something about. | ||
| 372 | This pointer probably came from your data or bss segments. | ||
| 373 | .It Dq malloc() has never been called. | ||
| 374 | Nothing has ever been allocated, yet something is being freed or | ||
| 375 | realloc'ed. | ||
| 376 | .It Dq modified (chunk-/page-) pointer. | ||
| 377 | The pointer passed to free or realloc has been modified. | ||
| 378 | .It Dq pointer to wrong page. | ||
| 379 | The pointer that | ||
| 380 | .Fn malloc | ||
| 381 | is trying to free is not pointing to | ||
| 382 | a sensible page. | ||
| 383 | .It Dq recursive call. | ||
| 384 | An attempt was made to call recursively into these functions, i.e., from a | ||
| 385 | signal handler. | ||
| 386 | This behavior is not supported. | ||
| 387 | In particular, signal handlers should | ||
| 388 | .Em not | ||
| 389 | use any of the | ||
| 390 | .Fn malloc | ||
| 391 | functions nor utilize any other functions which may call | ||
| 392 | .Fn malloc | ||
| 393 | (e.g., | ||
| 394 | .Xr stdio 3 | ||
| 395 | routines). | ||
| 396 | .It Dq unknown char in MALLOC_OPTIONS | ||
| 397 | We found something we didn't understand. | ||
| 398 | .El | ||
| 71 | .Sh SEE ALSO | 399 | .Sh SEE ALSO |
| 72 | .Xr brk 2 , | 400 | .Xr brk 2 , |
| 73 | .Xr getpagesize 2 , | ||
| 74 | .Xr free 3 , | ||
| 75 | .Xr calloc 3 , | ||
| 76 | .Xr alloca 3 , | 401 | .Xr alloca 3 , |
| 77 | .Xr realloc 3 , | 402 | .Xr getpagesize 3 , |
| 78 | .Xr memory 3 | 403 | .Xr memory 3 |
| 79 | .Sh STANDARDS | 404 | .Sh STANDARDS |
| 80 | The | 405 | The |
| 81 | .Fn malloc | 406 | .Fn malloc |
| 82 | function conforms to | 407 | function conforms to |
| 83 | .St -ansiC . | 408 | .St -ansiC . |
| 84 | .Sh BUGS | 409 | .Sh HISTORY |
| 85 | The current implementation of | 410 | The present implementation of |
| 86 | .Xr malloc | 411 | .Fn malloc |
| 87 | does not always fail gracefully when system | 412 | started out as a filesystem on a drum |
| 88 | memory limits are approached. | 413 | attached to a 20-bit binary challenged computer built with discrete germanium |
| 89 | It may fail to allocate memory when larger free blocks could be broken | 414 | transistors, and it has since graduated to handle primary storage rather than |
| 90 | up, or when limits are exceeded because the size is rounded up. | 415 | secondary. |
| 91 | It is optimized for sizes that are powers of two. | 416 | .Pp |
| 417 | The main difference from other | ||
| 418 | .Fn malloc | ||
| 419 | implementations are believed to be that | ||
| 420 | the free pages are not accessed until allocated. | ||
| 421 | Most | ||
| 422 | .Fn malloc | ||
| 423 | implementations will store a data structure containing a, | ||
| 424 | possibly double-, linked list in the free chunks of memory, used to tie | ||
| 425 | all the free memory together. | ||
| 426 | That is a quite suboptimal thing to do. | ||
| 427 | Every time the free-list is traversed, all the otherwise unused, and very | ||
| 428 | likely paged out, pages get faulted into primary memory, just to see what | ||
| 429 | lies after them in the list. | ||
| 430 | .Pp | ||
| 431 | On systems which are paging, this can increase the page-faults | ||
| 432 | of a process by a factor of five. | ||
