summaryrefslogtreecommitdiff
path: root/src
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
parent81262c3c4515a67e0c45a334c3212fb1c5de4b7e (diff)
downloadopenbsd-cc20851f7c7e8c47582db29709f2c1f2726cc0ec.tar.gz
openbsd-cc20851f7c7e8c47582db29709f2c1f2726cc0ec.tar.bz2
openbsd-cc20851f7c7e8c47582db29709f2c1f2726cc0ec.zip
Import malloc(3) manual page from FreeBSD
Diffstat (limited to 'src')
-rw-r--r--src/lib/libc/stdlib/Makefile.inc15
-rw-r--r--src/lib/libc/stdlib/free.382
-rw-r--r--src/lib/libc/stdlib/malloc.3138
-rw-r--r--src/lib/libc/stdlib/realloc.3100
4 files changed, 127 insertions, 208 deletions
diff --git a/src/lib/libc/stdlib/Makefile.inc b/src/lib/libc/stdlib/Makefile.inc
index 625f0f9a91..6c635ef354 100644
--- a/src/lib/libc/stdlib/Makefile.inc
+++ b/src/lib/libc/stdlib/Makefile.inc
@@ -1,5 +1,4 @@
1# from: @(#)Makefile.inc 5.6 (Berkeley) 6/4/91 1# $OpenBSD: Makefile.inc,v 1.3 1996/08/10 04:18:48 tholo Exp $
2# $Id: Makefile.inc,v 1.2 1996/03/25 23:31:43 tholo Exp $
3 2
4# stdlib sources 3# stdlib sources
5.PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/stdlib ${.CURDIR}/stdlib 4.PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/stdlib ${.CURDIR}/stdlib
@@ -33,17 +32,17 @@ SRCS+= abs.c div.c labs.c ldiv.c
33.endif 32.endif
34 33
35MAN+= abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 bsearch.3 \ 34MAN+= abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 bsearch.3 \
36 calloc.3 div.3 exit.3 free.3 getenv.3 getopt.3 labs.3 ldiv.3 \ 35 calloc.3 div.3 exit.3 getenv.3 getopt.3 labs.3 ldiv.3 malloc.3 \
37 malloc.3 memory.3 qabs.3 qdiv.3 qsort.3 radixsort.3 rand48.3 \ 36 memory.3 qabs.3 qdiv.3 qsort.3 radixsort.3 rand48.3 rand.3 \
38 rand.3 random.3 realloc.3 realpath.3 strtod.3 strtol.3 strtoul.3 \ 37 random.3 realpath.3 strtod.3 strtol.3 strtoul.3 system.3
39 system.3
40 38
41MLINKS+=getenv.3 setenv.3 getenv.3 unsetenv.3 getenv.3 putenv.3 39MLINKS+=getenv.3 setenv.3 getenv.3 unsetenv.3 getenv.3 putenv.3
40MLINKS+=malloc.3 free.3 malloc.3 realloc.3
42MLINKS+=qsort.3 heapsort.3 qsort.3 mergesort.3 41MLINKS+=qsort.3 heapsort.3 qsort.3 mergesort.3
43MLINKS+=rand.3 srand.3 42MLINKS+=rand.3 srand.3
44MLINKS+=strtol.3 strtoq.3
45MLINKS+=strtoul.3 strtouq.3
46MLINKS+=random.3 initstate.3 random.3 setstate.3 random.3 srandom.3 43MLINKS+=random.3 initstate.3 random.3 setstate.3 random.3 srandom.3
47MLINKS+=rand48.3 drand48.3 rand48.3 erand48.3 rand48.3 lrand48.3 44MLINKS+=rand48.3 drand48.3 rand48.3 erand48.3 rand48.3 lrand48.3
48MLINKS+=rand48.3 mrand48.3 rand48.3 nrand48.3 rand48.3 jrand48.3 45MLINKS+=rand48.3 mrand48.3 rand48.3 nrand48.3 rand48.3 jrand48.3
49MLINKS+=rand48.3 srand48.3 rand48.3 seed48.3 rand48.3 lcong48.3 46MLINKS+=rand48.3 srand48.3 rand48.3 seed48.3 rand48.3 lcong48.3
47MLINKS+=strtol.3 strtoq.3
48MLINKS+=strtoul.3 strtouq.3
diff --git a/src/lib/libc/stdlib/free.3 b/src/lib/libc/stdlib/free.3
deleted file mode 100644
index 3d0131d7de..0000000000
--- a/src/lib/libc/stdlib/free.3
+++ /dev/null
@@ -1,82 +0,0 @@
1.\" Copyright (c) 1991 The Regents of the University of California.
2.\" 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.\" from: @(#)free.3 5.2 (Berkeley) 6/29/91
37.\" $Id: free.3,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $
38.\"
39.Dd June 29, 1991
40.Dt FREE 3
41.Os
42.Sh NAME
43.Nm free
44.Nd free up memory allocated with malloc, calloc or realloc
45.Sh SYNOPSIS
46.Fd #include <stdlib.h>
47.Ft void
48.Fn free "void *ptr"
49.Sh DESCRIPTION
50The
51.Fn free
52function causes the space pointed to by
53.Fa ptr
54to be deallocated, that is, made available
55for further allocation.
56If
57.Fa ptr
58is a null pointer, no action occurs.
59Otherwise, if the argument does not match a pointer earlier
60returned by the
61.Xr calloc ,
62.Xr malloc ,
63or
64.Xr realloc
65function, or if the space has been deallocated by a call to
66.Fn free
67or
68.Xr realloc ,
69general havoc may occur.
70.Sh RETURN VALUES
71The
72.Fn free
73function returns no value.
74.Sh SEE ALSO
75.Xr calloc 3 ,
76.Xr malloc 3 ,
77.Xr realloc 3
78.Sh STANDARDS
79The
80.Fn free
81function conforms to
82.St -ansiC .
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.
diff --git a/src/lib/libc/stdlib/realloc.3 b/src/lib/libc/stdlib/realloc.3
deleted file mode 100644
index 66f09b2081..0000000000
--- a/src/lib/libc/stdlib/realloc.3
+++ /dev/null
@@ -1,100 +0,0 @@
1.\" Copyright (c) 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\" notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\" notice, this list of conditions and the following disclaimer in the
11.\" documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\" must display the following acknowledgement:
14.\" This product includes software developed by the University of
15.\" California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" from: @(#)realloc.3 5.1 (Berkeley) 5/2/91
33.\" $Id: realloc.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $
34.\"
35.Dd May 2, 1991
36.Dt REALLOC 3
37.Os
38.Sh NAME
39.Nm realloc
40.Nd reallocation of memory function
41.Sh SYNOPSIS
42.Fd #include <stdlib.h>
43.Ft void *
44.Fn realloc "void *ptr" "size_t size"
45.Sh DESCRIPTION
46The
47.Fn realloc
48function changes the size of the object pointed to by
49.Fa ptr
50to the size specified by
51.Fa size .
52The contents of the object are unchanged up to the lesser
53of the new and old sizes.
54If the new size is larger, the value of the newly allocated portion
55of the object is indeterminate.
56If
57.Fa ptr
58is a null pointer, the
59.Fn realloc
60function behaves like the
61.Xr malloc 3
62function for the specified size.
63Otherwise, if
64.Fa ptr
65does not match a pointer earlier returned by the
66.Xr calloc 3 ,
67.Xr malloc 3 ,
68or
69.Fn realloc
70function, or if the space has been deallocated
71by a call to the
72.Xr free
73or
74.Fn realloc
75function, unpredictable and usually detrimental
76behavior will occur.
77If the space cannot be allocated, the object
78pointed to by
79.Fa ptr
80is unchanged.
81If
82.Fa size
83is zero and
84.Fa ptr
85is not a null pointer, the object it points to is freed.
86.Pp
87The
88.Fn realloc
89function returns either a null pointer or a pointer
90to the possibly moved allocated space.
91.Sh SEE ALSO
92.Xr alloca 3 ,
93.Xr calloc 3 ,
94.Xr free 3 ,
95.Xr malloc 3 ,
96.Sh STANDARDS
97The
98.Fn realloc
99function conforms to
100.St -ansiC .