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

mempool.c File Reference

#include <string.h>
#include <assert.h>
#include "mempool.h"
#include "ram.h"

Go to the source code of this file.

Compounds

struct  MemBlock
struct  MemPool

Defines

#define RamCalloc(size)   geRam_AllocateClear(size)
#define RamFree(mem)   geRam_Free(mem)
#define RamRealloc(mem, size)   geRam_Realloc(mem,size)
#define memclear(mem, size)   memset(mem,0,size);

Typedefs

typedef MemBlock MemBlock

Functions

int MemBlock_IsValid (MemBlock *mb)
int MemPool_IsValid (MemPool *Pool)
MemPoolMemPool_Create (int HunkLength, int NumHunks, int AutoExtendNumItems)
void MemPool_Destroy (MemPool **pPool)
void * MemPool_GetHunk (MemPool *Pool)
void MemPool_Reset (MemPool *Pool)
int MemPool_Extend (MemPool *Pool, int NumHunks)
int MemPool_FreeHunk (MemPool *Pool, void *Hunk)
void * MemPool_WalkNext (MemPool *Pool, void *Hunk)

Variables

MemBlockWalkMemBlock
char * WalkPtr
char * WalkPtrEnd
MemPoolLastPool


Define Documentation

#define memclear mem,
size   )     memset(mem,0,size);
 

Definition at line 46 of file mempool.c.

#define RamCalloc size   )     geRam_AllocateClear(size)
 

Definition at line 41 of file mempool.c.

Referenced by MemPool_Create(), and MemPool_Extend().

#define RamFree mem   )     geRam_Free(mem)
 

Definition at line 42 of file mempool.c.

Referenced by MemPool_Create(), MemPool_Destroy(), and MemPool_Extend().

#define RamRealloc mem,
size   )     geRam_Realloc(mem,size)
 

Definition at line 43 of file mempool.c.

Referenced by MemPool_FreeHunk().


Typedef Documentation

typedef struct MemBlock MemBlock
 

Definition at line 52 of file mempool.c.

Referenced by MemPool_Destroy(), MemPool_Extend(), MemPool_GetHunk(), and MemPool_Reset().


Function Documentation

int MemBlock_IsValid MemBlock mb  ) 
 

Definition at line 75 of file mempool.c.

References geRam_IsValidPtr(), MemBlock::MemBase, MemBlock::MemFree, MemBlock::MemLength, MemBlock::MemPtr, and MemBlock::Next.

Referenced by MemPool_IsValid().

00076 {
00077         if ( ! mb ) return 0;
00078         if ( mb->Next )
00079         {
00080                 if ( ! MemBlock_IsValid(mb->Next) )
00081                         return 0;
00082         }
00083 
00084         assert( geRam_IsValidPtr(mb->MemBase) );
00085 
00086         if ( mb->MemFree < 0 || mb->MemFree > mb->MemLength )
00087                 return 0;
00088         if ( mb->MemPtr != mb->MemBase + (mb->MemLength - mb->MemFree) )
00089                 return 0;
00090 return 1;
00091 }

MemPool* MemPool_Create int  HunkLength,
int  NumHunks,
int  AutoExtendNumItems
 

Definition at line 107 of file mempool.c.

References MemPool::AutoExtendNumItems, MemPool::CurMemBlock, MemPool::FreedHunks, MemPool::HunkLength, MemPool::MaxNumFreedHunks, MemPool::MemList, MemPool_Extend(), NULL, MemPool::NumFreedHunks, MemPool::NumItemsActive, RamCalloc, and RamFree.

Referenced by geBitmap_Start(), List_Start(), PalCreate_Start(), and Palettize_Start().

