src/misc/extra/extraUtilMemory.c File Reference

#include "extra.h"
Include dependency graph for extraUtilMemory.c:

Go to the source code of this file.

Data Structures

struct  Extra_MmFixed_t_
struct  Extra_MmFlex_t_
struct  Extra_MmStep_t_

Functions

Extra_MmFixed_tExtra_MmFixedStart (int nEntrySize)
void Extra_MmFixedPrint (Extra_MmFixed_t *p)
void Extra_MmFixedStop (Extra_MmFixed_t *p)
char * Extra_MmFixedEntryFetch (Extra_MmFixed_t *p)
void Extra_MmFixedEntryRecycle (Extra_MmFixed_t *p, char *pEntry)
void Extra_MmFixedRestart (Extra_MmFixed_t *p)
int Extra_MmFixedReadMemUsage (Extra_MmFixed_t *p)
int Extra_MmFixedReadMaxEntriesUsed (Extra_MmFixed_t *p)
Extra_MmFlex_tExtra_MmFlexStart ()
void Extra_MmFlexPrint (Extra_MmFlex_t *p)
void Extra_MmFlexStop (Extra_MmFlex_t *p)
char * Extra_MmFlexEntryFetch (Extra_MmFlex_t *p, int nBytes)
int Extra_MmFlexReadMemUsage (Extra_MmFlex_t *p)
Extra_MmStep_tExtra_MmStepStart (int nSteps)
void Extra_MmStepStop (Extra_MmStep_t *p)
char * Extra_MmStepEntryFetch (Extra_MmStep_t *p, int nBytes)
void Extra_MmStepEntryRecycle (Extra_MmStep_t *p, char *pEntry, int nBytes)
int Extra_MmStepReadMemUsage (Extra_MmStep_t *p)

Function Documentation

char* Extra_MmFixedEntryFetch ( Extra_MmFixed_t p  ) 

Function*************************************************************

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 199 of file extraUtilMemory.c.

00200 {
00201     char * pTemp;
00202     int i;
00203 
00204     // check if there are still free entries
00205     if ( p->nEntriesUsed == p->nEntriesAlloc )
00206     { // need to allocate more entries
00207         assert( p->pEntriesFree == NULL );
00208         if ( p->nChunks == p->nChunksAlloc )
00209         {
00210             p->nChunksAlloc *= 2;
00211             p->pChunks = REALLOC( char *, p->pChunks, p->nChunksAlloc ); 
00212         }
00213         p->pEntriesFree = ALLOC( char, p->nEntrySize * p->nChunkSize );
00214         p->nMemoryAlloc += p->nEntrySize * p->nChunkSize;
00215         // transform these entries into a linked list
00216         pTemp = p->pEntriesFree;
00217         for ( i = 1; i < p->nChunkSize; i++ )
00218         {
00219             *((char **)pTemp) = pTemp + p->nEntrySize;
00220             pTemp += p->nEntrySize;
00221         }
00222         // set the last link
00223         *((char **)pTemp) = NULL;
00224         // add the chunk to the chunk storage
00225         p->pChunks[ p->nChunks++ ] = p->pEntriesFree;
00226         // add to the number of entries allocated
00227         p->nEntriesAlloc += p->nChunkSize;
00228     }
00229     // incrememt the counter of used entries
00230     p->nEntriesUsed++;
00231     if ( p->nEntriesMax < p->nEntriesUsed )
00232         p->nEntriesMax = p->nEntriesUsed;
00233     // return the first entry in the free entry list
00234     pTemp = p->pEntriesFree;
00235     p->pEntriesFree = *((char **)pTemp);
00236     return pTemp;
00237 }

void Extra_MmFixedEntryRecycle ( Extra_MmFixed_t p,
char *  pEntry 
)

Function*************************************************************

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 250 of file extraUtilMemory.c.

00251 {
00252     // decrement the counter of used entries
00253     p->nEntriesUsed--;
00254     // add the entry to the linked list of free entries
00255     *((char **)pEntry) = p->pEntriesFree;
00256     p->pEntriesFree = pEntry;
00257 }

void Extra_MmFixedPrint ( Extra_MmFixed_t p  ) 

Function*************************************************************

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 158 of file extraUtilMemory.c.

