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

Go to the source code of this file.

Data Structures

struct  Fraig_MemFixed_t_
 DECLARATIONS ///. More...
 

Functions

Fraig_MemFixed_tFraig_MemFixedStart (int nEntrySize)
 FUNCTION DEFINITIONS ///. More...
 
void Fraig_MemFixedStop (Fraig_MemFixed_t *p, int fVerbose)
 
char * Fraig_MemFixedEntryFetch (Fraig_MemFixed_t *p)
 
void Fraig_MemFixedEntryRecycle (Fraig_MemFixed_t *p, char *pEntry)
 
void Fraig_MemFixedRestart (Fraig_MemFixed_t *p)
 
int Fraig_MemFixedReadMemUsage (Fraig_MemFixed_t *p)
 

Function Documentation

char* Fraig_MemFixedEntryFetch ( Fraig_MemFixed_t p)

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

Synopsis [Extracts one entry from the memory manager.]

Description []

SideEffects []

SeeAlso []

Definition at line 131 of file fraigMem.c.

132 {
133  char * pTemp;
134  int i;
135 
136  // check if there are still free entries
137  if ( p->nEntriesUsed == p->nEntriesAlloc )
138  { // need to allocate more entries
139  assert( p->pEntriesFree == NULL );
140  if ( p->nChunks == p->nChunksAlloc )
141  {
142  p->nChunksAlloc *= 2;
143  p->pChunks = ABC_REALLOC( char *, p->pChunks, p->nChunksAlloc );
144  }
145  p->pEntriesFree = ABC_ALLOC( char, p->nEntrySize * p->nChunkSize );
146  p->nMemoryAlloc += p->nEntrySize * p->nChunkSize;
147  // transform these entries into a linked list
148  pTemp = p->pEntriesFree;
149  for ( i = 1; i < p->nChunkSize; i++ )
150  {
151  *((char **)pTemp) = pTemp + p->nEntrySize;
152  pTemp += p->nEntrySize;
153  }
154  // set the last link
155  *((char **)pTemp) = NULL;
156  // add the chunk to the chunk storage
157  p->pChunks[ p->nChunks++ ] = p->pEntriesFree;
158  // add to the number of entries allocated
159  p->nEntriesAlloc += p->nChunkSize;
160  }
161  // incrememt the counter of used entries
162  p->nEntriesUsed++;
163  if ( p->nEntriesMax < p->nEntriesUsed )
164  p->nEntriesMax = p->nEntriesUsed;
165  // return the first entry in the free entry list
166  pTemp = p->pEntriesFree;
167  p->pEntriesFree = *((char **)pTemp);
168  return pTemp;
169 }
char * pEntriesFree
Definition: fraigMem.c:35
#define ABC_REALLOC(type, obj, num)
Definition: abc_global.h:233
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
char ** pChunks
Definition: fraigMem.c:41
#define assert(ex)
Definition: util_old.h:213
void Fraig_MemFixedEntryRecycle ( Fraig_MemFixed_t p,
char *  pEntry 
)

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

Synopsis [Returns one entry into the memory manager.]

Description []

SideEffects []

SeeAlso []

Definition at line 182 of file fraigMem.c.

183 {
184  // decrement the counter of used entries
185  p->nEntriesUsed--;
186  // add the entry to the linked list of free entries
187  *((char **)pEntry) = p->pEntriesFree;
188  p->pEntriesFree = pEntry;
189 }
char * pEntriesFree
Definition: fraigMem.c:35
int Fraig_MemFixedReadMemUsage ( Fraig_MemFixed_t p)

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

Synopsis [Reports the memory usage.]

Description []

SideEffects []

SeeAlso []

Definition at line 240 of file fraigMem.c.

241 {
242  return p->nMemoryAlloc;
243 }
void Fraig_MemFixedRestart ( Fraig_MemFixed_t p)

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

Synopsis [Frees all associated memory and resets the manager.]

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

SideEffects []

SeeAlso []

Definition at line 202 of file fraigMem.c.

203 {
204  int i;
205  char * pTemp;
206 
207  // deallocate all chunks except the first one
208  for ( i = 1; i < p->nChunks; i++ )
209  ABC_FREE( p->pChunks[i] );
210  p->nChunks = 1;
211  // transform these entries into a linked list
212  pTemp = p->pChunks[0];
213  for ( i = 1; i < p->nChunkSize; i++ )
214  {
215  *((char **)pTemp) = pTemp + p->nEntrySize;
216  pTemp += p->nEntrySize;
217  }
218  // set the last link
219  *((char **)pTemp) = NULL;
220  // set the free entry list
221  p->pEntriesFree = p->pChunks[0];
222  // set the correct statistics
223  p->nMemoryAlloc = p->nEntrySize * p->nChunkSize;
224  p->nMemoryUsed = 0;
225  p->nEntriesAlloc = p->nChunkSize;
226  p->nEntriesUsed = 0;
227 }
char * pEntriesFree
Definition: fraigMem.c:35
char ** pChunks
Definition: fraigMem.c:41
#define ABC_FREE(obj)
Definition: abc_global.h:232
Fraig_MemFixed_t* Fraig_MemFixedStart ( int  nEntrySize)

FUNCTION DEFINITIONS ///.

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

Synopsis [Starts the internal memory manager.]

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

SideEffects []

SeeAlso []

Definition at line 63 of file fraigMem.c.

64 {
66 
67  p = ABC_ALLOC( Fraig_MemFixed_t, 1 );
68  memset( p, 0, sizeof(Fraig_MemFixed_t) );
69 
70  p->nEntrySize = nEntrySize;
71  p->nEntriesAlloc = 0;
72  p->nEntriesUsed = 0;
73  p->pEntriesFree = NULL;
74 
75  if ( nEntrySize * (1 << 10) < (1<<16) )
76  p->nChunkSize = (1 << 10);
77  else
78  p->nChunkSize = (1<<16) / nEntrySize;
79  if ( p->nChunkSize < 8 )
80  p->nChunkSize = 8;
81 
82  p->nChunksAlloc = 64;
83  p->nChunks = 0;
84  p->pChunks = ABC_ALLOC( char *, p->nChunksAlloc );
85 
86  p->nMemoryUsed = 0;
87  p->nMemoryAlloc = 0;
88  return p;
89 }
char * pEntriesFree
Definition: fraigMem.c:35
char * memset()
static Llb_Mgr_t * p
Definition: llb3Image.c:950
DECLARATIONS ///.
Definition: fraigMem.c:28
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
char ** pChunks
Definition: fraigMem.c:41
void Fraig_MemFixedStop ( Fraig_MemFixed_t p,
int  fVerbose 
)

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

Synopsis [Stops the internal memory manager.]

Description []

SideEffects []

SeeAlso []

Definition at line 102 of file fraigMem.c.

103 {
104  int i;
105  if ( p == NULL )
106  return;
107  if ( fVerbose )
108  {
109  printf( "Fixed memory manager: Entry = %5d. Chunk = %5d. Chunks used = %5d.\n",
110  p->nEntrySize, p->nChunkSize, p->nChunks );
111  printf( " Entries used = %8d. Entries peak = %8d. Memory used = %8d. Memory alloc = %8d.\n",
113  }
114  for ( i = 0; i < p->nChunks; i++ )
115  ABC_FREE( p->pChunks[i] );
116  ABC_FREE( p->pChunks );
117  ABC_FREE( p );
118 }
char ** pChunks
Definition: fraigMem.c:41
#define ABC_FREE(obj)
Definition: abc_global.h:232