* functions memRecReport & memMemoryReport:

- removed these functions and all calls to it if REALLY_DEBUG_MALLOC isn't defined, because they don't do anything currently when REALLY_DEBUG_MALLOC isn't defined
  - moved the declaration of memRecReport from memint.h to mem.h where it is, together with memMemoryReport, optionally declared depending on REALLY_DEBUG_MALLOC
 * fixed yet another set of signed/unsigned errors (src/seqdisp.c & textdraw.c/h)
 * removed a cast from char* to char* (that's what I call unnecessary; transforming something into something it already is)
 * memory system:
  - added some comments to clarify what certain pieces of code actually do
  - removed some redundant code (e.g. why would you ever want to overwrite memory to zero just before passing it to free()?)
  - made debug-version of free() return immediately when being passed a NULL pointer (i.e. after raising an assert)
  - moved some variable declarations down to the first point of usage
  - *IMPORTANT*: commented the definition of MEMORY_SET out (line 38 mem.c), this might break code that expects freshly malloc'ed memory to be pre-initialized, if you experience any problems due to commenting this out please say so on the mailinglists!

git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@957 4a71c877-e1ca-e34f-864e-861f7616d084
master
Giel van Schijndel 2007-01-04 18:41:24 +00:00
parent bebc8316da
commit 851b570bc5
7 changed files with 50 additions and 54 deletions

View File

@ -135,7 +135,9 @@ void blkDestroy(BLOCK_HEAP *psHeap)
if (psHeap->psMemTreap != NULL)
{
debug( LOG_NEVER, "blkDestroy: %s at %d: memory allocated :\n", psHeap->pFileName, psHeap->line );
#ifdef REALLY_DEBUG_MALLOC
memRecReport(psHeap->psMemTreap);
#endif
}
#endif
@ -491,7 +493,9 @@ void blkReset(BLOCK_HEAP *psHeap)
if (psHeap->psMemTreap != NULL)
{
debug( LOG_NEVER, "blkReset: %s at %d: memory allocated :\n", psHeap->pFileName, psHeap->line );
#ifdef REALLY_DEBUG_MALLOC
memRecReport(psHeap->psMemTreap);
#endif
}
psHeap->psMemTreap = NULL;
psHeap->free = FALSE;

View File

@ -32,7 +32,11 @@
* is trashed before it is freed.
* This is done automatically by Visual C's memory routines.
*/
#define MEMORY_SET TRUE
// No piece of code should ever depend upon memory being initialized (RAII: Resource Acquisition Is Initialization)
// Therefore I humbly comment this line out, which might very well result in
// buggy code becoming even buggier (just search this file for the next
// instance of MEMORY_SET). -- Giel
//#define MEMORY_SET TRUE
/* Number of bytes after which memory amounts are displayed in Kb */
#define SHOW_KB_LIMIT (0x400)
@ -84,7 +88,9 @@ static void memTreapDestroy(TREAP_NODE *psRoot)
void memShutDown(void)
{
// Report any memory still allocated
#ifdef REALLY_DEBUG_MALLOC
memMemoryReport();
#endif
// Free up the allocated memory
memTreapDestroy((TREAP_NODE *)psMemRoot);
@ -169,6 +175,8 @@ void *memMalloc(const char *pFileName, SDWORD LineNumber, size_t Size)
}
/* Got the main bit of memory - set up the node entry */
// This part of code adds information about which file requested
// on what line what amount of memory.
psNode = (MEM_NODE *)pMemBase;
psNode->pFile = (char *)RMALLOC( strlen(pFileName)+1 );
if (!psNode->pFile)
@ -190,12 +198,15 @@ void *memMalloc(const char *pFileName, SDWORD LineNumber, size_t Size)
treapAddNode((TREAP_NODE **)&psMemRoot, (TREAP_NODE *)psNode, memBlockCmp);
/* Now initialise the memory - try to catch unitialised variables */
memset((UBYTE *)(pMemBase) + sizeof(MEM_NODE),
// Fill up the safety zone left of (or before) and right of (or after)
// the requested memory page, with SAFETY_ZONE_BYTE to be able to
// catch writing out of bounds.
memset((UBYTE *)(pMemBase) + sizeof(MEM_NODE), // Left or before the requested memchunk
SAFETY_ZONE_BYTE, SAFETY_ZONE_SIZE);
memset((UBYTE *)(pMemBase) + sizeof(MEM_NODE) + SAFETY_ZONE_SIZE + Size,
memset((UBYTE *)(pMemBase) + sizeof(MEM_NODE) + SAFETY_ZONE_SIZE + Size, // Right or after chunck
SAFETY_ZONE_BYTE, SAFETY_ZONE_SIZE);
#ifdef MEMORY_SET
/* The PC initialises malloc'ed memory already, no need to do it again */
// Initialize memory before returning its address (WRONG!!! The code that requests mem should ensure initialization)
memset((UBYTE *)(pMemBase) + sizeof(MEM_NODE) + SAFETY_ZONE_SIZE,
INITIALISE_BYTE, Size);
#endif
@ -232,25 +243,14 @@ void *memMallocRelease(size_t Size)
*/
void memFree(const char *pFileName, SDWORD LineNumber, void *pMemToFree)
{
MEM_NODE sNode, *psDeleted;
SDWORD i, InvalidBottom, InvalidTop;
UBYTE *pMemBase;
BLOCK_HEAP *psBlock;
#ifdef MEMORY_SET
SDWORD Size;
#endif
(void)LineNumber;
(void)pFileName;
ASSERT( (pFileName != NULL), "No filename passed to mem_Free" );
ASSERT( (pFileName != NULL), "No filename passed to memFree" );
ASSERT( (pMemToFree != NULL), "Attempt to free NULL pointer, called by:\n"
"File: %s\nLine: %d\n", pFileName, LineNumber );
if (pMemToFree == NULL) return;
// see if the pointer was allocated in a block
psBlock = blkFind(pMemToFree);
BLOCK_HEAP *psBlock = blkFind(pMemToFree);
if (psBlock != NULL)
{
// use a block heap rather than normal free
@ -261,24 +261,28 @@ void memFree(const char *pFileName, SDWORD LineNumber, void *pMemToFree)
// Create a dummy node for the search
// This is only looked at by memBlockCmp so only need to set the object and size
MEM_NODE sNode;
sNode.pObj = ((UBYTE *)pMemToFree) - sizeof(MEM_NODE) - SAFETY_ZONE_SIZE;
sNode.size = 1;
/* Get the node for the memory block */
psDeleted = (MEM_NODE *)treapDelRec((TREAP_NODE **)&psMemRoot,
MEM_NODE* psDeleted = (MEM_NODE *)treapDelRec((TREAP_NODE **)&psMemRoot,
(void*)&sNode, memBlockCmp);
ASSERT( psDeleted != NULL,
"Invalid pointer passed to mem_Free by:\n"
"Invalid pointer passed to memFree by:\n"
"File: %s\nLine: %d\n\n"
"Attempt to free already freed pointer?",
pFileName, LineNumber );
if (psDeleted)
{
/* The pointer is valid, check the buffer zones */
pMemBase = (UBYTE *)(pMemToFree) - SAFETY_ZONE_SIZE;
InvalidBottom = InvalidTop = 0;
UBYTE* pMemBase = (UBYTE *)(pMemToFree) - SAFETY_ZONE_SIZE;
UWORD InvalidBottom = 0, InvalidTop = 0, i = 0;
// Check wether out of bound writes have occured since memMalloc()
for(i=0; i<SAFETY_ZONE_SIZE; i++)
{
if (pMemBase[i] != SAFETY_ZONE_BYTE)
@ -301,12 +305,6 @@ void memFree(const char *pFileName, SDWORD LineNumber, void *pMemToFree)
psDeleted->pFile, psDeleted->line,
pFileName, LineNumber );
/* Trash the memory before it is freed (The PC already does this) */
#ifdef MEMORY_SET
Size = psDeleted->size;
memset(pMemToFree, FREE_BYTE, Size);
#endif
/* Now free the memory */
RFREE((char *)psDeleted->pFile);
@ -321,10 +319,8 @@ void memFree(const char *pFileName, SDWORD LineNumber, void *pMemToFree)
/* Replacement for Free for release builds */
void memFreeRelease(void *pMemToFree)
{
BLOCK_HEAP *psBlock;
// see if the pointer was allocated in a block
psBlock = blkFind(pMemToFree);
BLOCK_HEAP* psBlock = blkFind(pMemToFree);
if (psBlock != NULL)
{
// use a block heap rather than normal free
@ -332,7 +328,6 @@ void memFreeRelease(void *pMemToFree)
return;
}
RFREE(pMemToFree);
}
#endif
@ -372,10 +367,10 @@ BOOL memPointerValid(void *pPtr, size_t size)
}
#ifdef REALLY_DEBUG_MALLOC
/* Recursive function to print out the list of memory blocks */
SDWORD memRecReport(MEM_NODE *psRoot)
{
#ifdef REALLY_DEBUG_MALLOC
if (psRoot)
{
if (psRoot->size < SHOW_KB_LIMIT)
@ -393,10 +388,9 @@ SDWORD memRecReport(MEM_NODE *psRoot)
memRecReport((MEM_NODE *)psRoot->psRight) +
psRoot->size;
}
#endif
psRoot = psRoot;
return 0;
}
#endif
#ifdef DEBUG_MALLOC
#define MAXMODULES (32)
@ -479,11 +473,11 @@ void memMemoryDump(MEM_NODE *Node)
}
#ifdef REALLY_DEBUG_MALLOC
/* Report on currently allocated memory.
*/
void memMemoryReport(void)
{
#ifdef DEBUG_MALLOC
SDWORD TotMem;
if (!psMemRoot)
@ -504,9 +498,8 @@ void memMemoryReport(void)
}
}
#endif
}
#endif
/* Display the memory treap */
void memDisplayTreap(void)

View File

@ -59,9 +59,13 @@ void memFreeRelease(void *pMemToFree);
/* Check a pointer refers to a valid block of memory */
BOOL memPointerValid(void *pPtr, size_t Size);
#ifdef REALLY_DEBUG_MALLOC
/* Report on currently allocated memory */
void memMemoryReport(void);
/* Recursive function to print out the list of memory blocks */
extern SDWORD memRecReport(MEM_NODE *psRoot);
#endif
/* Display the memory treap */
void memDisplayTreap(void);
@ -80,5 +84,4 @@ void memDisplayTreap(void);
#endif // DEBUG_MALLOC
#endif
#endif // _mem_h_

View File

@ -26,9 +26,5 @@ typedef struct _mem_node
/* compare two memory blocks */
extern SDWORD memBlockCmp(void *key1, void *key2);
/* Recursive function to print out the list of memory blocks */
extern SDWORD memRecReport(MEM_NODE *psRoot);
#endif

View File

@ -58,7 +58,7 @@ extern void pie_FillTextExtents(int BorderThickness,UBYTE r,UBYTE g,UBYTE b,BOOL
extern UDWORD pie_DrawFormattedText(char *String, UDWORD x, UDWORD y, UDWORD Width, UDWORD Justify, BOOL DrawBack);
extern void pie_DrawText(char *string,UDWORD x,UDWORD y);
extern void pie_DrawTextToSurface(char *String, int XPos, int YPos);
extern void pie_DrawTextToSurface(char *String, UDWORD XPos, UDWORD YPos);
extern void pie_DrawText270(char *String,int XPos,int YPos);
extern void pie_RenderBlueTintedBitmap(iBitmap *bmp, int x, int y, int w, int h, int ow);
extern void pie_RenderDeepBlueTintedBitmap(iBitmap *bmp, int x, int y, int w, int h, int ow);

View File

@ -294,11 +294,11 @@ UBYTE ExtentsMode=EXTENTS_USEMAXWIDTH;
//
UDWORD pie_DrawFormattedText(char *String, UDWORD x, UDWORD y, UDWORD Width, UDWORD Justify, BOOL DrawBack)
{
int i,si,osi;
int Len = strlen(String);
int i,osi;
UDWORD si, Len = strlen(String);
int jx = x; // Default to left justify.
int jy = y;
int WWidth;
UDWORD WWidth;
BOOL GotSpace;
BOOL NewLine;
BOOL AddLeadingSpace = FALSE;
@ -313,7 +313,7 @@ UDWORD pie_DrawFormattedText(char *String, UDWORD x, UDWORD y, UDWORD Width, UDW
while(si < Len) {
// Remove leading spaces, usefull when doing centre justify.
if(FFlags & FTEXTF_SKIP_LEADING_SPACES) {
while( (si < strlen((char*)String)) && (String[si] == ' ') ) {
while( (si < strlen(String)) && (String[si] == ' ') ) {
si++;
}
}
@ -632,7 +632,7 @@ void pie_RenderDeepBlueTintedBitmap(iBitmap *bmp, int x, int y, int w, int h, in
// Partial fix for rendering text on 'video', you can read it now. -Q
// --still to do, add 'boxes' under text so you can see it better.
//===========================================================
void pie_DrawTextToSurface(char *string, int x, int y)
void pie_DrawTextToSurface(char *string, UDWORD x, UDWORD y)
{
int Index;
UWORD ImageID;

View File

@ -57,10 +57,10 @@
typedef struct {
char pText[MAX_STR_LENGTH];
SDWORD x;
SDWORD y;
SDWORD startFrame;
SDWORD endFrame;
UDWORD x;
UDWORD y;
UDWORD startFrame;
UDWORD endFrame;
BOOL bSubtitle;
} SEQTEXT;