diff options
Diffstat (limited to 'src/lib/libc/stdlib/malloc.3')
-rw-r--r-- | src/lib/libc/stdlib/malloc.3 | 419 |
1 files changed, 419 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..8f05c38e86 --- /dev/null +++ b/src/lib/libc/stdlib/malloc.3 | |||
@@ -0,0 +1,419 @@ | |||
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. All advertising materials mentioning features or use of this software | ||
18 | .\" must display the following acknowledgement: | ||
19 | .\" This product includes software developed by the University of | ||
20 | .\" California, Berkeley and its contributors. | ||
21 | .\" 4. Neither the name of the University nor the names of its contributors | ||
22 | .\" may be used to endorse or promote products derived from this software | ||
23 | .\" without specific prior written permission. | ||
24 | .\" | ||
25 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
26 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
27 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
28 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
29 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
30 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
31 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
32 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
33 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
34 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
35 | .\" SUCH DAMAGE. | ||
36 | .\" | ||
37 | .\" $OpenBSD: malloc.3,v 1.24 2001/12/05 09:49:39 deraadt Exp $ | ||
38 | .\" | ||
39 | .Dd August 27, 1996 | ||
40 | .Dt MALLOC 3 | ||
41 | .Os | ||
42 | .Sh NAME | ||
43 | .Nm malloc , | ||
44 | .Nm calloc , | ||
45 | .Nm realloc , | ||
46 | .Nm free , | ||
47 | .Nm cfree | ||
48 | .Nd memory allocation and deallocation | ||
49 | .Sh SYNOPSIS | ||
50 | .Fd #include <stdlib.h> | ||
51 | .Ft void * | ||
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 | ||
63 | .Sh DESCRIPTION | ||
64 | The | ||
65 | .Fn malloc | ||
66 | function allocates uninitialized space for an object whose | ||
67 | size is specified by | ||
68 | .Fa size . | ||
69 | The | ||
70 | .Fn malloc | ||
71 | function maintains multiple lists of free blocks according to size, allocating | ||
72 | space from the appropriate list. | ||
73 | .Pp | ||
74 | The allocated space is | ||
75 | suitably aligned (after possible pointer | ||
76 | coercion) for storage of any type of object. | ||
77 | If the space is of | ||
78 | .Em pagesize | ||
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. | ||
266 | .Sh RETURN VALUES | ||
267 | The | ||
268 | .Fn malloc | ||
269 | and | ||
270 | .Fn calloc | ||
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 | ||
385 | .Sh SEE ALSO | ||
386 | .Xr brk 2 , | ||
387 | .Xr alloca 3 , | ||
388 | .Xr getpagesize 3 , | ||
389 | .Xr memory 3 | ||
390 | .Pa /usr/share/doc/papers/malloc.ascii.gz | ||
391 | .Sh STANDARDS | ||
392 | The | ||
393 | .Fn malloc | ||
394 | function conforms to | ||
395 | .St -ansiC . | ||
396 | .Sh HISTORY | ||
397 | The present implementation of | ||
398 | .Fn malloc | ||
399 | started out as a filesystem on a drum | ||
400 | attached to a 20-bit binary challenged computer built with discrete germanium | ||
401 | transistors, and it has since graduated to handle primary storage rather than | ||
402 | secondary. | ||
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. | ||