Main Page | Alphabetical List | Compound List | File List | Compound Members | File Members

Ram.c File Reference

#include <memory.h>
#include <malloc.h>
#include <assert.h>
#include <crtdbg.h>
#include "ram.h"

Go to the source code of this file.

Defines

#define _CRTDBG_MAP_ALLOC
#define HEADER_SIZE   (((SizeSize + MemStampSize)+7)&(~(uint32)7))
#define EXTRA_SIZE   (((HEADER_SIZE + MemStampSize)+7)&(~(uint32)7))

Enumerations

enum  geRam_MemoryInitialization { DONT_INITIALIZE = 0, INITIALIZE_MEMORY = 1 }

Functions

GENESISAPI int geRam_EnableCriticalCallback (int add)
GENESISAPI geRam_CriticalCallbackFunction geRam_SetCriticalCallback (geRam_CriticalCallbackFunction critical_callback)
int geRam_DoCriticalCallback (void)
GENESISAPI void * geRam_AllocateClear (uint32 size)
void geRam_SetupBlock (char *p, uint32 size, geRam_MemoryInitialization InitMem)
GENESISAPI void * _geRam_DebugAllocate (uint32 size, const char *pFile, int line)
char * ram_verify_block (void *ptr)
GENESISAPI void geRam_Free_ (void *ptr)
GENESISAPI void * _geRam_DebugRealloc (void *ptr, uint32 newsize, const char *pFile, int line)
GENESISAPI void geRam_ReportAllocations (void)
GENESISAPI void geRam_AddAllocation (int n, uint32 size)
geBoolean geRam_IsValidPtr (void *ptr)

Variables

void * StupidUnusedPointer
int geRam_CriticalAllocationCount = 0
geRam_CriticalCallbackFunction geRam_CriticalCallback = NULL
int32 geRam_CurrentlyUsed = 0
int32 geRam_MaximumUsed = 0
int32 geRam_NumberOfAllocations = 0
int32 geRam_MaximumNumberOfAllocations = 0
char MemStamp [] = {"!CHECKME!"}
const int MemStampSize = sizeof (MemStamp)
const int SizeSize = sizeof (uint32)
const unsigned char AllocFillerByte = (unsigned char)0xA5
const unsigned char FreeFillerByte = (unsigned char)0xB6


Define Documentation

#define _CRTDBG_MAP_ALLOC
 

Definition at line 27 of file Ram.c.

#define EXTRA_SIZE   (((HEADER_SIZE + MemStampSize)+7)&(~(uint32)7))
 

Definition at line 206 of file Ram.c.

Referenced by _geRam_DebugAllocate(), _geRam_DebugRealloc(), and geRam_Free_().

#define HEADER_SIZE   (((SizeSize + MemStampSize)+7)&(~(uint32)7))
 

Definition at line 205 of file Ram.c.

Referenced by _geRam_DebugAllocate(), _geRam_DebugRealloc(), geRam_IsValidPtr(), geRam_SetupBlock(), and ram_verify_block().


Enumeration Type Documentation

enum geRam_MemoryInitialization
 

Enumeration values:
DONT_INITIALIZE 
INITIALIZE_MEMORY 

Definition at line 217 of file Ram.c.

Referenced by geRam_SetupBlock().

00218         {
00219                 DONT_INITIALIZE = 0, 
00220                 INITIALIZE_MEMORY = 1
00221         } geRam_MemoryInitialization;


Function Documentation

GENESISAPI void* _geRam_DebugAllocate uint32  size,
const char *  pFile,
int  line
 

Definition at line 247 of file Ram.c.

References EXTRA_SIZE, GENESISAPI, geRam_CurrentlyUsed, geRam_DoCriticalCallback(), geRam_MaximumNumberOfAllocations, geRam_MaximumUsed, geRam_NumberOfAllocations, geRam_SetupBlock(), HEADER_SIZE, INITIALIZE_MEMORY, and NULL.

Referenced by _geRam_DebugRealloc().

