src/aig/aig/aigMem.c File Reference

#include "aig.h"
Include dependency graph for aigMem.c:

Go to the source code of this file.

Data Structures

struct  Aig_MmFixed_t_
struct  Aig_MmFlex_t_
struct  Aig_MmStep_t_

Defines

#define ALLOC(type, num)   ((type *) malloc(sizeof(type) * (num)))
#define FREE(obj)   ((obj) ? (free((char *) (obj)), (obj) = 0) : 0)
#define REALLOC(type, obj, num)

Functions

Aig_MmFixed_tAig_MmFixedStart (int nEntrySize, int nEntriesMax)
void Aig_MmFixedStop (Aig_MmFixed_t *p, int fVerbose)
char * Aig_MmFixedEntryFetch (Aig_MmFixed_t *p)
void Aig_MmFixedEntryRecycle (Aig_MmFixed_t *p, char *pEntry)
void Aig_MmFixedRestart (Aig_MmFixed_t *p)
int Aig_MmFixedReadMemUsage (Aig_MmFixed_t *p)
int Aig_MmFixedReadMaxEntriesUsed (Aig_MmFixed_t *p)
Aig_MmFlex_tAig_MmFlexStart ()
void Aig_MmFlexStop (Aig_MmFlex_t *p, int fVerbose)
char * Aig_MmFlexEntryFetch (Aig_MmFlex_t *p, int nBytes)
void Aig_MmFlexRestart (Aig_MmFlex_t *p)
int Aig_MmFlexReadMemUsage (Aig_MmFlex_t *p)
Aig_MmStep_tAig_MmStepStart (int nSteps)
void Aig_MmStepStop (Aig_MmStep_t *p, int fVerbose)
char * Aig_MmStepEntryFetch (Aig_MmStep_t *p, int nBytes)
void Aig_MmStepEntryRecycle (Aig_MmStep_t *p, char *pEntry, int nBytes)
int Aig_MmStepReadMemUsage (Aig_MmStep_t *p)

Define Documentation

#define ALLOC ( type,
num   )     ((type *) malloc(sizeof(type) * (num)))

Definition at line 73 of file aigMem.c.

#define FREE ( obj   )     ((obj) ? (free((char *) (obj)), (obj) = 0) : 0)

Definition at line 74 of file aigMem.c.

#define REALLOC ( type,
obj,
num   ) 
Value:
((obj) ? ((type *) realloc((char *)(obj), sizeof(type) * (num))) : \
             ((type *) malloc(sizeof(type) * (num))))

Definition at line 75 of file aigMem.c.


Function Documentation

char* Aig_MmFixedEntryFetch ( Aig_MmFixed_t p  ) 

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 160 of file aigMem.c.

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

void Aig_MmFixedEntryRecycle ( Aig_MmFixed_t p,
char *  pEntry 
)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 211 of file aigMem.c.

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

int Aig_MmFixedReadMaxEntriesUsed ( Aig_MmFixed_t p  ) 

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 286 of file aigMem.c.

00287 {
00288     return p->nEntriesMax;
00289 }

int Aig_MmFixedReadMemUsage ( Aig_MmFixed_t p  ) 

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 270 of file aigMem.c.

00271 {
00272     return p->nMemoryAlloc;
00273 }

void Aig_MmFixedRestart ( Aig_MmFixed_t p  ) 

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

Synopsis []

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

SideEffects []

SeeAlso []

Definition at line 231 of file aigMem.c.

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

Aig_MmFixed_t* Aig_MmFixedStart ( int  nEntrySize,
int  nEntriesMax 
)

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 95 of file aigMem.c.

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

void Aig_MmFixedStop ( Aig_MmFixed_t p,
int  fVerbose 
)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 131 of file aigMem.c.

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

char* Aig_MmFlexEntryFetch ( Aig_MmFlex_t p,
int  nBytes 
)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 365 of file aigMem.c.

