237 lines
7.7 KiB
C
237 lines
7.7 KiB
C
/*
|
|
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
* SCR_Mem.c
|
|
* Managment of memory.
|
|
*
|
|
* Scripts, Beaudet Christophe
|
|
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
*/
|
|
|
|
/*
|
|
*=================================================================================================
|
|
* Includes.
|
|
*=================================================================================================
|
|
*/
|
|
|
|
#define __DeclareGlobalVariableMmgScr_h__
|
|
#define __DeclareGlobalVariableErrScr_h__
|
|
#include "SCR.h"
|
|
#undef __DeclareGlobalVariableMmgScr_h__
|
|
#undef __DeclareGlobalVariableErrScr_h__
|
|
|
|
/*
|
|
*=================================================================================================
|
|
* Global variables.
|
|
*=================================================================================================
|
|
*/
|
|
|
|
unsigned char g_uc_Mem_CurrentMemLevel;
|
|
unsigned char g_uc_Mem_CurrentLinkLevel;
|
|
unsigned char g_uc_Mem_CurrentAllocationType;
|
|
unsigned long g_a_ul_Mem_BlocAllocationSize[E_ucScrMaxBlocksNb];
|
|
unsigned long g_a_ul_Mem_MemoryAllocated[256];
|
|
unsigned char g_uc_Mem_LastMemLevel;
|
|
|
|
/*
|
|
*=================================================================================================
|
|
* Functions.
|
|
*=================================================================================================
|
|
*/
|
|
|
|
/*
|
|
*-------------------------------------------------------------------------------------------------
|
|
* Init of memory managment.
|
|
*-------------------------------------------------------------------------------------------------
|
|
*/
|
|
void fn_v_Mem_InitModule(void)
|
|
{
|
|
static char s_cFirstInit = 1;
|
|
|
|
/* Must init only one time error module (in case of multiple script init/close) */
|
|
if(s_cFirstInit == 1)
|
|
{
|
|
Erm_M_InitErrMsg(Scr);
|
|
s_cFirstInit = 0;
|
|
}
|
|
Mmg_M_InitMmg(Scr);
|
|
|
|
memset(&g_a_ul_Mem_BlocAllocationSize, 0, E_ucScrMaxBlocksNb * sizeof(unsigned long));
|
|
memset(&g_a_ul_Mem_MemoryAllocated, 0, E_ucScrMaxBlocksNb * sizeof(unsigned long));
|
|
g_uc_Mem_CurrentMemLevel = 0;
|
|
g_uc_Mem_CurrentLinkLevel = 0;
|
|
g_uc_Mem_LastMemLevel = 0;
|
|
g_uc_Mem_CurrentAllocationType = E_ucDynamic;
|
|
}
|
|
|
|
/*
|
|
*-------------------------------------------------------------------------------------------------
|
|
* Close.
|
|
*-------------------------------------------------------------------------------------------------
|
|
*/
|
|
void fn_v_Mem_CloseModule(void)
|
|
{
|
|
SCR_fn_v_Mem_DeleteWithMemLevel(0, 255);
|
|
memset(&g_a_ul_Mem_BlocAllocationSize, 0, E_ucScrMaxBlocksNb * sizeof(unsigned long));
|
|
memset(&g_a_ul_Mem_MemoryAllocated, 0, E_ucScrMaxBlocksNb * sizeof(unsigned long));
|
|
g_uc_Mem_CurrentMemLevel = 0;
|
|
g_uc_Mem_CurrentLinkLevel = 0;
|
|
g_uc_Mem_LastMemLevel = 0;
|
|
g_uc_Mem_CurrentAllocationType = E_ucDynamic;
|
|
Mmg_fn_v_StopMmg(Erm_M_ucGiveModuleId(Scr));
|
|
}
|
|
|
|
/*
|
|
*-------------------------------------------------------------------------------------------------
|
|
* Init of memory managment.
|
|
* Use of MMG and ERM modules.
|
|
* _p_ulMemorySize : Address of unsigned long size for blocs. Size if 0 for a dynamic malloc.
|
|
*-------------------------------------------------------------------------------------------------
|
|
*/
|
|
void SCR_fn_v_Mem_InitWithMemLevel(unsigned long *_p_ulMemorySize)
|
|
{
|
|
unsigned char ucIndex;
|
|
|
|
for(ucIndex = 0; ucIndex < E_ucScrMaxBlocksNb; ucIndex++)
|
|
{
|
|
if(g_a_ul_Mem_BlocAllocationSize[ucIndex] == 0)
|
|
{
|
|
g_a_ul_Mem_BlocAllocationSize[ucIndex] = *_p_ulMemorySize;
|
|
if(*_p_ulMemorySize)
|
|
{
|
|
Mmg_M_InitBlockV5_1_0(Scr, ucIndex, *_p_ulMemorySize,200000);
|
|
}
|
|
}
|
|
_p_ulMemorySize++;
|
|
}
|
|
}
|
|
|
|
/*
|
|
*-------------------------------------------------------------------------------------------------
|
|
* To reduce memory size.
|
|
*-------------------------------------------------------------------------------------------------
|
|
*/
|
|
void SCR_fn_v_Mem_ReduceMemory(void)
|
|
SCR_BEGIN
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
fn_v_Anl_ReduceMemory();
|
|
fn_v_File_ReduceMemory();
|
|
fn_v_Link_ReduceMemory();
|
|
fn_v_Ntfy_ReduceMemory();
|
|
fn_v_Sect_ReduceMemory();
|
|
fn_v_Vars_ReduceMemory();
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
SCR_CEND(;)
|
|
|
|
/*
|
|
*-------------------------------------------------------------------------------------------------
|
|
* To delete what correspond to a given priority.
|
|
*-------------------------------------------------------------------------------------------------
|
|
*/
|
|
void SCR_fn_v_Mem_DeleteWithMemLevel(unsigned char _ucMin, unsigned char _ucMax)
|
|
SCR_BEGIN
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
short wMemLevel;
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
/*
|
|
* Parse all levels.
|
|
* We finish with level 0 cause it's the level for basic scripts datas.
|
|
*/
|
|
for(wMemLevel = _ucMax; wMemLevel >= _ucMin; wMemLevel--)
|
|
{
|
|
fn_v_Anl_DeleteWithMemLevel(_ucMin, _ucMax);
|
|
fn_v_File_DeleteWithMemLevel(_ucMin, _ucMax);
|
|
fn_v_Link_DeleteWithMemLevel(_ucMin, _ucMax);
|
|
fn_v_Ntfy_DeleteWithMemLevel(_ucMin, _ucMax);
|
|
fn_v_Sect_DeleteWithMemLevel(_ucMin, _ucMax);
|
|
fn_v_Vars_DeleteWithMemLevel(_ucMin, _ucMax);
|
|
|
|
/* Delete static bloc */
|
|
if((unsigned char) wMemLevel < E_ucScrMaxBlocksNb)
|
|
{
|
|
if(g_a_ul_Mem_BlocAllocationSize[(unsigned char) wMemLevel])
|
|
{
|
|
Mmg_M_DeleteBlock(Scr, (unsigned char) wMemLevel);
|
|
g_a_ul_Mem_BlocAllocationSize[(unsigned char) wMemLevel] = 0;
|
|
}
|
|
}
|
|
}
|
|
SCR_fn_v_Mem_ReduceMemory();
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
SCR_CEND(;)
|
|
|
|
/*
|
|
*-------------------------------------------------------------------------------------------------
|
|
* To get and set current memory level.
|
|
*-------------------------------------------------------------------------------------------------
|
|
*/
|
|
unsigned char SCR_fn_uc_Mem_GetCurrentLevel(void)
|
|
{
|
|
return g_uc_Mem_CurrentMemLevel;
|
|
}
|
|
|
|
void SCR_fn_v_Mem_SetCurrentLevel(unsigned char _ucLevel)
|
|
{
|
|
g_uc_Mem_CurrentMemLevel = _ucLevel;
|
|
}
|
|
|
|
unsigned char SCR_fn_uc_Mem_GetCurrentLinkLevel(void)
|
|
{
|
|
return g_uc_Mem_CurrentLinkLevel;
|
|
}
|
|
|
|
void SCR_fn_v_Mem_SetCurrentLinkLevel(unsigned char _ucLevel)
|
|
{
|
|
g_uc_Mem_CurrentLinkLevel = _ucLevel;
|
|
}
|
|
|
|
/*
|
|
*-------------------------------------------------------------------------------------------------
|
|
*-------------------------------------------------------------------------------------------------
|
|
*/
|
|
void fn_v_Mem_SetMode(unsigned char _ucPrio)
|
|
{
|
|
g_uc_Mem_LastMemLevel = _ucPrio;
|
|
if
|
|
(
|
|
(_ucPrio >= E_ucScrMaxBlocksNb)
|
|
|| (g_a_ul_Mem_BlocAllocationSize[_ucPrio] == 0)
|
|
)
|
|
{
|
|
g_uc_Mem_CurrentAllocationType = E_ucDynamic;
|
|
}
|
|
else
|
|
{
|
|
g_uc_Mem_CurrentAllocationType = _ucPrio;
|
|
}
|
|
}
|
|
|
|
/*
|
|
*-------------------------------------------------------------------------------------------------
|
|
* Get the maximum allocation for a given memory level.
|
|
* _ucLevelMin : Min memory level to retreive
|
|
* _ucLevelMax : Max memory level to retreive
|
|
*-------------------------------------------------------------------------------------------------
|
|
*/
|
|
unsigned long SCR_fn_ul_Mem_GetAllocationSize(unsigned char _ucLevelMin, unsigned long _ucLevelMax)
|
|
{
|
|
int iIndex;
|
|
unsigned long ulTot = 0;
|
|
|
|
for(iIndex = (int) _ucLevelMin; iIndex <= (int) _ucLevelMax; iIndex++)
|
|
{
|
|
ulTot += g_a_ul_Mem_MemoryAllocated[(unsigned char) iIndex];
|
|
}
|
|
return ulTot;
|
|
}
|
|
|
|
/*
|
|
*-------------------------------------------------------------------------------------------------
|
|
*-------------------------------------------------------------------------------------------------
|
|
*/
|
|
#ifndef RETAIL
|
|
void Scr_PrintUsedStaticMemory(void)
|
|
{
|
|
Mmg_M_PrintUsedStaticMemoryInModule(Scr);
|
|
}
|
|
#endif /* RETAIL */
|