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.3417
1 files changed, 387 insertions, 30 deletions
diff --git a/src/lib/libc/stdlib/malloc.3 b/src/lib/libc/stdlib/malloc.3
index 3bbf2bf65e..c3566e37e8 100644
--- a/src/lib/libc/stdlib/malloc.3
+++ b/src/lib/libc/stdlib/malloc.3
@@ -1,5 +1,6 @@
1.\" Copyright (c) 1980, 1991 Regents of the University of California. 1.\"
2.\" All rights reserved. 2.\" Copyright (c) 1980, 1991, 1993
3.\" The Regents of the University of California. All rights reserved.
3.\" 4.\"
4.\" This code is derived from software contributed to Berkeley by 5.\" This code is derived from software contributed to Berkeley by
5.\" the American National Standards Committee X3, on Information 6.\" the American National Standards Committee X3, on Information
@@ -13,11 +14,7 @@
13.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the 15.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution. 16.\" documentation and/or other materials provided with the distribution.
16.\" 3. All advertising materials mentioning features or use of this software 17.\" 3. Neither the name of the University nor the names of its contributors
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 18.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission. 19.\" without specific prior written permission.
23.\" 20.\"
@@ -33,19 +30,32 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 31.\" SUCH DAMAGE.
35.\" 32.\"
36.\" from: @(#)malloc.3 6.7 (Berkeley) 6/29/91 33.\" $OpenBSD: malloc.3,v 1.60 2008/12/30 07:44:51 djm Exp $
37.\" $Id: malloc.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $
38.\" 34.\"
39.Dd June 29, 1991 35.Dd $Mdocdate: December 30 2008 $
40.Dt MALLOC 3 36.Dt MALLOC 3
41.Os BSD 4 37.Os
42.Sh NAME 38.Sh NAME
43.Nm malloc 39.Nm malloc ,
44.Nd general memory allocation function 40.Nm calloc ,
41.Nm realloc ,
42.Nm free ,
43.Nm cfree
44.Nd memory allocation and deallocation
45.Sh SYNOPSIS 45.Sh SYNOPSIS
46.Fd #include <stdlib.h> 46.Fd #include <stdlib.h>
47.Ft void * 47.Ft void *
48.Fn malloc "size_t size" 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 ;
49.Sh DESCRIPTION 59.Sh DESCRIPTION
50The 60The
51.Fn malloc 61.Fn malloc
@@ -59,33 +69,380 @@ space from the appropriate list.
59.Pp 69.Pp
60The allocated space is 70The allocated space is
61suitably aligned (after possible pointer 71suitably aligned (after possible pointer
62coercion) for storage of any type of object. If the space is of 72coercion) for storage of any type of object.
73If the space is of
63.Em pagesize 74.Em pagesize
64or larger, the memory returned will be page-aligned. 75or larger, the memory returned will be page-aligned.
76.Pp
77Allocation of a zero size object returns a pointer to a zero size object.
78This zero size object is access protected, so any access to it will
79generate an exception (SIGSEGV).
80Many zero-sized objects can be placed consecutively in shared
81protected pages.
82The minimum size of the protection on each object is suitably aligned and
83sized as previously stated, but the protection may extend further depending
84on where in a protected zone the object lands.
85.Pp
86When using
87.Fn malloc
88be careful to avoid the following idiom:
89.Bd -literal -offset indent
90if ((p = malloc(num * size)) == NULL)
91 err(1, "malloc");
92.Ed
93.Pp
94The multiplication may lead to an integer overflow.
95To avoid this,
96.Fn calloc
97is recommended.
98.Pp
99If
100.Fn malloc
101must be used, be sure to test for overflow:
102.Bd -literal -offset indent
103if (size && num > SIZE_MAX / size) {
104 errno = ENOMEM;
105 err(1, "overflow");
106}
107.Ed
108.Pp
109The
110.Fn calloc
111function allocates space for an array of
112.Fa nmemb
113objects, each of whose size is
114.Fa size .
115The space is initialized to zero.
116The use of
117.Fn calloc
118is strongly encouraged when allocating multiple sized objects
119in order to avoid possible integer overflows.
120.Pp
121The
122.Fn free
123function causes the space pointed to by
124.Fa ptr
125to be either placed on a list of free pages to make it available for future
126allocation or, if required, to be returned to the kernel using
127.Xr munmap 2 .
128If
129.Fa ptr
130is a null pointer, no action occurs.
131.Pp
132A
133.Fn cfree
134function is also provided for compatibility with old systems and other
135.Nm malloc
136libraries; it is simply an alias for
137.Fn free .
138.Pp
139The
140.Fn realloc
141function changes the size of the object pointed to by
142.Fa ptr
143to
144.Fa size
145bytes and returns a pointer to the (possibly moved) object.
146The contents of the object are unchanged up to the lesser
147of the new and old sizes.
148If the new size is larger, the value of the newly allocated portion
149of the object is indeterminate and uninitialized.
150If
151.Fa ptr
152is a null pointer, the
153.Fn realloc
154function behaves like the
155.Fn malloc
156function for the specified size.
157If the space cannot be allocated, the object
158pointed to by
159.Fa ptr
160is unchanged.
161If
162.Fa size
163is zero and
164.Fa ptr
165is not a null pointer, the object it points to is freed and a new zero size
166object is returned.
167.Pp
168When using
169.Fn realloc
170be careful to avoid the following idiom:
171.Bd -literal -offset indent
172size += 50;
173if ((p = realloc(p, size)) == NULL)
174 return (NULL);
175.Ed
176.Pp
177Do not adjust the variable describing how much memory has been allocated
178until the allocation has been successful.
179This can cause aberrant program behavior if the incorrect size value is used.
180In most cases, the above sample will also result in a leak of memory.
181As stated earlier, a return value of
182.Dv NULL
183indicates that the old object still remains allocated.
184Better code looks like this:
185.Bd -literal -offset indent
186newsize = size + 50;
187if ((newp = realloc(p, newsize)) == NULL) {
188 free(p);
189 p = NULL;
190 size = 0;
191 return (NULL);
192}
193p = newp;
194size = newsize;
195.Ed
196.Pp
197As with
198.Fn malloc
199it is important to ensure the new size value will not overflow;
200i.e. avoid allocations like the following:
201.Bd -literal -offset indent
202if ((newp = realloc(p, num * size)) == NULL) {
203 ...
204.Ed
205.Pp
206Malloc will first look for a symbolic link called
207.Pa /etc/malloc.conf
208and next check the environment for a variable called
209.Ev MALLOC_OPTIONS
210and finally for the global variable
211.Va malloc_options
212and scan them for flags in that order.
213Flags are single letters, uppercase means on, lowercase means off.
214.Bl -tag -width indent
215.It Cm A
216.Dq Abort .
217.Fn malloc
218will coredump the process, rather than tolerate internal
219inconsistencies or incorrect usage.
220This is the default and a very handy debugging aid,
221since the core file represents the time of failure,
222rather than when the bogus pointer was used.
223.It Cm D
224.Dq Dump .
225.Fn malloc
226will dump statistics in a file called
227.Pa malloc.out
228at exit.
229This option requires the library to have been compiled with -DMALLOC_STATS in
230order to have any effect.
231.It Cm F
232.Dq Freeguard .
233Enable use after free protection.
234Unused pages on the freelist are read and write protected to
235cause a segmentation fault upon access.
236.It Cm G
237.Dq Guard .
238Enable guard pages.
239Each page size or larger allocation is followed by a guard page that will
240cause a segmentation fault upon any access.
241.It Cm H
242.Dq Hint .
243Pass a hint to the kernel about pages we don't use.
244If the machine is paging a lot this may help a bit.
245.It Cm J
246.Dq Junk .
247Fill some junk into the area allocated.
248Currently junk is bytes of 0xd0 when allocating; this is pronounced
249.Dq Duh .
250\&:-)
251Freed chunks are filled with 0xdf.
252.It Cm P
253.Dq Move allocations within a page.
254Allocations larger than half a page but smaller than a page
255are aligned to the end of a page to catch buffer overruns in more
256cases.
257This is the default.
258.It Cm R
259.Dq realloc .
260Always reallocate when
261.Fn realloc
262is called, even if the initial allocation was big enough.
263This can substantially aid in compacting memory.
264.\".Pp
265.\".It Cm U
266.\".Dq utrace .
267.\"Generate entries for
268.\".Xr ktrace 1
269.\"for all operations.
270.\"Consult the source for this one.
271.It Cm X
272.Dq xmalloc .
273Rather than return failure,
274.Xr abort 3
275the program with a diagnostic message on stderr.
276It is the intention that this option be set at compile time by
277including in the source:
278.Bd -literal -offset indent
279extern char *malloc_options;
280malloc_options = "X";
281.Ed
282.Pp
283Note that this will cause code that is supposed to handle
284out-of-memory conditions gracefully to abort instead.
285.It Cm Z
286.Dq Zero .
287Fill some junk into the area allocated (see
288.Cm J ) ,
289except for the exact length the user asked for, which is zeroed.
290.It Cm <
291.Dq Half the cache size .
292Decrease the size of the free page cache by a factor of two.
293.It Cm >
294.Dq Double the cache size .
295Increase the size of the free page cache by a factor of two.
296.El
297.Pp
298So to set a systemwide reduction of cache size and use guard pages:
299.Dl # ln -s 'G\*(Lt' /etc/malloc.conf
300.Pp
301The
302.Cm J
303and
304.Cm Z
305flags are mostly for testing and debugging.
306If a program changes behavior if either of these options are used,
307it is buggy.
308.Pp
309The default number of free pages cached is 64.
65.Sh RETURN VALUES 310.Sh RETURN VALUES
66The 311The
67.Fn malloc 312.Fn malloc
68function returns 313and
69a pointer to the allocated space if successful; otherwise 314.Fn calloc
70a null pointer is returned. 315functions return a pointer to the allocated space if successful; otherwise,
316a null pointer is returned and
317.Va errno
318is set to
319.Er ENOMEM .
320.Pp
321The
322.Fn free
323and
324.Fn cfree
325functions return no value.
326.Pp
327The
328.Fn realloc
329function returns a pointer to the (possibly moved) allocated space
330if successful; otherwise, a null pointer is returned and
331.Va errno
332is set to
333.Er ENOMEM .
334.Sh ENVIRONMENT
335.Bl -tag -width Ev
336.It Ev MALLOC_OPTIONS
337See above.
338.El
339.Sh FILES
340.Bl -tag -width "/etc/malloc.conf"
341.It Pa /etc/malloc.conf
342symbolic link to filename containing option flags
343.El
344.Sh DIAGNOSTICS
345If
346.Fn malloc ,
347.Fn calloc ,
348.Fn realloc ,
349or
350.Fn free
351detect an error condition,
352a message will be printed to file descriptor
3532 (not using stdio).
354Errors will result in the process being aborted,
355unless the
356.Cm a
357option has been specified.
358.Pp
359Here is a brief description of the error messages and what they mean:
360.Bl -tag -width Ds
361.It Dq out of memory
362If the
363.Cm X
364option is specified it is an error for
365.Fn malloc ,
366.Fn calloc ,
367or
368.Fn realloc
369to return
370.Dv NULL .
371.It Dq malloc init mmap failed
372This is a rather weird condition that is most likely to indicate a
373seriously overloaded system or a ulimit restriction.
374.It Dq bogus pointer (double free?)
375An attempt to
376.Fn free
377or
378.Fn realloc
379an unallocated pointer was made.
380.It Dq chunk is already free
381There was an attempt to free a chunk that had already been freed.
382.It Dq modified chunk-pointer
383The pointer passed to
384.Fn free
385or
386.Fn realloc
387has been modified.
388.It Dq recursive call
389An attempt was made to call recursively into these functions, i.e., from a
390signal handler.
391This behavior is not supported.
392In particular, signal handlers should
393.Em not
394use any of the
395.Fn malloc
396functions nor utilize any other functions which may call
397.Fn malloc
398(e.g.,
399.Xr stdio 3
400routines).
401.It Dq unknown char in MALLOC_OPTIONS
402We found something we didn't understand.
403.It Dq malloc cache overflow/underflow
404The internal malloc page cache has been corrupted.
405.It Dq malloc free slot lost
406The internal malloc page cache has been corrupted.
407.It Dq guard size
408An inconsistent guard size was detected.
409.It any other error
410.Fn malloc
411detected an internal error;
412consult sources and/or wizards.
413.El
71.Sh SEE ALSO 414.Sh SEE ALSO
72.Xr brk 2 , 415.Xr brk 2 ,
73.Xr getpagesize 2 , 416.Xr mmap 2 ,
74.Xr free 3 , 417.Xr munmap 2 ,
75.Xr calloc 3 ,
76.Xr alloca 3 , 418.Xr alloca 3 ,
77.Xr realloc 3 , 419.Xr getpagesize 3
78.Xr memory 3
79.Sh STANDARDS 420.Sh STANDARDS
80The 421The
81.Fn malloc 422.Fn malloc
82function conforms to 423function conforms to
83.St -ansiC . 424.St -ansiC .
84.Sh BUGS 425.Sh HISTORY
85The current implementation of 426The present implementation of
86.Xr malloc 427.Fn malloc
87does not always fail gracefully when system 428started out as a filesystem on a drum
88memory limits are approached. 429attached to a 20-bit binary challenged computer built with discrete germanium
89It may fail to allocate memory when larger free blocks could be broken 430transistors, and it has since graduated to handle primary storage rather than
90up, or when limits are exceeded because the size is rounded up. 431secondary.
91It is optimized for sizes that are powers of two. 432.Pp
433The main difference from other
434.Fn malloc
435implementations are believed to be that
436the free pages are not accessed until allocated.
437Most
438.Fn malloc
439implementations will store a data structure containing a,
440possibly double-, linked list in the free chunks of memory, used to tie
441all the free memory together.
442That is a quite suboptimal thing to do.
443Every time the free-list is traversed, all the otherwise unused, and very
444likely paged out, pages get faulted into primary memory, just to see what
445lies after them in the list.
446.Pp
447On systems which are paging, this can increase the page-faults
448of a process by a factor of five.