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