#include <Global/Main_Global.h>

APTR		MemoryPool;
APTR AllocVecPooled(APTR,ULONG);
VOID FreeVecPooled(APTR);

BOOL Startup_MemoryManager( void )
{

    MemoryPool = CreatePool(MEMF_PUBLIC|MEMF_CLEAR,32768,16384);
    if(MemoryPool)
      return(TRUE);
     else
      return(FALSE);

}

void Shutdown_MemoryManager(void )
{


    if(MemoryPool)
    {
       DeletePool(MemoryPool);
    }
    else
     printf("Problem with the pool\n Maybe it needs drained?\n");
    

}
APTR Allocate_Memory(ULONG Amount)
{

   APTR Memory;
   Memory = AllocVecPooled(MemoryPool,Amount);
   if(Memory)
       {
       // KPrintF("Allocated %ld Bytes of Memory At 0x%lx\n",Amount,&Memory);
        return(Memory);
       }
   else
    {
       DisplayBeep(0);
       printf("Very Low On Memory, Please Reboot!\n");
       return(NULL);
    }
      

}

void Free_Memory( APTR Memory)
{

     
      FreeVecPooled(Memory);
      

}

APTR AllocVecPooled( APTR pool, ULONG size )
{

        ULONG *memblock;

        if( !pool || !size ) return( NULL );

        if( !(memblock = AllocPooled( pool, size + 2*sizeof(APTR) ) ) )
                return( NULL );

        *memblock++ = (ULONG) pool;
        *memblock++ = size + 2*sizeof( APTR );

        return( memblock );

}

void FreeVecPooled( APTR memblock )
{

        ULONG *m = memblock;
        APTR pool;
        ULONG size;

        if( m )
        {
                size = *--m;
                pool = (APTR) *--m;
                FreePooled( pool, m, size );
        }

}       






