summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib/malloc.3
diff options
context:
space:
mode:
authortholo <>1996-08-10 04:18:49 +0000
committertholo <>1996-08-10 04:18:49 +0000
commitcc20851f7c7e8c47582db29709f2c1f2726cc0ec (patch)
tree9f8277db909d533c2a42e25b93e12f1f14907cf2 /src/lib/libc/stdlib/malloc.3
parent81262c3c4515a67e0c45a334c3212fb1c5de4b7e (diff)
downloadopenbsd-cc20851f7c7e8c47582db29709f2c1f2726cc0ec.tar.gz
openbsd-cc20851f7c7e8c47582db29709f2c1f2726cc0ec.tar.bz2
openbsd-cc20851f7c7e8c47582db29709f2c1f2726cc0ec.zip
Import malloc(3) manual page from FreeBSD
Diffstat (limited to 'src/lib/libc/stdlib/malloc.3')
-rw-r--r--src/lib/libc/stdlib/malloc.3138
1 files changed, 120 insertions, 18 deletions
diff --git a/src/lib/libc/stdlib/malloc.3 b/src/lib/libc/stdlib/malloc.3
index 3bbf2bf65e..b6acd64854 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,28 @@
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.2 1996/08/10 04:18:49 tholo 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 June 4, 1993
40.Dt MALLOC 3 39.Dt MALLOC 3
41.Os BSD 4 40.Os BSD 4
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.Nd free up memory allocated with malloc, calloc or realloc
47.Pp
48.Nm realloc
49.Nd reallocation of memory function
45.Sh SYNOPSIS 50.Sh SYNOPSIS
46.Fd #include <stdlib.h> 51.Fd #include <stdlib.h>
47.Ft void * 52.Ft void *
48.Fn malloc "size_t size" 53.Fn malloc "size_t size"
54.Ft void
55.Fn free "void *ptr"
56.Ft void *
57.Fn realloc "void *ptr" "size_t size"
49.Sh DESCRIPTION 58.Sh DESCRIPTION
50The 59The
51.Fn malloc 60.Fn malloc
@@ -62,30 +71,123 @@ suitably aligned (after possible pointer
62coercion) for storage of any type of object. If the space is of 71coercion) for storage of any type of object. If the space is of
63.Em pagesize 72.Em pagesize
64or larger, the memory returned will be page-aligned. 73or larger, the memory returned will be page-aligned.
74.Pp
75The
76.Fn free
77function causes the space pointed to by
78.Fa ptr
79to be deallocated, that is, at least made available for further allocation,
80but if possible, it will passed back to the kernel with
81.Xr sbrk 2 .
82If
83.Fa ptr
84is a null pointer, no action occurs.
85.Pp
86The
87.Fn realloc
88function changes the size of the object pointed to by
89.Fa ptr
90to the size specified by
91.Fa size .
92The contents of the object are unchanged up to the lesser
93of the new and old sizes.
94If the new size is larger, the value of the newly allocated portion
95of the object is indeterminate.
96If
97.Fa ptr
98is a null pointer, the
99.Fn realloc
100function behaves like the
101.Fn malloc
102function for the specified size.
103If the space cannot be allocated, the object
104pointed to by
105.Fa ptr
106is unchanged.
107If
108.Fa size
109is zero and
110.Fa ptr
111is not a null pointer, the object it points to is freed.
112.Pp
113
114.Sh ENVIRONMENT
115This malloc will check the environment for a variable called
116.Em MALLOC_OPTIONS
117and scan it for flags.
118Flags are single letters, uppercase means on, lowercase means off.
119.Bl -tag -width indent
120.It A
121``abort'' malloc will coredump the process, rather than tolerate failure.
122This is a very handy debugging aid, since the core file will represent the
123time of failure,
124rather than when the NULL pointer was accessed.
125
126.It D
127``dump'' malloc will dump statistics in a file called ``malloc.out'' at exit.
128This option requires the library to have been compiled with -DMALLOC_STATS in
129order to have any effect.
130
131.It J
132``junk'' fill some junk into the area allocated.
133Currently junk is bytes of 0xd0, this is pronounced ``Duh'' :-)
134
135.It R
136``realloc'' always reallocate when
137.Fn realloc
138is called, even if the initial allocation was big enough.
139This can substantially aid in compacting memory.
140
141.It Z
142``zero'' fill some junk into the area allocated (see ``J''),
143except for the exact length the user asked for, which is zeroed.
144
145.El
146.Pp
147The ``J'' and ``Z'' is mostly for testing and debugging,
148if a program changes behavior if either of these options are used,
149it is buggy.
65.Sh RETURN VALUES 150.Sh RETURN VALUES
66The 151The
67.Fn malloc 152.Fn malloc
68function returns 153function returns
69a pointer to the allocated space if successful; otherwise 154a pointer to the allocated space if successful; otherwise
70a null pointer is returned. 155a null pointer is returned.
156.Pp
157The
158.Fn free
159function returns no value.
160.Pp
161The
162.Fn realloc
163function returns either a null pointer or a pointer
164to the possibly moved allocated space.
71.Sh SEE ALSO 165.Sh SEE ALSO
72.Xr brk 2 , 166.Xr brk 2 ,
73.Xr getpagesize 2 ,
74.Xr free 3 ,
75.Xr calloc 3 ,
76.Xr alloca 3 , 167.Xr alloca 3 ,
77.Xr realloc 3 , 168.Xr calloc 3 ,
169.Xr getpagesize 3 ,
78.Xr memory 3 170.Xr memory 3
79.Sh STANDARDS 171.Sh STANDARDS
80The 172The
81.Fn malloc 173.Fn malloc
82function conforms to 174function conforms to
83.St -ansiC . 175.St -ansiC .
84.Sh BUGS 176.Sh HISTORY
85The current implementation of 177The present implementation of malloc started out as a filesystem on a drum
86.Xr malloc 178attached to a 20bit binary challenged computer built with discrete germanium
87does not always fail gracefully when system 179transistors, and it has since graduated to handle primary storage rather than
88memory limits are approached. 180secondary.
89It may fail to allocate memory when larger free blocks could be broken 181.Pp
90up, or when limits are exceeded because the size is rounded up. 182The main difference from other malloc implementations are believed to be that
91It is optimized for sizes that are powers of two. 183the free pages are not accessed until allocated.
184Most malloc implementations will store a data structure containing a,
185possibly double-, linked list in the free chunks of memory, used to tie
186all the free memory together.
187That is a quite suboptimal thing to do.
188Every time the free-list is traversed, all the otherwise unused, and very
189likely paged out, pages get faulted into primary memory, just to see what
190lies after them in the list.
191.Pp
192On systems which are paging, this can make a factor five in difference on the
193page-faults of a process.