diff options
Diffstat (limited to '')
-rw-r--r-- | src/lib/libc/stdlib/malloc.3 | 355 |
1 files changed, 355 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..9edff6709c --- /dev/null +++ b/src/lib/libc/stdlib/malloc.3 | |||
@@ -0,0 +1,355 @@ | |||
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.11 1999/03/23 21:07:57 millert 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 | ||
105 | .Fa size | ||
106 | bytes and returns a pointer to the (possibly moved) object. | ||
107 | The contents of the object are unchanged up to the lesser | ||
108 | of the new and old sizes. | ||
109 | If the new size is larger, the value of the newly allocated portion | ||
110 | of the object is indeterminate and uninitialized. | ||
111 | If | ||
112 | .Fa ptr | ||
113 | is a null pointer, the | ||
114 | .Fn realloc | ||
115 | function behaves like the | ||
116 | .Fn malloc | ||
117 | function for the specified size. | ||
118 | If the space cannot be allocated, the object | ||
119 | pointed to by | ||
120 | .Fa ptr | ||
121 | is unchanged. | ||
122 | If | ||
123 | .Fa size | ||
124 | is zero and | ||
125 | .Fa ptr | ||
126 | is not a null pointer, the object it points to is freed and a new zero size | ||
127 | object is returned. | ||
128 | .Pp | ||
129 | When using | ||
130 | .Fn realloc | ||
131 | one must be careful to avoid the following idiom: | ||
132 | .Pp | ||
133 | .Bd -literal -offset indent | ||
134 | if ((p = realloc(p, nsize)) == NULL) | ||
135 | return NULL; | ||
136 | .Ed | ||
137 | .Pp | ||
138 | In most cases, this will result in a leak of memory. | ||
139 | As stated earlier, a return value of | ||
140 | .Fa NULL | ||
141 | indicates that the old object still remains allocated. | ||
142 | Better code looks like this: | ||
143 | .Bd -literal -offset indent | ||
144 | if ((p2 = realloc(p, nsize)) == NULL) { | ||
145 | if (p) | ||
146 | free(p); | ||
147 | p = NULL; | ||
148 | return NULL; | ||
149 | } | ||
150 | p = p2; | ||
151 | .Ed | ||
152 | .Pp | ||
153 | Malloc will first look for a symbolic link called | ||
154 | .Pa /etc/malloc.conf | ||
155 | and next check the environment for a variable called | ||
156 | .Ev MALLOC_OPTIONS | ||
157 | and finally for the global variable | ||
158 | .Va malloc_options | ||
159 | and scan them for flags in that order. | ||
160 | Flags are single letters, uppercase means on, lowercase means off. | ||
161 | .Bl -tag -width indent | ||
162 | .It A | ||
163 | ``abort'' malloc will coredump the process, rather than tolerate failure. | ||
164 | This is a very handy debugging aid, since the core file will represent the | ||
165 | time of failure, | ||
166 | rather than when the NULL pointer was accessed. | ||
167 | |||
168 | .It D | ||
169 | ``dump'' malloc will dump statistics in a file called ``malloc.out'' at exit. | ||
170 | This option requires the library to have been compiled with -DMALLOC_STATS in | ||
171 | order to have any effect. | ||
172 | |||
173 | .It J | ||
174 | ``junk'' fill some junk into the area allocated. | ||
175 | Currently junk is bytes of 0xd0, this is pronounced ``Duh'' :-) | ||
176 | |||
177 | .It H | ||
178 | ``hint'' pass a hint to the kernel about pages we don't use. If the | ||
179 | machine is paging a lot this may help a bit. | ||
180 | |||
181 | .It N | ||
182 | Do not output warning messages when encountering possible corruption | ||
183 | or bad pointers. | ||
184 | |||
185 | .It R | ||
186 | ``realloc'' always reallocate when | ||
187 | .Fn realloc | ||
188 | is called, even if the initial allocation was big enough. | ||
189 | This can substantially aid in compacting memory. | ||
190 | |||
191 | .It U | ||
192 | ``utrace'' generate entries for | ||
193 | .Xr ktrace 1 | ||
194 | for all operations. | ||
195 | Consult the source for this one. | ||
196 | |||
197 | .It X | ||
198 | ``xmalloc'' | ||
199 | rather than return failure, | ||
200 | .Xr abort 3 | ||
201 | the program with a diagnostic message on stderr. | ||
202 | It is the intention that this option be set at compile time by | ||
203 | including in the source: | ||
204 | .Bd -literal -offset indent | ||
205 | extern char *malloc_options; | ||
206 | malloc_options = "X"; | ||
207 | .Ed | ||
208 | |||
209 | .It Z | ||
210 | ``zero'' fill some junk into the area allocated (see ``J''), | ||
211 | except for the exact length the user asked for, which is zeroed. | ||
212 | |||
213 | .It < | ||
214 | ``Half the cache size'' Reduce the size of the cache by a factor of two. | ||
215 | |||
216 | .It > | ||
217 | ``Double the cache size'' Double the size of the cache by a factor of two. | ||
218 | .El | ||
219 | .Pp | ||
220 | So to set a systemwide reduction of cache size and coredumps on problems | ||
221 | one would: | ||
222 | .Li ln -s 'A<' /etc/malloc.conf | ||
223 | .Pp | ||
224 | The ``J'' and ``Z'' is mostly for testing and debugging, | ||
225 | if a program changes behavior if either of these options are used, | ||
226 | it is buggy. | ||
227 | .Pp | ||
228 | The default cache size is 16 pages. | ||
229 | .Sh ENVIRONMENT | ||
230 | See above. | ||
231 | .Sh RETURN VALUES | ||
232 | The | ||
233 | .Fn malloc | ||
234 | function returns | ||
235 | a pointer to the allocated space if successful; otherwise | ||
236 | a null pointer is returned. | ||
237 | .Pp | ||
238 | The | ||
239 | .Fn free | ||
240 | function returns no value. | ||
241 | .Pp | ||
242 | The | ||
243 | .Fn realloc | ||
244 | function a pointer to the possibly moved allocated space; | ||
245 | otherwise a null pointer is returned. | ||
246 | .Sh MESSAGES | ||
247 | If | ||
248 | .Fn malloc , | ||
249 | .Fn free | ||
250 | or | ||
251 | .Fn realloc | ||
252 | detects an error or warning condition, | ||
253 | a message will be printed to filedescriptor | ||
254 | 2 (not using stdio). | ||
255 | Errors will always result in the process being | ||
256 | .Xr abort 2 'ed, | ||
257 | If the ``A'' option has been specified, also warnings will | ||
258 | .Xr abort 2 | ||
259 | the process. | ||
260 | .Pp | ||
261 | Here is a brief description of the error messages and what they mean: | ||
262 | .Pp | ||
263 | ``(ES): mumble mumble mumble'': | ||
264 | malloc have been compiled with -DEXTRA_SANITY and something looks | ||
265 | fishy in there. Consult sources and or wizards. | ||
266 | .Pp | ||
267 | ``allocation failed'' | ||
268 | if the ``A'' option is specified it is an error for | ||
269 | .Fn malloc | ||
270 | or | ||
271 | .Fn realloc | ||
272 | to return NULL. | ||
273 | .Pp | ||
274 | ``mmap(2) failed, check limits.'' | ||
275 | This is a rather weird condition that is most likely to mean that | ||
276 | the system is seriously overloaded or that your ulimits are sick. | ||
277 | .Pp | ||
278 | ``freelist is destroyed.'' | ||
279 | mallocs internal freelist has been stomped on. | ||
280 | .Pp | ||
281 | Here is a brief description of the warning messages and what they mean: | ||
282 | .Pp | ||
283 | ``chunk/page is already free.'' | ||
284 | A pointer to a free chunk is attempted freed again. | ||
285 | .Pp | ||
286 | ``junk pointer, too high to make sense.'' | ||
287 | The pointer doesn't make sense. It's above the area of memory that | ||
288 | malloc knows something about. | ||
289 | This could be a pointer from some | ||
290 | .Xr mmap 2 'ed | ||
291 | memory. | ||
292 | .Pp | ||
293 | ``junk pointer, too low to make sense.'' | ||
294 | The pointer doesn't make sense. It's below the area of memory that | ||
295 | malloc knows something about. | ||
296 | This pointer probably came from your data or bss segments. | ||
297 | .Pp | ||
298 | ``malloc() has never been called.'' | ||
299 | Nothing has ever been allocated, yet something is being freed or | ||
300 | realloc'ed. | ||
301 | .Pp | ||
302 | ``modified (chunk-/page-) pointer.'' | ||
303 | The pointer passed to free or realloc has been modified. | ||
304 | .Pp | ||
305 | ``pointer to wrong page.'' | ||
306 | The pointer that malloc is trying to free is not pointing to | ||
307 | a sensible page. | ||
308 | .Pp | ||
309 | ``recursive call.'' | ||
310 | You have tried to call recursively into these functions. | ||
311 | I can only imagine this as happening if you call one of these | ||
312 | functions from a signal function, which happens to be called | ||
313 | while you're already in here. | ||
314 | Well, sorry to say: that's not supported. | ||
315 | If this is a problem for you I'd like to hear about it. It | ||
316 | would be possible to add a sigblock() around this package, | ||
317 | but it would have a performance penalty that is not acceptable | ||
318 | as the default. | ||
319 | .Pp | ||
320 | ``unknown char in MALLOC_OPTIONS'' | ||
321 | we found something we didn't understand. | ||
322 | .Sh FILES | ||
323 | .Bl -tag -width "/etc/malloc.conf" | ||
324 | .It Pa /etc/malloc.conf | ||
325 | symbolic link to file containing option flags | ||
326 | .Sh SEE ALSO | ||
327 | .Xr brk 2 , | ||
328 | .Xr alloca 3 , | ||
329 | .Xr calloc 3 , | ||
330 | .Xr getpagesize 3 , | ||
331 | .Xr memory 3 | ||
332 | .Pa /usr/share/doc/papers/malloc.ascii.gz | ||
333 | .Sh STANDARDS | ||
334 | The | ||
335 | .Fn malloc | ||
336 | function conforms to | ||
337 | .St -ansiC . | ||
338 | .Sh HISTORY | ||
339 | The present implementation of malloc started out as a filesystem on a drum | ||
340 | attached to a 20bit binary challenged computer built with discrete germanium | ||
341 | transistors, and it has since graduated to handle primary storage rather than | ||
342 | secondary. | ||
343 | .Pp | ||
344 | The main difference from other malloc implementations are believed to be that | ||
345 | the free pages are not accessed until allocated. | ||
346 | Most malloc implementations will store a data structure containing a, | ||
347 | possibly double-, linked list in the free chunks of memory, used to tie | ||
348 | all the free memory together. | ||
349 | That is a quite suboptimal thing to do. | ||
350 | Every time the free-list is traversed, all the otherwise unused, and very | ||
351 | likely paged out, pages get faulted into primary memory, just to see what | ||
352 | lies after them in the list. | ||
353 | .Pp | ||
354 | On systems which are paging, this can make a factor five in difference on the | ||
355 | page-faults of a process. | ||