00366 {
00367     char * pTemp;
00368     // check if there are still free entries
00369     if ( p->pCurrent == NULL || p->pCurrent + nBytes > p->pEnd )
00370     { // need to allocate more entries
00371         if ( p->nChunks == p->nChunksAlloc )
00372         {
00373             p->nChunksAlloc *= 2;
00374             p->pChunks = REALLOC( char *, p->pChunks, p->nChunksAlloc ); 
00375         }
00376         if ( nBytes > p->nChunkSize )
00377         {
00378             // resize the chunk size if more memory is requested than it can give
00379             // (ideally, this should never happen)
00380             p->nChunkSize = 2 * nBytes;
00381         }
00382         p->pCurrent = ALLOC( char, p->nChunkSize );
00383         p->pEnd     = p->pCurrent + p->nChunkSize;
00384         p->nMemoryAlloc += p->nChunkSize;
00385         // add the chunk to the chunk storage
00386         p->pChunks[ p->nChunks++ ] = p->pCurrent;
00387     }
00388     assert( p->pCurrent + nBytes <= p->pEnd );
00389     // increment the counter of used entries
00390     p->nEntriesUsed++;
00391     // keep track of the memory used
00392     p->nMemoryUsed += nBytes;
00393     // return the next entry
00394     pTemp = p->pCurrent;
00395     p->pCurrent += nBytes;
00396     return pTemp;
00397 }

int Aig_MmFlexReadMemUsage ( Aig_MmFlex_t p  ) 

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 438 of file aigMem.c.

00439 {
00440     return p->nMemoryUsed;
00441 }

void Aig_MmFlexRestart ( Aig_MmFlex_t p  ) 

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

Synopsis []

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

SideEffects []

SeeAlso []

Definition at line 410 of file aigMem.c.

00411 {
00412     int i;
00413     if ( p->nChunks == 0 )
00414         return;
00415     // deallocate all chunks except the first one
00416     for ( i = 1; i < p->nChunks; i++ )
00417         free( p->pChunks[i] );
00418     p->nChunks  = 1;
00419     p->nMemoryAlloc = p->nChunkSize;
00420     // transform these entries into a linked list
00421     p->pCurrent = p->pChunks[0];
00422     p->pEnd     = p->pCurrent + p->nChunkSize;
00423     p->nEntriesUsed = 0;
00424     p->nMemoryUsed = 0;
00425 }

Aig_MmFlex_t* Aig_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 304 of file aigMem.c.

00305 {
00306     Aig_MmFlex_t * p;
00307 
00308     p = ALLOC( Aig_MmFlex_t, 1 );
00309     memset( p, 0, sizeof(Aig_MmFlex_t) );
00310 
00311     p->nEntriesUsed  = 0;
00312     p->pCurrent      = NULL;
00313     p->pEnd          = NULL;
00314 
00315     p->nChunkSize    = (1 << 18);
00316     p->nChunksAlloc  = 64;
00317     p->nChunks       = 0;
00318     p->pChunks       = ALLOC( char *, p->nChunksAlloc );
00319 
00320     p->nMemoryUsed   = 0;
00321     p->nMemoryAlloc  = 0;
00322     return p;
00323 }

void Aig_MmFlexStop ( Aig_MmFlex_t p,
int  fVerbose 
)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 336 of file aigMem.c.

00337 {
00338     int i;
00339     if ( p == NULL )
00340         return;
00341     if ( fVerbose )
00342     {
00343         printf( "Flexible memory manager: Chunk size = %d. Chunks used = %d.\n",
00344             p->nChunkSize, p->nChunks );
00345         printf( "   Entries used = %d. Memory used = %d. Memory alloc = %d.\n",
00346             p->nEntriesUsed, p->nMemoryUsed, p->nMemoryAlloc );
00347     }
00348     for ( i = 0; i < p->nChunks; i++ )
00349         free( p->pChunks[i] );
00350     free( p->pChunks );
00351     free( p );
00352 }

char* Aig_MmStepEntryFetch ( Aig_MmStep_t p,
int  nBytes 
)

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

Synopsis [Creates the entry.]

Description []

SideEffects []

SeeAlso []

Definition at line 530 of file aigMem.c.

