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