summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib/malloc.3
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/stdlib/malloc.3')
-rw-r--r--src/lib/libc/stdlib/malloc.3297
1 files changed, 278 insertions, 19 deletions
diff --git a/src/lib/libc/stdlib/malloc.3 b/src/lib/libc/stdlib/malloc.3
index 3bbf2bf65e..d5f8837ec2 100644
--- a/src/lib/libc/stdlib/malloc.3
+++ b/src/lib/libc/stdlib/malloc.3
@@ -1,5 +1,5 @@
1.\" Copyright (c) 1980, 1991 Regents of the University of California. 1.\" Copyright (c) 1980, 1991, 1993
2.\" All rights reserved. 2.\" The Regents of the University of California. All rights reserved.
3.\" 3.\"
4.\" This code is derived from software contributed to Berkeley by 4.\" This code is derived from software contributed to Berkeley by
5.\" the American National Standards Committee X3, on Information 5.\" the American National Standards Committee X3, on Information
@@ -33,19 +33,33 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)malloc.3 6.7 (Berkeley) 6/29/91 36.\" $OpenBSD: malloc.3,v 1.9 1998/08/15 20:32:02 deraadt Exp $
37.\" $Id: malloc.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd August 27, 1996
40.Dt MALLOC 3 39.Dt MALLOC 3
41.Os BSD 4 40.Os OpenBSD
42.Sh NAME 41.Sh NAME
43.Nm malloc 42.Nm malloc ,
44.Nd general memory allocation function 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
45.Sh SYNOPSIS 51.Sh SYNOPSIS
46.Fd #include <stdlib.h> 52.Fd #include <stdlib.h>
47.Ft void * 53.Ft void *
48.Fn malloc "size_t size" 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
49.Sh DESCRIPTION 63.Sh DESCRIPTION
50The 64The
51.Fn malloc 65.Fn malloc
@@ -62,30 +76,275 @@ suitably aligned (after possible pointer
62coercion) for storage of any type of object. If the space is of 76coercion) for storage of any type of object. If the space is of
63.Em pagesize 77.Em pagesize
64or larger, the memory returned will be page-aligned. 78or larger, the memory returned will be page-aligned.
79.Pp
80Allocation of a zero size object returns a pointer to a zero size object.
81.Pp
82The
83.Fn free
84function causes the space pointed to by
85.Fa ptr
86to be deallocated, that is, at least made available for further allocation,
87but if possible, it will passed back to the kernel with
88.Xr sbrk 2 .
89If
90.Fa ptr
91is a null pointer, no action occurs.
92.Pp
93A
94.Fn cfree
95function is also provided for compatibility with old systems and other
96.Nm malloc
97libraries; it is simply an alias for
98.Fn free .
99.Pp
100The
101.Fn realloc
102function changes the size of the object pointed to by
103.Fa ptr
104to the size specified by
105.Fa size .
106The contents of the object are unchanged up to the lesser
107of the new and old sizes.
108If the new size is larger, the value of the newly allocated portion
109of the object is indeterminate and uninitialized.
110If
111.Fa ptr
112is a null pointer, the
113.Fn realloc
114function behaves like the
115.Fn malloc
116function for the specified size.
117If the space cannot be allocated, the object
118pointed to by
119.Fa ptr
120is unchanged.
121If
122.Fa size
123is zero and
124.Fa ptr
125is not a null pointer, the object it points to is freed and a new zero size
126object is returned.
127.Pp
128When using
129.Fn realloc
130one must be careful to avoid the following idiom:
131.Pp
132.Bd -literal -offset indent
133if ((p = realloc(p, nsize)) == NULL)
134 return NULL;
135.Ed
136.Pp
137In most cases, this will result in a leak of memory.
138As stated earlier, a return value of
139.Fa NULL
140indicates that the old object still remains allocated.
141Better code looks like this:
142.Bd -literal -offset indent
143if ((p2 = realloc(p, nsize)) == NULL) {
144 if (p)
145 free(p);
146 p = NULL;
147 return NULL;
148}
149p = p2;
150.Ed
151.Pp
152Malloc will first look for a symbolic link called
153.Pa /etc/malloc.conf
154and next check the environment for a variable called
155.Ev MALLOC_OPTIONS
156and finally for the global variable
157.Va malloc_options
158and scan them for flags in that order.
159Flags 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.
163This is a very handy debugging aid, since the core file will represent the
164time of failure,
165rather 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.
169This option requires the library to have been compiled with -DMALLOC_STATS in
170order to have any effect.
171
172.It J
173``junk'' fill some junk into the area allocated.
174Currently 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
178machine is paging a lot this may help a bit.
179
180.It N
181Do not output warning messages when encountering possible corruption
182or bad pointers.
183
184.It R
185``realloc'' always reallocate when
186.Fn realloc
187is called, even if the initial allocation was big enough.
188This can substantially aid in compacting memory.
189
190.It U
191``utrace'' generate entries for
192.Xr ktrace 1
193for all operations.
194Consult the source for this one.
195
196.It X
197``xmalloc''
198rather than return failure,
199.Xr abort 3
200the program with a diagnostic message on stderr.
201It is the intention that this option be set at compile time by
202including in the source:
203.Bd -literal -offset indent
204extern char *malloc_options;
205malloc_options = "X";
206.Ed
207
208.It Z
209``zero'' fill some junk into the area allocated (see ``J''),
210except 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
219So to set a systemwide reduction of cache size and coredumps on problems
220one would:
221.Li ln -s 'A<' /etc/malloc.conf
222.Pp
223The ``J'' and ``Z'' is mostly for testing and debugging,
224if a program changes behavior if either of these options are used,
225it is buggy.
226.Pp
227The default cache size is 16 pages.
228.Sh ENVIRONMENT
229See above.
65.Sh RETURN VALUES 230.Sh RETURN VALUES
66The 231The
67.Fn malloc 232.Fn malloc
68function returns 233function returns
69a pointer to the allocated space if successful; otherwise 234a pointer to the allocated space if successful; otherwise
70a null pointer is returned. 235a null pointer is returned.
236.Pp
237The
238.Fn free
239function returns no value.
240.Pp
241The
242.Fn realloc
243function a pointer to the possibly moved allocated space;
244otherwise a null pointer is returned.
245.Sh MESSAGES
246If
247.Fn malloc ,
248.Fn free
249or
250.Fn realloc
251detects an error or warning condition,
252a message will be printed to filedescriptor
2532 (not using stdio).
254Errors will always result in the process being
255.Xr abort 2 'ed,
256If the ``A'' option has been specified, also warnings will
257.Xr abort 2
258the process.
259.Pp
260Here is a brief description of the error messages and what they mean:
261.Pp
262``(ES): mumble mumble mumble'':
263malloc have been compiled with -DEXTRA_SANITY and something looks
264fishy in there. Consult sources and or wizards.
265.Pp
266``allocation failed''
267if the ``A'' option is specified it is an error for
268.Fn malloc
269or
270.Fn realloc
271to return NULL.
272.Pp
273``mmap(2) failed, check limits.''
274This is a rather weird condition that is most likely to mean that
275the system is seriously overloaded or that your ulimits are sick.
276.Pp
277``freelist is destroyed.''
278mallocs internal freelist has been stomped on.
279.Pp
280Here is a brief description of the warning messages and what they mean:
281.Pp
282``chunk/page is already free.''
283A pointer to a free chunk is attempted freed again.
284.Pp
285``junk pointer, too high to make sense.''
286The pointer doesn't make sense. It's above the area of memory that
287malloc knows something about.
288This could be a pointer from some
289.Xr mmap 2 'ed
290memory.
291.Pp
292``junk pointer, too low to make sense.''
293The pointer doesn't make sense. It's below the area of memory that
294malloc knows something about.
295This pointer probably came from your data or bss segments.
296.Pp
297``malloc() has never been called.''
298Nothing has ever been allocated, yet something is being freed or
299realloc'ed.
300.Pp
301``modified (chunk-/page-) pointer.''
302The pointer passed to free or realloc has been modified.
303.Pp
304``pointer to wrong page.''
305The pointer that malloc is trying to free is not pointing to
306a sensible page.
307.Pp
308``recursive call.''
309You have tried to call recursively into these functions.
310I can only imagine this as happening if you call one of these
311functions from a signal function, which happens to be called
312while you're already in here.
313Well, sorry to say: that's not supported.
314If this is a problem for you I'd like to hear about it. It
315would be possible to add a sigblock() around this package,
316but it would have a performance penalty that is not acceptable
317as the default.
318.Pp
319``unknown char in MALLOC_OPTIONS''
320we found something we didn't understand.
71.Sh SEE ALSO 321.Sh SEE ALSO
72.Xr brk 2 , 322.Xr brk 2 ,
73.Xr getpagesize 2 ,
74.Xr free 3 ,
75.Xr calloc 3 ,
76.Xr alloca 3 , 323.Xr alloca 3 ,
77.Xr realloc 3 , 324.Xr calloc 3 ,
325.Xr getpagesize 3 ,
78.Xr memory 3 326.Xr memory 3
327.Pa /usr/share/doc/papers/malloc.ascii.gz
79.Sh STANDARDS 328.Sh STANDARDS
80The 329The
81.Fn malloc 330.Fn malloc
82function conforms to 331function conforms to
83.St -ansiC . 332.St -ansiC .
84.Sh BUGS 333.Sh HISTORY
85The current implementation of 334The present implementation of malloc started out as a filesystem on a drum
86.Xr malloc 335attached to a 20bit binary challenged computer built with discrete germanium
87does not always fail gracefully when system 336transistors, and it has since graduated to handle primary storage rather than
88memory limits are approached. 337secondary.
89It may fail to allocate memory when larger free blocks could be broken 338.Pp
90up, or when limits are exceeded because the size is rounded up. 339The main difference from other malloc implementations are believed to be that
91It is optimized for sizes that are powers of two. 340the free pages are not accessed until allocated.
341Most malloc implementations will store a data structure containing a,
342possibly double-, linked list in the free chunks of memory, used to tie
343all the free memory together.
344That is a quite suboptimal thing to do.
345Every time the free-list is traversed, all the otherwise unused, and very
346likely paged out, pages get faulted into primary memory, just to see what
347lies after them in the list.
348.Pp
349On systems which are paging, this can make a factor five in difference on the
350page-faults of a process.