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

* Use decent assertion expressions for some asserts (e.g. !"string", so that debuggers actually display something more interesting than 'FALSE')

git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@1815 4a71c877-e1ca-e34f-864e-861f7616d084
master
Giel van Schijndel 2007-06-04 20:46:30 +00:00
parent 5839fb21ea
commit d65f88beb5
6 changed files with 48 additions and 92 deletions

View File

@ -32,7 +32,6 @@
#include "types.h"
#include "debug.h"
#include "heap.h"
#include "treap.h"
#include "strres.h"
#include "strresly.h"

View File

@ -32,7 +32,6 @@
#include "types.h"
#include "debug.h"
#include "heap.h"
#include "treap.h"
#include "treapint.h"
@ -95,14 +94,6 @@ BOOL treapCreate(TREAP **ppsTreap, TREAP_CMP cmp, UDWORD init, UDWORD ext)
return FALSE;
}
if (!HEAP_CREATE(&((*ppsTreap)->psNodes), sizeof(TREAP_NODE), init, ext))
{
debug( LOG_ERROR, "treapCreate: Out of memory" );
abort();
free(*ppsTreap);
return FALSE;
}
// Store the comparison function if there is one, use the default otherwise
if (cmp)
{
@ -189,10 +180,11 @@ void treapAddNode(TREAP_NODE **ppsRoot, TREAP_NODE *psNew, TREAP_CMP cmp)
*/
BOOL treapAdd(TREAP *psTreap, void *key, void *pObj)
{
TREAP_NODE *psNew;
TREAP_NODE* psNew = malloc(sizeof(TREAP_NODE));
if (!HEAP_ALLOC(psTreap->psNodes, (void**)&psNew))
if (psNew == NULL)
{
debug(LOG_ERROR, "treapAdd: Out of memory");
return FALSE;
}
psNew->priority = (UDWORD)rand();
@ -297,7 +289,7 @@ BOOL treapDel(TREAP *psTreap, void *key)
#ifdef DEBUG_TREAP
free(psDel->pFile);
#endif
HEAP_FREE(psTreap->psNodes, psDel);
free(psDel);
return TRUE;
}
@ -344,7 +336,7 @@ static void treapReportRec(TREAP_NODE *psRoot)
{
if (psRoot)
{
debug( LOG_NEVER, ((" %s, line %d\n", psRoot->pFile, psRoot->line );
debug( LOG_NEVER, ((" %s, line %d\n", psRoot->pFile, psRoot->line )));
treapReportRec(psRoot->psLeft);
treapReportRec(psRoot->psRight);
}
@ -353,7 +345,7 @@ static void treapReportRec(TREAP_NODE *psRoot)
/* Recursively free a treap */
static void treapDestroyRec(TREAP_NODE *psRoot, OBJ_HEAP *psHeap)
static void treapDestroyRec(TREAP_NODE *psRoot)
{
if (psRoot == NULL)
{
@ -361,18 +353,18 @@ static void treapDestroyRec(TREAP_NODE *psRoot, OBJ_HEAP *psHeap)
}
// free the sub branches
treapDestroyRec(psRoot->psLeft, psHeap);
treapDestroyRec(psRoot->psRight, psHeap);
treapDestroyRec(psRoot->psLeft);
treapDestroyRec(psRoot->psRight);
// free the root
HEAP_FREE(psHeap, psRoot);
free(psRoot);
}
/* Release all the nodes in the treap */
void treapReset(TREAP *psTreap)
{
treapDestroyRec(psTreap->psRoot, psTreap->psNodes);
treapDestroyRec(psTreap->psRoot);
psTreap->psRoot = NULL;
}
@ -389,8 +381,7 @@ void treapDestroy(TREAP *psTreap)
free(psTreap->pFile);
#endif
treapDestroyRec(psTreap->psRoot, psTreap->psNodes);
HEAP_DESTROY(psTreap->psNodes);
treapDestroyRec(psTreap->psRoot);
free(psTreap);
}

View File

@ -31,7 +31,6 @@
#include "types.h"
#include "debug.h"
#include "heap.h"
/* Turn on and off the treap debugging */
#ifdef DEBUG
@ -78,7 +77,6 @@ typedef struct _treap_node
typedef struct _treap
{
TREAP_CMP cmp; // comparison function
OBJ_HEAP *psNodes; // node heap
TREAP_NODE *psRoot; // root of the tree
#ifdef DEBUG_TREAP

View File

@ -40,13 +40,6 @@ static VAL_CREATE_FUNC *asCreateFuncs;
static VAL_RELEASE_FUNC *asReleaseFuncs;
static UDWORD numFuncs;
// Heap for value chunks
static OBJ_HEAP *psValHeap;
// Heap for active triggers
static OBJ_HEAP *psTrigHeap;
// Heap for contexts
static OBJ_HEAP *psContHeap;
// The list of currently active triggers
ACTIVE_TRIGGER *psTrigList;
@ -104,25 +97,6 @@ void eventTimeReset(UDWORD initTime)
/* Initialise the event system */
BOOL eventInitialise(EVENT_INIT *psInit)
{
// Create the value heap
if (!HEAP_CREATE(&psValHeap, sizeof(VAL_CHUNK), psInit->valInit, psInit->valExt))
{
debug(LOG_ERROR, "eventInitialise: HEAP_CREATE failed for values");
return FALSE;
}
// Create the trigger heap
if (!HEAP_CREATE(&psTrigHeap, sizeof(ACTIVE_TRIGGER), psInit->trigInit, psInit->trigExt))
{
debug(LOG_ERROR, "eventInitialise: HEAP_CREATE failed for triggers");
return FALSE;
}
// Create the context heap
if (!HEAP_CREATE(&psContHeap, sizeof(SCRIPT_CONTEXT), psInit->contInit, psInit->contExt))
{
debug(LOG_ERROR, "eventInitialise: HEAP_CREATE failed for context");
return FALSE;
}
psTrigList = NULL;
psCallbackList = NULL;
psContList = NULL;
@ -155,7 +129,7 @@ void eventReset(void)
}
#endif
eventRemoveContext(psCurr->psContext);
HEAP_FREE(psTrigHeap, psCurr);
free(psCurr);
}
// Free any active callback triggers and their context's
while (psCallbackList)
@ -169,7 +143,7 @@ void eventReset(void)
}
#endif
eventRemoveContext(psCurr->psContext);
HEAP_FREE(psTrigHeap, psCurr);
free(psCurr);
}
// Now free any context's that are left
@ -198,10 +172,6 @@ void eventShutDown(void)
{
eventReset();
HEAP_DESTROY(psValHeap);
HEAP_DESTROY(psTrigHeap);
HEAP_DESTROY(psContHeap);
if (asCreateFuncs)
{
free(asCreateFuncs);
@ -396,9 +366,10 @@ BOOL eventNewContext(SCRIPT_CODE *psCode, CONTEXT_RELEASE release,
"eventNewContext: Invalid code pointer" );
// Get a new context
if (!HEAP_ALLOC(psContHeap, (void**) &psContext))
psContext = malloc(sizeof(SCRIPT_CONTEXT));
if (psContext == NULL)
{
debug(LOG_ERROR,"eventNewContext: HEAP_ALLOC failed");
debug(LOG_ERROR, "eventNewContext: Out of memory");
return FALSE;
}
@ -475,14 +446,15 @@ BOOL eventNewContext(SCRIPT_CODE *psCode, CONTEXT_RELEASE release,
while (val >= 0)
{
if (!HEAP_ALLOC(psValHeap, (void**) &psNewChunk))
psNewChunk = malloc(sizeof(VAL_CHUNK));
if (psNewChunk == NULL)
{
for(psNewChunk=psContext->psGlobals; psNewChunk; psNewChunk = psNextChunk)
{
psNextChunk = psNewChunk->psNext;
HEAP_FREE(psValHeap, psNewChunk);
free(psNewChunk);
}
HEAP_FREE(psContHeap, psContext);
free(psContext);
return FALSE;
}
@ -515,13 +487,13 @@ BOOL eventNewContext(SCRIPT_CODE *psCode, CONTEXT_RELEASE release,
{
if (!asCreateFuncs[type](psNewChunk->asVals + storeIndex))
{
HEAP_FREE(psValHeap, psNewChunk);
free(psNewChunk);
for(psNewChunk=psContext->psGlobals; psNewChunk; psNewChunk = psNextChunk)
{
psNextChunk = psNewChunk->psNext;
HEAP_FREE(psValHeap, psNewChunk);
free(psNewChunk);
}
HEAP_FREE(psContHeap, psContext);
free(psContext);
return FALSE;
}
}
@ -712,7 +684,7 @@ void eventRemoveContext(SCRIPT_CONTEXT *psContext)
for(psCChunk = psContext->psGlobals; psCChunk; psCChunk = psNChunk)
{
psNChunk = psCChunk->psNext;
HEAP_FREE(psValHeap, psCChunk);
free(psCChunk);
}
// Remove it from the context list
@ -720,7 +692,7 @@ void eventRemoveContext(SCRIPT_CONTEXT *psContext)
{
psCCont = psContList;
psContList = psContList->psNext;
HEAP_FREE(psContHeap, psCCont);
free(psCCont);
}
else
{
@ -732,7 +704,7 @@ void eventRemoveContext(SCRIPT_CONTEXT *psContext)
if (psCCont)
{
psPCont->psNext = psCCont->psNext;
HEAP_FREE(psContHeap, psContext);
free(psContext);
}
else
{
@ -869,8 +841,10 @@ static BOOL eventInitTrigger(ACTIVE_TRIGGER **ppsTrigger, SCRIPT_CONTEXT *psCont
}
// Get a trigger object
if (!HEAP_ALLOC(psTrigHeap, (void**) &psNewTrig))
psNewTrig = malloc(sizeof(ACTIVE_TRIGGER));
if (psNewTrig == NULL)
{
debug(LOG_ERROR, "eventInitTrigger: Out of memory");
return FALSE;
}
@ -903,7 +877,8 @@ BOOL eventLoadTrigger(UDWORD time, SCRIPT_CONTEXT *psContext,
"eventLoadTrigger: Trigger out of range" );
// Get a trigger object
if (!HEAP_ALLOC(psTrigHeap, (void**) &psNewTrig))
psNewTrig = malloc(sizeof(ACTIVE_TRIGGER));
if (psNewTrig == NULL)
{
debug( LOG_ERROR, "eventLoadTrigger: out of memory" );
abort();
@ -936,8 +911,10 @@ BOOL eventAddPauseTrigger(SCRIPT_CONTEXT *psContext, UDWORD event, UDWORD offset
"eventAddTrigger: Event out of range" );
// Get a trigger object
if (!HEAP_ALLOC(psTrigHeap, (void**) &psNewTrig))
psNewTrig = malloc(sizeof(ACTIVE_TRIGGER));
if (psNewTrig == NULL)
{
debug(LOG_ERROR, "eventAddPauseTrigger: Out of memory");
return FALSE;
}
@ -987,7 +964,7 @@ static void eventFreeTrigger(ACTIVE_TRIGGER *psTrigger)
// Free the context as well
eventRemoveContext(psTrigger->psContext);
}
HEAP_FREE(psTrigHeap, psTrigger);
free(psTrigger);
}
@ -1263,7 +1240,7 @@ static void eventRemoveTriggerFromList(ACTIVE_TRIGGER **ppsList,
{
psCurr = *ppsList;
*ppsList = (*ppsList)->psNext;
HEAP_FREE(psTrigHeap, psCurr);
free(psCurr);
}
}
else
@ -1287,7 +1264,7 @@ static void eventRemoveTriggerFromList(ACTIVE_TRIGGER **ppsList,
else if (psCurr)
{
psPrev->psNext = psCurr->psNext;
HEAP_FREE(psTrigHeap, psCurr);
free(psCurr);
}
}
}
@ -1339,7 +1316,7 @@ BOOL eventSetTrigger(void)
{
psCurr = psTrigList;
psTrigList = psTrigList->psNext;
HEAP_FREE(psTrigHeap, psCurr);
free(psCurr);
}
}
else
@ -1363,7 +1340,7 @@ BOOL eventSetTrigger(void)
else if (psCurr)
{
psPrev->psNext = psCurr->psNext;
HEAP_FREE(psTrigHeap, psCurr);
free(psCurr);
}
}
// Remove any old callback trigger from the list
@ -1372,7 +1349,7 @@ BOOL eventSetTrigger(void)
{
psCurr = psCallbackList;
psCallbackList = psCallbackList->psNext;
HEAP_FREE(psTrigHeap, psCurr);
free(psCurr);
}
else
{
@ -1388,7 +1365,7 @@ BOOL eventSetTrigger(void)
if (psCurr)
{
psPrev->psNext = psCurr->psNext;
HEAP_FREE(psTrigHeap, psCurr);
free(psCurr);
}
}*/
}

View File

@ -46,7 +46,7 @@ BOOL buttonCreate(W_BUTTON **ppsWidget, W_BUTINIT *psInit)
if (psInit->style & ~(WBUT_PLAIN | WIDG_HIDDEN | WFORM_NOCLICKMOVE |
WBUT_NOPRIMARY | WBUT_SECONDARY | WBUT_TXTCENTRE ))
{
ASSERT( FALSE, "Unknown button style" );
ASSERT(!"unknown button style", "buttonCreate: unknown button style");
return FALSE;
}
@ -54,7 +54,7 @@ BOOL buttonCreate(W_BUTTON **ppsWidget, W_BUTINIT *psInit)
*ppsWidget = (W_BUTTON *)malloc(sizeof(W_BUTTON));
if (*ppsWidget == NULL)
{
ASSERT( FALSE, "buttonCreate: Out of memory" );
debug(LOG_ERROR, "buttonCreate: Out of memory" );
return FALSE;
}
/* Allocate memory for the text and copy it if necessary */

View File

@ -43,9 +43,6 @@
UDWORD gridWidth, gridHeight;
// The heap for the grid arrays
OBJ_HEAP *psGridHeap;
// The map grid
GRID_ARRAY *apsMapGrid[GRID_MAXAREA];
#define GridIndex(a,b) (((b)*gridWidth) + (a))
@ -77,11 +74,6 @@ SDWORD gridObjRange(BASE_OBJECT *psObj);
// initialise the grid system
BOOL gridInitialise(void)
{
if (!HEAP_CREATE(&psGridHeap, sizeof(GRID_ARRAY), GRID_HEAPINIT, GRID_HEAPEXT))
{
return FALSE;
}
// memset(apsMapGrid, 0, sizeof(GRID_ARRAY *) * GRID_WIDTH * GRID_HEIGHT);
memset(apsMapGrid, 0, sizeof(GRID_ARRAY *) * GRID_MAXAREA);
@ -109,7 +101,7 @@ void gridClear(void)
for(psCurr = apsMapGrid[GridIndex(x,y)]; psCurr != NULL; psCurr = psNext)
{
psNext = psCurr->psNext;
HEAP_FREE(psGridHeap, psCurr);
free(psCurr);
}
// apsMapGrid[x][y] = NULL;
apsMapGrid[GridIndex(x,y)] = NULL;
@ -158,8 +150,6 @@ void gridShutDown(void)
{
//gridReset();
gridClear();
HEAP_DESTROY(psGridHeap);
}
@ -416,9 +406,10 @@ void gridAddArrayObject(SDWORD x, SDWORD y, BASE_OBJECT *psObj)
}
// allocate a new array chunk
if (!HEAP_ALLOC(psGridHeap, (void**) &psNew))
psNew = malloc(sizeof(GRID_ARRAY));
if (psNew == NULL)
{
debug( LOG_NEVER, "help - %d\n", psObj->id );
debug(LOG_ERROR, "gridAddArrayObject: Out of memory");
return;
}
@ -517,7 +508,7 @@ void gridCompactArray(SDWORD x, SDWORD y)
while (psDone)
{
psNext = psDone->psNext;
HEAP_FREE(psGridHeap, psDone);
free(psDone);
psDone = psNext;
}
}