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