src/sat/msat/msatMem.c File Reference

#include "msatInt.h"
Include dependency graph for msatMem.c:

Go to the source code of this file.

Data Structures

struct  Msat_MmFixed_t_
struct  Msat_MmFlex_t_
struct  Msat_MmStep_t_

Functions

Msat_MmFixed_tMsat_MmFixedStart (int nEntrySize)
void Msat_MmFixedStop (Msat_MmFixed_t *p, int fVerbose)
char * Msat_MmFixedEntryFetch (Msat_MmFixed_t *p)
void Msat_MmFixedEntryRecycle (Msat_MmFixed_t *p, char *pEntry)
void Msat_MmFixedRestart (Msat_MmFixed_t *p)
int Msat_MmFixedReadMemUsage (Msat_MmFixed_t *p)
Msat_MmFlex_tMsat_MmFlexStart ()
void Msat_MmFlexStop (Msat_MmFlex_t *p, int fVerbose)
char * Msat_MmFlexEntryFetch (Msat_MmFlex_t *p, int nBytes)
int Msat_MmFlexReadMemUsage (Msat_MmFlex_t *p)
Msat_MmStep_tMsat_MmStepStart (int nSteps)
void Msat_MmStepStop (Msat_MmStep_t *p, int fVerbose)
char * Msat_MmStepEntryFetch (Msat_MmStep_t *p, int nBytes)
void Msat_MmStepEntryRecycle (Msat_MmStep_t *p, char *pEntry, int nBytes)
int Msat_MmStepReadMemUsage (Msat_MmStep_t *p)

Function Documentation

char* Msat_MmFixedEntryFetch ( Msat_MmFixed_t p  ) 

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 158 of file msatMem.c.

00159 {
00160     char * pTemp;
00161     int i;
00162 
00163     // check if there are still free entries
00164     if ( p->nEntriesUsed == p->nEntriesAlloc )
00165     { // need to allocate more entries
00166         assert( p->pEntriesFree == NULL );
00167         if ( p->nChunks == p->nChunksAlloc )
00168         {
00169             p->nChunksAlloc *= 2;
00170             p->pChunks = REALLOC( char *, p->pChunks, p->nChunksAlloc ); 
00171         }
00172         p->pEntriesFree = ALLOC( char, p->nEntrySize * p->nChunkSize );
00173         p->nMemoryAlloc += p->nEntrySize * p->nChunkSize;
00174         // transform these entries into a linked list
00175         pTemp = p->pEntriesFree;
00176         for ( i = 1; i < p->nChunkSize; i++ )
00177         {
00178             *((char **)pTemp) = pTemp + p->nEntrySize;
00179             pTemp += p->nEntrySize;
00180         }
00181         // set the last link
00182         *((char **)pTemp) = NULL;
00183         // add the chunk to the chunk storage
00184         p->pChunks[ p->nChunks++ ] = p->pEntriesFree;
00185         // add to the number of entries allocated
00186         p->nEntriesAlloc += p->nChunkSize;
00187     }
00188     // incrememt the counter of used entries
00189     p->nEntriesUsed++;
00190     if ( p->nEntriesMax < p->nEntriesUsed )
00191         p->nEntriesMax = p->nEntriesUsed;
00192     // return the first entry in the free entry list
00193     pTemp = p->pEntriesFree;
00194     p->pEntriesFree = *((char **)pTemp);
00195     return pTemp;
00196 }

void Msat_MmFixedEntryRecycle ( Msat_MmFixed_t p,
char *  pEntry 
)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 209 of file msatMem.c.

00210 {
00211     // decrement the counter of used entries
00212     p->nEntriesUsed--;
00213     // add the entry to the linked list of free entries
00214     *((char **)pEntry) = p->pEntriesFree;
00215     p->pEntriesFree = pEntry;
00216 }

int Msat_MmFixedReadMemUsage ( Msat_MmFixed_t p  ) 

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 267 of file msatMem.c.

00268 {
00269     return p->nMemoryAlloc;
00270 }

void Msat_MmFixedRestart ( Msat_MmFixed_t p  ) 

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

Synopsis []

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

SideEffects []

SeeAlso []

Definition at line 229 of file msatMem.c.