00159 {
00160     printf( "Fixed memory manager: Entry = %5d. Chunk = %5d. Chunks used = %5d.\n",
00161         p->nEntrySize, p->nChunkSize, p->nChunks );
00162     printf( "   Entries used = %8d. Entries peak = %8d. Memory used = %8d. Memory alloc = %8d.\n",
00163         p->nEntriesUsed, p->nEntriesMax, p->nEntrySize * p->nEntriesUsed, p->nMemoryAlloc );
00164 }

int Extra_MmFixedReadMaxEntriesUsed ( Extra_MmFixed_t p  ) 

Function*************************************************************

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 324 of file extraUtilMemory.c.

00325 {
00326     return p->nEntriesMax;
00327 }

int Extra_MmFixedReadMemUsage ( Extra_MmFixed_t p  ) 

Function*************************************************************

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 308 of file extraUtilMemory.c.

00309 {
00310     return p->nMemoryAlloc;
00311 }

void Extra_MmFixedRestart ( Extra_MmFixed_t p  ) 

Function*************************************************************

Synopsis []

Description [Relocates all the memory except the first chunk.]

SideEffects []

SeeAlso []

Definition at line 270 of file extraUtilMemory.c.

00271 {
00272     int i;
00273     char * pTemp;
00274 
00275     // deallocate all chunks except the first one
00276     for ( i = 1; i < p->nChunks; i++ )
00277         free( p->pChunks[i] );
00278     p->nChunks = 1;
00279     // transform these entries into a linked list
00280     pTemp = p->pChunks[0];
00281     for ( i = 1; i < p->nChunkSize; i++ )
00282     {
00283         *((char **)pTemp) = pTemp + p->nEntrySize;
00284         pTemp += p->nEntrySize;
00285     }
00286     // set the last link
00287     *((char **)pTemp) = NULL;
00288     // set the free entry list
00289     p->pEntriesFree  = p->pChunks[0];
00290     // set the correct statistics
00291     p->nMemoryAlloc  = p->nEntrySize * p->nChunkSize;
00292     p->nMemoryUsed   = 0;
00293     p->nEntriesAlloc = p->nChunkSize;
00294     p->nEntriesUsed  = 0;
00295 }

Extra_MmFixed_t* Extra_MmFixedStart ( int  nEntrySize  ) 

AutomaticStart AutomaticEnd Function*************************************************************

Synopsis [Allocates memory pieces of fixed size.]

Description [The size of the chunk is computed as the minimum of 1024 entries and 64K. Can only work with entry size at least 4 byte long.]

SideEffects []

SeeAlso []

Definition at line 119 of file extraUtilMemory.c.

00120 {
00121     Extra_MmFixed_t * p;
00122 
00123     p = ALLOC( Extra_MmFixed_t, 1 );
00124     memset( p, 0, sizeof(Extra_MmFixed_t) );
00125 
00126     p->nEntrySize    = nEntrySize;
00127     p->nEntriesAlloc = 0;
00128     p->nEntriesUsed  = 0;
00129     p->pEntriesFree  = NULL;
00130 
00131     if ( nEntrySize * (1 << 10) < (1<<16) )
00132         p->nChunkSize = (1 << 10);
00133     else
00134         p->nChunkSize = (1<<16) / nEntrySize;
00135     if ( p->nChunkSize < 8 )
00136         p->nChunkSize = 8;
00137 
00138     p->nChunksAlloc  = 64;
00139     p->nChunks       = 0;
00140     p->pChunks       = ALLOC( char *, p->nChunksAlloc );
00141 
00142     p->nMemoryUsed   = 0;
00143     p->nMemoryAlloc  = 0;
00144     return p;
00145 }

void Extra_MmFixedStop ( Extra_MmFixed_t p  ) 

Function*************************************************************

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 177 of file extraUtilMemory.c.

00178 {
00179     int i;
00180     if ( p == NULL )
00181         return;
00182     for ( i = 0; i < p->nChunks; i++ )
00183         free( p->pChunks[i] );
00184     free( p->pChunks );
00185     free( p );
00186 }

char* Extra_MmFlexEntryFetch ( Extra_MmFlex_t p,
int  nBytes 
)

Function*************************************************************

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 415 of file extraUtilMemory.c.