00248         {
00249       char *p;
00250 
00251       do
00252       {
00253           p = (char*)_malloc_dbg (size + EXTRA_SIZE, _NORMAL_BLOCK, pFile, line);
00254       } while ((p == NULL) && geRam_DoCriticalCallback ());
00255 
00256       if (p == NULL)
00257       {
00258          return NULL;
00259       }
00260 
00261       // setup size stamps and memory overwrite checks
00262       geRam_SetupBlock (p, size, INITIALIZE_MEMORY);
00263 
00264       // and update the allocations stuff
00265       geRam_NumberOfAllocations++;
00266       geRam_CurrentlyUsed += size;
00267 
00268       if (geRam_NumberOfAllocations > geRam_MaximumNumberOfAllocations)
00269       {
00270           geRam_MaximumNumberOfAllocations = geRam_NumberOfAllocations;
00271       }
00272       if (geRam_CurrentlyUsed > geRam_MaximumUsed)
00273       {
00274           geRam_MaximumUsed = geRam_CurrentlyUsed;
00275       }
00276 
00277       return p+HEADER_SIZE;
00278         }

GENESISAPI void* _geRam_DebugRealloc void *  ptr,
uint32  newsize,
const char *  pFile,
int  line
 

Definition at line 384 of file Ram.c.

References _geRam_DebugAllocate(), DONT_INITIALIZE, EXTRA_SIZE, GENESISAPI, geRam_CurrentlyUsed, geRam_DoCriticalCallback(), geRam_Free, geRam_MaximumUsed, geRam_SetupBlock(), HEADER_SIZE, NULL, ram_verify_block(), and uint32.

00385     {
00386         char *p;
00387         char * NewPtr;
00388         uint32 size;
00389 
00390         // if realloc is called with NULL, just treat it like an alloc
00391         if (ptr == NULL)
00392         {
00393             return _geRam_DebugAllocate(newsize, pFile, line);
00394         }
00395 
00396         // verify the block
00397         p = ram_verify_block (ptr);
00398         if (p == NULL)
00399         {
00400             return NULL;
00401         }
00402 
00403         // if newsize is NULL, then it's a free and return NULL
00404         if (newsize == 0)
00405         {
00406             geRam_Free (ptr);
00407             return NULL;
00408         }
00409 
00410         // gotta get the size before I realloc it...
00411         size = *((uint32 *)p);
00412 
00413         do
00414         {
00415             NewPtr = (char *)_realloc_dbg(p, newsize+EXTRA_SIZE, _NORMAL_BLOCK, pFile, line);
00416         } while ((NewPtr == NULL) && geRam_DoCriticalCallback ());
00417 
00418         // if allocation failed, return NULL...
00419         if (NewPtr == NULL)
00420         {
00421             return NULL;
00422         }
00423 
00424         geRam_SetupBlock (NewPtr, newsize, DONT_INITIALIZE);
00425 
00426         geRam_CurrentlyUsed += (newsize - size);
00427         if (geRam_CurrentlyUsed > geRam_MaximumUsed)
00428         {
00429             geRam_MaximumUsed = geRam_CurrentlyUsed;
00430         }
00431         assert ((geRam_CurrentlyUsed >= 0) && "free()d more ram than you allocated!");
00432 
00433         return NewPtr + HEADER_SIZE;
00434     }

GENESISAPI void geRam_AddAllocation int  n,
uint32  size
 

Definition at line 503 of file Ram.c.

References GENESISAPI, geRam_CurrentlyUsed, geRam_MaximumNumberOfAllocations, geRam_MaximumUsed, and geRam_NumberOfAllocations.

00504     {
00505         // and update the allocations stuff
00506         geRam_NumberOfAllocations += n;
00507         geRam_CurrentlyUsed += size;
00508 
00509         if (geRam_NumberOfAllocations > geRam_MaximumNumberOfAllocations)
00510         {
00511             geRam_MaximumNumberOfAllocations = geRam_NumberOfAllocations;
00512         }
00513         if (geRam_CurrentlyUsed > geRam_MaximumUsed)
00514         {
00515             geRam_MaximumUsed = geRam_CurrentlyUsed;
00516         }
00517     }

GENESISAPI void* geRam_AllocateClear uint32  size  ) 
 

Definition at line 107 of file Ram.c.

References GENESISAPI, geRam_Allocate, and uint32.

Referenced by createPaletteFast(), createPaletteGoodSub(), and Particles_Create().

