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.3426
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
60The
61.Fn malloc
62function allocates uninitialized space for an object whose
63size is specified by
64.Fa size .
65The
66.Fn malloc
67function maintains multiple lists of free blocks according to size, allocating
68space from the appropriate list.
69.Pp
70The allocated space is
71suitably aligned (after possible pointer
72coercion) for storage of any type of object.
73If the space is of
74.Em pagesize
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
86The
87.Fn calloc
88function allocates space for an array of
89.Fa nmemb
90objects, each of whose size is
91.Fa size .
92The space is initialized to all bits zero.
93.Pp
94The
95.Fn free
96function causes the space pointed to by
97.Fa ptr
98to be deallocated, that is, at least made available for further allocation,
99but if possible, it will passed back to the kernel with
100.Xr sbrk 2 .
101If
102.Fa ptr
103is a null pointer, no action occurs.
104.Pp
105A
106.Fn cfree
107function is also provided for compatibility with old systems and other
108.Nm malloc
109libraries; it is simply an alias for
110.Fn free .
111.Pp
112The
113.Fn realloc
114function changes the size of the object pointed to by
115.Fa ptr
116to
117.Fa size
118bytes and returns a pointer to the (possibly moved) object.
119The contents of the object are unchanged up to the lesser
120of the new and old sizes.
121If the new size is larger, the value of the newly allocated portion
122of the object is indeterminate and uninitialized.
123If
124.Fa ptr
125is a null pointer, the
126.Fn realloc
127function behaves like the
128.Fn malloc
129function for the specified size.
130If the space cannot be allocated, the object
131pointed to by
132.Fa ptr
133is unchanged.
134If
135.Fa size
136is zero and
137.Fa ptr
138is not a null pointer, the object it points to is freed and a new zero size
139object is returned.
140.Pp
141When using
142.Fn realloc
143one must be careful to avoid the following idiom:
144.Pp
145.Bd -literal -offset indent
146size += 50;
147if ((p = realloc(p, size)) == NULL)
148 return (NULL);
149.Ed
150.Pp
151Do not adjust the variable describing how much memory has been allocated
152until one knows the allocation has been successful.
153This can cause aberrant program behavior if the incorrect size value is used.
154In most cases, the above sample will also result in a leak of memory.
155As stated earlier, a return value of
156.Dv NULL
157indicates that the old object still remains allocated.
158Better code looks like this:
159.Bd -literal -offset indent
160newsize = size + 50;
161if ((newp = realloc(p, newsize)) == NULL) {
162 free(p);
163 p = NULL;
164 size = 0;
165 return (NULL);
166}
167p = newp;
168size = newsize;
169.Ed
170.Pp
171Malloc will first look for a symbolic link called
172.Pa /etc/malloc.conf
173and next check the environment for a variable called
174.Ev MALLOC_OPTIONS
175and finally for the global variable
176.Va malloc_options
177and scan them for flags in that order.
178Flags are single letters, uppercase means on, lowercase means off.
179.Bl -tag -width indent
180.It Cm A
181.Dq Abort .
182.Fn malloc
183will coredump the process, rather than tolerate failure.
184This is a very handy debugging aid, since the core file will represent the
185time of failure, rather than when the null pointer was accessed.
186.Pp
187.It Cm D
188.Dq Dump .
189.Fn malloc
190will dump statistics in a file called
191.Pa malloc.out
192at exit.
193This option requires the library to have been compiled with -DMALLOC_STATS in
194order to have any effect.
195.Pp
196.It Cm G
197Enable guard pages and chunk randomization.
198Each page size or larger allocation is followed by a guard page that will
199cause a segmentation fault upon any access.
200Smaller than page size chunks are returned in a random order.
201.Pp
202.It Cm J
203.Dq Junk .
204Fill some junk into the area allocated.
205Currently junk is bytes of 0xd0; this is pronounced
206.Dq Duh .
207\&:-)
208.Pp
209.It Cm H
210.Dq Hint .
211Pass a hint to the kernel about pages we don't use.
212If the machine is paging a lot this may help a bit.
213.Pp
214.It Cm N
215Do not output warning messages when encountering possible corruption
216or bad pointers.
217.Pp
218.It Cm R
219.Dq realloc .
220Always reallocate when
221.Fn realloc
222is called, even if the initial allocation was big enough.
223This 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 .
234Rather than return failure,
235.Xr abort 3
236the program with a diagnostic message on stderr.
237It is the intention that this option be set at compile time by
238including in the source:
239.Bd -literal -offset indent
240extern char *malloc_options;
241malloc_options = "X";
242.Ed
243.Pp
244.It Cm Z
245.Dq Zero .
246Fill some junk into the area allocated (see
247.Cm J ) ,
248except for the exact length the user asked for, which is zeroed.
249.Pp
250.It Cm <
251.Dq Half the cache size .
252Reduce the size of the cache by a factor of two.
253.Pp
254.It Cm >
255.Dq Double the cache size .
256Double the size of the cache by a factor of two.
257.El
258.Pp
259So to set a systemwide reduction of cache size and coredumps on problems
260one would:
261.Li ln -s 'A<' /etc/malloc.conf
262.Pp
263The
264.Cm J
265and
266.Cm Z
267flags are mostly for testing and debugging.
268If a program changes behavior if either of these options are used,
269it is buggy.
270.Pp
271The default cache size is 16 pages.
272.Sh RETURN VALUES
273The
274.Fn malloc
275and
276.Fn calloc
277functions return a pointer to the allocated space if successful; otherwise,
278a null pointer is returned and
279.Va errno
280is set to
281.Er ENOMEM .
282.Pp
283The
284.Fn free
285and
286.Fn cfree
287functions return no value.
288.Pp
289The
290.Fn realloc
291function returns a pointer to the (possibly moved) allocated space
292if successful; otherwise, a null pointer is returned and
293.Va errno
294is set to
295.Er ENOMEM .
296.Sh ENVIRONMENT
297See above.
298.Sh FILES
299.Bl -tag -width "/etc/malloc.conf"
300.It Pa /etc/malloc.conf
301symbolic link to filename containing option flags
302.El
303.Sh DIAGNOSTICS
304If
305.Fn malloc ,
306.Fn calloc ,
307.Fn realloc ,
308or
309.Fn free
310detect an error or warning condition,
311a message will be printed to file descriptor
3122 (not using stdio).
313Errors will always result in the process being
314.Xr abort 3 'ed.
315If the
316.Cm A
317option has been specified, warnings will also
318.Xr abort 3
319the process.
320.Pp
321Here 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
325has been compiled with
326.Dv \&-DEXTRA_SANITY
327and something looks fishy in there.
328Consult sources and/or wizards.
329.It Dq allocation failed
330If the
331.Cm A
332option is specified it is an error for
333.Fn malloc ,
334.Fn calloc ,
335or
336.Fn realloc
337to return
338.Dv NULL .
339.It Dq mmap(2) failed, check limits.
340This is a rather weird condition that is most likely to indicate a
341seriously overloaded system or a
342.Xr ulimit 1
343restriction.
344.It Dq freelist is destroyed.
345.Fn malloc Ns 's
346internal freelist has been stomped on.
347.El
348.Pp
349Here 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.
352A pointer to a free chunk is attempted freed again.
353.It Dq junk pointer, too high to make sense.
354The pointer doesn't make sense.
355It's above the area of memory that
356.Fn malloc
357knows something about.
358This could be a pointer from some
359.Xr mmap 2 'ed
360memory.
361.It Dq junk pointer, too low to make sense.
362The pointer doesn't make sense.
363It's below the area of memory that
364.Fn malloc
365knows something about.
366This pointer probably came from your data or bss segments.
367.It Dq malloc() has never been called.
368Nothing has ever been allocated, yet something is being freed or
369realloc'ed.
370.It Dq modified (chunk-/page-) pointer.
371The pointer passed to free or realloc has been modified.
372.It Dq pointer to wrong page.
373The pointer that
374.Fn malloc
375is trying to free is not pointing to
376a sensible page.
377.It Dq recursive call.
378An attempt was made to call recursively into these functions, i.e., from a
379signal handler.
380This behavior is not supported.
381In particular, signal handlers should
382.Em not
383use any of the
384.Fn malloc
385functions nor utilize any other functions which may call
386.Fn malloc
387(e.g.,
388.Xr stdio 3
389routines).
390.It Dq unknown char in MALLOC_OPTIONS
391We 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
399The
400.Fn malloc
401function conforms to
402.St -ansiC .
403.Sh HISTORY
404The present implementation of
405.Fn malloc
406started out as a filesystem on a drum
407attached to a 20-bit binary challenged computer built with discrete germanium
408transistors, and it has since graduated to handle primary storage rather than
409secondary.
410.Pp
411The main difference from other
412.Fn malloc
413implementations are believed to be that
414the free pages are not accessed until allocated.
415Most
416.Fn malloc
417implementations will store a data structure containing a,
418possibly double-, linked list in the free chunks of memory, used to tie
419all the free memory together.
420That is a quite suboptimal thing to do.
421Every time the free-list is traversed, all the otherwise unused, and very
422likely paged out, pages get faulted into primary memory, just to see what
423lies after them in the list.
424.Pp
425On systems which are paging, this can make a factor five in difference on the
426page-faults of a process.