00108 {
00109 MemPool * Ret;
00110 
00111 if ( (Ret = RamCalloc(sizeof(MemPool))) == NULL )
00112   return(NULL);
00113 
00114 Ret->HunkLength = (HunkLength+3)&(~3);
00115 Ret->CurMemBlock = NULL;
00116 Ret->MemList = NULL;
00117 Ret->NumFreedHunks = 0;
00118 Ret->MaxNumFreedHunks = 16;
00119 Ret->AutoExtendNumItems = AutoExtendNumItems;
00120 Ret->NumItemsActive = 0;
00121 
00122 if ( (Ret->FreedHunks = RamCalloc(Ret->MaxNumFreedHunks*sizeof(void *))) == NULL )
00123 {
00124         RamFree(Ret);
00125         return(NULL);
00126 }
00127 
00128 if ( ! MemPool_Extend(Ret,NumHunks) )
00129 {
00130         RamFree(Ret->FreedHunks);
00131         RamFree(Ret);
00132         return(NULL);
00133 }
00134 
00135 return(Ret);
00136 }

void MemPool_Destroy MemPool **  pPool  ) 
 

Definition at line 138 of file mempool.c.

References MemPool::FreedHunks, MemBlock::MemBase, MemBlock, MemPool::MemList, MemBlock::Next, NULL, and RamFree.

Referenced by geBitmap_Stop(), List_Stop(), PalCreate_Stop(), and Palettize_Stop().

00139 {
00140 MemBlock * CurMemBlock;
00141 MemBlock * NextMemBlock;
00142 MemPool * Pool;
00143 
00144         assert(pPool);
00145 
00146         Pool = *pPool;
00147 
00148         if ( Pool == NULL ) return;
00149 
00150         CurMemBlock = Pool->MemList;
00151         while(CurMemBlock)
00152         {
00153                 RamFree(CurMemBlock->MemBase);
00154                 NextMemBlock = CurMemBlock->Next;
00155                 RamFree(CurMemBlock);
00156                 CurMemBlock = NextMemBlock;
00157         }
00158 
00159         RamFree(Pool->FreedHunks);
00160         RamFree(Pool);
00161 
00162         *pPool = NULL;
00163 }

int MemPool_Extend MemPool Pool,
int  NumHunks
 

Definition at line 220 of file mempool.c.

References MemPool::CurMemBlock, MemPool::HunkLength, MemBlock::MemBase, MemBlock, MemBlock::MemFree, MemBlock::MemLength, MemPool::MemList, MemBlock::MemPtr, MemBlock::Next, NULL, RamCalloc, and RamFree.

Referenced by MemPool_Create(), and MemPool_GetHunk().

00221 {
00222 MemBlock * NewMemBlock;
00223 
00224         NewMemBlock = Pool->MemList;
00225         while ( NewMemBlock )
00226         {
00227                 if ( NewMemBlock->MemFree > Pool->HunkLength )
00228                 {
00229                         Pool->CurMemBlock = NewMemBlock;
00230                         return(1);
00231                 }
00232                 NewMemBlock = NewMemBlock->Next;
00233         }
00234 
00235         if ( (NewMemBlock = RamCalloc(sizeof(MemBlock))) == NULL )
00236                 return(0);
00237 
00238         NewMemBlock->MemLength = NumHunks * Pool->HunkLength;
00239         NewMemBlock->MemFree = NewMemBlock->MemLength;
00240 
00241         if ( (NewMemBlock->MemBase = RamCalloc(NewMemBlock->MemLength)) == NULL )
00242         {
00243                 RamFree(NewMemBlock);
00244                 return(0);
00245         }
00246 
00247         NewMemBlock->MemPtr = NewMemBlock->MemBase;
00248 
00249         NewMemBlock->Next = Pool->MemList;
00250         Pool->MemList = NewMemBlock;
00251 
00252         Pool->CurMemBlock = NewMemBlock;
00253 
00254 return(1);
00255 }

int MemPool_FreeHunk MemPool Pool,
void *  Hunk
 

Definition at line 257 of file mempool.c.

References MemPool::FreedHunks, MemPool::HunkLength, MemPool::MaxNumFreedHunks, memclear, MemPool::NumFreedHunks, MemPool::NumItemsActive, and RamRealloc.

