 
/*#define GEO_D_DebugMalloc*/
/*#define GEO_D_DebugStatistic*/
 
#include <assert.h>
#include "cpa_std.h"
 
#include "acp_base.h"
 
#define __DeclareGlobalVariableErrGEO_h__
#include "GEO\ErrGEO.h"
#undef __DeclareGlobalVariableErrGEO_h__
#define __DeclareGlobalVariableMmgGEO_h__
#include "GEO\MmgGEO.h"
#undef __DeclareGlobalVariableMmgGEO_h__
 
#include "GEO\GEOMem.h"
#include "Gli\Init_Gli.h"
#ifndef U64
#include "SCR.h"
#endif
#include "Gam\ZeMem.h"
 
/* XB 16/06/99 */
#ifdef U64
extern char _lddcodeSegmentStart[], _lddcodeSegmentEnd[];
#endif /* U64 */
/* End XB 16/06/99 */
 
/* XB 22/06/99 */
#ifndef U64
extern CPA_EXPORT void GLI_TEX_vSignalCurrentMemoryChannel(unsigned char ucCurrentChannel);
extern CPA_EXPORT void GLI_TEX_vKillMemoryChannel(unsigned char ucCurrentChannel);
#endif /* U64 */
/* End XB 22/06/99 */
 
 
#if !defined(RETAIL) || defined(FINAL_VERSION_FOR_TESTERS)
void GEO_PrintUsedStaticMemory()
{
	Mmg_M_PrintUsedStaticMemoryInModule(GEO);
}
#endif /* !defined(RETAIL) || defined(FINAL_VERSION_FOR_TESTERS) */
 
/**************************************************************************/
/*XB*/
#ifdef CHECK_MEMORY
void GEO_CheckMemory(void)
{
	Mmg_M_CheckMemory(GEO);
}
#endif /* CHECK_MEMORY */
/*End XB*/
 
/**************************************************************************/
/* XB 02/06/99 */
#ifndef FINAL_VERSION
unsigned long GEO_fn_ulGetUsedStaticMemory(void)
{
	return Mmg_M_GetUsedStaticMemory(GEO);
}
#endif /* FINAL_VERSION */
/* End XB 02/06/99 */
 
 
void
GEO_xInitGEOError()
{
 Erm_M_InitErrMsg(GEO);
 Mmg_M_InitMmg(GEO);

/* XB 22/06/99 */
#ifndef U64
 GLI_TEX_vSignalCurrentMemoryChannel(E_ucDynamic);
#endif /* U64 */
/* End XB 22/06/99 */
     
 #if defined(_DEBUG)
 GEO_g_bDynamicAllocation = GEO_C_CanUseDynamic;
 #else
 GEO_g_bDynamicAllocation = GEO_C_CanNotUseDynamic;
 #endif /* _DEBUG */
}
 
void
GEO_xCreateMemoryChannel(unsigned char ucChannel,unsigned long ulSize)
{
#ifndef U64
 
	Mmg_M_InitBlockV5_1_0(GEO, ucChannel, ulSize,300000);
#else /* !U64 */
	if((ucChannel==ACP_U64_FixMemoryChannel)||(ucChannel==ACP_U64_LevelMemoryChannel))
	{
	#ifdef CHECK_MEMORY
		Mmg_M_InitSpecificBlock(GEO, ucChannel, ulSize, 300000, 8, C_BlockWithFreeFlag | C_Check_AlignementFlag | C_Check_OverflowFlag);
	#else /* CHECK_MEMORY */
		Mmg_M_InitSpecificBlock(GEO, ucChannel, ulSize, 300000, 8, C_BlockWithoutFreeFlag | C_Check_AlignementFlag | C_Check_OverflowFlag);
	#endif /* CHECK_MEMORY */
	}
	else
	/* XB 16/06/99 */
	if(ucChannel==ACP_U64_LDDMemoryChannel)
	{
		g_ucUseLDDBloc=1;
	#ifdef CHECK_MEMORY
		Mmg_M_InitSpecificBlock(GEO, ucChannel, _lddcodeSegmentEnd-_lddcodeSegmentStart-4, 300000, 4, C_BlockWithFreeFlag | C_Check_AlignementFlag | C_Check_OverflowFlag);
	#else /* CHECK_MEMORY */
		Mmg_M_InitSpecificBlock(GEO, ucChannel, _lddcodeSegmentEnd-_lddcodeSegmentStart-4, 300000, 4, C_BlockWithoutFreeFlag | C_Check_AlignementFlag | C_Check_OverflowFlag);
	#endif /* CHECK_MEMORY */
		g_ucUseLDDBloc=0;
	}
	else
	/* End XB 16/06/99 */
	{
	#ifdef CHECK_MEMORY
		Mmg_M_InitSpecificBlock(GEO, ucChannel, ulSize, 300000, 4, C_BlockWithFreeFlag | C_Check_AlignementFlag | C_Check_OverflowFlag);
	#else /* CHECK_MEMORY */
		Mmg_M_InitSpecificBlock(GEO, ucChannel, ulSize, 300000, 4, C_BlockWithoutFreeFlag | C_Check_AlignementFlag | C_Check_OverflowFlag);
	#endif /* CHECK_MEMORY */
	}
#endif /* !U64 */
}
 
