diff options
Diffstat (limited to 'src/lib/libc/stdlib/malloc.3')
-rw-r--r-- | src/lib/libc/stdlib/malloc.3 | 350 |
1 files changed, 350 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..d5f8837ec2 --- /dev/null +++ b/src/lib/libc/stdlib/malloc.3 | |||
@@ -0,0 +1,350 @@ | |||
1 | .\" Copyright (c) 1980, 1991, 1993 | ||
2 | .\" The Regents of the University of California. All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" the American National Standards Committee X3, on Information | ||
6 | .\" Processing Systems. | ||
7 | .\" | ||
8 | .\" Redistribution and use in source and binary forms, with or without | ||
9 | .\" modification, are permitted provided that the following conditions | ||
10 | .\" are met: | ||
11 | .\" 1. Redistributions of source code must retain the above copyright | ||
12 | .\" notice, this list of conditions and the following disclaimer. | ||
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
14 | .\" notice, this list of conditions and the following disclaimer in the | ||
15 | .\" documentation and/or other materials provided with the distribution. | ||
16 | .\" 3. All advertising materials mentioning features or use of this software | ||
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 | ||
22 | .\" without specific prior written permission. | ||
23 | .\" | ||
24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
34 | .\" SUCH DAMAGE. | ||
35 | .\" | ||
36 | .\" $OpenBSD: malloc.3,v 1.9 1998/08/15 20:32:02 deraadt Exp $ | ||
37 | .\" | ||
38 | .Dd August 27, 1996 | ||
39 | .Dt MALLOC 3 | ||
40 | .Os OpenBSD | ||
41 | .Sh NAME | ||
42 | .Nm malloc , | ||
43 | .Nd general memory allocation function | ||
44 | .Pp | ||
45 | .Nm free , | ||
46 | .Nm cfree | ||
47 | .Nd free up memory allocated with malloc, calloc or realloc | ||
48 | .Pp | ||
49 | .Nm realloc | ||
50 | .Nd reallocation of memory function | ||
51 | .Sh SYNOPSIS | ||
52 | .Fd #include <stdlib.h> | ||
53 | .Ft void * | ||
54 | .Fn malloc "size_t size" | ||
55 | .Ft void | ||
56 | .Fn free "void *ptr" | ||
57 | .Ft void | ||
58 | .Fn cfree "void *ptr" | ||
59 | .Ft void * | ||
60 | .Fn realloc "void *ptr" "size_t size" | ||
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. If the space is of | ||
77 | .Em pagesize | ||
78 | or larger, the memory returned will be page-aligned. | ||
79 | .Pp | ||
80 | Allocation of a zero size object returns a pointer to a zero size object. | ||
81 | .Pp | ||
82 | The | ||
83 | .Fn free | ||
84 | function causes the space pointed to by | ||
85 | .Fa ptr | ||
86 | to be deallocated, that is, at least made available for further allocation, | ||
87 | but if possible, it will passed back to the kernel with | ||
88 | .Xr sbrk 2 . | ||
89 | If | ||
90 | .Fa ptr | ||
91 | is a null pointer, no action occurs. | ||
92 | .Pp | ||
93 | A | ||
94 | .Fn cfree | ||
95 | function is also provided for compatibility with old systems and other | ||
96 | .Nm malloc | ||
97 | libraries; it is simply an alias for | ||
98 | .Fn free . | ||
99 | .Pp | ||
100 | The | ||
101 | .Fn realloc | ||
102 | function changes the size of the object pointed to by | ||
103 | .Fa ptr | ||
104 | to the size specified by | ||
105 | .Fa size . | ||
106 | The contents of the object are unchanged up to the lesser | ||
107 | of the new and old sizes. | ||
108 | If the new size is larger, the value of the newly allocated portion | ||
109 | of the object is indeterminate and uninitialized. | ||
110 | If | ||
111 | .Fa ptr | ||
112 | is a null pointer, the | ||
113 | .Fn realloc | ||
114 | function behaves like the | ||
115 | .Fn malloc | ||
116 | function for the specified size. | ||
117 | If the space cannot be allocated, the object | ||
118 | pointed to by | ||
119 | .Fa ptr | ||
120 | is unchanged. | ||
121 | If | ||
122 | .Fa size | ||
123 | is zero and | ||
124 | .Fa ptr | ||
125 | is not a null pointer, the object it points to is freed and a new zero size | ||
126 | object is returned. | ||
127 | .Pp | ||
128 | When using | ||
129 | .Fn realloc | ||
130 | one must be careful to avoid the following idiom: | ||
131 | .Pp | ||
132 | .Bd -literal -offset indent | ||
133 | if ((p = realloc(p, nsize)) == NULL) | ||
134 | return NULL; | ||
135 | .Ed | ||
136 | .Pp | ||
137 | In most cases, this will result in a leak of memory. | ||
138 | As stated earlier, a return value of | ||
139 | .Fa NULL | ||
140 | indicates that the old object still remains allocated. | ||
141 | Better code looks like this: | ||
142 | .Bd -literal -offset indent | ||
143 | if ((p2 = realloc(p, nsize)) == NULL) { | ||
144 | if (p) | ||
145 | free(p); | ||
146 | p = NULL; | ||
147 | return NULL; | ||
148 | } | ||
149 | p = p2; | ||
150 | .Ed | ||
151 | .Pp | ||
152 | Malloc will first look for a symbolic link called | ||
153 | .Pa /etc/malloc.conf | ||
154 | and next check the environment for a variable called | ||
155 | .Ev MALLOC_OPTIONS | ||
156 | and finally for the global variable | ||
157 | .Va malloc_options | ||
158 | and scan them for flags in that order. | ||
159 | Flags are single letters, uppercase means on, lowercase means off. | ||
160 | .Bl -tag -width indent | ||
161 | .It A | ||
162 | ``abort'' malloc will coredump the process, rather than tolerate failure. | ||
163 | This is a very handy debugging aid, since the core file will represent the | ||
164 | time of failure, | ||
165 | rather than when the NULL pointer was accessed. | ||
166 | |||
167 | .It D | ||
168 | ``dump'' malloc will dump statistics in a file called ``malloc.out'' at exit. | ||
169 | This option requires the library to have been compiled with -DMALLOC_STATS in | ||
170 | order to have any effect. | ||
171 | |||
172 | .It J | ||
173 | ``junk'' fill some junk into the area allocated. | ||
174 | Currently junk is bytes of 0xd0, this is pronounced ``Duh'' :-) | ||
175 | |||
176 | .It H | ||
177 | ``hint'' pass a hint to the kernel about pages we don't use. If the | ||
178 | machine is paging a lot this may help a bit. | ||
179 | |||
180 | .It N | ||
181 | Do not output warning messages when encountering possible corruption | ||
182 | or bad pointers. | ||
183 | |||
184 | .It R | ||
185 | ``realloc'' always reallocate when | ||
186 | .Fn realloc | ||
187 | is called, even if the initial allocation was big enough. | ||
188 | This can substantially aid in compacting memory. | ||
189 | |||
190 | .It U | ||
191 | ``utrace'' generate entries for | ||
192 | .Xr ktrace 1 | ||
193 | for all operations. | ||
194 | Consult the source for this one. | ||
195 | |||
196 | .It X | ||
197 | ``xmalloc'' | ||
198 | rather than return failure, | ||
199 | .Xr abort 3 | ||
200 | the program with a diagnostic message on stderr. | ||
201 | It is the intention that this option be set at compile time by | ||
202 | including in the source: | ||
203 | .Bd -literal -offset indent | ||
204 | extern char *malloc_options; | ||
205 | malloc_options = "X"; | ||
206 | .Ed | ||
207 | |||
208 | .It Z | ||
209 | ``zero'' fill some junk into the area allocated (see ``J''), | ||
210 | except for the exact length the user asked for, which is zeroed. | ||
211 | |||
212 | .It < | ||
213 | ``Half the cache size'' Reduce the size of the cache by a factor of two. | ||
214 | |||
215 | .It > | ||
216 | ``Double the cache size'' Double the size of the cache by a factor of two. | ||
217 | .El | ||
218 | .Pp | ||
219 | So to set a systemwide reduction of cache size and coredumps on problems | ||
220 | one would: | ||
221 | .Li ln -s 'A<' /etc/malloc.conf | ||
222 | .Pp | ||
223 | The ``J'' and ``Z'' is mostly for testing and debugging, | ||
224 | if a program changes behavior if either of these options are used, | ||
225 | it is buggy. | ||
226 | .Pp | ||
227 | The default cache size is 16 pages. | ||
228 | .Sh ENVIRONMENT | ||
229 | See above. | ||
230 | .Sh RETURN VALUES | ||
231 | The | ||
232 | .Fn malloc | ||
233 | function returns | ||
234 | a pointer to the allocated space if successful; otherwise | ||
235 | a null pointer is returned. | ||
236 | .Pp | ||
237 | The | ||
238 | .Fn free | ||
239 | function returns no value. | ||
240 | .Pp | ||
241 | The | ||
242 | .Fn realloc | ||
243 | function a pointer to the possibly moved allocated space; | ||
244 | otherwise a null pointer is returned. | ||
245 | .Sh MESSAGES | ||
246 | If | ||
247 | .Fn malloc , | ||
248 | .Fn free | ||
249 | or | ||
250 | .Fn realloc | ||
251 | detects an error or warning condition, | ||
252 | a message will be printed to filedescriptor | ||
253 | 2 (not using stdio). | ||
254 | Errors will always result in the process being | ||
255 | .Xr abort 2 'ed, | ||
256 | If the ``A'' option has been specified, also warnings will | ||
257 | .Xr abort 2 | ||
258 | the process. | ||
259 | .Pp | ||
260 | Here is a brief description of the error messages and what they mean: | ||
261 | .Pp | ||
262 | ``(ES): mumble mumble mumble'': | ||
263 | malloc have been compiled with -DEXTRA_SANITY and something looks | ||
264 | fishy in there. Consult sources and or wizards. | ||
265 | .Pp | ||
266 | ``allocation failed'' | ||
267 | if the ``A'' option is specified it is an error for | ||
268 | .Fn malloc | ||
269 | or | ||
270 | .Fn realloc | ||
271 | to return NULL. | ||
272 | .Pp | ||
273 | ``mmap(2) failed, check limits.'' | ||
274 | This is a rather weird condition that is most likely to mean that | ||
275 | the system is seriously overloaded or that your ulimits are sick. | ||
276 | .Pp | ||
277 | ``freelist is destroyed.'' | ||
278 | mallocs internal freelist has been stomped on. | ||
279 | .Pp | ||
280 | Here is a brief description of the warning messages and what they mean: | ||
281 | .Pp | ||
282 | ``chunk/page is already free.'' | ||
283 | A pointer to a free chunk is attempted freed again. | ||
284 | .Pp | ||
285 | ``junk pointer, too high to make sense.'' | ||
286 | The pointer doesn't make sense. It's above the area of memory that | ||
287 | malloc knows something about. | ||
288 | This could be a pointer from some | ||
289 | .Xr mmap 2 'ed | ||
290 | memory. | ||
291 | .Pp | ||
292 | ``junk pointer, too low to make sense.'' | ||
293 | The pointer doesn't make sense. It's below the area of memory that | ||
294 | malloc knows something about. | ||
295 | This pointer probably came from your data or bss segments. | ||
296 | .Pp | ||
297 | ``malloc() has never been called.'' | ||
298 | Nothing has ever been allocated, yet something is being freed or | ||
299 | realloc'ed. | ||
300 | .Pp | ||
301 | ``modified (chunk-/page-) pointer.'' | ||
302 | The pointer passed to free or realloc has been modified. | ||
303 | .Pp | ||
304 | ``pointer to wrong page.'' | ||
305 | The pointer that malloc is trying to free is not pointing to | ||
306 | a sensible page. | ||
307 | .Pp | ||
308 | ``recursive call.'' | ||
309 | You have tried to call recursively into these functions. | ||
310 | I can only imagine this as happening if you call one of these | ||
311 | functions from a signal function, which happens to be called | ||
312 | while you're already in here. | ||
313 | Well, sorry to say: that's not supported. | ||
314 | If this is a problem for you I'd like to hear about it. It | ||
315 | would be possible to add a sigblock() around this package, | ||
316 | but it would have a performance penalty that is not acceptable | ||
317 | as the default. | ||
318 | .Pp | ||
319 | ``unknown char in MALLOC_OPTIONS'' | ||
320 | we found something we didn't understand. | ||
321 | .Sh SEE ALSO | ||
322 | .Xr brk 2 , | ||
323 | .Xr alloca 3 , | ||
324 | .Xr calloc 3 , | ||
325 | .Xr getpagesize 3 , | ||
326 | .Xr memory 3 | ||
327 | .Pa /usr/share/doc/papers/malloc.ascii.gz | ||
328 | .Sh STANDARDS | ||
329 | The | ||
330 | .Fn malloc | ||
331 | function conforms to | ||
332 | .St -ansiC . | ||
333 | .Sh HISTORY | ||
334 | The present implementation of malloc started out as a filesystem on a drum | ||
335 | attached to a 20bit binary challenged computer built with discrete germanium | ||
336 | transistors, and it has since graduated to handle primary storage rather than | ||
337 | secondary. | ||
338 | .Pp | ||
339 | The main difference from other malloc implementations are believed to be that | ||
340 | the free pages are not accessed until allocated. | ||
341 | Most malloc implementations will store a data structure containing a, | ||
342 | possibly double-, linked list in the free chunks of memory, used to tie | ||
343 | all the free memory together. | ||
344 | That is a quite suboptimal thing to do. | ||
345 | Every time the free-list is traversed, all the otherwise unused, and very | ||
346 | likely paged out, pages get faulted into primary memory, just to see what | ||
347 | lies after them in the list. | ||
348 | .Pp | ||
349 | On systems which are paging, this can make a factor five in difference on the | ||
350 | page-faults of a process. | ||