// ------------------------------------------------------------------------------------------------
// MyMalloc.c
//
// This contains SH4 memory allocation procs for installation into
// audio64's SH4 memory allocation shell.
// 
// NOTE: audio64 does not allocate SH4 memory, the only time this
// shell is used is in the samples.
//
// COPYRIGHT (C) SEGA OF AMERICA, INC. 1998
// All rights reserved.
//
// Information and source code samples contained herein are 
// provided "as-is", without representations or warranties, and 
// are subject to change without notice.
//
// SEGA OF AMERICA, INC. cannot support modifications or 
// derivative works created from the sample source code provided. 
// Developers may use, reproduce, and modify portions or entire 
// sections of the sample source code for the purposes of creating
// Dreamcast applications.  
//
// Sample source code contained herein may not be appropriate 
// for use under all circumstances.  Please consult Developer 
// documentation provided along with your Dreamcast unit for
// additional information and suggestions.
//
//
// Author: Ralph "Cooksey" Thomas
//
// Tabs:	4
//
// Rev:		06/04/99	File Originated
// ------------------------------------------------------------------------------------------------

#include <am.h>
#include "MyMalloc.h"

// XSTARTDOC ---------------------------------------------------------------------------------------
// FUNCTION NAME MySh4Alloc
// TERSE DESCRIPTION The default proc installed if none supplied.
//
// FORMAT
//
// #include <am.h>
//
// KTBOOL MySh4Alloc(volatile KTU32 ** base,volatile KTU32 ** aligned,KTU32 size,KTU32 alignment) 
// 
// PARAMETERS
//
// volatile KTU32 ** base, an unaligned pointer to the block allocated
// volatile KTU32 ** aligned, a pointer to the first aligned address after the base address
// KTU32 size,KTU32 alignment, the alignment desired
// 
// RETURN VALUE
//
// KTBOOL, KTTRUE if a block was successfully allocated
// 
// FUNCTION
//
// This is the default malloc proc that will be installed in smOpen() if a custom malloc proc is not
// provided using the amMemInstallAlternateMemoryManager() call
// 
// XENDDOC -----------------------------------------------------------------------------------------

KTBOOL MySh4Alloc(volatile KTU32 ** base,volatile KTU32 ** aligned,KTU32 size,KTU32 alignment)
{
volatile KTU32 *	baseAddress		=	KTNULL;
volatile KTU32 *	alignedAddress	=	KTNULL;

if((baseAddress=(volatile KTU32 *)syMalloc(size + (alignment-1)))==0x00)
	return KTFALSE;

alignedAddress = (volatile KTU32 *)(((KTU32)baseAddress) + (alignment - ((KTU32)baseAddress) % alignment));

*base		=	baseAddress;
*aligned	=	alignedAddress;

return KTTRUE;
}

// XSTARTDOC ---------------------------------------------------------------------------------------
// FUNCTION NAME MySh4Free
// TERSE DESCRIPTION The default proc installed if none supplied.
//
// FORMAT
//
// #include <am.h>
//
// void MySh4Free(volatile KTU32 * block)
// 
// PARAMETERS
//
// volatile KTU32 * block, a pointer to the unaligned base address of the block to be freed
// 
// RETURN VALUE
//
// void
// 
// FUNCTION
//
// This is the default free proc that will be installed in smOpen() if a custom free proc is not
// provided using the amMemInstallAlternateMemoryManager() call
// 
// XENDDOC -----------------------------------------------------------------------------------------

void MySh4Free(volatile KTU32 * block)
{
syFree((void *)block);
}


