|
@ -33,31 +33,30 @@ |
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|
|
.\" SUCH DAMAGE. |
|
|
.\" SUCH DAMAGE. |
|
|
.\" |
|
|
.\" |
|
|
.\" $OpenBSD: malloc.3,v 1.15 1999/06/29 18:36:21 aaron Exp $ |
|
|
|
|
|
|
|
|
.\" $OpenBSD: malloc.3,v 1.16 1999/11/09 22:12:36 aaron Exp $ |
|
|
.\" |
|
|
.\" |
|
|
.Dd August 27, 1996 |
|
|
.Dd August 27, 1996 |
|
|
.Dt MALLOC 3 |
|
|
.Dt MALLOC 3 |
|
|
.Os |
|
|
.Os |
|
|
.Sh NAME |
|
|
.Sh NAME |
|
|
.Nm malloc , |
|
|
.Nm malloc , |
|
|
.Nd general memory allocation function |
|
|
|
|
|
.Pp |
|
|
|
|
|
|
|
|
.Nm calloc , |
|
|
|
|
|
.Nm realloc , |
|
|
.Nm free , |
|
|
.Nm free , |
|
|
.Nm cfree |
|
|
.Nm cfree |
|
|
.Nd free up memory allocated with malloc, calloc or realloc |
|
|
|
|
|
.Pp |
|
|
|
|
|
.Nm realloc |
|
|
|
|
|
.Nd reallocation of memory function |
|
|
|
|
|
|
|
|
.Nd memory allocation and deallocation |
|
|
.Sh SYNOPSIS |
|
|
.Sh SYNOPSIS |
|
|
.Fd #include <stdlib.h> |
|
|
.Fd #include <stdlib.h> |
|
|
.Ft void * |
|
|
.Ft void * |
|
|
.Fn malloc "size_t size" |
|
|
.Fn malloc "size_t size" |
|
|
|
|
|
.Ft void * |
|
|
|
|
|
.Fn calloc "size_t nmemb" "size_t size" |
|
|
|
|
|
.Ft void * |
|
|
|
|
|
.Fn realloc "void *ptr" "size_t size" |
|
|
.Ft void |
|
|
.Ft void |
|
|
.Fn free "void *ptr" |
|
|
.Fn free "void *ptr" |
|
|
.Ft void |
|
|
.Ft void |
|
|
.Fn cfree "void *ptr" |
|
|
.Fn cfree "void *ptr" |
|
|
.Ft void * |
|
|
|
|
|
.Fn realloc "void *ptr" "size_t size" |
|
|
|
|
|
.Ft char * |
|
|
.Ft char * |
|
|
.Va malloc_options |
|
|
.Va malloc_options |
|
|
.Sh DESCRIPTION |
|
|
.Sh DESCRIPTION |
|
@ -80,6 +79,14 @@ or larger, the memory returned will be page-aligned. |
|
|
Allocation of a zero size object returns a pointer to a zero size object. |
|
|
Allocation of a zero size object returns a pointer to a zero size object. |
|
|
.Pp |
|
|
.Pp |
|
|
The |
|
|
The |
|
|
|
|
|
.Fn calloc |
|
|
|
|
|
function allocates space for an array of |
|
|
|
|
|
.Fa nmemb |
|
|
|
|
|
objects, each of whose size is |
|
|
|
|
|
.Fa size . |
|
|
|
|
|
The space is initialized to all bits zero. |
|
|
|
|
|
.Pp |
|
|
|
|
|
The |
|
|
.Fn free |
|
|
.Fn free |
|
|
function causes the space pointed to by |
|
|
function causes the space pointed to by |
|
|
.Fa ptr |
|
|
.Fa ptr |
|
@ -231,13 +238,17 @@ See above. |
|
|
.Sh RETURN VALUES |
|
|
.Sh RETURN VALUES |
|
|
The |
|
|
The |
|
|
.Fn malloc |
|
|
.Fn malloc |
|
|
function returns |
|
|
|
|
|
|
|
|
and |
|
|
|
|
|
.Fn calloc |
|
|
|
|
|
functions return |
|
|
a pointer to the allocated space if successful; otherwise |
|
|
a pointer to the allocated space if successful; otherwise |
|
|
a null pointer is returned. |
|
|
a null pointer is returned. |
|
|
.Pp |
|
|
.Pp |
|
|
The |
|
|
The |
|
|
.Fn free |
|
|
.Fn free |
|
|
function returns no value. |
|
|
|
|
|
|
|
|
and |
|
|
|
|
|
.Fn cfree |
|
|
|
|
|
functions return no value. |
|
|
.Pp |
|
|
.Pp |
|
|
The |
|
|
The |
|
|
.Fn realloc |
|
|
.Fn realloc |
|
@ -286,14 +297,16 @@ A pointer to a free chunk is attempted freed again. |
|
|
.Pp |
|
|
.Pp |
|
|
``junk pointer, too high to make sense.'' |
|
|
``junk pointer, too high to make sense.'' |
|
|
The pointer doesn't make sense. It's above the area of memory that |
|
|
The pointer doesn't make sense. It's above the area of memory that |
|
|
malloc knows something about. |
|
|
|
|
|
|
|
|
.Fn malloc |
|
|
|
|
|
knows something about. |
|
|
This could be a pointer from some |
|
|
This could be a pointer from some |
|
|
.Xr mmap 2 'ed |
|
|
.Xr mmap 2 'ed |
|
|
memory. |
|
|
memory. |
|
|
.Pp |
|
|
.Pp |
|
|
``junk pointer, too low to make sense.'' |
|
|
``junk pointer, too low to make sense.'' |
|
|
The pointer doesn't make sense. It's below the area of memory that |
|
|
The pointer doesn't make sense. It's below the area of memory that |
|
|
malloc knows something about. |
|
|
|
|
|
|
|
|
.Fn malloc |
|
|
|
|
|
knows something about. |
|
|
This pointer probably came from your data or bss segments. |
|
|
This pointer probably came from your data or bss segments. |
|
|
.Pp |
|
|
.Pp |
|
|
``malloc() has never been called.'' |
|
|
``malloc() has never been called.'' |
|
@ -304,7 +317,9 @@ realloc'ed. |
|
|
The pointer passed to free or realloc has been modified. |
|
|
The pointer passed to free or realloc has been modified. |
|
|
.Pp |
|
|
.Pp |
|
|
``pointer to wrong page.'' |
|
|
``pointer to wrong page.'' |
|
|
The pointer that malloc is trying to free is not pointing to |
|
|
|
|
|
|
|
|
The pointer that |
|
|
|
|
|
.Fn malloc |
|
|
|
|
|
is trying to free is not pointing to |
|
|
a sensible page. |
|
|
a sensible page. |
|
|
.Pp |
|
|
.Pp |
|
|
``recursive call.'' |
|
|
``recursive call.'' |
|
@ -338,14 +353,20 @@ The |
|
|
function conforms to |
|
|
function conforms to |
|
|
.St -ansiC . |
|
|
.St -ansiC . |
|
|
.Sh HISTORY |
|
|
.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 |
|
|
|
|
|
|
|
|
The present implementation of |
|
|
|
|
|
.Fn malloc |
|
|
|
|
|
started out as a filesystem on a drum |
|
|
|
|
|
attached to a 20-bit binary challenged computer built with discrete germanium |
|
|
transistors, and it has since graduated to handle primary storage rather than |
|
|
transistors, and it has since graduated to handle primary storage rather than |
|
|
secondary. |
|
|
secondary. |
|
|
.Pp |
|
|
.Pp |
|
|
The main difference from other malloc implementations are believed to be that |
|
|
|
|
|
|
|
|
The main difference from other |
|
|
|
|
|
.Fn malloc |
|
|
|
|
|
implementations are believed to be that |
|
|
the free pages are not accessed until allocated. |
|
|
the free pages are not accessed until allocated. |
|
|
Most malloc implementations will store a data structure containing a, |
|
|
|
|
|
|
|
|
Most |
|
|
|
|
|
.Fn malloc |
|
|
|
|
|
implementations will store a data structure containing a, |
|
|
possibly double-, linked list in the free chunks of memory, used to tie |
|
|
possibly double-, linked list in the free chunks of memory, used to tie |
|
|
all the free memory together. |
|
|
all the free memory together. |
|
|
That is a quite suboptimal thing to do. |
|
|
That is a quite suboptimal thing to do. |
|
|