/*-- RemAlloc.c --------------------------------------------------------*/
/*  ChipAlloc(), ExtAlloc(), RemAlloc(), RemFree().                     */
/*  ALLOCators which REMember the size allocated, for simpler freeing.  */
/*----------------------------------------------------------------------*/
 
#include <exec/types.h>
#include <exec/nodes.h>
#include <exec/memory.h>
 
 
/*--------------- Function declarations for MANX ---------------*/
 
extern void *AllocMem();
 
/* first 4 bytes of allocated area contain the length of the area.
 * This length is the number of bytes allocated by AllocMem, ie includes
 * the 4 bytes for itself */
 
/* size in bytes.  That is what the "sizeof" function returns.
 * Example:
 *   struct BitMap *bm;
 *   bm = RemAlloc( sizeof(struct BitMap), ...flags... );
 */
UBYTE *RemAlloc(size,flags)
 LONG size, flags;
 {
    register LONG *p;
    register LONG asize = size+4;
    p = (LONG *)AllocMem(asize,flags);
    if (p != NULL)
        *p++ = asize;   /* post-bump p to point at clients area*/
    return((UBYTE *)p);
 }
 
/* ALLOCator that remembers size, allocates in CHIP-accessable memory.
 * Use for all data to be displayed on screen, all sound data, all data to be
 * blitted, disk buffers, or access by any other DMA channel.
 * Does clear memory being allocated.*/
 
UBYTE *ChipAlloc(size)
LONG size;
{
    return(RemAlloc(size, MEMF_CLEAR|MEMF_PUBLIC|MEMF_CHIP));
}
 
/* ALLOCator that remembers size, allocates in extended memory.
 * Does clear memory being allocated.
 * NOTICE: does NOT declare "MEMF_FAST".  This allows machines
 * lacking extended memory to allocate within chip memory,
 * assuming there is enough memory left.  */
 
UBYTE *ExtAlloc(size)
LONG size;
{
    return(RemAlloc(size, MEMF_CLEAR|MEMF_PUBLIC));
}
 
/* FREEs either chip or extended memory, if allocated with an allocator
 * which REMembers size allocated.
 * Safe: won't attempt to de-allocate a NULL pointer.*/
 
RemFree(p)
UBYTE *p;
{
    if (p != NULL) {
        p -= 4;
        return(FreeMem(p, *((LONG *)p)));
    }
}
 
