/* Copyright 1990 by Christopher A. Wichura.
   See file GIFMachine.doc for full description of rights.
*/

#include <exec/types.h>
#include <exec/lists.h>
#include <exec/nodes.h>
#include <exec/memory.h>
#include <proto/exec.h>

struct MyMem {
	struct MinNode mm_Node;
	ULONG mm_Size;
};

struct MinList Mem[2];
UWORD CurrentMem;

void InitMemory(void)
{
	NewList((struct List *)&Mem[0]);
	NewList((struct List *)&Mem[1]);
	CurrentMem = 0;
}

char *MyAlloc(ULONG size)
{
	register struct MyMem *theMemory;
	register ULONG newsize;

	newsize = size + sizeof(struct MyMem);

	if (!(theMemory = (struct MyMem *)AllocMem(newsize, MEMF_PUBLIC|MEMF_CLEAR)))
		return NULL;

	AddTail((struct List *)&Mem[CurrentMem], (struct Node *)&theMemory->mm_Node);
	theMemory->mm_Size = newsize;

	return (char *)theMemory + sizeof(struct MyMem);
}

void MyFree(char *MemPtr)
{
	register struct MyMem *theMemory;

	theMemory = (struct MyMem *)(MemPtr - sizeof(struct MyMem));

	Remove((struct Node *)&theMemory->mm_Node);
	FreeMem((char *)theMemory, theMemory->mm_Size);
}

void FreeAll(UWORD memlist)
{
	register struct MyMem *theMemory;

	while (theMemory = (struct MyMem *)RemTail((struct List *)&Mem[memlist]))
		FreeMem((char *)theMemory, theMemory->mm_Size);
}
