/*****************************************
//										//
//		Management of the Module : GEO	//
//										//
// File Name		: MmgGEO.h			//
// Date		: 03/02/97					//
// Author		: Philippe Vimont		//
//										//
*****************************************/
 
#ifndef __GEO_MMGGEO_H__
#define __GEO_MMGGEO_H__
 
/*#include <malloc.h>*/
 
 
#ifdef __cplusplus
extern "C" {
#endif 
 
#include "ERM.h"
#include "MMG.h"
#include "errgeo.h"
#include "TMP.h"
#ifdef U64
#include "GLD/Specif/DisplMsg.h"
#include "ZeMem.h"
#endif
 
/* For DLLs who are using this module : */ 
#undef CPA_EXPORT
#if defined(CPA_WANTS_IMPORT)
 #define CPA_EXPORT __declspec(dllimport)
#elif defined(CPA_WANTS_EXPORT)
 #define CPA_EXPORT __declspec(dllexport)
#else
 #define CPA_EXPORT
#endif
 
typedef enum e_ucGEOStaticBlocks_{
	E_ucGEOBlock1, 
	E_ucGEOBlock2,
	E_ucGEOBlock3,
	E_ucGEOBlock4,	   
	//XB 16/06/99
	E_ucGEOBlock5,
	//End XB 16/06/99
	E_ucGEOMaxBlocksNb  /* maximum number of static block, You have to follow this syntax 'E_uc+ Abbreviation Module +MaxBlocksNb' */
} e_ucGEOStaticBlocks;
 
 
#undef __MMGGEO_EXTERN
#ifndef __DeclareGlobalVariableMmgGEO_h__
#define __MMGGEO_EXTERN extern /*external declaration*/
#else /*__DeclareGlobalVariableErrGEO_h__*/
#define __MMGGEO_EXTERN /*replace by nothing : we have to declare*/
#endif /*__DeclareGlobalVariableErrGEO_h__*/
 
__MMGGEO_EXTERN CPA_EXPORT  unsigned char g_ucGEOMMemMallocMode /*number of identification of the Erm module*/
#if defined(__DeclareGlobalVariableMmgGEO_h__) && !defined(CPA_WANTS_IMPORT)
= E_ucDynamic
#endif /*__DeclareGlobalVariableErrGEO_h__&& CPA_WANTS_IMPORT*/
;
 
__MMGGEO_EXTERN CPA_EXPORT unsigned char GEO_g_bDynamicAllocation;
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
__MMGGEO_EXTERN CPA_EXPORT struct tdstBlockInfo_ GEO_g_stMyBlockInfo;
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
__MMGGEO_EXTERN CPA_EXPORT struct tdstBlockInfo_ g_a_stGEOBlocksInfo[E_ucGEOMaxBlocksNb];
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
#ifdef __DYNAMIC_MALLOC_ALLOWED__
#ifdef __DEBUG_MALLOC_MODE__
__MMGGEO_EXTERN CPA_EXPORT  struct tdstDynInfo_ g_stGEODynInfo;
#endif /*__DEBUG_MALLOC_MODE__*/
#endif /*__DYNAMIC_MALLOC_ALLOWED__*/
 
#define GEO_C_ActionMalloc  0
#define GEO_C_ActionRealloc 1
#define GEO_C_ActionFree    2
 
#define GEO_C_CanNotUseDynamic 0
#define GEO_C_CanUseDynamic    1
#define GEO_C_UseDynamic       2
 
#define M_GEOInitMem() Mmg_M_SetModeAlloc4Ch( GEO , g_ucGEOMMemMallocMode ,C_ucMmgDefaultChannel)
 

/* XB
#define GEO_M_CPAMallocAlign8( Var, cast, ulSize, error) \
{\
  if ( (ulSize)!=0 )\
  {\
    Mmg_M_SetModeAlloc4Ch( GEO , g_ucGEOMMemMallocMode+2,C_ucMmgDefaultChannel); \
    (Var) = (cast)Mmg_fn_p_vAlloc4Ch(ulSize,C_ucMmgDefaultChannel);\
    memset(Var,0,ulSize);\
  }\
}
*/
 
#if !defined(U64)
 
#define GEO_M_CPAMalloc( Var, cast, ulSize, error) \
{\
  if ( (ulSize)!=0 )\
  {\
    if (GEO_g_bDynamicAllocation!=GEO_C_UseDynamic)\
    {\
     M_GEOInitMem();\
     (Var) = (cast)Mmg_fn_p_vAlloc4Ch(ulSize,C_ucMmgDefaultChannel);\
    }\
    if (GEO_g_bDynamicAllocation==GEO_C_UseDynamic||(GEO_g_bDynamicAllocation==GEO_C_CanUseDynamic&&(Var)==NULL))\
    {\
     Erm_M_ClearLastError ( C_ucErmDefaultChannel );\
     if (GEO_g_bDynamicAllocation==GEO_C_CanUseDynamic)\
     {\
      Erm_M_UpdateLastError( GEO, C_ucErmDefaultChannel,E_uwGEODebugDynamicAllocation, C_lErmNoDebugData, C_ucErmOpenInfoWindow, C_ucAllowStopForDebug, NULL);\
      Erm_M_ClearLastError ( C_ucErmDefaultChannel );\
      GEO_g_bDynamicAllocation=GEO_C_UseDynamic;\
     }\
     Mmg_M_SetModeAlloc4Ch( GEO , E_ucDynamic , C_ucMmgDefaultChannel);\
     (Var) = (cast)Mmg_fn_p_vAlloc4Ch(ulSize,C_ucMmgDefaultChannel);\
    }\
    if ((Var) == NULL)\
    {\
      Erm_M_ClearLastError ( C_ucErmDefaultChannel );\
      Erm_M_UpdateLastError( GEO, C_ucErmDefaultChannel, (error), C_lErmNoDebugData, C_ucErmOpenInfoWindow, C_ucAllowStopForDebug, NULL);\
    }\
    memset((Var),0,ulSize);\
    GEO_fn_vMemoryLogFile((Var),GEO_C_ActionMalloc,__FILE__,__LINE__,ulSize); \
  }\
}
 
#define GEO_M_CPARealloc( Var, OldVar, cast, ulSize, error) \
{\
  if ( (ulSize)!=0 )\
  {\
    M_GEOInitMem();\
    (Var) = (cast)Mmg_fn_p_vRealloc4Ch(OldVar,ulSize,C_ucMmgDefaultChannel);\
    if (GEO_g_bDynamicAllocation==GEO_C_UseDynamic||(GEO_g_bDynamicAllocation==GEO_C_CanUseDynamic&&(Var)==NULL))\
    {\
     Erm_M_ClearLastError ( C_ucErmDefaultChannel );\
     if (GEO_g_bDynamicAllocation==GEO_C_CanUseDynamic)\
     {\
      Erm_M_UpdateLastError( GEO, C_ucErmDefaultChannel,E_uwGEODebugDynamicAllocation, C_lErmNoDebugData, C_ucErmOpenInfoWindow, C_ucAllowStopForDebug, NULL);\
      Erm_M_ClearLastError ( C_ucErmDefaultChannel );\
      GEO_g_bDynamicAllocation=GEO_C_UseDynamic;\
     }\
     GEO_M_CPAMalloc( (Var), cast, (ulSize), (error));\
     memcpy((Var),(OldVar),(ulSize));\
     GEO_M_CPAFree((OldVar));\
    }\
    if ((Var)==NULL)\
    {\
	    Erm_M_ClearLastError ( C_ucErmDefaultChannel );\
	    Erm_M_UpdateLastError( GEO, C_ucErmDefaultChannel, (error), C_lErmNoDebugData, C_ucErmOpenInfoWindow, C_ucAllowStopForDebug, NULL );\
    }\
    GEO_fn_vMemoryLogFile((Var),GEO_C_ActionRealloc,__FILE__,__LINE__,ulSize); \
  }\
}
 
#define GEO_M_CPAFree( Var ) \
{\
  M_GEOInitMem();\
  GEO_fn_vMemoryLogFile((Var),GEO_C_ActionFree,__FILE__,__LINE__,0); \
  if (GEO_g_bDynamicAllocation!=GEO_C_CanNotUseDynamic)\
  {\
   Mmg_fn_vGiveInformationBlock(Erm_M_ucGiveModuleId(GEO),(unsigned char)g_ucGEOMMemMallocMode,&GEO_g_stMyBlockInfo);\
   if ((void*)(Var)<(void*)GEO_g_stMyBlockInfo.p_cBeginBlock||(void*)(Var)>(void*)GEO_g_stMyBlockInfo.p_cEndBlock)\
    Mmg_M_SetModeAlloc4Ch(GEO,E_ucDynamic,C_ucMmgDefaultChannel);\
  }\
  Mmg_fn_vFree4Ch((void*)(Var),C_ucMmgDefaultChannel);\
  if ( Erm_M_uwCheckError(Mmg, C_ucErmDefaultChannel) )\
  {\
		 Erm_M_ClearLastError(C_ucErmDefaultChannel);\
		 Erm_M_UpdateLastError(GEO, C_ucErmDefaultChannel, E_uwGEOCanNotFree , C_lErmNoDebugData, C_ucErmOpenInfoWindow, C_ucAllowStopForDebug, NULL);\
  }\
  (Var) = NULL;\
}
 
#else /* U64 */
 
/* XB 22/06/99 */
	void *GEO_fn_pvMalloc(unsigned long _ulSize);
	#define GEO_M_CPAMalloc( Var, cast, ulSize, error) Var=(cast)GEO_fn_pvMalloc(ulSize);
 
	void *GEO_fn_pvMallocAlign8(unsigned long _ulSize);
	#define GEO_M_CPAMallocAlign8( Var, cast, ulSize, error) Var=(cast)GEO_fn_pvMallocAlign8(ulSize);
 
	void *GEO_fn_pvMallocLDD(unsigned long _ulSize);
	#define GEO_M_CPAMallocLDD( Var, cast, ulSize, error) Var=(cast)GEO_fn_pvMallocLDD(ulSize);
	/* End XB 22/06/99 */
 
#endif /* U64 */
 
 
#define GEO_M_ucGetMemMallocMode() (g_ucGEOMMemMallocMode)
 
/*#define GEO_M_CPAMalloc( Var, cast, ulSize, error) \
  Var = (cast)malloc(ulSize);
 
#define GEO_M_CPARealloc( Var, OldVar, cast, ulSize, error) \
   Var = (cast)realloc(ulSize);\
   memset(Var,0,ulSize);
 
#define GEO_M_CPAFree( Var ) \
  free ((void*)var);\*/
 
/* ANNECY - MT 11/05/98 {*/
#define GEO_M_TMPMalloc(Var,cast,Size)		(Var) = (cast) TMP_M_p_Malloc( Size )
#define GEO_M_TMPFree(Var)					TMP_M_Free( Var )
/* END ANNECY MT }*/
 
 
#ifdef __cplusplus
};
#endif /* __cplusplus */
 
#endif /* __MMGGEO_H__*/