void
GEO_xSelectMemoryChannel(unsigned char ucChannel)
{
  if ((E_ucGEOMaxBlocksNb < ucChannel)
	  && (ucChannel != E_ucDynamic))
  {
 	 Erm_M_ClearLastError ( C_ucErmDefaultChannel );
	 Erm_M_UpdateLastError( GEO, C_ucErmDefaultChannel, E_uwGEOUnAvailableChannel , C_lErmNoDebugData, C_ucErmOpenInfoWindow, C_ucAllowStopForDebug, "Malloc allocation");
  }
  g_ucGEOMMemMallocMode = ucChannel;

/* XB 22/06/99 */
#ifndef U64
  GLI_vSignalCurrentMemoryChannel(ucChannel);
#endif /* U64 */
/* End XB 22/06/99 */
}
 
 
void
GEO_xDeleteMemoryChannel(unsigned char ucChannel)
{
  if ((E_ucGEOMaxBlocksNb < ucChannel)
	  && (ucChannel != E_ucDynamic))
  {
 	 Erm_M_ClearLastError ( C_ucErmDefaultChannel );
	 Erm_M_UpdateLastError( GEO, C_ucErmDefaultChannel, E_uwGEOUnAvailableChannel , C_lErmNoDebugData, C_ucErmOpenInfoWindow, C_ucAllowStopForDebug, "Malloc allocation");
  }

/* XB 22/06/99 */
#ifndef U64
  GLI_vKillMemoryChannel(ucChannel);
#endif /* U64 */
/* End XB 22/06/99 */

  Mmg_M_DeleteBlock(GEO, ucChannel);
}

/**************************************************************************/
/* XB 22/06/99 */
#ifdef U64
void *GEO_PRI_fn_pvMalloc(unsigned long _ulSize)
{
	void	*pvBuffer=NULL;

	if(_ulSize!=0)
	{
#ifndef FINAL_VERSION
		g_ucModuleIdForDebug=0;
#endif /* FINAL_VERSION */

    pvBuffer=Mmg_fn_p_vAlloc4Ch(_ulSize,C_ucMmgDefaultChannel);

#ifndef FINAL_VERSION
		g_ucModuleIdForDebug=0xff;
#endif /* FINAL_VERSION */
	}

	return pvBuffer;
}
#endif /* U64 */
/* End XB 22/06/99 */

/**************************************************************************/
/* XB 22/06/99 */
#ifdef U64
void *GEO_fn_pvMalloc(unsigned long _ulSize)
{
	void	*pvBuffer;

  M_GEOInitMem();
	pvBuffer=GEO_PRI_fn_pvMalloc(_ulSize);
	memset(pvBuffer,0,_ulSize);

	return pvBuffer;
}
#endif /* U64 */
/* End XB 22/06/99 */

/**************************************************************************/
/* XB 22/06/99 */
#ifdef U64
void *GEO_fn_pvMallocAlign8(unsigned long _ulSize)
{
	void	*pvBuffer=NULL;

	Mmg_M_SetModeAlloc4Ch(GEO,g_ucGEOMMemMallocMode+2,C_ucMmgDefaultChannel);
	pvBuffer=GEO_PRI_fn_pvMalloc(_ulSize);
	memset(pvBuffer,0,_ulSize);

	return pvBuffer;
}
#endif /* U64 */
/* End XB 22/06/99 */

/**************************************************************************/
/* XB 22/06/99 */
#ifdef U64
void *GEO_fn_pvMallocLDD(unsigned long _ulSize)
{
	void	*pvBuffer=NULL;

	Mmg_M_SetModeAlloc4Ch(GEO,ACP_U64_LDDMemoryChannel,C_ucMmgDefaultChannel);
	pvBuffer=GEO_PRI_fn_pvMalloc(_ulSize);

	return pvBuffer;
}
#endif /* U64 */
/* End XB 22/06/99 */
 
