abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
aigMem.c File Reference
#include "aig.h"

Go to the source code of this file.

Data Structures

struct  Aig_MmFixed_t_
 DECLARATIONS ///. More...
 
struct  Aig_MmFlex_t_
 
struct  Aig_MmStep_t_
 

Functions

Aig_MmFixed_tAig_MmFixedStart (int nEntrySize, int nEntriesMax)
 FUNCTION DEFINITIONS ///. More...
 
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)
 

Function Documentation

char* Aig_MmFixedEntryFetch ( Aig_MmFixed_t p)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 161 of file aigMem.c.

162 {
163  char * pTemp;
164  int i;
165 
166  // check if there are still free entries
167  if ( p->nEntriesUsed == p->nEntriesAlloc )
168  { // need to allocate more entries
169  assert( p->pEntriesFree == NULL );
170  if ( p->nChunks == p->nChunksAlloc )
171  {
172  p->nChunksAlloc *= 2;
173  p->pChunks = ABC_REALLOC( char *, p->pChunks, p->nChunksAlloc );
174  }
175  p->pEntriesFree = ABC_ALLOC( char, p->nEntrySize * p->nChunkSize );
176  p->nMemoryAlloc += p->nEntrySize * p->nChunkSize;
177  // transform these entries into a linked list
178  pTemp = p->pEntriesFree;
179  for ( i = 1; i < p->nChunkSize; i++ )
180  {
181  *((char **)pTemp) = pTemp + p->nEntrySize;
182  pTemp += p->nEntrySize;
183  }
184  // set the last link
185  *((char **)pTemp) = NULL;
186  // add the chunk to the chunk storage
187  p->pChunks[ p->nChunks++ ] = p->pEntriesFree;
188  // add to the number of entries allocated
189  p->nEntriesAlloc += p->nChunkSize;
190  }
191  // incrememt the counter of used entries
192  p->nEntriesUsed++;
193  if ( p->nEntriesMax < p->nEntriesUsed )
194  p->nEntriesMax = p->nEntriesUsed;
195  // return the first entry in the free entry list
196  pTemp = p->pEntriesFree;
197  p->pEntriesFree = *((char **)pTemp);
198  return pTemp;
199 }
#define ABC_REALLOC(type, obj, num)
Definition: abc_global.h:233
int nChunks
Definition: aigMem.c:42
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
char ** pChunks
Definition: aigMem.c:43
char * pEntriesFree
Definition: aigMem.c:37
int nChunkSize
Definition: aigMem.c:40
int nEntrySize
Definition: aigMem.c:33
int nMemoryAlloc
Definition: aigMem.c:47
int nEntriesMax
Definition: aigMem.c:36
int nEntriesUsed
Definition: aigMem.c:35
#define assert(ex)
Definition: util_old.h:213
int nChunksAlloc
Definition: aigMem.c:41
int nEntriesAlloc
Definition: aigMem.c:34
void Aig_MmFixedEntryRecycle ( Aig_MmFixed_t p,
char *  pEntry 
)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 212 of file aigMem.c.

213 {
214  // decrement the counter of used entries
215  p->nEntriesUsed--;
216  // add the entry to the linked list of free entries
217  *((char **)pEntry) = p->pEntriesFree;
218  p->pEntriesFree = pEntry;
219 }
char * pEntriesFree
Definition: aigMem.c:37
int nEntriesUsed
Definition: aigMem.c:35
int Aig_MmFixedReadMaxEntriesUsed ( Aig_MmFixed_t p)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 287 of file aigMem.c.

288 {
289  return p->nEntriesMax;
290 }
int nEntriesMax
Definition: aigMem.c:36
int Aig_MmFixedReadMemUsage ( Aig_MmFixed_t p)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 271 of file aigMem.c.

272 {
273  return p->nMemoryAlloc;
274 }
int nMemoryAlloc
Definition: aigMem.c:47
void Aig_MmFixedRestart ( Aig_MmFixed_t p)

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

Synopsis []

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

SideEffects []

SeeAlso []

Definition at line 232 of file aigMem.c.