00230 {
00231     int i;
00232     char * pTemp;
00233 
00234     // deallocate all chunks except the first one
00235     for ( i = 1; i < p->nChunks; i++ )
00236         free( p->pChunks[i] );
00237     p->nChunks = 1;
00238     // transform these entries into a linked list
00239     pTemp = p->pChunks[0];
00240     for ( i = 1; i < p->nChunkSize; i++ )
00241     {
00242         *((char **)pTemp) = pTemp + p->nEntrySize;
00243         pTemp += p->nEntrySize;
00244     }
00245     // set the last link
00246     *((char **)pTemp) = NULL;
00247     // set the free entry list
00248     p->pEntriesFree  = p->pChunks[0];
00249     // set the correct statistics
00250     p->nMemoryAlloc  = p->nEntrySize * p->nChunkSize;
00251     p->nMemoryUsed   = 0;
00252     p->nEntriesAlloc = p->nChunkSize;
00253     p->nEntriesUsed  = 0;
00254 }

Msat_MmFixed_t* Msat_MmFixedStart ( int  nEntrySize  ) 

FUNCTION DEFINITIONS ///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 90 of file msatMem.c.

00091 {
00092     Msat_MmFixed_t * p;
00093 
00094     p = ALLOC( Msat_MmFixed_t, 1 );
00095     memset( p, 0, sizeof(Msat_MmFixed_t) );
00096 
00097     p->nEntrySize    = nEntrySize;
00098     p->nEntriesAlloc = 0;
00099     p->nEntriesUsed  = 0;
00100     p->pEntriesFree  = NULL;
00101 
00102     if ( nEntrySize * (1 << 10) < (1<<16) )
00103         p->nChunkSize = (1 << 10);
00104     else
00105         p->nChunkSize = (1<<16) / nEntrySize;
00106     if ( p->nChunkSize < 8 )
00107         p->nChunkSize = 8;
00108 
00109     p->nChunksAlloc  = 64;
00110     p->nChunks       = 0;
00111     p->pChunks       = ALLOC( char *, p->nChunksAlloc );
00112 
00113     p->nMemoryUsed   = 0;
00114     p->nMemoryAlloc  = 0;
00115     return p;
00116 }

void Msat_MmFixedStop ( Msat_MmFixed_t p,
int  fVerbose 
)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 129 of file msatMem.c.

00130 {
00131     int i;
00132     if ( p == NULL )
00133         return;
00134     if ( fVerbose )
00135     {
00136         printf( "Fixed memory manager: Entry = %5d. Chunk = %5d. Chunks used = %5d.\n",
00137             p->nEntrySize, p->nChunkSize, p->nChunks );
00138         printf( "   Entries used = %8d. Entries peak = %8d. Memory used = %8d. Memory alloc = %8d.\n",
00139             p->nEntriesUsed, p->nEntriesMax, p->nEntrySize * p->nEntriesUsed, p->nMemoryAlloc );
00140     }
00141     for ( i = 0; i < p->nChunks; i++ )
00142         free( p->pChunks[i] );
00143     free( p->pChunks );
00144     free( p );
00145 }

char* Msat_MmFlexEntryFetch ( Msat_MmFlex_t p,
int  nBytes 
)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 346 of file msatMem.c.

00347 {
00348     char * pTemp;
00349     // check if there are still free entries
00350     if ( p->pCurrent == NULL || p->pCurrent + nBytes > p->pEnd )
00351     { // need to allocate more entries
00352         if ( p->nChunks == p->nChunksAlloc )
00353         {
00354             p->nChunksAlloc *= 2;
00355             p->pChunks = REALLOC( char *, p->pChunks, p->nChunksAlloc ); 
00356         }
00357         if ( nBytes > p->nChunkSize )
00358         {
00359             // resize the chunk size if more memory is requested than it can give
00360             // (ideally, this should never happen)
00361             p->nChunkSize = 2 * nBytes;
00362         }
00363         p->pCurrent = ALLOC( char, p->nChunkSize );
00364         p->pEnd     = p->pCurrent + p->nChunkSize;
00365         p->nMemoryAlloc += p->nChunkSize;
00366         // add the chunk to the chunk storage
00367         p->pChunks[ p->nChunks++ ] = p->pCurrent;
00368     }
00369     assert( p->pCurrent + nBytes <= p->pEnd );
00370     // increment the counter of used entries
00371     p->nEntriesUsed++;
00372     // keep track of the memory used
00373     p->nMemoryUsed += nBytes;
00374     // return the next entry
00375     pTemp = p->pCurrent;
00376     p->pCurrent += nBytes;
00377     return pTemp;
00378 }

int Msat_MmFlexReadMemUsage ( Msat_MmFlex_t p  ) 

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 391 of file msatMem.c.

00392 {
00393     return p->nMemoryAlloc;
00394 }

Msat_MmFlex_t* Msat_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 285 of file msatMem.c.

