MEMMAN(3SDF)

NAME
mnew, mfree - functions for fast allocation of small memory chunks.

SYNOPSIS
 char *mnew(int size)
 void mfree(char **memblk, int size)

DESCRIPTION
Mnew() returns a memory block of the requested size. The parameter size is in bytes. All bytes in the block are cleared. Mfree() returns a memory block to the free list so that it can be used by mnew() again. Mnew is much faster than malloc(3) because it obtains large blocks of memory from the system by calling sbrk(2). Each time mnew() is called it just returns a small piece of this large block. Only once in many times the large block is exhausted, urging mnew() to call sbrk() again. All the sealib functions call mnew() and mfree() to deal with memory allocation for the structures that make up the in-core seadif tree (see sealib(3SDF)).

RETURN VALUE
Mnew returns a pointer to the requested block. Alignment of the pointer is guaranteed to be suitable for any data type in the C programming language. If the requested block size is out of range, mnew() returns NIL.

LIMITATION
Mnew() and mfree() do not deal with blocks larger than 256 bytes.

EXAMPLE
Suppose we have a structure named DTYPE, then the following two #define's provide functionality to create new objects of type DTYPE and also to get rid of them at will:
 #define NewDtype(dptr) \
         { \
         (dptr)=(DTYPE *)mnew(sizeof(DTYPE)); \
         (dptr)->field=initvalue; \
         }
 #define FreeDtype(dptr) \
         mfree((char **)dptr,sizeof(DTYPE)) If initvalue is 0 then the initializing statement in the NewDtype macro is unnecessary. We don't check if mnew() returns NIL because we know at compile time that sizeof(DTYPE) is within the permitted range.

AUTHOR
Paul Stravers, Delft University of Technology. E-mail address is ocean@donau.et.tudelft.nl.

SEE ALSO
malloc(3), sbrk(2), sealib(3SDF).