#define GEO_C_NumberOfSource 200
 
 #if defined(_DEBUG)&&(defined(GEO_D_DebugStatistic)||defined(GEO_D_DebugMalloc))
 unsigned long GEO_g_ulNumberOfAllocation = 0;
 #endif /* _DEBUG&&(GEO_D_DebugStatistic||GEO_D_DebugMalloc) */
 
 #if defined(_DEBUG)&&defined(GEO_D_DebugStatistic)
 unsigned long GEO_g_ulMaxSources = 0;
 struct GEO_tdstAllocInformation_
 {
  char GEO_szSourceName[_MAX_PATH];
  unsigned long GEO_ulSourceLine;
  unsigned short GEO_usSourceBloc;
  unsigned long GEO_ulSourceSize;
  unsigned long GEO_ulSourceNumberOfAlloc;
  unsigned long GEO_ulSourceNumberOfRealloc;
  unsigned long GEO_ulSourceNumberOfFree;
 } GEO_g_a_stAllocInformation[GEO_C_NumberOfSource];
 #endif /*_DEBUG&&GEO_D_DebugStatistic*/
 
#ifndef _FIRE_DEADCODE_U64_
void 
GEO_fn_vMemoryLogFile(void *p_vPointer,unsigned char ucAction,char *szFile,unsigned long ulLine,unsigned long ulSize)
{
 #if defined(_DEBUG)&&(defined(GEO_D_DebugStatistic)||defined(GEO_D_DebugMalloc))
/* static unsigned long GEO_g_ulNumberOfAllocation = 0;*/
 unsigned short uwBlocId;
 void *p_vBeginBloc;
 #endif /* _DEBUG&&(GEO_D_DebugStatistic||GEO_D_DebugMalloc) */
 
 #if defined(_DEBUG)&&defined(GEO_D_DebugStatistic)
 unsigned long i;
 #endif /*_DEBUG&&GEO_D_DebugStatistic*/
 
 #if defined(_DEBUG)&&defined(GEO_D_DebugMalloc)
 FILE *p_stFile;
 char szFileName[30];
 #endif /* _DEBUG && GEO_D_DebugMalloc */
 
 #if defined(_DEBUG)&&(defined(GEO_D_DebugStatistic)||defined(GEO_D_DebugMalloc))
 Mmg_fn_vWhereIs((void*)p_vPointer,&uwBlocId,&p_vBeginBloc);
 
 if (ucAction==GEO_C_ActionMalloc) 
  GEO_g_ulNumberOfAllocation++;
 else if (ucAction==GEO_C_ActionFree) 
  GEO_g_ulNumberOfAllocation--;
 #endif /* _DEBUG&&(GEO_D_DebugStatistic||GEO_D_DebugMalloc) */
 
 #if defined(_DEBUG)&&defined(GEO_D_DebugStatistic)
 for (i=0;i<GEO_g_ulMaxSources&&(stricmp(szFile,GEO_g_a_stAllocInformation[i].GEO_szSourceName)||GEO_g_a_stAllocInformation[i].GEO_ulSourceLine!=ulLine||GEO_g_a_stAllocInformation[i].GEO_usSourceBloc!=uwBlocId);i++);
 if (i==GEO_g_ulMaxSources)
 {
  strcpy(GEO_g_a_stAllocInformation[i].GEO_szSourceName,szFile);
  GEO_g_a_stAllocInformation[i].GEO_ulSourceLine=ulLine;
  GEO_g_a_stAllocInformation[i].GEO_usSourceBloc = uwBlocId;
  GEO_g_a_stAllocInformation[i].GEO_ulSourceSize=0;
  GEO_g_a_stAllocInformation[i].GEO_ulSourceNumberOfAlloc=0;
  GEO_g_a_stAllocInformation[i].GEO_ulSourceNumberOfRealloc=0;
  GEO_g_a_stAllocInformation[i].GEO_ulSourceNumberOfFree=0;
  GEO_g_ulMaxSources++;
  if (GEO_g_ulMaxSources>=GEO_C_NumberOfSource)
   Erm_M_UpdateLastError( GEO, C_ucErmDefaultChannel,E_uwGEOIncreaseNumberOfSource, C_lErmNoDebugData, C_ucErmOpenInfoWindow, C_ucAllowStopForDebug, NULL);
 }
 
 if (ucAction==GEO_C_ActionMalloc)
 {
  GEO_g_a_stAllocInformation[i].GEO_ulSourceSize+=ulSize;
  GEO_g_a_stAllocInformation[i].GEO_ulSourceNumberOfAlloc++;
 }
 else if (ucAction==GEO_C_ActionRealloc)
 {
  GEO_g_a_stAllocInformation[i].GEO_ulSourceSize+=ulSize;
  GEO_g_a_stAllocInformation[i].GEO_ulSourceNumberOfRealloc++;
 }
 else if (ucAction==GEO_C_ActionFree)
 {
  GEO_g_a_stAllocInformation[i].GEO_ulSourceNumberOfFree++;
 }
 #endif /*_DEBUG&&GEO_D_DebugStatistic*/
 
 #if defined(_DEBUG)&&defined(GEO_D_DebugMalloc)
 sprintf(szFileName,"Mem%04x.log",uwBlocId);
 if ((p_stFile = fopen(szFileName,"rt"))!=NULL) 
  fclose(p_stFile);
 else
  GEO_g_ulNumberOfAllocation = 0;
 p_stFile = fopen(szFileName,"at");
 if (p_stFile!=NULL)
 {
  if (ucAction==GEO_C_ActionMalloc) 
   fprintf(p_stFile,"Malloc  : Number=#%010d, Block=#%04x, Offset=%p (%20s,%10d)\n",GEO_g_ulNumberOfAllocation,uwBlocId,(long)p_vPointer-(long)p_vBeginBloc,szFile,ulLine);
  else if (ucAction==GEO_C_ActionRealloc) 
   fprintf(p_stFile,"Realloc : Number=#%010d, Block=#%04x, Offset=%p (%20s,%10d)\n",GEO_g_ulNumberOfAllocation,uwBlocId,(long)p_vPointer-(long)p_vBeginBloc,szFile,ulLine);
  else if (ucAction==GEO_C_ActionFree) 
   fprintf(p_stFile,"Free    : Number=#%010d, Block=#%04x, Offset=%p (%20s,%10d)\n",GEO_g_ulNumberOfAllocation,uwBlocId,(long)p_vPointer-(long)p_vBeginBloc,szFile,ulLine);
  fclose(p_stFile);
 }
 #endif /* _DEBUG && GEO_D_DebugMalloc */
}
 
 
 
