diff options
Diffstat (limited to 'src/lib/libc/stdlib/malloc.3')
-rw-r--r-- | src/lib/libc/stdlib/malloc.3 | 426 |
1 files changed, 426 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..2af8900656 --- /dev/null +++ b/src/lib/libc/stdlib/malloc.3 | |||
@@ -0,0 +1,426 @@ | |||
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.32 2003/10/16 17:05:04 tedu 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 | 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 | .Pp | ||
145 | .Bd -literal -offset indent | ||
146 | size += 50; | ||
147 | if ((p = realloc(p, size)) == NULL) | ||
148 | return (NULL); | ||
149 | .Ed | ||
150 | .Pp | ||
151 | Do not adjust the variable describing how much memory has been allocated | ||
152 | until one knows the allocation has been successful. | ||
153 | This can cause aberrant program behavior if the incorrect size value is used. | ||
154 | In most cases, the above sample will also 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 | newsize = size + 50; | ||
161 | if ((newp = realloc(p, newsize)) == NULL) { | ||
162 | free(p); | ||
163 | p = NULL; | ||
164 | size = 0; | ||
165 | return (NULL); | ||
166 | } | ||
167 | p = newp; | ||
168 | size = newsize; | ||
169 | .Ed | ||
170 | .Pp | ||
171 | Malloc will first look for a symbolic link called | ||
172 | .Pa /etc/malloc.conf | ||
173 | and next check the environment for a variable called | ||
174 | .Ev MALLOC_OPTIONS | ||
175 | and finally for the global variable | ||
176 | .Va malloc_options | ||
177 | and scan them for flags in that order. | ||
178 | Flags are single letters, uppercase means on, lowercase means off. | ||
179 | .Bl -tag -width indent | ||
180 | .It Cm A | ||
181 | .Dq Abort . | ||
182 | .Fn malloc | ||
183 | will coredump the process, rather than tolerate failure. | ||
184 | This is a very handy debugging aid, since the core file will represent the | ||
185 | time of failure, rather than when the null pointer was accessed. | ||
186 | .Pp | ||
187 | .It Cm D | ||
188 | .Dq Dump . | ||
189 | .Fn malloc | ||
190 | will dump statistics in a file called | ||
191 | .Pa malloc.out | ||
192 | at exit. | ||
193 | This option requires the library to have been compiled with -DMALLOC_STATS in | ||
194 | order to have any effect. | ||
195 | .Pp | ||
196 | .It Cm G | ||
197 | Enable guard pages and chunk randomization. | ||
198 | Each page size or larger allocation is followed by a guard page that will | ||
199 | cause a segmentation fault upon any access. | ||
200 | Smaller than page size chunks are returned in a random order. | ||
201 | .Pp | ||
202 | .It Cm J | ||
203 | .Dq Junk . | ||
204 | Fill some junk into the area allocated. | ||
205 | Currently junk is bytes of 0xd0; this is pronounced | ||
206 | .Dq Duh . | ||
207 | \&:-) | ||
208 | .Pp | ||
209 | .It Cm H | ||
210 | .Dq Hint . | ||
211 | Pass a hint to the kernel about pages we don't use. | ||
212 | If the machine is paging a lot this may help a bit. | ||
213 | .Pp | ||
214 | .It Cm N | ||
215 | Do not output warning messages when encountering possible corruption | ||
216 | or bad pointers. | ||
217 | .Pp | ||
218 | .It Cm R | ||
219 | .Dq realloc . | ||
220 | Always reallocate when | ||
221 | .Fn realloc | ||
222 | is called, even if the initial allocation was big enough. | ||
223 | This can substantially aid in compacting memory. | ||
224 | .\".Pp | ||
225 | .\".It Cm U | ||
226 | .\".Dq utrace . | ||
227 | .\"Generate entries for | ||
228 | .\".Xr ktrace 1 | ||
229 | .\"for all operations. | ||
230 | .\"Consult the source for this one. | ||
231 | .Pp | ||
232 | .It Cm X | ||
233 | .Dq xmalloc . | ||
234 | Rather than return failure, | ||
235 | .Xr abort 3 | ||
236 | the program with a diagnostic message on stderr. | ||
237 | It is the intention that this option be set at compile time by | ||
238 | including in the source: | ||
239 | .Bd -literal -offset indent | ||
240 | extern char *malloc_options; | ||
241 | malloc_options = "X"; | ||
242 | .Ed | ||
243 | .Pp | ||
244 | .It Cm Z | ||
245 | .Dq Zero . | ||
246 | Fill some junk into the area allocated (see | ||
247 | .Cm J ) , | ||
248 | except for the exact length the user asked for, which is zeroed. | ||
249 | .Pp | ||
250 | .It Cm < | ||
251 | .Dq Half the cache size . | ||
252 | Reduce the size of the cache by a factor of two. | ||
253 | .Pp | ||
254 | .It Cm > | ||
255 | .Dq Double the cache size . | ||
256 | Double the size of the cache by a factor of two. | ||
257 | .El | ||
258 | .Pp | ||
259 | So to set a systemwide reduction of cache size and coredumps on problems | ||
260 | one would: | ||
261 | .Li ln -s 'A<' /etc/malloc.conf | ||
262 | .Pp | ||
263 | The | ||
264 | .Cm J | ||
265 | and | ||
266 | .Cm Z | ||
267 | flags are mostly for testing and debugging. | ||
268 | If a program changes behavior if either of these options are used, | ||
269 | it is buggy. | ||
270 | .Pp | ||
271 | The default cache size is 16 pages. | ||
272 | .Sh RETURN VALUES | ||
273 | The | ||
274 | .Fn malloc | ||
275 | and | ||
276 | .Fn calloc | ||
277 | functions return a pointer to the allocated space if successful; otherwise, | ||
278 | a null pointer is returned and | ||
279 | .Va errno | ||
280 | is set to | ||
281 | .Er ENOMEM . | ||
282 | .Pp | ||
283 | The | ||
284 | .Fn free | ||
285 | and | ||
286 | .Fn cfree | ||
287 | functions return no value. | ||
288 | .Pp | ||
289 | The | ||
290 | .Fn realloc | ||
291 | function returns a pointer to the (possibly moved) allocated space | ||
292 | if successful; otherwise, a null pointer is returned and | ||
293 | .Va errno | ||
294 | is set to | ||
295 | .Er ENOMEM . | ||
296 | .Sh ENVIRONMENT | ||
297 | See above. | ||
298 | .Sh FILES | ||
299 | .Bl -tag -width "/etc/malloc.conf" | ||
300 | .It Pa /etc/malloc.conf | ||
301 | symbolic link to filename containing option flags | ||
302 | .El | ||
303 | .Sh DIAGNOSTICS | ||
304 | If | ||
305 | .Fn malloc , | ||
306 | .Fn calloc , | ||
307 | .Fn realloc , | ||
308 | or | ||
309 | .Fn free | ||
310 | detect an error or warning condition, | ||
311 | a message will be printed to file descriptor | ||
312 | 2 (not using stdio). | ||
313 | Errors will always result in the process being | ||
314 | .Xr abort 3 'ed. | ||
315 | If the | ||
316 | .Cm A | ||
317 | option has been specified, warnings will also | ||
318 | .Xr abort 3 | ||
319 | the process. | ||
320 | .Pp | ||
321 | Here is a brief description of the error messages and what they mean: | ||
322 | .Bl -tag -width Fl | ||
323 | .It Dq (ES): mumble mumble mumble | ||
324 | .Fn malloc | ||
325 | has been compiled with | ||
326 | .Dv \&-DEXTRA_SANITY | ||
327 | and something looks fishy in there. | ||
328 | Consult sources and/or wizards. | ||
329 | .It Dq allocation failed | ||
330 | If the | ||
331 | .Cm A | ||
332 | option is specified it is an error for | ||
333 | .Fn malloc , | ||
334 | .Fn calloc , | ||
335 | or | ||
336 | .Fn realloc | ||
337 | to return | ||
338 | .Dv NULL . | ||
339 | .It Dq mmap(2) failed, check limits. | ||
340 | This is a rather weird condition that is most likely to indicate a | ||
341 | seriously overloaded system or a | ||
342 | .Xr ulimit 1 | ||
343 | restriction. | ||
344 | .It Dq freelist is destroyed. | ||
345 | .Fn malloc Ns 's | ||
346 | internal freelist has been stomped on. | ||
347 | .El | ||
348 | .Pp | ||
349 | Here is a brief description of the warning messages and what they mean: | ||
350 | .Bl -tag -width Fl | ||
351 | .It Dq chunk/page is already free. | ||
352 | A pointer to a free chunk is attempted freed again. | ||
353 | .It Dq junk pointer, too high to make sense. | ||
354 | The pointer doesn't make sense. | ||
355 | It's above the area of memory that | ||
356 | .Fn malloc | ||
357 | knows something about. | ||
358 | This could be a pointer from some | ||
359 | .Xr mmap 2 'ed | ||
360 | memory. | ||
361 | .It Dq junk pointer, too low to make sense. | ||
362 | The pointer doesn't make sense. | ||
363 | It's below the area of memory that | ||
364 | .Fn malloc | ||
365 | knows something about. | ||
366 | This pointer probably came from your data or bss segments. | ||
367 | .It Dq malloc() has never been called. | ||
368 | Nothing has ever been allocated, yet something is being freed or | ||
369 | realloc'ed. | ||
370 | .It Dq modified (chunk-/page-) pointer. | ||
371 | The pointer passed to free or realloc has been modified. | ||
372 | .It Dq pointer to wrong page. | ||
373 | The pointer that | ||
374 | .Fn malloc | ||
375 | is trying to free is not pointing to | ||
376 | a sensible page. | ||
377 | .It Dq recursive call. | ||
378 | An attempt was made to call recursively into these functions, i.e., from a | ||
379 | signal handler. | ||
380 | This behavior is not supported. | ||
381 | In particular, signal handlers should | ||
382 | .Em not | ||
383 | use any of the | ||
384 | .Fn malloc | ||
385 | functions nor utilize any other functions which may call | ||
386 | .Fn malloc | ||
387 | (e.g., | ||
388 | .Xr stdio 3 | ||
389 | routines). | ||
390 | .It Dq unknown char in MALLOC_OPTIONS | ||
391 | We found something we didn't understand. | ||
392 | .El | ||
393 | .Sh SEE ALSO | ||
394 | .Xr brk 2 , | ||
395 | .Xr alloca 3 , | ||
396 | .Xr getpagesize 3 , | ||
397 | .Xr memory 3 | ||
398 | .Sh STANDARDS | ||
399 | The | ||
400 | .Fn malloc | ||
401 | function conforms to | ||
402 | .St -ansiC . | ||
403 | .Sh HISTORY | ||
404 | The present implementation of | ||
405 | .Fn malloc | ||
406 | started out as a filesystem on a drum | ||
407 | attached to a 20-bit binary challenged computer built with discrete germanium | ||
408 | transistors, and it has since graduated to handle primary storage rather than | ||
409 | secondary. | ||
410 | .Pp | ||
411 | The main difference from other | ||
412 | .Fn malloc | ||
413 | implementations are believed to be that | ||
414 | the free pages are not accessed until allocated. | ||
415 | Most | ||
416 | .Fn malloc | ||
417 | implementations will store a data structure containing a, | ||
418 | possibly double-, linked list in the free chunks of memory, used to tie | ||
419 | all the free memory together. | ||
420 | That is a quite suboptimal thing to do. | ||
421 | Every time the free-list is traversed, all the otherwise unused, and very | ||
422 | likely paged out, pages get faulted into primary memory, just to see what | ||
423 | lies after them in the list. | ||
424 | .Pp | ||
425 | On systems which are paging, this can make a factor five in difference on the | ||
426 | page-faults of a process. | ||