00416 {
00417     char * pTemp;
00418     // check if there are still free entries
00419     if ( p->pCurrent == NULL || p->pCurrent + nBytes > p->pEnd )
00420     { // need to allocate more entries
00421         if ( p->nChunks == p->nChunksAlloc )
00422         {
00423             p->nChunksAlloc *= 2;
00424             p->pChunks = REALLOC( char *, p->pChunks, p->nChunksAlloc ); 
00425         }
00426         if ( nBytes > p->nChunkSize )
00427         {
00428             // resize the chunk size if more memory is requested than it can give
00429             // (ideally, this should never happen)
00430             p->nChunkSize = 2 * nBytes;
00431         }
00432         p->pCurrent = ALLOC( char, p->nChunkSize );
00433         p->pEnd     = p->pCurrent + p->nChunkSize;
00434         p->nMemoryAlloc += p->nChunkSize;
00435         // add the chunk to the chunk storage
00436         p->pChunks[ p->nChunks++ ] = p->pCurrent;
00437     }
00438     assert( p->pCurrent + nBytes <= p->pEnd );
00439     // increment the counter of used entries
00440     p->nEntriesUsed++;
00441     // keep track of the memory used
00442     p->nMemoryUsed += nBytes;
00443     // return the next entry
00444     pTemp = p->pCurrent;
00445     p->pCurrent += nBytes;
00446     return pTemp;
00447 }

void Extra_MmFlexPrint ( Extra_MmFlex_t p  ) 

Function*************************************************************

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 373 of file extraUtilMemory.c.

00374 {
00375     printf( "Flexible memory manager: Chunk size = %d. Chunks used = %d.\n",
00376         p->nChunkSize, p->nChunks );
00377     printf( "   Entries used = %d. Memory used = %d. Memory alloc = %d.\n",
00378         p->nEntriesUsed, p->nMemoryUsed, p->nMemoryAlloc );
00379 }

int Extra_MmFlexReadMemUsage ( Extra_MmFlex_t p  ) 

Function*************************************************************

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 460 of file extraUtilMemory.c.

00461 {
00462     return p->nMemoryAlloc;
00463 }

Extra_MmFlex_t* Extra_MmFlexStart (  ) 

Function*************************************************************

Synopsis [Allocates entries of flexible size.]

Description [Can only work with entry size at least 4 byte long.]

SideEffects []

SeeAlso []

Definition at line 341 of file extraUtilMemory.c.

00342 {
00343     Extra_MmFlex_t * p;
00344 //printf( "allocing flex\n" );
00345     p = ALLOC( Extra_MmFlex_t, 1 );
00346     memset( p, 0, sizeof(Extra_MmFlex_t) );
00347 
00348     p->nEntriesUsed  = 0;
00349     p->pCurrent      = NULL;
00350     p->pEnd          = NULL;
00351 
00352     p->nChunkSize    = (1 << 10);
00353     p->nChunksAlloc  = 64;
00354     p->nChunks       = 0;
00355     p->pChunks       = ALLOC( char *, p->nChunksAlloc );
00356 
00357     p->nMemoryUsed   = 0;
00358     p->nMemoryAlloc  = 0;
00359     return p;
00360 }

void Extra_MmFlexStop ( Extra_MmFlex_t p  ) 

Function*************************************************************

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 392 of file extraUtilMemory.c.

00393 {
00394     int i;
00395     if ( p == NULL )
00396         return;
00397 //printf( "deleting flex\n" );
00398     for ( i = 0; i < p->nChunks; i++ )
00399         free( p->pChunks[i] );
00400     free( p->pChunks );
00401     free( p );
00402 }

char* Extra_MmStepEntryFetch ( Extra_MmStep_t p,
int  nBytes 
)

Function*************************************************************

Synopsis [Creates the entry.]

Description []

SideEffects []

SeeAlso []

Definition at line 552 of file extraUtilMemory.c.

00553 {
00554     if ( nBytes == 0 )
00555         return NULL;
00556     if ( nBytes > p->nMapSize )
00557     {
00558 //        printf( "Allocating %d bytes.\n", nBytes );
00559 /*
00560         if ( p->nLargeChunks == p->nLargeChunksAlloc )
00561         {
00562             if ( p->nLargeChunksAlloc == 0 )
00563                 p->nLargeChunksAlloc = 5;
00564             p->nLargeChunksAlloc *= 2;
00565             p->pLargeChunks = REALLOC( char *, p->pLargeChunks, p->nLargeChunksAlloc ); 
00566         }
00567         p->pLargeChunks[ p->nLargeChunks++ ] = ALLOC( char, nBytes );
00568         return p->pLargeChunks[ p->nLargeChunks - 1 ];
00569 */
00570         return ALLOC( char, nBytes );
00571     }
00572     return Extra_MmFixedEntryFetch( p->pMap[nBytes] );
00573 }

