Some more heap abolishing:

* Replace a lot of heap usage with malloc/free usage

git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@1816 4a71c877-e1ca-e34f-864e-861f7616d084
master
Giel van Schijndel 2007-06-04 22:02:05 +00:00
parent d65f88beb5
commit 7ae1b1bee7
5 changed files with 26 additions and 74 deletions

View File

@ -101,20 +101,6 @@ hashTable_Create( HASHTABLE **ppsTable, UDWORD udwTableSize,
(*ppsTable)->ppsNode = (HASHNODE**)malloc( udwSize );
memset( (*ppsTable)->ppsNode, 0, udwSize );
/* allocate heaps */
if ( !HEAP_CREATE( &(*ppsTable)->psNodeHeap, sizeof(HASHNODE),
udwInitElements, udwExtElements) )
{
return FALSE;
}
if ( !HEAP_CREATE( &(*ppsTable)->psElementHeap, udwElementSize,
udwInitElements, udwExtElements) )
{
// my_error("",0,"","htc FAIL!\n");
return FALSE;
}
/* init members */
(*ppsTable)->udwTableSize = udwTableSize;
(*ppsTable)->udwElements = udwInitElements;
@ -144,10 +130,6 @@ hashTable_Destroy( HASHTABLE *psTable )
hashTable_Clear( psTable );
/* destroy heaps */
HEAP_DESTROY( psTable->psNodeHeap );
HEAP_DESTROY( psTable->psElementHeap );
/* free table */
free( psTable->ppsNode );
free( psTable );
@ -188,13 +170,13 @@ hashTable_Clear( HASHTABLE *psTable )
}
/* free element */
HEAP_FREE( psTable->psElementHeap, psNode->psElement );
free(psNode->psElement);
/* return node to heap */
ASSERT( psNode != NULL,
"hashTable_Destroy: node pointer invalid\n" );
psNodeTmp = psNode->psNext;
HEAP_FREE( psTable->psNodeHeap, psNode );
free(psNode);
psNode = psNodeTmp;
}
@ -238,15 +220,16 @@ void *
hashTable_GetElement( HASHTABLE *psTable )
{
void *psElement;
BOOL result;
ASSERT( psTable != NULL,
"hashTable_GetElement: table pointer invalid\n" );
result=HEAP_ALLOC( psTable->psElementHeap, &psElement );
// if the alloc fails then return NULL
if (result==FALSE) return NULL;
psElement = malloc(psTable->udwElementSize);
if (psElement == NULL) // if the alloc fails then return NULL
{
debug(LOG_ERROR, "hashTable_GetElement: Out of memory");
return NULL;
}
return psElement;
@ -282,7 +265,7 @@ hashTable_InsertElement( HASHTABLE *psTable, void *psElement,
udwHashIndex = hashTable_GetHashKey( psTable, iKey1, iKey2 );
/* get node from heap */
HEAP_ALLOC( psTable->psNodeHeap, (void**) &psNode );
psNode = malloc(sizeof(HASHNODE));
/* set node elements */
psNode->iKey1 = iKey1;
@ -427,12 +410,12 @@ hashTable_RemoveElement( HASHTABLE *psTable, void *psElement,
/* return element to heap */
ASSERT( psNode->psElement != NULL,
"hashTable_RemoveElement: element pointer invalid\n" );
HEAP_FREE( psTable->psElementHeap, psNode->psElement );
free(psNode->psElement);
/* return node to heap */
ASSERT( psNode != NULL,
"hashTable_RemoveElement: node pointer invalid\n" );
HEAP_FREE( psTable->psNodeHeap, psNode );
free(psNode);
return TRUE;
}

View File

@ -78,8 +78,6 @@ HASHNODE;
typedef struct HASHTABLE
{
OBJ_HEAP *psNodeHeap;
OBJ_HEAP *psElementHeap;
HASHNODE **ppsNode;
HASHNODE *psNextNode;
HASHFUNC pHashFunc;

View File

@ -46,7 +46,6 @@ ARROW;
/***************************************************************************/
OBJ_HEAP *g_psArrowHeap;
ARROW *g_psArrowList = NULL;
/***************************************************************************/
@ -54,12 +53,6 @@ ARROW *g_psArrowList = NULL;
BOOL
arrowInit( void )
{
if (!HEAP_CREATE(&g_psArrowHeap, sizeof(ARROW),
ARROW_HEAP_INIT, ARROW_HEAP_EXT))
{
return FALSE;
}
return TRUE;
}
@ -68,7 +61,6 @@ arrowInit( void )
void
arrowShutDown( void )
{
HEAP_DESTROY(g_psArrowHeap);
}
/***************************************************************************/
@ -77,10 +69,10 @@ BOOL
arrowAdd( SDWORD iBaseX, SDWORD iBaseY, SDWORD iBaseZ,
SDWORD iHeadX, SDWORD iHeadY, SDWORD iHeadZ, UBYTE iColour )
{
ARROW *psArrow;
if ( !HEAP_ALLOC( g_psArrowHeap, (void**) &psArrow) )
ARROW *psArrow = malloc(sizeof(ARROW));
if (psArrow == NULL)
{
debug(LOG_ERROR, "arrowAdd: Out of memory");
return FALSE;
}
@ -118,7 +110,7 @@ arrowDrawAll( void )
{
draw3dLine( &psArrow->vecHead, &psArrow->vecBase, psArrow->iColour );
psArrowTemp = psArrow->psNext;
HEAP_FREE( g_psArrowHeap, psArrow );
free(psArrow);
psArrow = psArrowTemp;
}

View File

@ -82,10 +82,6 @@ FP_NODE **apsClosed;
FP_NODE **apsOpen;
#endif
// object heap to store nodes
OBJ_HEAP *psFPNodeHeap;
/*#define NUM_DIR 4
// Convert a direction into an offset
// dir 0 => x = 0, y = -1
@ -130,12 +126,6 @@ static void ClearAstarNodes(void)
// Initialise the findpath routine
BOOL astarInitialise(void)
{
// Create the node heap
if (!HEAP_CREATE(&psFPNodeHeap, sizeof(FP_NODE), FPATH_NODEINIT, FPATH_NODEEXT))
{
return FALSE;
}
#if OPEN_LIST == 2
apsNodes = (FP_NODE**)malloc(sizeof(FP_NODE *) * FPATH_TABLESIZE);
if (!apsNodes)
@ -166,7 +156,6 @@ BOOL astarInitialise(void)
// Shutdown the findpath routine
void fpathShutDown(void)
{
HEAP_DESTROY(psFPNodeHeap);
#if OPEN_LIST == 2
free(apsNodes);
#else
@ -342,26 +331,24 @@ static void fpathHashReset(void)
while (apsNodes[i])
{
psNext = apsNodes[i]->psNext;
HEAP_FREE(psFPNodeHeap, apsNodes[i]);
free(apsNodes[i]);
apsNodes[i] = psNext;
}
#else
while (apsOpen[i])
{
psNext = apsOpen[i]->psNext;
HEAP_FREE(psFPNodeHeap, apsOpen[i]);
free(apsOpen[i]);
apsOpen[i] = psNext;
}
while (apsClosed[i])
{
psNext = apsClosed[i]->psNext;
HEAP_FREE(psFPNodeHeap, apsClosed[i]);
free(apsClosed[i]);
apsClosed[i] = psNext;
}
#endif
}
HEAP_RESET(psFPNodeHeap);
}
@ -750,10 +737,11 @@ static SDWORD fpathEstimate(SDWORD x, SDWORD y, SDWORD fx, SDWORD fy)
// Generate a new node
static FP_NODE *fpathNewNode(SDWORD x, SDWORD y, SDWORD dist, FP_NODE *psRoute)
{
FP_NODE *psNode;
FP_NODE *psNode = malloc(sizeof(FP_NODE));
if (!HEAP_ALLOC(psFPNodeHeap, (void**) &psNode))
if (psNode == NULL)
{
debug(LOG_ERROR, "fpathNewNode: Out of memory");
return NULL;
}

View File

@ -65,9 +65,6 @@ static SDWORD fmLtRad = 80, fmMedRad = 100, fmHvyRad = 110;
#define FORMATION_SPEED_INIT 100000L
// The heap of formations
OBJ_HEAP *psFHeap;
// The list of allocated formations
FORMATION *psFormationList;
@ -76,11 +73,6 @@ SDWORD formationObjRadius(BASE_OBJECT *psObj);
// Initialise the formation system
BOOL formationInitialise(void)
{
if (!HEAP_CREATE(&psFHeap, sizeof(FORMATION), F_HEAPINIT, F_HEAPEXT))
{
return FALSE;
}
psFormationList = NULL;
return TRUE;
@ -95,11 +87,9 @@ void formationShutDown(void)
{
debug( LOG_NEVER, "formation with %d units still attached\n", psFormationList->refCount );
psNext = psFormationList->psNext;
HEAP_FREE(psFHeap, psFormationList);
free(psFormationList);
psFormationList = psNext;
}
HEAP_DESTROY(psFHeap);
}
#ifdef TEST_BED
@ -131,12 +121,13 @@ SDWORD sum;
BOOL formationNew(FORMATION **ppsFormation, FORMATION_TYPE type,
SDWORD x, SDWORD y, SDWORD dir)
{
FORMATION *psNew;
SDWORD i;
FORMATION *psNew = malloc(sizeof(FORMATION));
// get a heap structure
if (!HEAP_ALLOC(psFHeap, (void**) &psNew))
if (psNew == NULL)
{
debug(LOG_ERROR, "formationNew: Out of memory");
return FALSE;
}
@ -352,7 +343,7 @@ void formationLeave(FORMATION *psFormation, BASE_OBJECT *psObj)
}
psPrev->psNext = psFormation->psNext;
}
HEAP_FREE(psFHeap, psFormation);
free(psFormation);
}
}