Referenced by geBitmap_Destroy_Base(), Hash_DeleteNode(), Hash_Destroy(), Link_Destroy(), Link_Pop(), List_CutHead(), List_CutTail(), List_Destroy(), and List_FreeNode().

00258 {
00259         assert( Pool );
00260         // <> assert Hunk is in Pool !
00261 
00262         // we must use this growing array for Freed Hunks, because
00263         //  to use a linked list of freed hunks, we'd need to use MemPool !!!
00264 
00265         if ( Pool->NumFreedHunks >= Pool->MaxNumFreedHunks )
00266         {
00267                 Pool->MaxNumFreedHunks <<= 1;
00268 
00269                 Pool->FreedHunks = RamRealloc(Pool->FreedHunks,Pool->MaxNumFreedHunks*sizeof(void *));
00270                 assert(Pool->FreedHunks);
00271                 if ( ! Pool->FreedHunks )
00272                         return(0);
00273         }
00274 
00275         Pool->FreedHunks[Pool->NumFreedHunks] = Hunk;
00276         Pool->NumFreedHunks++;
00277 
00278         Pool->NumItemsActive--;
00279 
00280         memclear(Hunk,Pool->HunkLength);
00281 
00282 return(1);
00283 }

void* MemPool_GetHunk MemPool Pool  ) 
 

Definition at line 165 of file mempool.c.

References MemPool::AutoExtendNumItems, MemPool::CurMemBlock, MemPool::FreedHunks, MemPool::HunkLength, MemBlock, MemBlock::MemFree, MemPool_Extend(), MemBlock::MemPtr, NULL, MemPool::NumFreedHunks, and MemPool::NumItemsActive.

Referenced by addHash(), addOctNode(), closestPalInit(), createPaletteFast(), createPaletteGoodSub(), findClosestPal(), geBitmap_Create_Base(), Hash_Add(), Hash_Create(), Link_Create(), Link_Push(), List_AddHead(), List_AddTail(), and List_Create().

00166 {
00167 void * Ret;
00168 MemBlock * CurMemBlock;
00169 
00170         if ( Pool->NumFreedHunks > 0 )
00171         {
00172                 Pool->NumFreedHunks--;
00173                 Ret = Pool->FreedHunks[Pool->NumFreedHunks];
00174 
00175                 goto GotHunk;
00176         }
00177 
00178         if ( (CurMemBlock = Pool->CurMemBlock) == NULL )
00179                 return(NULL);
00180 
00181         if ( CurMemBlock->MemFree < Pool->HunkLength )
00182         {
00183                 if ( ! MemPool_Extend(Pool,Pool->AutoExtendNumItems) )
00184                                 return(NULL);
00185                 CurMemBlock = Pool->CurMemBlock;
00186         }
00187 
00188         Ret = (void *) CurMemBlock->MemPtr;
00189 
00190         CurMemBlock->MemFree -= Pool->HunkLength;
00191         CurMemBlock->MemPtr     += Pool->HunkLength;
00192 
00193 GotHunk:
00194 
00195 // clear at alloc & reset & Free, not in Get
00196 //      memclear(Ret,Pool->HunkLength);
00197 
00198         Pool->NumItemsActive++;
00199 
00200 return(Ret);
00201 }

int MemPool_IsValid MemPool Pool  ) 
 

Definition at line 93 of file mempool.c.

References MemPool::AutoExtendNumItems, MemPool::HunkLength, MemPool::MaxNumFreedHunks, MemBlock_IsValid(), MemPool::MemList, MemPool::NumFreedHunks, and MemPool::NumItemsActive.

