reman3/Rayman_X/cpa/tempgrp/SCR/SCR_DyAr.c

128 lines
3.8 KiB
C

/*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
* SCR_DyAr.c
* Dynamic array
*
* Scripts, Beaudet Christophe
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/
/*
*=================================================================================================
* Includes.
*=================================================================================================
*/
#include <memory.h>
#include "SCR.h"
/*
*=================================================================================================
* Functions.
*=================================================================================================
*/
/*
*-------------------------------------------------------------------------------------------------
* To initialize a dynamic array.
* _p_stArray : Array to initialize.
*-------------------------------------------------------------------------------------------------
*/
void fn_v_DyAr_InitArray(SCR_tdst_DyAr_Description *_p_stArray)
{
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
SCR_M_Dbg_Assert_P(_p_stArray != NULL);
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
_p_stArray->d_stDynArray = NULL;
_p_stArray->uiNumValues = 0;
_p_stArray->uiMaxValues = 0;
}
/*
*-------------------------------------------------------------------------------------------------
* To add a new element to a dynamic array.
* _p_stArray : Array concerned.
* _p_vElement : Element to add
* _cInitMem : Initialise the eventual allocated memory ?
* Returns index of allocated place, uiMaxValues in case of error.
*-------------------------------------------------------------------------------------------------
*/
unsigned int fn_ui_DyAr_AllocateElement(SCR_tdst_DyAr_Description *_p_stArray, char _cInitMem)
{
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
unsigned int uiIndex;
unsigned int uiLastMax;
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
fn_v_Mem_SetMode(0);
/*
* We searched a free place in array. If uiIndex is equal to uiMaxValues, that means
* that array is full and needs to be reallocate.
*/
uiIndex = _p_stArray->uiNumValues;
/* Need an allocation ? */
if(uiIndex == _p_stArray->uiMaxValues)
{
uiLastMax = _p_stArray->uiMaxValues;
_p_stArray->uiMaxValues += SCR_C_ui_DyAr_BaseForAllocation; /* Can be 0 */
/*
* Is there's an already allocated array ? If it's the case, we realloc it.
*/
if(_p_stArray->d_stDynArray)
{
SCR_M_Mem_Realloc
(
SCR_tdst_DyAr_Element,
_p_stArray->d_stDynArray,
_p_stArray->d_stDynArray,
_p_stArray->uiMaxValues,
SCR_C_ui_DyAr_BaseForAllocation
);
if(_cInitMem)
{
memset
(
&_p_stArray->d_stDynArray[uiLastMax],
0,
(_p_stArray->uiMaxValues - uiLastMax) * sizeof(SCR_tdst_DyAr_Element)
);
}
}
/*
* Else we create a new one
*/
else
{
if(_cInitMem)
{
SCR_M_Mem_AllocAndSet
(
SCR_tdst_DyAr_Element,
_p_stArray->d_stDynArray,
SCR_C_ui_DyAr_BaseForAllocation
);
}
else
{
SCR_M_Mem_Alloc
(
SCR_tdst_DyAr_Element,
_p_stArray->d_stDynArray,
SCR_C_ui_DyAr_BaseForAllocation
);
}
}
}
/*
* One more value in array.
*/
_p_stArray->uiNumValues++;
/* Returns index */
return uiIndex;
}