void Extra_MmStepEntryRecycle ( Extra_MmStep_t p,
char *  pEntry,
int  nBytes 
)

Function*************************************************************

Synopsis [Recycles the entry.]

Description []

SideEffects []

SeeAlso []

Definition at line 587 of file extraUtilMemory.c.

00588 {
00589     if ( nBytes == 0 )
00590         return;
00591     if ( nBytes > p->nMapSize )
00592     {
00593         free( pEntry );
00594         return;
00595     }
00596     Extra_MmFixedEntryRecycle( p->pMap[nBytes], pEntry );
00597 }

int Extra_MmStepReadMemUsage ( Extra_MmStep_t p  ) 

Function*************************************************************

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 610 of file extraUtilMemory.c.

00611 {
00612     int i, nMemTotal = 0;
00613     for ( i = 0; i < p->nMems; i++ )
00614         nMemTotal += p->pMems[i]->nMemoryAlloc;
00615     return nMemTotal;
00616 }

Extra_MmStep_t* Extra_MmStepStart ( int  nSteps  ) 

Function*************************************************************

Synopsis [Starts the hierarchical memory manager.]

Description [This manager can allocate entries of any size. Iternally they are mapped into the entries with the number of bytes equal to the power of 2. The smallest entry size is 8 bytes. The next one is 16 bytes etc. So, if the user requests 6 bytes, he gets 8 byte entry. If we asks for 25 bytes, he gets 32 byte entry etc. The input parameters "nSteps" says how many fixed memory managers are employed internally. Calling this procedure with nSteps equal to 10 results in 10 hierarchically arranged internal memory managers, which can allocate up to 4096 (1Kb) entries. Requests for larger entries are handed over to malloc() and then free()ed.]

SideEffects []

SeeAlso []

Definition at line 489 of file extraUtilMemory.c.

00490 {
00491     Extra_MmStep_t * p;
00492     int i, k;
00493     p = ALLOC( Extra_MmStep_t, 1 );
00494     memset( p, 0, sizeof(Extra_MmStep_t) );
00495     p->nMems = nSteps;
00496     // start the fixed memory managers
00497     p->pMems = ALLOC( Extra_MmFixed_t *, p->nMems );
00498     for ( i = 0; i < p->nMems; i++ )
00499         p->pMems[i] = Extra_MmFixedStart( (8<<i) );
00500     // set up the mapping of the required memory size into the corresponding manager
00501     p->nMapSize = (4<<p->nMems);
00502     p->pMap = ALLOC( Extra_MmFixed_t *, p->nMapSize+1 );
00503     p->pMap[0] = NULL;
00504     for ( k = 1; k <= 4; k++ )
00505         p->pMap[k] = p->pMems[0];
00506     for ( i = 0; i < p->nMems; i++ )
00507         for ( k = (4<<i)+1; k <= (8<<i); k++ )
00508             p->pMap[k] = p->pMems[i];
00509 //for ( i = 1; i < 100; i ++ )
00510 //printf( "%10d: size = %10d\n", i, p->pMap[i]->nEntrySize );
00511     return p;
00512 }

void Extra_MmStepStop ( Extra_MmStep_t p  ) 

Function*************************************************************

Synopsis [Stops the memory manager.]

Description []

SideEffects []

SeeAlso []

Definition at line 525 of file extraUtilMemory.c.

00526 {
00527     int i;
00528     for ( i = 0; i < p->nMems; i++ )
00529         Extra_MmFixedStop( p->pMems[i] );
00530 //    if ( p->pLargeChunks ) 
00531 //    {
00532 //      for ( i = 0; i < p->nLargeChunks; i++ )
00533 //          free( p->pLargeChunks[i] );
00534 //      free( p->pLargeChunks );
00535 //    }
00536     free( p->pMems );
00537     free( p->pMap );
00538     free( p );
00539 }


Generated on Tue Jan 5 12:19:15 2010 for abc70930 by  doxygen 1.6.1