int GEO_fn_vMemorySort(const void *elem1, const void *elem2)
{
 #if defined(_DEBUG)&&defined(GEO_D_DebugStatistic)
 return(((struct GEO_tdstAllocInformation_*)elem2)->GEO_ulSourceSize-((struct GEO_tdstAllocInformation_*)elem1)->GEO_ulSourceSize);
 #else 
 return(0);
 #endif /*_DEBUG&&GEO_D_DebugStatistic*/
}
 
 
void
GEO_fn_vPrintMemoryInformation(void)
{
 #if defined(_DEBUG)&&defined(GEO_D_DebugStatistic)
 FILE *p_stFile;
 unsigned long i;
 
 qsort(GEO_g_a_stAllocInformation,GEO_g_ulMaxSources, sizeof(struct GEO_tdstAllocInformation_),GEO_fn_vMemorySort);
 
 if ((p_stFile = fopen("GeoMemInfo.log","wt"))!=NULL)
 {
  for (i=0;i<GEO_g_ulMaxSources;i++)
  {
   fprintf(p_stFile,"%s (line %d, bloc %04x) :\n",GEO_g_a_stAllocInformation[i].GEO_szSourceName,GEO_g_a_stAllocInformation[i].GEO_ulSourceLine,GEO_g_a_stAllocInformation[i].GEO_usSourceBloc);
   fprintf(p_stFile,"   Number of allocations    : %d\n",GEO_g_a_stAllocInformation[i].GEO_ulSourceNumberOfAlloc);
   fprintf(p_stFile,"   Number of reallocations  : %d\n",GEO_g_a_stAllocInformation[i].GEO_ulSourceNumberOfRealloc);
   fprintf(p_stFile,"   Number of free           : %d\n",GEO_g_a_stAllocInformation[i].GEO_ulSourceNumberOfFree);
   fprintf(p_stFile,"   Total size of allocation : %d\n",GEO_g_a_stAllocInformation[i].GEO_ulSourceSize);
  }
   fprintf(p_stFile,"\nTotal number of allocation : %d\n",GEO_g_ulNumberOfAllocation);
 }
 fclose(p_stFile);
 #endif /*_DEBUG&&GEO_D_DebugStatistic*/
}
 
 
unsigned char 
GEO_fn_ucGetBlocNumberOf(void *p_vPointer)
{
 unsigned char ucReturn;
 unsigned short uwBlocId;
 void *p_vBeginBloc;
 
 Mmg_fn_vWhereIs((void*)p_vPointer,&uwBlocId,&p_vBeginBloc);
 
 ucReturn = (unsigned char)(uwBlocId&0x00ff);
 return(ucReturn);
}
#endif /* _FIRE_DEADCODE_U64_ */