00108 {
00109 void * mem;
00110         size = (size + 3)&(~(uint32)3);
00111         mem = geRam_Allocate(size);
00112         if ( mem )
00113         {
00114                 memset(mem,0,size);
00115         }
00116 return mem;
00117 }

int geRam_DoCriticalCallback void   )  [static]
 

Definition at line 92 of file Ram.c.

References geRam_CriticalAllocationCount, geRam_CriticalCallback, and NULL.

Referenced by _geRam_DebugAllocate(), and _geRam_DebugRealloc().

00095 {
00096     if ((geRam_CriticalAllocationCount != 0) && (geRam_CriticalCallback != NULL))
00097     {
00098         return geRam_CriticalCallback ();
00099     }
00100     else
00101     {
00102         return 0;
00103     }
00104 }

GENESISAPI int geRam_EnableCriticalCallback int  add  ) 
 

Definition at line 64 of file Ram.c.

References GENESISAPI, and geRam_CriticalAllocationCount.

00065 {
00066         geRam_CriticalAllocationCount += add;
00067         return geRam_CriticalAllocationCount;
00068 }

GENESISAPI void geRam_Free_ void *  ptr  ) 
 

Definition at line 353 of file Ram.c.

References EXTRA_SIZE, FreeFillerByte, GENESISAPI, geRam_CurrentlyUsed, geRam_NumberOfAllocations, NULL, ram_verify_block(), and uint32.

00354     {
00355         char *p;
00356         uint32 size;
00357 
00358         // make sure it's a valid block...
00359         p = ram_verify_block (ptr);
00360         if (p == NULL)
00361         {
00362             return;
00363         }
00364 
00365         // gotta get the size before you free it
00366         size = *((uint32 *)p);
00367 
00368         // fill it with trash...
00369         memset (p, FreeFillerByte, size+EXTRA_SIZE);
00370 
00371         // free the memory
00372         free (p);
00373 
00374         // update allocations
00375         geRam_NumberOfAllocations--;
00376         assert ((geRam_NumberOfAllocations >= 0) && "free()d more ram than you allocated!");
00377 
00378         geRam_CurrentlyUsed -= size;
00379         assert ((geRam_CurrentlyUsed >= 0) && "free()d more ram than you allocated!");
00380     }

geBoolean geRam_IsValidPtr void *  ptr  ) 
 

Definition at line 523 of file Ram.c.

References GE_FALSE, GE_TRUE, geBoolean, HEADER_SIZE, MemStamp, MemStampSize, NULL, SizeSize, and uint32.

Referenced by MemBlock_IsValid().

00524 {
00525 char * p = ptr;
00526 uint32 size;
00527 
00528         if (p == NULL) return GE_FALSE;
00529 
00530         // make p point to the beginning of the block
00531         p -= HEADER_SIZE;
00532 
00533         // get size from block
00534         size = *((uint32 *)p);
00535 
00536         // check stamp at front
00537         if (memcmp (p+SizeSize, MemStamp, MemStampSize) != 0)
00538         {
00539                 return GE_FALSE;
00540         }
00541 
00542         // and at back
00543         if (memcmp (p+HEADER_SIZE+size, MemStamp, MemStampSize) != 0)
00544         {
00545                 return GE_FALSE;
00546         }
00547 
00548 return GE_TRUE;
00549 }

GENESISAPI void geRam_ReportAllocations void   ) 
 

Definition at line 494 of file Ram.c.

References GENESISAPI.

00495 {
00496         _CrtDumpMemoryLeaks();
00497 }

GENESISAPI geRam_CriticalCallbackFunction geRam_SetCriticalCallback geRam_CriticalCallbackFunction  critical_callback  ) 
 

Definition at line 76 of file Ram.c.

References geRam_CriticalCallback, and geRam_CriticalCallbackFunction.

00079 {
00080     geRam_CriticalCallbackFunction OldCallback;
00081 
00082     OldCallback = geRam_CriticalCallback;
00083         geRam_CriticalCallback = critical_callback;
00084     return OldCallback;
00085 }

void geRam_SetupBlock char *  p,
uint32  size,
geRam_MemoryInitialization  InitMem
[static]
 

Definition at line 224 of file Ram.c.

References AllocFillerByte, geRam_MemoryInitialization, HEADER_SIZE, INITIALIZE_MEMORY, MemStamp, MemStampSize, SizeSize, and uint32.

