From 25f5863bbe78f4a2d23bcbf842618945bc4990e9 Mon Sep 17 00:00:00 2001 From: tholo <> Date: Sat, 10 Aug 1996 04:18:49 +0000 Subject: [PATCH] Import malloc(3) manual page from FreeBSD --- src/lib/libc/stdlib/Makefile.inc | 15 ++-- src/lib/libc/stdlib/free.3 | 82 ------------------ src/lib/libc/stdlib/malloc.3 | 138 +++++++++++++++++++++++++++---- src/lib/libc/stdlib/realloc.3 | 100 ---------------------- 4 files changed, 127 insertions(+), 208 deletions(-) delete mode 100644 src/lib/libc/stdlib/free.3 delete mode 100644 src/lib/libc/stdlib/realloc.3 diff --git a/src/lib/libc/stdlib/Makefile.inc b/src/lib/libc/stdlib/Makefile.inc index 625f0f9a..6c635ef3 100644 --- a/src/lib/libc/stdlib/Makefile.inc +++ b/src/lib/libc/stdlib/Makefile.inc @@ -1,5 +1,4 @@ -# from: @(#)Makefile.inc 5.6 (Berkeley) 6/4/91 -# $Id: Makefile.inc,v 1.2 1996/03/25 23:31:43 tholo Exp $ +# $OpenBSD: Makefile.inc,v 1.3 1996/08/10 04:18:48 tholo Exp $ # stdlib sources .PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/stdlib ${.CURDIR}/stdlib @@ -33,17 +32,17 @@ SRCS+= abs.c div.c labs.c ldiv.c .endif MAN+= abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 bsearch.3 \ - calloc.3 div.3 exit.3 free.3 getenv.3 getopt.3 labs.3 ldiv.3 \ - malloc.3 memory.3 qabs.3 qdiv.3 qsort.3 radixsort.3 rand48.3 \ - rand.3 random.3 realloc.3 realpath.3 strtod.3 strtol.3 strtoul.3 \ - system.3 + calloc.3 div.3 exit.3 getenv.3 getopt.3 labs.3 ldiv.3 malloc.3 \ + memory.3 qabs.3 qdiv.3 qsort.3 radixsort.3 rand48.3 rand.3 \ + random.3 realpath.3 strtod.3 strtol.3 strtoul.3 system.3 MLINKS+=getenv.3 setenv.3 getenv.3 unsetenv.3 getenv.3 putenv.3 +MLINKS+=malloc.3 free.3 malloc.3 realloc.3 MLINKS+=qsort.3 heapsort.3 qsort.3 mergesort.3 MLINKS+=rand.3 srand.3 -MLINKS+=strtol.3 strtoq.3 -MLINKS+=strtoul.3 strtouq.3 MLINKS+=random.3 initstate.3 random.3 setstate.3 random.3 srandom.3 MLINKS+=rand48.3 drand48.3 rand48.3 erand48.3 rand48.3 lrand48.3 MLINKS+=rand48.3 mrand48.3 rand48.3 nrand48.3 rand48.3 jrand48.3 MLINKS+=rand48.3 srand48.3 rand48.3 seed48.3 rand48.3 lcong48.3 +MLINKS+=strtol.3 strtoq.3 +MLINKS+=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 3d0131d7..00000000 --- a/src/lib/libc/stdlib/free.3 +++ /dev/null @@ -1,82 +0,0 @@ -.\" Copyright (c) 1991 The Regents of the University of California. -.\" All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" the American National Standards Committee X3, on Information -.\" Processing Systems. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. -.\" 4. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" from: @(#)free.3 5.2 (Berkeley) 6/29/91 -.\" $Id: free.3,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $ -.\" -.Dd June 29, 1991 -.Dt FREE 3 -.Os -.Sh NAME -.Nm free -.Nd free up memory allocated with malloc, calloc or realloc -.Sh SYNOPSIS -.Fd #include -.Ft void -.Fn free "void *ptr" -.Sh DESCRIPTION -The -.Fn free -function causes the space pointed to by -.Fa ptr -to be deallocated, that is, made available -for further allocation. -If -.Fa ptr -is a null pointer, no action occurs. -Otherwise, if the argument does not match a pointer earlier -returned by the -.Xr calloc , -.Xr malloc , -or -.Xr realloc -function, or if the space has been deallocated by a call to -.Fn free -or -.Xr realloc , -general havoc may occur. -.Sh RETURN VALUES -The -.Fn free -function returns no value. -.Sh SEE ALSO -.Xr calloc 3 , -.Xr malloc 3 , -.Xr realloc 3 -.Sh STANDARDS -The -.Fn free -function conforms to -.St -ansiC . diff --git a/src/lib/libc/stdlib/malloc.3 b/src/lib/libc/stdlib/malloc.3 index 3bbf2bf6..b6acd648 100644 --- a/src/lib/libc/stdlib/malloc.3 +++ b/src/lib/libc/stdlib/malloc.3 @@ -1,5 +1,5 @@ -.\" Copyright (c) 1980, 1991 Regents of the University of California. -.\" All rights reserved. +.\" Copyright (c) 1980, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. .\" .\" This code is derived from software contributed to Berkeley by .\" the American National Standards Committee X3, on Information @@ -33,19 +33,28 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" from: @(#)malloc.3 6.7 (Berkeley) 6/29/91 -.\" $Id: malloc.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $ +.\" $OpenBSD: malloc.3,v 1.2 1996/08/10 04:18:49 tholo Exp $ .\" -.Dd June 29, 1991 +.Dd June 4, 1993 .Dt MALLOC 3 .Os BSD 4 .Sh NAME -.Nm malloc +.Nm malloc , .Nd general memory allocation function +.Pp +.Nm free +.Nd free up memory allocated with malloc, calloc or realloc +.Pp +.Nm realloc +.Nd reallocation of memory function .Sh SYNOPSIS .Fd #include .Ft void * .Fn malloc "size_t size" +.Ft void +.Fn free "void *ptr" +.Ft void * +.Fn realloc "void *ptr" "size_t size" .Sh DESCRIPTION The .Fn malloc @@ -62,30 +71,123 @@ suitably aligned (after possible pointer coercion) for storage of any type of object. If the space is of .Em pagesize or larger, the memory returned will be page-aligned. +.Pp +The +.Fn free +function causes the space pointed to by +.Fa ptr +to be deallocated, that is, at least made available for further allocation, +but if possible, it will passed back to the kernel with +.Xr sbrk 2 . +If +.Fa ptr +is a null pointer, no action occurs. +.Pp +The +.Fn realloc +function changes the size of the object pointed to by +.Fa ptr +to the size specified by +.Fa size . +The contents of the object are unchanged up to the lesser +of the new and old sizes. +If the new size is larger, the value of the newly allocated portion +of the object is indeterminate. +If +.Fa ptr +is a null pointer, the +.Fn realloc +function behaves like the +.Fn malloc +function for the specified size. +If the space cannot be allocated, the object +pointed to by +.Fa ptr +is unchanged. +If +.Fa size +is zero and +.Fa ptr +is not a null pointer, the object it points to is freed. +.Pp + +.Sh ENVIRONMENT +This malloc will check the environment for a variable called +.Em MALLOC_OPTIONS +and scan it for flags. +Flags are single letters, uppercase means on, lowercase means off. +.Bl -tag -width indent +.It A +``abort'' malloc will coredump the process, rather than tolerate failure. +This is a very handy debugging aid, since the core file will represent the +time of failure, +rather than when the NULL pointer was accessed. + +.It D +``dump'' malloc will dump statistics in a file called ``malloc.out'' at exit. +This option requires the library to have been compiled with -DMALLOC_STATS in +order to have any effect. + +.It J +``junk'' fill some junk into the area allocated. +Currently junk is bytes of 0xd0, this is pronounced ``Duh'' :-) + +.It R +``realloc'' always reallocate when +.Fn realloc +is called, even if the initial allocation was big enough. +This can substantially aid in compacting memory. + +.It Z +``zero'' fill some junk into the area allocated (see ``J''), +except for the exact length the user asked for, which is zeroed. + +.El +.Pp +The ``J'' and ``Z'' is mostly for testing and debugging, +if a program changes behavior if either of these options are used, +it is buggy. .Sh RETURN VALUES The .Fn malloc function returns a pointer to the allocated space if successful; otherwise a null pointer is returned. +.Pp +The +.Fn free +function returns no value. +.Pp +The +.Fn realloc +function returns either a null pointer or a pointer +to the possibly moved allocated space. .Sh SEE ALSO .Xr brk 2 , -.Xr getpagesize 2 , -.Xr free 3 , -.Xr calloc 3 , .Xr alloca 3 , -.Xr realloc 3 , +.Xr calloc 3 , +.Xr getpagesize 3 , .Xr memory 3 .Sh STANDARDS The .Fn malloc function conforms to .St -ansiC . -.Sh BUGS -The current implementation of -.Xr malloc -does not always fail gracefully when system -memory limits are approached. -It may fail to allocate memory when larger free blocks could be broken -up, or when limits are exceeded because the size is rounded up. -It is optimized for sizes that are powers of two. +.Sh HISTORY +The present implementation of malloc started out as a filesystem on a drum +attached to a 20bit binary challenged computer built with discrete germanium +transistors, and it has since graduated to handle primary storage rather than +secondary. +.Pp +The main difference from other malloc implementations are believed to be that +the free pages are not accessed until allocated. +Most malloc implementations will store a data structure containing a, +possibly double-, linked list in the free chunks of memory, used to tie +all the free memory together. +That is a quite suboptimal thing to do. +Every time the free-list is traversed, all the otherwise unused, and very +likely paged out, pages get faulted into primary memory, just to see what +lies after them in the list. +.Pp +On systems which are paging, this can make a factor five in difference on the +page-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 66f09b20..00000000 --- a/src/lib/libc/stdlib/realloc.3 +++ /dev/null @@ -1,100 +0,0 @@ -.\" Copyright (c) 1991 The Regents of the University of California. -.\" All rights reserved. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. -.\" 4. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" from: @(#)realloc.3 5.1 (Berkeley) 5/2/91 -.\" $Id: realloc.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $ -.\" -.Dd May 2, 1991 -.Dt REALLOC 3 -.Os -.Sh NAME -.Nm realloc -.Nd reallocation of memory function -.Sh SYNOPSIS -.Fd #include -.Ft void * -.Fn realloc "void *ptr" "size_t size" -.Sh DESCRIPTION -The -.Fn realloc -function changes the size of the object pointed to by -.Fa ptr -to the size specified by -.Fa size . -The contents of the object are unchanged up to the lesser -of the new and old sizes. -If the new size is larger, the value of the newly allocated portion -of the object is indeterminate. -If -.Fa ptr -is a null pointer, the -.Fn realloc -function behaves like the -.Xr malloc 3 -function for the specified size. -Otherwise, if -.Fa ptr -does not match a pointer earlier returned by the -.Xr calloc 3 , -.Xr malloc 3 , -or -.Fn realloc -function, or if the space has been deallocated -by a call to the -.Xr free -or -.Fn realloc -function, unpredictable and usually detrimental -behavior will occur. -If the space cannot be allocated, the object -pointed to by -.Fa ptr -is unchanged. -If -.Fa size -is zero and -.Fa ptr -is not a null pointer, the object it points to is freed. -.Pp -The -.Fn realloc -function returns either a null pointer or a pointer -to the possibly moved allocated space. -.Sh SEE ALSO -.Xr alloca 3 , -.Xr calloc 3 , -.Xr free 3 , -.Xr malloc 3 , -.Sh STANDARDS -The -.Fn realloc -function conforms to -.St -ansiC .