00286 {
00287     Msat_MmFlex_t * p;
00288 
00289     p = ALLOC( Msat_MmFlex_t, 1 );
00290     memset( p, 0, sizeof(Msat_MmFlex_t) );
00291 
00292     p->nEntriesUsed  = 0;
00293     p->pCurrent      = NULL;
00294     p->pEnd          = NULL;
00295 
00296     p->nChunkSize    = (1 << 12);
00297     p->nChunksAlloc  = 64;
00298     p->nChunks       = 0;
00299     p->pChunks       = ALLOC( char *, p->nChunksAlloc );
00300 
00301     p->nMemoryUsed   = 0;
00302     p->nMemoryAlloc  = 0;
00303     return p;
00304 }

void Msat_MmFlexStop ( Msat_MmFlex_t p,
int  fVerbose 
)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 317 of file msatMem.c.

00318 {
00319     int i;
00320     if ( p == NULL )
00321         return;
00322     if ( fVerbose )
00323     {
00324         printf( "Flexible memory manager: Chunk size = %d. Chunks used = %d.\n",
00325             p->nChunkSize, p->nChunks );
00326         printf( "   Entries used = %d. Memory used = %d. Memory alloc = %d.\n",
00327             p->nEntriesUsed, p->nMemoryUsed, p->nMemoryAlloc );
00328     }
00329     for ( i = 0; i < p->nChunks; i++ )
00330         free( p->pChunks[i] );
00331     free( p->pChunks );
00332     free( p );
00333 }

char* Msat_MmStepEntryFetch ( Msat_MmStep_t p,
int  nBytes 
)

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

Synopsis [Creates the entry.]

Description []

SideEffects []

SeeAlso []

Definition at line 476 of file msatMem.c.

00477 {
00478     if ( nBytes == 0 )
00479         return NULL;
00480     if ( nBytes > p->nMapSize )
00481     {
00482 //        printf( "Allocating %d bytes.\n", nBytes );
00483         return ALLOC( char, nBytes );
00484     }
00485     return Msat_MmFixedEntryFetch( p->pMap[nBytes] );
00486 }

void Msat_MmStepEntryRecycle ( Msat_MmStep_t p,
char *  pEntry,
int  nBytes 
)

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

Synopsis [Recycles the entry.]

Description []

SideEffects []

SeeAlso []

Definition at line 500 of file msatMem.c.

00501 {
00502     if ( nBytes == 0 )
00503         return;
00504     if ( nBytes > p->nMapSize )
00505     {
00506         free( pEntry );
00507         return;
00508     }
00509     Msat_MmFixedEntryRecycle( p->pMap[nBytes], pEntry );
00510 }

int Msat_MmStepReadMemUsage ( Msat_MmStep_t p  ) 

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 523 of file msatMem.c.

00524 {
00525     int i, nMemTotal = 0;
00526     for ( i = 0; i < p->nMems; i++ )
00527         nMemTotal += p->pMems[i]->nMemoryAlloc;
00528     return nMemTotal;
00529 }

Msat_MmStep_t* Msat_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 420 of file msatMem.c.

00421 {
00422     Msat_MmStep_t * p;
00423     int i, k;
00424     p = ALLOC( Msat_MmStep_t, 1 );
00425     p->nMems = nSteps;
00426     // start the fixed memory managers
00427     p->pMems = ALLOC( Msat_MmFixed_t *, p->nMems );
00428     for ( i = 0; i < p->nMems; i++ )
00429         p->pMems[i] = Msat_MmFixedStart( (8<<i) );
00430     // set up the mapping of the required memory size into the corresponding manager
00431     p->nMapSize = (4<<p->nMems);
00432     p->pMap = ALLOC( Msat_MmFixed_t *, p->nMapSize+1 );
00433     p->pMap[0] = NULL;
00434     for ( k = 1; k <= 4; k++ )
00435         p->pMap[k] = p->pMems[0];
00436     for ( i = 0; i < p->nMems; i++ )
00437         for ( k = (4<<i)+1; k <= (8<<i); k++ )
00438             p->pMap[k] = p->pMems[i];
00439 //for ( i = 1; i < 100; i ++ )
00440 //printf( "%10d: size = %10d\n", i, p->pMap[i]->nEntrySize );
00441     return p;
00442 }

void Msat_MmStepStop ( Msat_MmStep_t p,
int  fVerbose 
)

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

Synopsis [Stops the memory manager.]

Description []

SideEffects []

SeeAlso []

Definition at line 455 of file msatMem.c.

00456 {
00457     int i;
00458     for ( i = 0; i < p->nMems; i++ )
00459         Msat_MmFixedStop( p->pMems[i], fVerbose );
00460     free( p->pMems );
00461     free( p->pMap );
00462     free( p );
00463 }


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