MALLOC(3)

NAME
malloc, - general memory allocation function

free - free up memory allocated with malloc, calloc or realloc

realloc - reallocation of memory function

SYNOPSIS
#include <stdlib.h>
void *
malloc(size_t size);
void
free(void *ptr);
void *
realloc(void *ptr, size_t size);

DESCRIPTION
The malloc() function allocates uninitialized space for an object whose size is specified by size. The malloc() function maintains multiple lists of free blocks according to size, allocating space from the appropriate list.

The allocated space is suitably aligned (after possible pointer coercion) for storage of any type of object. If the space is of pagesize or larger, the memory returned will be page-aligned.

The free() function causes the space pointed to by ptr to be deallocated, that is, at least made available for further allocation, but if possible, it will passed back to the kernel with sbrk(2). If ptr is a null pointer, no action occurs.

The realloc() function changes the size of the object pointed to by ptr to the size specified by 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 ptr is a null pointer, the realloc() function behaves like the malloc() function for the specified size. If the space cannot be allocated, the object pointed to by ptr is unchanged. If size is zero and ptr is not a null pointer, the object it points to is freed.

ENVIRONMENT
This malloc will check the environment for a variable called MALLOC_OPTIONS and scan it for flags. Flags are single letters, uppercase means on, lowercase means off.

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.

D   ``dump'' malloc will dump statistics in a file called ``malloc.out'' at exit.

J   ``junk'' fill some junk into the area allocated. Currently junk is bytes of 0xd0, this is pronounced ``Duh'' :-)

R   ``realloc'' always reallocate when realloc() is called, even if the initial allocation was big enough. This can substantially aid in compacting memory.

Z   ``zero'' fill some junk into the area allocated (see ``J''), except for the exact length the user asked for, which is zeroed.

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.

RETURN VALUES
The malloc() function returns a pointer to the allocated space if successful; otherwise a null pointer is returned.

The free() function returns no value.

The realloc() function returns either a null pointer or a pointer to the possibly moved allocated space.

SEE ALSO
brk(2), alloca(3), calloc(3), getpagesize(3), memory(3)

STANDARDS
The malloc() function conforms to ansiC.

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.

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.

On systems which are paging, this can make a factor five in difference on the page-faults of a process.