233 {
234  int i;
235  char * pTemp;
236  if ( p->nChunks == 0 )
237  return;
238  // deallocate all chunks except the first one
239  for ( i = 1; i < p->nChunks; i++ )
240  ABC_FREE( p->pChunks[i] );
241  p->nChunks = 1;
242  // transform these entries into a linked list
243  pTemp = p->pChunks[0];
244  for ( i = 1; i < p->nChunkSize; i++ )
245  {
246  *((char **)pTemp) = pTemp + p->nEntrySize;
247  pTemp += p->nEntrySize;
248  }
249  // set the last link
250  *((char **)pTemp) = NULL;
251  // set the free entry list
252  p->pEntriesFree = p->pChunks[0];
253  // set the correct statistics
254  p->nMemoryAlloc = p->nEntrySize * p->nChunkSize;
255  p->nMemoryUsed = 0;
256  p->nEntriesAlloc = p->nChunkSize;
257  p->nEntriesUsed = 0;
258 }
int nChunks
Definition: aigMem.c:42
char ** pChunks
Definition: aigMem.c:43
char * pEntriesFree
Definition: aigMem.c:37
int nChunkSize
Definition: aigMem.c:40
int nEntrySize
Definition: aigMem.c:33
int nMemoryAlloc
Definition: aigMem.c:47
int nEntriesUsed
Definition: aigMem.c:35
#define ABC_FREE(obj)
Definition: abc_global.h:232
int nMemoryUsed
Definition: aigMem.c:46
int nEntriesAlloc
Definition: aigMem.c:34
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 96 of file aigMem.c.

97 {
98  Aig_MmFixed_t * p;
99 
100  p = ABC_ALLOC( Aig_MmFixed_t, 1 );
101  memset( p, 0, sizeof(Aig_MmFixed_t) );
102 
103  p->nEntrySize = nEntrySize;
104  p->nEntriesAlloc = 0;
105  p->nEntriesUsed = 0;
106  p->pEntriesFree = NULL;
107 
108  p->nChunkSize = nEntriesMax / 8;
109  if ( p->nChunkSize < 8 )
110  p->nChunkSize = 8;
111 
112  p->nChunksAlloc = 64;
113  p->nChunks = 0;
114  p->pChunks = ABC_ALLOC( char *, p->nChunksAlloc );
115 
116  p->nMemoryUsed = 0;
117  p->nMemoryAlloc = 0;
118  return p;
119 }
char * memset()
static Llb_Mgr_t * p
Definition: llb3Image.c:950
int nChunks
Definition: aigMem.c:42
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
char ** pChunks
Definition: aigMem.c:43
char * pEntriesFree
Definition: aigMem.c:37
DECLARATIONS ///.
Definition: aigMem.c:30
int nChunkSize
Definition: aigMem.c:40
int nEntrySize
Definition: aigMem.c:33
int nMemoryAlloc
Definition: aigMem.c:47
int nEntriesUsed
Definition: aigMem.c:35
int nChunksAlloc
Definition: aigMem.c:41
int nMemoryUsed
Definition: aigMem.c:46
int nEntriesAlloc
Definition: aigMem.c:34
void Aig_MmFixedStop ( Aig_MmFixed_t p,
int  fVerbose 
)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 132 of file aigMem.c.

