/**
 * INCLUDES
 **/

#ifndef T1GST
#include "global.h"
#endif

#include <proto/exec.h>
#include <exec/memory.h>

#ifdef MEMTRACE
#include <stdio.h>
#endif

#ifdef DEBUG
#include <clib/debug_protos.h>
#endif


/**
 * LOCAL PROTOTYPES
 **/
static void *__inline MyAllocVec(void *pool, ULONG size);
static void __inline MyFreeVec(void *pool, void *memory);


/**
 * addmemory
 **/
void *addmemory(struct T1GlyphEngine *engine, long size)
{
	engine->AmishPool = CreatePool(MEMF_PUBLIC, size, size);	// TetiSoft: Should now work without MEMF_CLEAR
	return engine->AmishPool;
}


/**
 * delmemory
 **/
void delmemory(struct T1GlyphEngine *engine)
{
	if (engine->AmishPool)
		DeletePool(engine->AmishPool);
	engine->AmishPool = NULL;
}


/**
 * xiMalloc
 **/
char *xiMalloc(struct T1GlyphEngine *engine, unsigned int Size)
{
	return (char *)MyAllocVec(engine->AmishPool, Size);
}


/**
 * xiFree
 **/
void xiFree(struct T1GlyphEngine *engine, void *addr)
{
	if (addr)
		MyFreeVec(engine->AmishPool, addr);
	return;
}


static void *__inline MyAllocVec(void *pool, ULONG size)
{
	UBYTE *memory;

	size += sizeof(ULONG);

	memory = AllocPooled(pool, size);

	if (!memory)
	{
#ifdef DEBUG
		kprintf("AllocPooled (%ld bytes) failed\n", size);
#endif
		return NULL;
	}

	*((ULONG *) memory) = size;

	return (void *)(memory + sizeof(ULONG));
}


static void __inline MyFreeVec(void *pool, void *memory)
{
	void *realMemory;
	ULONG size;

	if (!memory)
		return;

	realMemory = (UBYTE *) memory - sizeof(ULONG);
	size = *((ULONG *) realMemory);

	FreePooled(pool, realMemory, size);
}

#ifdef MEMTRACE
FILE *tempfp;
#endif

/**
 * addmemory2
 **/
void *addmemory2(struct T1GlyphEngine *engine, long size)
{
#ifdef MEMTRACE
	tempfp = fopen("ram:memtrace", "w");
#endif
	engine->AmishPool2 = CreatePool(MEMF_PUBLIC, size, size);	// TetiSoft: Should now work without MEMF_CLEAR
	return engine->AmishPool2;
}


/**
 * delmemory2
 **/
void delmemory2(struct T1GlyphEngine *engine)
{
	if (engine->AmishPool2)
		DeletePool(engine->AmishPool2);
	engine->AmishPool2 = NULL;
#ifdef MEMTRACE
	fclose(tempfp);
#endif
}

#ifdef MEMTRACE
char *Xalloc(struct T1GlyphEngine *engine, int size, char *file, char *func, int linenum)
#else
char *Xalloc(struct T1GlyphEngine *engine, int size)
#endif
{
#ifdef MEMTRACE
	unsigned long *tmp;
	static unsigned long idnum = 0;

	tmp = (unsigned long *)MyAllocVec(engine->AmishPool2, size+sizeof(unsigned long));
	tmp[0] = idnum++;
	fprintf(tempfp, "%4d calloc   %15s%5d\t%16s\t%lx\t%d\n", tmp[0], file, linenum, func, (unsigned int)tmp+1, size);

	return (char *)(tmp + 1);
#else
	return (char *)MyAllocVec(engine->AmishPool2, size);
#endif
}


#ifdef MEMTRACE
void Xfree(struct T1GlyphEngine *engine, char *p, char *file, char *func, int linenum)
#else
void Xfree(struct T1GlyphEngine *engine, char *p)
#endif
{
#ifdef MEMTRACE
	unsigned long *tmp = NULL;

	if (!p)
		return;

	tmp = ((unsigned long *)p) - 1;
	fprintf(tempfp, "%4d free     %15s%5d\t%16s\t%lx\n", tmp[0], file, linenum, func, (unsigned int)tmp+1);
	MyFreeVec(engine->AmishPool2, (void *)tmp);
#else
	MyFreeVec(engine->AmishPool2, (void *)p);
#endif
	return;
}