00531 {
00532     if ( nBytes == 0 )
00533         return NULL;
00534     if ( nBytes > p->nMapSize )
00535     {
00536 //        printf( "Allocating %d bytes.\n", nBytes );
00537 /*
00538         if ( p->nLargeChunks == p->nLargeChunksAlloc )
00539         {
00540             if ( p->nLargeChunksAlloc == 0 )
00541                 p->nLargeChunksAlloc = 5;
00542             p->nLargeChunksAlloc *= 2;
00543             p->pLargeChunks = REALLOC( char *, p->pLargeChunks, p->nLargeChunksAlloc ); 
00544         }
00545         p->pLargeChunks[ p->nLargeChunks++ ] = ALLOC( char, nBytes );
00546         return p->pLargeChunks[ p->nLargeChunks - 1 ];
00547 */
00548         return ALLOC( char, nBytes );
00549     }
00550     return Aig_MmFixedEntryFetch( p->pMap[nBytes] );
00551 }

void Aig_MmStepEntryRecycle ( Aig_MmStep_t p,
char *  pEntry,
int  nBytes 
)

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

Synopsis [Recycles the entry.]

Description []

SideEffects []

SeeAlso []

Definition at line 565 of file aigMem.c.

00566 {
00567     if ( nBytes == 0 )
00568         return;
00569     if ( nBytes > p->nMapSize )
00570     {
00571         free( pEntry );
00572         return;
00573     }
00574     Aig_MmFixedEntryRecycle( p->pMap[nBytes], pEntry );
00575 }

int Aig_MmStepReadMemUsage ( Aig_MmStep_t p  ) 

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 588 of file aigMem.c.

00589 {
00590     int i, nMemTotal = 0;
00591     for ( i = 0; i < p->nMems; i++ )
00592         nMemTotal += p->pMems[i]->nMemoryAlloc;
00593     return nMemTotal;
00594 }

Aig_MmStep_t* Aig_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 467 of file aigMem.c.

00468 {
00469     Aig_MmStep_t * p;
00470     int i, k;
00471     p = ALLOC( Aig_MmStep_t, 1 );
00472     memset( p, 0, sizeof(Aig_MmStep_t) );
00473     p->nMems = nSteps;
00474     // start the fixed memory managers
00475     p->pMems = ALLOC( Aig_MmFixed_t *, p->nMems );
00476     for ( i = 0; i < p->nMems; i++ )
00477         p->pMems[i] = Aig_MmFixedStart( (8<<i), (1<<13) );
00478     // set up the mapping of the required memory size into the corresponding manager
00479     p->nMapSize = (4<<p->nMems);
00480     p->pMap = ALLOC( Aig_MmFixed_t *, p->nMapSize+1 );
00481     p->pMap[0] = NULL;
00482     for ( k = 1; k <= 4; k++ )
00483         p->pMap[k] = p->pMems[0];
00484     for ( i = 0; i < p->nMems; i++ )
00485         for ( k = (4<<i)+1; k <= (8<<i); k++ )
00486             p->pMap[k] = p->pMems[i];
00487 //for ( i = 1; i < 100; i ++ )
00488 //printf( "%10d: size = %10d\n", i, p->pMap[i]->nEntrySize );
00489     return p;
00490 }

void Aig_MmStepStop ( Aig_MmStep_t p,
int  fVerbose 
)

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

Synopsis [Stops the memory manager.]

Description []

SideEffects []

SeeAlso []

Definition at line 503 of file aigMem.c.

00504 {
00505     int i;
00506     for ( i = 0; i < p->nMems; i++ )
00507         Aig_MmFixedStop( p->pMems[i], fVerbose );
00508 //    if ( p->pLargeChunks ) 
00509 //    {
00510 //      for ( i = 0; i < p->nLargeChunks; i++ )
00511 //          free( p->pLargeChunks[i] );
00512 //      free( p->pLargeChunks );
00513 //    }
00514     free( p->pMems );
00515     free( p->pMap );
00516     free( p );
00517 }


Generated on Tue Jan 5 12:18:11 2010 for abc70930 by  doxygen 1.6.1