imem(3)

Section: Interp SDK (3)
Updated: 2007-09-24

NAME

imem - wrappers for memory management routines used by interp

SYNOPSIS

#include imem.h

void *Mem_alloc( long nbytes );
void *Mem_calloc( long count, long nbytes );
void Mem_free( void *ptr );

ALLOC(nbytes)
CALLOC(count,nbytes)
NEW(p)
FREE(p)

DESCRIPTION

These functions and macros localize any future changes to memory management. This is a very important strategy because c libraries for embedded targets often have reduced functionality and may or may not have the level of memory management required by interp.

Interp requires all memory allocations to begin on at least a 4-byte (32-bit word) boundary. This is important information if you have to deal with a primitive memory allocator that insists on allocating on byte boundaries.

LIST OF FUNCTIONS AND MACROS

Macro/Function
Description
Mem_alloc
Allocates the requested number of bytes, and returns a pointer to it.
Mem_calloc
Allocates the requested number of bytes, initializes them to zero, and returns a pointer to it.
Mem_free
If the input pointer is not NULL, the memory is freed.
ALLOC
Macro wrapper for Mem_alloc.
CALLOC
Macro wrapper for Mem_calloc.
NEW
Macro wrapper for Mem_alloc that also assigns the memory address of the allocated memory to p.
FREE
Macro wrapper for Mem_free that also clears (zeros) the memory pointer p.

EXAMPLES OF APPROPRIATE MACRO USAGE

Macro Example
Description
definition_ptr = ALLOC(len+1);
Allocates len+1 bytes of memory with this statement:
 definition_ptr = Mem_alloc( (len+1) );
newlp = (iword *) CALLOC( (long)MAX_VAR32, (long)sizeof(iword));
Allocates, and zeros the space for MAX_VAR32 words with this statement:
 newlp = (iword *) Mem_calloc( (long)MAX_VAR32, (long)sizeof(iword));
NEW(cp);
Allocates the space for the structure pointed to by cp with this statement:
 cp = ALLOC( (long) sizeof *cp).
FREE(cp->lfp);
Frees the (previously allocated) memory pointed by cp->lfp, and zeros the pointer with this statement:
 (void) Mem_free( (cp->lfp) ), (cp->lfp) = 0;

SEE ALSO

malloc(3)