Referenced by _geRam_DebugAllocate(), and _geRam_DebugRealloc().

00229     {
00230         if (InitMem == INITIALIZE_MEMORY)
00231         {
00232             // fill the memory block
00233             memset (p+HEADER_SIZE, AllocFillerByte, size);
00234         }
00235 
00236         // add the size at the front
00237         *((uint32 *)p) = size;
00238 
00239         // copy the memstamp to the front of the block
00240         memcpy (p+SizeSize, MemStamp, MemStampSize);
00241 
00242         // and to the end of the block
00243         memcpy (p+HEADER_SIZE+size, MemStamp, MemStampSize);
00244     }

char* ram_verify_block void *  ptr  )  [static]
 

Definition at line 317 of file Ram.c.

References HEADER_SIZE, MemStamp, MemStampSize, NULL, SizeSize, and uint32.

Referenced by _geRam_DebugRealloc(), and geRam_Free_().

00320     {
00321         char * p = ptr;
00322         uint32 size;
00323 
00324         if (p == NULL)
00325         {
00326             assert (0 && "freeing NULL");
00327             return NULL;
00328         }
00329 
00330         // make p point to the beginning of the block
00331         p -= HEADER_SIZE;
00332 
00333         // get size from block
00334         size = *((uint32 *)p);
00335 
00336         // check stamp at front
00337         if (memcmp (p+SizeSize, MemStamp, MemStampSize) != 0)
00338         {
00339             assert (0 && "ram_verify_block:  Memory block corrupted at front");
00340             return NULL;
00341         }
00342 
00343         // and at back
00344         if (memcmp (p+HEADER_SIZE+size, MemStamp, MemStampSize) != 0)
00345         {
00346             assert (0 && "ram_verify_block:  Memory block corrupted at tail");
00347             return NULL;
00348         }
00349 
00350         return p;
00351     }


Variable Documentation

const unsigned char AllocFillerByte = (unsigned char)0xA5 [static]
 

Definition at line 207 of file Ram.c.

Referenced by geRam_SetupBlock().

const unsigned char FreeFillerByte = (unsigned char)0xB6 [static]
 

Definition at line 208 of file Ram.c.

Referenced by geRam_Free_().

int geRam_CriticalAllocationCount = 0 [static]
 

Definition at line 55 of file Ram.c.

Referenced by geRam_DoCriticalCallback(), and geRam_EnableCriticalCallback().

geRam_CriticalCallbackFunction geRam_CriticalCallback = NULL [static]
 

Definition at line 57 of file Ram.c.

Referenced by geRam_DoCriticalCallback(), and geRam_SetCriticalCallback().

int32 geRam_CurrentlyUsed = 0
 

Definition at line 195 of file Ram.c.

Referenced by _geRam_DebugAllocate(), _geRam_DebugRealloc(), geRam_AddAllocation(), and geRam_Free_().

int32 geRam_MaximumNumberOfAllocations = 0
 

Definition at line 198 of file Ram.c.

Referenced by _geRam_DebugAllocate(), and geRam_AddAllocation().

int32 geRam_MaximumUsed = 0
 

Definition at line 196 of file Ram.c.

Referenced by _geRam_DebugAllocate(), _geRam_DebugRealloc(), and geRam_AddAllocation().

int32 geRam_NumberOfAllocations = 0
 

Definition at line 197 of file Ram.c.

Referenced by _geRam_DebugAllocate(), geRam_AddAllocation(), and geRam_Free_().

char MemStamp[] = {"!CHECKME!"} [static]
 

Definition at line 201 of file Ram.c.

Referenced by geRam_IsValidPtr(), geRam_SetupBlock(), and ram_verify_block().

const int MemStampSize = sizeof (MemStamp) [static]
 

Definition at line 202 of file Ram.c.

Referenced by geRam_IsValidPtr(), geRam_SetupBlock(), and ram_verify_block().

const int SizeSize = sizeof (uint32) [static]
 

Definition at line 203 of file Ram.c.

Referenced by geRam_IsValidPtr(), geRam_SetupBlock(), and ram_verify_block().

void* StupidUnusedPointer
 

Definition at line 51 of file Ram.c.


Generated on Tue Sep 30 12:37:59 2003 for GTestAndEngine by doxygen 1.3.2