diff options
Diffstat (limited to 'src/lib/libc/stdlib/malloc.3')
-rw-r--r-- | src/lib/libc/stdlib/malloc.3 | 427 |
1 files changed, 397 insertions, 30 deletions
diff --git a/src/lib/libc/stdlib/malloc.3 b/src/lib/libc/stdlib/malloc.3 index 3bbf2bf65e..6a012fd23d 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.70 2011/07/22 07:00:44 otto 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 $Mdocdate: July 22 2011 $ |
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,390 @@ 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 | 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 (size && num > SIZE_MAX / 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 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 either placed on a list of free pages to make it available for future | ||
126 | allocation or, if required, to be returned to the kernel using | ||
127 | .Xr munmap 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 | .Sh MALLOC_OPTIONS | ||
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 internal | ||
219 | inconsistencies or incorrect usage. | ||
220 | This is the default and a very handy debugging aid, | ||
221 | since the core file represents the time of failure, | ||
222 | rather than when the bogus pointer was used. | ||
223 | .It Cm D | ||
224 | .Dq Dump . | ||
225 | .Fn malloc | ||
226 | will dump statistics to the file | ||
227 | .Pa ./malloc.out , | ||
228 | if it already exists, | ||
229 | at exit. | ||
230 | This option requires the library to have been compiled with -DMALLOC_STATS in | ||
231 | order to have any effect. | ||
232 | .It Cm F | ||
233 | .Dq Freeguard . | ||
234 | Enable use after free protection. | ||
235 | Unused pages on the freelist are read and write protected to | ||
236 | cause a segmentation fault upon access. | ||
237 | This will also switch off the delayed freeing of chunks, | ||
238 | reducing random behaviour but detecting double | ||
239 | .Fn free | ||
240 | calls as early as possible. | ||
241 | .It Cm G | ||
242 | .Dq Guard . | ||
243 | Enable guard pages. | ||
244 | Each page size or larger allocation is followed by a guard page that will | ||
245 | cause a segmentation fault upon any access. | ||
246 | .It Cm H | ||
247 | .Dq Hint . | ||
248 | Pass a hint to the kernel about pages we don't use. | ||
249 | If the machine is paging a lot this may help a bit. | ||
250 | .It Cm J | ||
251 | .Dq Junk . | ||
252 | Fill some junk into the area allocated. | ||
253 | Currently junk is bytes of 0xd0 when allocating; this is pronounced | ||
254 | .Dq Duh . | ||
255 | \&:-) | ||
256 | Freed chunks are filled with 0xdf. | ||
257 | .It Cm P | ||
258 | .Dq Move allocations within a page. | ||
259 | Allocations larger than half a page but smaller than a page | ||
260 | are aligned to the end of a page to catch buffer overruns in more | ||
261 | cases. | ||
262 | This is the default. | ||
263 | .It Cm R | ||
264 | .Dq realloc . | ||
265 | Always reallocate when | ||
266 | .Fn realloc | ||
267 | is called, even if the initial allocation was big enough. | ||
268 | This can substantially aid in compacting memory. | ||
269 | .\".Pp | ||
270 | .\".It Cm U | ||
271 | .\".Dq utrace . | ||
272 | .\"Generate entries for | ||
273 | .\".Xr ktrace 1 | ||
274 | .\"for all operations. | ||
275 | .\"Consult the source for this one. | ||
276 | .It Cm S | ||
277 | Enable all options suitable for security auditing. | ||
278 | .It Cm X | ||
279 | .Dq xmalloc . | ||
280 | Rather than return failure, | ||
281 | .Xr abort 3 | ||
282 | the program with a diagnostic message on stderr. | ||
283 | It is the intention that this option be set at compile time by | ||
284 | including in the source: | ||
285 | .Bd -literal -offset indent | ||
286 | extern char *malloc_options; | ||
287 | malloc_options = "X"; | ||
288 | .Ed | ||
289 | .Pp | ||
290 | Note that this will cause code that is supposed to handle | ||
291 | out-of-memory conditions gracefully to abort instead. | ||
292 | .It Cm Z | ||
293 | .Dq Zero . | ||
294 | Fill some junk into the area allocated (see | ||
295 | .Cm J ) , | ||
296 | except for the exact length the user asked for, which is zeroed. | ||
297 | .It Cm < | ||
298 | .Dq Half the cache size . | ||
299 | Decrease the size of the free page cache by a factor of two. | ||
300 | .It Cm > | ||
301 | .Dq Double the cache size . | ||
302 | Increase the size of the free page cache by a factor of two. | ||
303 | .El | ||
304 | .Pp | ||
305 | So to set a systemwide reduction of the cache to a quarter of the | ||
306 | default size and use guard pages: | ||
307 | .Dl # ln -s 'G\*(Lt\*(Lt' /etc/malloc.conf | ||
308 | .Pp | ||
309 | The flags are mostly for testing and debugging. | ||
310 | If a program changes behavior if any of these options (except | ||
311 | .Cm X ) | ||
312 | are used, | ||
313 | it is buggy. | ||
314 | .Pp | ||
315 | The default number of free pages cached is 64. | ||
65 | .Sh RETURN VALUES | 316 | .Sh RETURN VALUES |
66 | The | 317 | The |
67 | .Fn malloc | 318 | .Fn malloc |
68 | function returns | 319 | and |
69 | a pointer to the allocated space if successful; otherwise | 320 | .Fn calloc |
70 | a null pointer is returned. | 321 | functions return a pointer to the allocated space if successful; otherwise, |
322 | a null pointer is returned and | ||
323 | .Va errno | ||
324 | is set to | ||
325 | .Er ENOMEM . | ||
326 | .Pp | ||
327 | The | ||
328 | .Fn free | ||
329 | and | ||
330 | .Fn cfree | ||
331 | functions return no value. | ||
332 | .Pp | ||
333 | The | ||
334 | .Fn realloc | ||
335 | function returns a pointer to the (possibly moved) allocated space | ||
336 | if successful; otherwise, a null pointer is returned and | ||
337 | .Va errno | ||
338 | is set to | ||
339 | .Er ENOMEM . | ||
340 | .Sh ENVIRONMENT | ||
341 | .Bl -tag -width Ev | ||
342 | .It Ev MALLOC_OPTIONS | ||
343 | See above. | ||
344 | .El | ||
345 | .Sh FILES | ||
346 | .Bl -tag -width "/etc/malloc.conf" | ||
347 | .It Pa /etc/malloc.conf | ||
348 | symbolic link to filename containing option flags | ||
349 | .El | ||
350 | .Sh DIAGNOSTICS | ||
351 | If | ||
352 | .Fn malloc , | ||
353 | .Fn calloc , | ||
354 | .Fn realloc , | ||
355 | or | ||
356 | .Fn free | ||
357 | detect an error condition, | ||
358 | a message will be printed to file descriptor | ||
359 | 2 (not using stdio). | ||
360 | Errors will result in the process being aborted, | ||
361 | unless the | ||
362 | .Cm a | ||
363 | option has been specified. | ||
364 | .Pp | ||
365 | Here is a brief description of the error messages and what they mean: | ||
366 | .Bl -tag -width Ds | ||
367 | .It Dq out of memory | ||
368 | If the | ||
369 | .Cm X | ||
370 | option is specified it is an error for | ||
371 | .Fn malloc , | ||
372 | .Fn calloc , | ||
373 | or | ||
374 | .Fn realloc | ||
375 | to return | ||
376 | .Dv NULL . | ||
377 | .It Dq malloc init mmap failed | ||
378 | This is a rather weird condition that is most likely to indicate a | ||
379 | seriously overloaded system or a ulimit restriction. | ||
380 | .It Dq bogus pointer (double free?) | ||
381 | An attempt to | ||
382 | .Fn free | ||
383 | or | ||
384 | .Fn realloc | ||
385 | an unallocated pointer was made. | ||
386 | .It Dq chunk is already free | ||
387 | There was an attempt to free a chunk that had already been freed. | ||
388 | .It Dq modified chunk-pointer | ||
389 | The pointer passed to | ||
390 | .Fn free | ||
391 | or | ||
392 | .Fn realloc | ||
393 | has been modified. | ||
394 | .It Dq recursive call | ||
395 | An attempt was made to call recursively into these functions, i.e., from a | ||
396 | signal handler. | ||
397 | This behavior is not supported. | ||
398 | In particular, signal handlers should | ||
399 | .Em not | ||
400 | use any of the | ||
401 | .Fn malloc | ||
402 | functions nor utilize any other functions which may call | ||
403 | .Fn malloc | ||
404 | (e.g., | ||
405 | .Xr stdio 3 | ||
406 | routines). | ||
407 | .It Dq unknown char in MALLOC_OPTIONS | ||
408 | We found something we didn't understand. | ||
409 | .It Dq malloc cache overflow/underflow | ||
410 | The internal malloc page cache has been corrupted. | ||
411 | .It Dq malloc free slot lost | ||
412 | The internal malloc page cache has been corrupted. | ||
413 | .It Dq guard size | ||
414 | An inconsistent guard size was detected. | ||
415 | .It any other error | ||
416 | .Fn malloc | ||
417 | detected an internal error; | ||
418 | consult sources and/or wizards. | ||
419 | .El | ||
71 | .Sh SEE ALSO | 420 | .Sh SEE ALSO |
72 | .Xr brk 2 , | 421 | .Xr brk 2 , |
73 | .Xr getpagesize 2 , | 422 | .Xr mmap 2 , |
74 | .Xr free 3 , | 423 | .Xr munmap 2 , |
75 | .Xr calloc 3 , | ||
76 | .Xr alloca 3 , | 424 | .Xr alloca 3 , |
77 | .Xr realloc 3 , | 425 | .Xr getpagesize 3 , |
78 | .Xr memory 3 | 426 | .Xr posix_memalign 3 |
79 | .Sh STANDARDS | 427 | .Sh STANDARDS |
80 | The | 428 | The |
81 | .Fn malloc | 429 | .Fn malloc |
82 | function conforms to | 430 | function conforms to |
83 | .St -ansiC . | 431 | .St -ansiC . |
84 | .Sh BUGS | 432 | .Sh HISTORY |
85 | The current implementation of | 433 | The |
86 | .Xr malloc | 434 | .Nm |
87 | does not always fail gracefully when system | 435 | family of functions first appeared in |
88 | memory limits are approached. | 436 | .At v7 . |
89 | It may fail to allocate memory when larger free blocks could be broken | 437 | A new implementation by Chris Kingsley was introduced in |
90 | up, or when limits are exceeded because the size is rounded up. | 438 | .Bx 4.2 , |
91 | It is optimized for sizes that are powers of two. | 439 | followed by a complete rewrite by Poul-Henning Kamp which appeared in |
440 | .Fx 2.2 | ||
441 | and was included in | ||
442 | .Ox 2.0 . | ||
443 | These implementations were all | ||
444 | .Xr sbrk 2 | ||
445 | based. | ||
446 | In | ||
447 | .Ox 3.8 , | ||
448 | Thierry Deval rewrote | ||
449 | .Nm | ||
450 | to use the | ||
451 | .Xr mmap 2 | ||
452 | system call, | ||
453 | making the page addresses returned by | ||
454 | .Nm | ||
455 | random. | ||
456 | A rewrite by Otto Moerbeek introducing a new central data structure and more | ||
457 | randomization appeared in | ||
458 | .Ox 4.4 . | ||