00094 {
00095         if ( ! Pool ) return 0;
00096         if ( Pool->NumFreedHunks < 0 || Pool->AutoExtendNumItems < 0
00097                 || Pool->MaxNumFreedHunks < 0 || Pool->NumItemsActive || Pool->HunkLength )
00098                 return 0;
00099         if ( Pool->NumFreedHunks >= Pool->MaxNumFreedHunks ) return 0;
00100 
00101         // make sure curmemblock is in the memblock list
00102         // <> make sure the freedhunks are in the memblocks (this is the most likely error)
00103 
00104         return MemBlock_IsValid(Pool->MemList);
00105 }

void MemPool_Reset MemPool Pool  ) 
 

Definition at line 203 of file mempool.c.

References MemPool::CurMemBlock, MemBlock::MemBase, MemBlock, memclear, MemBlock::MemFree, MemBlock::MemLength, MemPool::MemList, MemBlock::MemPtr, MemBlock::Next, MemPool::NumFreedHunks, and MemPool::NumItemsActive.

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

00204 {
00205 MemBlock * CurMemBlock;
00206 
00207         Pool->NumFreedHunks = 0;
00208         Pool->NumItemsActive = 0;
00209         Pool->CurMemBlock = Pool->MemList;
00210 
00211         for( CurMemBlock = Pool->MemList; CurMemBlock ; CurMemBlock = CurMemBlock->Next)
00212         {
00213                 CurMemBlock->MemFree = CurMemBlock->MemLength;
00214                 CurMemBlock->MemPtr = CurMemBlock->MemBase;
00215 
00216                 memclear(CurMemBlock->MemBase,CurMemBlock->MemLength);
00217         }
00218 }

void* MemPool_WalkNext MemPool Pool,
void *  Hunk
 

Definition at line 290 of file mempool.c.

References MemPool::FreedHunks, MemPool::HunkLength, LastPool, MemBlock::MemBase, MemBlock::MemFree, MemBlock::MemLength, MemBlock::Next, NULL, MemPool::NumFreedHunks, WalkMemBlock, WalkPtr, and WalkPtrEnd.

00291 {
00292         if ( ! Pool ) return NULL;
00293         if ( ! Hunk )
00294         {
00295                 LastPool = Pool;
00296                 WalkMemBlock = Pool->MemList;
00297                 if ( ! WalkMemBlock ) return NULL;
00298                 WalkPtr = WalkMemBlock->MemBase;
00299                 WalkPtrEnd = WalkPtr + (WalkMemBlock->MemLength - WalkMemBlock->MemFree);
00300         }
00301         else
00302         {
00303                 assert( Pool == LastPool );
00304                 assert( WalkPtr == Hunk );
00305                 rewalk:
00306                 WalkPtr += Pool->HunkLength;
00307                 if ( WalkPtr >= WalkPtrEnd )
00308                 {
00309                         WalkMemBlock = WalkMemBlock->Next;
00310                         if ( ! WalkMemBlock ) return NULL;
00311                         WalkPtr = WalkMemBlock->MemBase;
00312                         WalkPtrEnd = WalkPtr + (WalkMemBlock->MemLength - WalkMemBlock->MemFree);
00313                 }
00314         }
00315 
00316         // must check to see if WalkPtr is in the FreedHunks ! 
00317         // this is so slow it makes this function worthless!
00318         {
00319         int i;
00320                 for(i=0;i<Pool->NumFreedHunks;i++)
00321                 {
00322                         if ( WalkPtr == Pool->FreedHunks[i] )
00323                         {
00324                                 Hunk = WalkPtr;
00325                                 goto rewalk;
00326                         }
00327                 }
00328         }
00329 
00330 return WalkPtr;
00331 }


Variable Documentation

MemPool* LastPool [static]
 

Definition at line 288 of file mempool.c.

Referenced by MemPool_WalkNext().

MemBlock* WalkMemBlock [static]
 

Definition at line 286 of file mempool.c.

Referenced by MemPool_WalkNext().

char* WalkPtr [static]
 

Definition at line 287 of file mempool.c.

Referenced by MemPool_WalkNext().

char * WalkPtrEnd [static]
 

Definition at line 287 of file mempool.c.

Referenced by MemPool_WalkNext().


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