7aa70c9e8e
"zero' and 'junk' options to help find and diagnose malloc abuse. EXTRA_SANITY defaults "junk" to on. Don't dump the internal state, unless EXTRA_SANITY. General code cleanup. Error messages cleaned up a bit, more checking earlier. EXTRA_SANITY is default at this time (just in case). Performance (without EXTRA_SANITY) is better, beats gnumalloc in both time & space most of the time: # In-memory test. ./malloc 50000000 2000 8192 159.2u 1.5s 2:41.85 99.3% 5+7742k 0+0io 0pf+0w ./gnumalloc 50000000 2000 8192 272.6u 0.4s 4:35.01 99.3% 5+8533k 0+0io 0pf+0w # Swap-space test. ./malloc 500000 14000 8192 6.5u 4.1s 4:08.87 4.3% 5+49209k 0+0io 9772pf+0w ./gnumalloc 500000 14000 8192 16.2u 14.5s 15:36.14 3.2% 5+54100k 0+0io 47651pf+0w # Small items test. ./malloc 20000000 20000 2048 67.0u 0.3s 1:07.83 99.2% 5+18199k 0+0io 4pf+0w ./gnumalloc 20000000 20000 2048 66.2u 0.3s 1:07.03 99.3% 5+18107k 0+0io 0pf+0w SANITY is not an option anymore. (!!)
192 lines
6.0 KiB
Groff
192 lines
6.0 KiB
Groff
.\" 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
|
|
.\" 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.
|
|
.\"
|
|
.\" @(#)malloc.3 8.1 (Berkeley) 6/4/93
|
|
.\"
|
|
.Dd June 4, 1993
|
|
.Dt MALLOC 3
|
|
.Os BSD 4
|
|
.Sh NAME
|
|
.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 <stdlib.h>
|
|
.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
|
|
function allocates uninitialized space for an object whose
|
|
size is specified by
|
|
.Fa size .
|
|
The
|
|
.Fn malloc
|
|
function maintains multiple lists of free blocks according to size, allocating
|
|
space from the appropriate list.
|
|
.Pp
|
|
The allocated space is
|
|
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 that tollerate 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.
|
|
|
|
.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 zerod.
|
|
|
|
.El
|
|
.Pp
|
|
The ``J'' and ``Z'' is mostly for testing and debugging,
|
|
if a program changes behaviour 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 pagesize 2 ,
|
|
.Xr calloc 3 ,
|
|
.Xr alloca 3 ,
|
|
.Xr memory 3
|
|
.Sh STANDARDS
|
|
The
|
|
.Fn malloc
|
|
function conforms to
|
|
.St -ansiC .
|
|
.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 belived 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
|
|
pagefaults of a process.
|