133 {
134  int i;
135  if ( p == NULL )
136  return;
137  if ( fVerbose )
138  {
139  printf( "Fixed memory manager: Entry = %5d. Chunk = %5d. Chunks used = %5d.\n",
140  p->nEntrySize, p->nChunkSize, p->nChunks );
141  printf( " Entries used = %8d. Entries peak = %8d. Memory used = %8d. Memory alloc = %8d.\n",
143  }
144  for ( i = 0; i < p->nChunks; i++ )
145  ABC_FREE( p->pChunks[i] );
146  ABC_FREE( p->pChunks );
147  ABC_FREE( p );
148 }
int nChunks
Definition: aigMem.c:42
char ** pChunks
Definition: aigMem.c:43
int nChunkSize
Definition: aigMem.c:40
int nEntrySize
Definition: aigMem.c:33
int nMemoryAlloc
Definition: aigMem.c:47
int nEntriesMax
Definition: aigMem.c:36
int nEntriesUsed
Definition: aigMem.c:35
#define ABC_FREE(obj)
Definition: abc_global.h:232
char* Aig_MmFlexEntryFetch ( Aig_MmFlex_t p,
int  nBytes 
)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 366 of file aigMem.c.

367 {
368  char * pTemp;
369  // check if there are still free entries
370  if ( p->pCurrent == NULL || p->pCurrent + nBytes > p->pEnd )
371  { // need to allocate more entries
372  if ( p->nChunks == p->nChunksAlloc )
373  {
374  p->nChunksAlloc *= 2;
375  p->pChunks = ABC_REALLOC( char *, p->pChunks, p->nChunksAlloc );
376  }
377  if ( nBytes > p->nChunkSize )
378  {
379  // resize the chunk size if more memory is requested than it can give
380  // (ideally, this should never happen)
381  p->nChunkSize = 2 * nBytes;
382  }
383  p->pCurrent = ABC_ALLOC( char, p->nChunkSize );
384  p->pEnd = p->pCurrent + p->nChunkSize;
385  p->nMemoryAlloc += p->nChunkSize;
386  // add the chunk to the chunk storage
387  p->pChunks[ p->nChunks++ ] = p->pCurrent;
388  }
389  assert( p->pCurrent + nBytes <= p->pEnd );
390  // increment the counter of used entries
391  p->nEntriesUsed++;
392  // keep track of the memory used
393  p->nMemoryUsed += nBytes;
394  // return the next entry
395  pTemp = p->pCurrent;
396  p->pCurrent += nBytes;
397  return pTemp;
398 }
int nMemoryAlloc
Definition: aigMem.c:65
int nChunks
Definition: aigMem.c:60
#define ABC_REALLOC(type, obj, num)
Definition: abc_global.h:233
int nChunksAlloc
Definition: aigMem.c:59
int nEntriesUsed
Definition: aigMem.c:53
int nChunkSize
Definition: aigMem.c:58
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
int nMemoryUsed
Definition: aigMem.c:64
char * pEnd
Definition: aigMem.c:55
char ** pChunks
Definition: aigMem.c:61
char * pCurrent
Definition: aigMem.c:54
#define assert(ex)
Definition: util_old.h:213
int Aig_MmFlexReadMemUsage ( Aig_MmFlex_t p)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 439 of file aigMem.c.

440 {
441  return p->nMemoryUsed;
442 }
int nMemoryUsed
Definition: aigMem.c:64
void Aig_MmFlexRestart ( Aig_MmFlex_t p)

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

Synopsis []

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

SideEffects []

SeeAlso []

Definition at line 411 of file aigMem.c.

412 {
413  int i;
414  if ( p->nChunks == 0 )
415  return;
416  // deallocate all chunks except the first one
417  for ( i = 1; i < p->nChunks; i++ )
418  ABC_FREE( p->pChunks[i] );
419  p->nChunks = 1;
420  p->nMemoryAlloc = p->nChunkSize;
421  // transform these entries into a linked list
422  p->pCurrent = p->pChunks[0];
423  p->pEnd = p->pCurrent + p->nChunkSize;
424  p->nEntriesUsed = 0;
425  p->nMemoryUsed = 0;
426 }
int nMemoryAlloc
Definition: aigMem.c:65
int nChunks
Definition: aigMem.c:60
int nEntriesUsed
Definition: aigMem.c:53
int nChunkSize
Definition: aigMem.c:58
int nMemoryUsed
Definition: aigMem.c:64
char * pEnd
Definition: aigMem.c:55
char ** pChunks
Definition: aigMem.c:61
#define ABC_FREE(obj)
Definition: abc_global.h:232
char * pCurrent
Definition: aigMem.c:54
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 305 of file aigMem.c.

306 {
307  Aig_MmFlex_t * p;
308 
309  p = ABC_ALLOC( Aig_MmFlex_t, 1 );
310  memset( p, 0, sizeof(Aig_MmFlex_t) );
311 
312  p->nEntriesUsed = 0;
313  p->pCurrent = NULL;
314  p->pEnd = NULL;
315 
316  p->nChunkSize = (1 << 18);
317  p->nChunksAlloc = 64;
318  p->nChunks = 0;
319  p->pChunks = ABC_ALLOC( char *, p->nChunksAlloc );
320 
321  p->nMemoryUsed = 0;
322  p->nMemoryAlloc = 0;
323  return p;
324 }
char * memset()
int nMemoryAlloc
Definition: aigMem.c:65
int nChunks
Definition: aigMem.c:60
static Llb_Mgr_t * p
Definition: llb3Image.c:950
int nChunksAlloc
Definition: aigMem.c:59
int nEntriesUsed
Definition: aigMem.c:53
int nChunkSize
Definition: aigMem.c:58
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
int nMemoryUsed
Definition: aigMem.c:64
char * pEnd
Definition: aigMem.c:55
char ** pChunks
Definition: aigMem.c:61
char * pCurrent
Definition: aigMem.c:54
void Aig_MmFlexStop ( Aig_MmFlex_t p,
int  fVerbose 
)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 337 of file aigMem.c.

338 {
339  int i;
340  if ( p == NULL )
341  return;
342  if ( fVerbose )
343  {
344  printf( "Flexible memory manager: Chunk size = %d. Chunks used = %d.\n",
345  p->nChunkSize, p->nChunks );
346  printf( " Entries used = %d. Memory used = %d. Memory alloc = %d.\n",
348  }
349  for ( i = 0; i < p->nChunks; i++ )
350  ABC_FREE( p->pChunks[i] );
351  ABC_FREE( p->pChunks );
352  ABC_FREE( p );
353 }
int nMemoryAlloc
Definition: aigMem.c:65
int nChunks
Definition: aigMem.c:60
int nEntriesUsed
Definition: aigMem.c:53
int nChunkSize
Definition: aigMem.c:58
int nMemoryUsed
Definition: aigMem.c:64
char ** pChunks
Definition: aigMem.c:61
#define ABC_FREE(obj)
Definition: abc_global.h:232
char* Aig_MmStepEntryFetch ( Aig_MmStep_t p,
int  nBytes 
)

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

Synopsis [Creates the entry.]

Description []

SideEffects []

SeeAlso []

Definition at line 534 of file aigMem.c.

535 {
536  if ( nBytes == 0 )
537  return NULL;
538  if ( nBytes > p->nMapSize )
539  {
540  if ( p->nChunks == p->nChunksAlloc )
541  {
542  p->nChunksAlloc *= 2;
543  p->pChunks = ABC_REALLOC( char *, p->pChunks, p->nChunksAlloc );
544  }
545  p->pChunks[ p->nChunks++ ] = ABC_ALLOC( char, nBytes );
546  return p->pChunks[p->nChunks-1];
547  }
548  return Aig_MmFixedEntryFetch( p->pMap[nBytes] );
549 }
#define ABC_REALLOC(type, obj, num)
Definition: abc_global.h:233
Aig_MmFixed_t ** pMap
Definition: aigMem.c:73
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
int nMapSize
Definition: aigMem.c:72
char * Aig_MmFixedEntryFetch(Aig_MmFixed_t *p)
Definition: aigMem.c:161
int nChunksAlloc
Definition: aigMem.c:75
int nChunks
Definition: aigMem.c:76
char ** pChunks
Definition: aigMem.c:77
void Aig_MmStepEntryRecycle ( Aig_MmStep_t p,
char *  pEntry,
int  nBytes 
)

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

Synopsis [Recycles the entry.]

Description []

SideEffects []

SeeAlso []

Definition at line 563 of file aigMem.c.

564 {
565  if ( nBytes == 0 )
566  return;
567  if ( nBytes > p->nMapSize )
568  {
569 // ABC_FREE( pEntry );
570  return;
571  }
572  Aig_MmFixedEntryRecycle( p->pMap[nBytes], pEntry );
573 }
Aig_MmFixed_t ** pMap
Definition: aigMem.c:73
int nMapSize
Definition: aigMem.c:72
void Aig_MmFixedEntryRecycle(Aig_MmFixed_t *p, char *pEntry)
Definition: aigMem.c:212
int Aig_MmStepReadMemUsage ( Aig_MmStep_t p)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 586 of file aigMem.c.

587 {
588  int i, nMemTotal = 0;
589  for ( i = 0; i < p->nMems; i++ )
590  nMemTotal += p->pMems[i]->nMemoryAlloc;
591  return nMemTotal;
592 }
int nMems
Definition: aigMem.c:70
Aig_MmFixed_t ** pMems
Definition: aigMem.c:71
int nMemoryAlloc
Definition: aigMem.c:47
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 ABC_FREE()ed.]

SideEffects []

SeeAlso []

Definition at line 468 of file aigMem.c.

469 {
470  Aig_MmStep_t * p;
471  int i, k;
472  p = ABC_ALLOC( Aig_MmStep_t, 1 );
473  memset( p, 0, sizeof(Aig_MmStep_t) );
474  p->nMems = nSteps;
475  // start the fixed memory managers
476  p->pMems = ABC_ALLOC( Aig_MmFixed_t *, p->nMems );
477  for ( i = 0; i < p->nMems; i++ )
478  p->pMems[i] = Aig_MmFixedStart( (8<<i), (1<<13) );
479  // set up the mapping of the required memory size into the corresponding manager
480  p->nMapSize = (4<<p->nMems);
481  p->pMap = ABC_ALLOC( Aig_MmFixed_t *, p->nMapSize+1 );
482  p->pMap[0] = NULL;
483  for ( k = 1; k <= 4; k++ )
484  p->pMap[k] = p->pMems[0];
485  for ( i = 0; i < p->nMems; i++ )
486  for ( k = (4<<i)+1; k <= (8<<i); k++ )
487  p->pMap[k] = p->pMems[i];
488 //for ( i = 1; i < 100; i ++ )
489 //printf( "%10d: size = %10d\n", i, p->pMap[i]->nEntrySize );
490  p->nChunksAlloc = 64;
491  p->nChunks = 0;
492  p->pChunks = ABC_ALLOC( char *, p->nChunksAlloc );
493  return p;
494 }
char * memset()
int nMems
Definition: aigMem.c:70
static Llb_Mgr_t * p
Definition: llb3Image.c:950
Aig_MmFixed_t ** pMap
Definition: aigMem.c:73
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
for(p=first;p->value< newval;p=p->next)
Aig_MmFixed_t * Aig_MmFixedStart(int nEntrySize, int nEntriesMax)
FUNCTION DEFINITIONS ///.
Definition: aigMem.c:96
DECLARATIONS ///.
Definition: aigMem.c:30
Aig_MmFixed_t ** pMems
Definition: aigMem.c:71
int nMapSize
Definition: aigMem.c:72
int nChunksAlloc
Definition: aigMem.c:75
int nChunks
Definition: aigMem.c:76
char ** pChunks
Definition: aigMem.c:77
void Aig_MmStepStop ( Aig_MmStep_t p,
int  fVerbose 
)

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

Synopsis [Stops the memory manager.]

Description []

SideEffects []

SeeAlso []

Definition at line 507 of file aigMem.c.

508 {
509  int i;
510  for ( i = 0; i < p->nMems; i++ )
511  Aig_MmFixedStop( p->pMems[i], fVerbose );
512  if ( p->nChunksAlloc )
513  {
514  for ( i = 0; i < p->nChunks; i++ )
515  ABC_FREE( p->pChunks[i] );
516  ABC_FREE( p->pChunks );
517  }
518  ABC_FREE( p->pMems );
519  ABC_FREE( p->pMap );
520  ABC_FREE( p );
521 }
int nMems
Definition: aigMem.c:70
Aig_MmFixed_t ** pMap
Definition: aigMem.c:73
Aig_MmFixed_t ** pMems
Definition: aigMem.c:71
int nChunksAlloc
Definition: aigMem.c:75
#define ABC_FREE(obj)
Definition: abc_global.h:232
void Aig_MmFixedStop(Aig_MmFixed_t *p, int fVerbose)
Definition: aigMem.c:132
int nChunks
Definition: aigMem.c:76
char ** pChunks
Definition: aigMem.c:77