reman3/Rayman_X/cpa/tempgrp/NET/MemCpa.c

245 lines
6.7 KiB
C

/********************************************************************************/
/* */
/* MemCpa.c */
/* */
/* This file defines the dynamic memory allocation interface */
/* */
/********************************************************************************/
#include "warnings.h"
#ifdef NET_MEMORY_DEBUG
#ifdef VISUAL
#include <malloc.h>
#endif /* VISUAL */
#ifdef WATCOM
#include <stdlib.h>
#endif /* WATCOM */
#endif /* NET_MEMORY_DEBUG */
#include <windows.h>
#include <stdarg.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include "NetMemCo.h"
#include <NET\NetDebug.h>
#include <MMG.h>
#define __DeclareGlobalVariableErrNet_h__
#include "ErrNet.h"
#undef __DeclareGlobalVariableErrNet_h__
#define __DeclareGlobalVariableMmgNet_h__
#include "MmgNet.h"
#undef __DeclareGlobalVariableMmgNet_h__
/* function required by P. Crepy for mmg debugging*/
/*
void Net_PrintUsedStaticMemory ()
{
Mmg_M_PrintUsedStaticMemoryInModule(Net);
}
*/
#ifdef NET_USE_DEBUG
static long gs_lNbrUnFree=0;
#endif
#ifdef NET_MEMORY_DEBUG
#ifdef VISUAL
/*****************************************************************************
* Description: pMallocDebug
* Allocate a memory bloc.
*****************************************************************************/
void *pMallocDebug(tduxBlockSize ulSize,int blockType,const char *filename,int linenumber)
{
if (ulSize)
{
#ifdef NET_USE_DEBUG
gs_lNbrUnFree++;
#endif
return _malloc_dbg(ulSize,blockType,filename,linenumber);
}
else return NULL;
}
/*****************************************************************************
* Description: vFreeDebug
* Free a memory bloc.
*****************************************************************************/
void vFreeDebug(void *p_vMemBlock, int blockType)
{
if (p_vMemBlock)
{
#ifdef NET_USE_DEBUG
gs_lNbrUnFree--;
#endif
_free_dbg(p_vMemBlock,blockType);
}
}
/*****************************************************************************
* Description: pReallocDebug
* Reallocate of a memory bloc with a new size.
*****************************************************************************/
void *pReallocDebug(void *p_vMemBlock,tduxBlockSize ulSize,int blockType,const char *filename, int linenumber)
{
if (p_vMemBlock)
{
if (ulSize)
return _realloc_dbg(p_vMemBlock,ulSize,blockType,filename,linenumber);
else
{
_free_dbg(p_vMemBlock,blockType);
return NULL;
}
}
else
{
if (ulSize)
return _malloc_dbg(ulSize,blockType,filename,linenumber);
else return NULL;
}
}
#endif /* VISUAL */
#else /* NET_MEMORY_DEBUG */
/*****************************************************************************
* Description: pMalloc
* Allocate a memory bloc.
*****************************************************************************/
void *pMalloc(tduxBlockSize ulSize)
{
void * p_vPtr;
#ifdef NET_USE_DEBUG
vDebugFormat(Net_C_Debug_Memory, "pMalloc(%lu)", ulSize);
#endif /* NET_USE_DEBUG */
if (ulSize)
{
#ifdef NET_USE_DEBUG
gs_lNbrUnFree++;
#endif
Mmg_M_SetModeAlloc4Ch(Net,E_ucNetBlock1,C_ucMmgDefaultChannel);
p_vPtr = (void *) Mmg_fn_p_vAlloc4Ch(ulSize,C_ucMmgDefaultChannel);
if(Erm_M_uwCheckError(Mmg,0)) {
Erm_M_ClearLastError(0);
}
#ifdef NET_USE_DEBUG
vDebugFormat(Net_C_Debug_Memory, "-->%lx", p_vPtr);
#endif /* NET_USE_DEBUG */
return p_vPtr;
}
else return NULL;
}
/*****************************************************************************
* Description: vFree
* Free a memory bloc.
*****************************************************************************/
void vFree(void *p_vMemBlock)
{
#ifdef NET_USE_DEBUG
vDebugFormat(Net_C_Debug_Memory, "vFree(%lx)", p_vMemBlock);
#endif /* NET_USE_DEBUG */
if (p_vMemBlock)
{
#ifdef NET_USE_DEBUG
gs_lNbrUnFree--;
#endif
Mmg_M_SetModeAlloc4Ch(Net,E_ucNetBlock1,C_ucMmgDefaultChannel);
Mmg_fn_vFree4Ch(p_vMemBlock,C_ucMmgDefaultChannel);
if(Erm_M_uwCheckError(Mmg,0)) {
Erm_M_ClearLastError(0);
}
}
}
/*****************************************************************************
* Description: pRealloc
* Reallocate of a memory bloc with a new size.
*****************************************************************************/
void *pRealloc(void *p_vMemBlock,tduxBlockSize ulSize)
{
void *p_vPtr;
#ifdef NET_USE_DEBUG
vDebugFormat(Net_C_Debug_Memory, "pRealloc(%lu)", ulSize);
#endif /* NET_USE_DEBUG */
Mmg_M_SetModeAlloc4Ch(Net,E_ucNetBlock1,C_ucMmgDefaultChannel);
if (p_vMemBlock)
{
if (ulSize) {
p_vPtr=Mmg_fn_p_vRealloc4Ch(p_vMemBlock,ulSize,C_ucMmgDefaultChannel);
if(Erm_M_uwCheckError(Mmg,0)) {
Erm_M_ClearLastError(0);
}
#ifdef NET_USE_DEBUG
vDebugFormat(Net_C_Debug_Memory, "-->%lx", p_vPtr);
#endif /* NET_USE_DEBUG */
return p_vPtr;
}
else
{
vFree(p_vMemBlock);
return NULL;
}
}
else
{
if (ulSize) return pMalloc(ulSize);
else return NULL;
}
}
#endif
/*****************************************************************************
* Description: uxInitMalloc
* Initialize memory unit.
*****************************************************************************/
tduxBlockSize uxInitMalloc(tduxBlockSize uxPoolSize, void *pAddress)
{
#ifdef NET_USE_DEBUG
gs_lNbrUnFree=0;
#endif
if (g_ucErmModuleId==C_ucModuleNotInitialized) Erm_M_InitErrMsg(Erm);
if (g_ucMmgModuleId==C_ucModuleNotInitialized) Erm_M_InitErrMsg(Mmg);
if (g_ucNetModuleId==C_ucModuleNotInitialized) Erm_M_InitErrMsg(Net);
Mmg_M_InitMmg(Net);
Mmg_M_InitBlock(Net,E_ucNetBlock1,uxPoolSize);
return uxPoolSize;
}
/*****************************************************************************
* Description: vDoneMalloc
* Quit memory unit.
*****************************************************************************/
void vDoneMalloc(void)
{
Mmg_M_DeleteBlock(Net,E_ucNetBlock1);
#ifdef NET_USE_DEBUG
/*if (gs_lNbrUnFree)*/ vDebugSI(Net_C_Debug_Memory,"Number malloc - Number free",gs_lNbrUnFree);
#endif
}