ALLOC(3)

NAME
Malloc, Salloc, Realloc, Srealloc, st_alloc, st_free - low level memory allocation routines

SYNOPSIS
#include <alloc.h>

char *Malloc(size)
unsigned int size;

char *Salloc(str, size)
char *str; unsigned int size;

char *Realloc(ptr, size) char *buf; unsigned int size;

char *Srealloc(str, size)
char *str;
unsigned int size;

char *st_alloc(phead, size, count)
char **phead;
unsigned int size;

st_free(ptr, phead, size)
char *ptr;
char **phead;
unsigned int size;

void clear(ptr, size)
char *ptr;
unsigned int size;

DESCRIPTION
This set of routines provides a checking memory allocation mechanism.

Malloc returns a pointer to a block of at least size bytes, beginning on a boundary suitable for any data type.

Salloc returns a pointer to a block of at least size bytes, initialized with the null-terminated string str.

Realloc changes the size of the block at buf to size bytes, and returns a pointer to the (possibly moved) block. If buf is a null pointer, Realloc behaves as Malloc.

Srealloc reallocates the string at str to size bytes. It actually does the same as Realloc, and exists only for backwards compatibility.

All these routines use malloc and realloc. The routine free can be used on pointers returned by these routines.

St_alloc and st_free provide a mechanism for maintaining free lists of structures. St_alloc takes three parameters: phead is a pointer to a field containing the head of the free list, size contains the size of the structures, and count indicates how many new structures must be allocated in case the free list is exhausted. It returns a pointer to a zero-initialized structure. St_free also takes three parameters: ptr is a pointer to the structure to be freed, phead is again a pointer to a field containing the head of the free list, and size again contains the size of the structures. These last two routines are best used in a macro.

Clear clears size bytes, starting at ptr.

FILES
~em/modules/h/alloc.h
~em/modules/lib/liballoc.a

MODULES USED
system(3)

SEE ALSO
malloc(3)

DIAGNOSTICS
Malloc, Salloc, Realloc, Srealloc, and st_alloc call a routine No_Mem if there is no memory available. This routine is not supposed to return. A default one, that gives an error message and stops execution, is provided.

BUGS
The st_alloc mechanism only works for structures that are large enough to contain one pointer. Also, st_free actually is a macro, and references its arguments more than once, so they better not have side-effects.