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

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 202 of file extraUtilMemory.c.

203 {
204  char * pTemp;
205  int i;
206 
207  // check if there are still free entries
208  if ( p->nEntriesUsed == p->nEntriesAlloc )
209  { // need to allocate more entries
210  assert( p->pEntriesFree == NULL );
211  if ( p->nChunks == p->nChunksAlloc )
212  {
213  p->nChunksAlloc *= 2;
214  p->pChunks = ABC_REALLOC( char *, p->pChunks, p->nChunksAlloc );
215  }
216  p->pEntriesFree = ABC_ALLOC( char, p->nEntrySize * p->nChunkSize );
217  p->nMemoryAlloc += p->nEntrySize * p->nChunkSize;
218  // transform these entries into a linked list
219  pTemp = p->pEntriesFree;
220  for ( i = 1; i < p->nChunkSize; i++ )
221  {
222  *((char **)pTemp) = pTemp + p->nEntrySize;
223  pTemp += p->nEntrySize;
224  }
225  // set the last link
226  *((char **)pTemp) = NULL;
227  // add the chunk to the chunk storage
228  p->pChunks[ p->nChunks++ ] = p->pEntriesFree;
229  // add to the number of entries allocated
230  p->nEntriesAlloc += p->nChunkSize;
231  }
232  // incrememt the counter of used entries
233  p->nEntriesUsed++;
234  if ( p->nEntriesMax < p->nEntriesUsed )
235  p->nEntriesMax = p->nEntriesUsed;
236  // return the first entry in the free entry list
237  pTemp = p->pEntriesFree;
238  p->pEntriesFree = *((char **)pTemp);
239  return pTemp;
240 }
#define ABC_REALLOC(type, obj, num)
Definition: abc_global.h:233
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
#define assert(ex)
Definition: util_old.h:213
void Extra_MmFixedEntryRecycle ( Extra_MmFixed_t p,
char *  pEntry 
)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 253 of file extraUtilMemory.c.

254 {
255  // decrement the counter of used entries
256  p->nEntriesUsed--;
257  // add the entry to the linked list of free entries
258  *((char **)pEntry) = p->pEntriesFree;
259  p->pEntriesFree = pEntry;
260 }
void Extra_MmFixedPrint ( Extra_MmFixed_t p)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 161 of file extraUtilMemory.c.

162 {
163  printf( "Fixed memory manager: Entry = %5d. Chunk = %5d. Chunks used = %5d.\n",
164  p->nEntrySize, p->nChunkSize, p->nChunks );
165  printf( " Entries used = %8d. Entries peak = %8d. Memory used = %8d. Memory alloc = %8d.\n",
167 }
int Extra_MmFixedReadMaxEntriesUsed ( Extra_MmFixed_t p)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 327 of file extraUtilMemory.c.

328 {
329  return p->nEntriesMax;
330 }
int Extra_MmFixedReadMemUsage ( Extra_MmFixed_t p)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 311 of file extraUtilMemory.c.

312 {
313  return p->nMemoryAlloc;
314 }
void Extra_MmFixedRestart ( Extra_MmFixed_t p)

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

Synopsis []

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

SideEffects []

SeeAlso []

Definition at line 273 of file extraUtilMemory.c.

274 {
275  int i;
276  char * pTemp;
277 
278  // deallocate all chunks except the first one
279  for ( i = 1; i < p->nChunks; i++ )
280  ABC_FREE( p->pChunks[i] );
281  p->nChunks = 1;
282  // transform these entries into a linked list
283  pTemp = p->pChunks[0];
284  for ( i = 1; i < p->nChunkSize; i++ )
285  {
286  *((char **)pTemp) = pTemp + p->nEntrySize;
287  pTemp += p->nEntrySize;
288  }
289  // set the last link
290  *((char **)pTemp) = NULL;
291  // set the free entry list
292  p->pEntriesFree = p->pChunks[0];
293  // set the correct statistics
294  p->nMemoryAlloc = p->nEntrySize * p->nChunkSize;
295  p->nMemoryUsed = 0;
296  p->nEntriesAlloc = p->nChunkSize;
297  p->nEntriesUsed = 0;
298 }
#define ABC_FREE(obj)
Definition: abc_global.h:232
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 122 of file extraUtilMemory.c.

123 {
124  Extra_MmFixed_t * p;
125 
126  p = ABC_ALLOC( Extra_MmFixed_t, 1 );
127  memset( p, 0, sizeof(Extra_MmFixed_t) );
128 
129  p->nEntrySize = nEntrySize;
130  p->nEntriesAlloc = 0;
131  p->nEntriesUsed = 0;
132  p->pEntriesFree = NULL;
133 
134  if ( nEntrySize * (1 << 10) < (1<<16) )
135  p->nChunkSize = (1 << 10);
136  else
137  p->nChunkSize = (1<<16) / nEntrySize;
138  if ( p->nChunkSize < 8 )
139  p->nChunkSize = 8;
140 
141  p->nChunksAlloc = 64;
142  p->nChunks = 0;
143  p->pChunks = ABC_ALLOC( char *, p->nChunksAlloc );
144 
145  p->nMemoryUsed = 0;
146  p->nMemoryAlloc = 0;
147  return p;
148 }
char * memset()
static Llb_Mgr_t * p
Definition: llb3Image.c:950
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
void Extra_MmFixedStop ( Extra_MmFixed_t p)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 180 of file extraUtilMemory.c.

181 {
182  int i;
183  if ( p == NULL )
184  return;
185  for ( i = 0; i < p->nChunks; i++ )
186  ABC_FREE( p->pChunks[i] );
187  ABC_FREE( p->pChunks );
188  ABC_FREE( p );
189 }
#define ABC_FREE(obj)
Definition: abc_global.h:232
char* Extra_MmFlexEntryFetch ( Extra_MmFlex_t p,
int  nBytes 
)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 418 of file extraUtilMemory.c.

419 {
420  char * pTemp;
421  // check if there are still free entries
422  if ( p->pCurrent == NULL || p->pCurrent + nBytes > p->pEnd )
423  { // need to allocate more entries
424  if ( p->nChunks == p->nChunksAlloc )
425  {
426  p->nChunksAlloc *= 2;
427  p->pChunks = ABC_REALLOC( char *, p->pChunks, p->nChunksAlloc );
428  }
429  if ( nBytes > p->nChunkSize )
430  {
431  // resize the chunk size if more memory is requested than it can give
432  // (ideally, this should never happen)
433  p->nChunkSize = 2 * nBytes;
434  }
435  p->pCurrent = ABC_ALLOC( char, p->nChunkSize );
436  p->pEnd = p->pCurrent + p->nChunkSize;
437  p->nMemoryAlloc += p->nChunkSize;
438  // add the chunk to the chunk storage
439  p->pChunks[ p->nChunks++ ] = p->pCurrent;
440  }
441  assert( p->pCurrent + nBytes <= p->pEnd );
442  // increment the counter of used entries
443  p->nEntriesUsed++;
444  // keep track of the memory used
445  p->nMemoryUsed += nBytes;
446  // return the next entry
447  pTemp = p->pCurrent;
448  p->pCurrent += nBytes;
449  return pTemp;
450 }
#define ABC_REALLOC(type, obj, num)
Definition: abc_global.h:233
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
#define assert(ex)
Definition: util_old.h:213
void Extra_MmFlexPrint ( Extra_MmFlex_t p)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 376 of file extraUtilMemory.c.

377 {
378  printf( "Flexible memory manager: Chunk size = %d. Chunks used = %d.\n",
379  p->nChunkSize, p->nChunks );
380  printf( " Entries used = %d. Memory used = %d. Memory alloc = %d.\n",
382 }
int Extra_MmFlexReadMemUsage ( Extra_MmFlex_t p)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 463 of file extraUtilMemory.c.

464 {
465  return p->nMemoryAlloc;
466 }
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 344 of file extraUtilMemory.c.

345 {
346  Extra_MmFlex_t * p;
347 //printf( "allocing flex\n" );
348  p = ABC_ALLOC( Extra_MmFlex_t, 1 );
349  memset( p, 0, sizeof(Extra_MmFlex_t) );
350 
351  p->nEntriesUsed = 0;
352  p->pCurrent = NULL;
353  p->pEnd = NULL;
354 
355  p->nChunkSize = (1 << 12);
356  p->nChunksAlloc = 64;
357  p->nChunks = 0;
358  p->pChunks = ABC_ALLOC( char *, p->nChunksAlloc );
359 
360  p->nMemoryUsed = 0;
361  p->nMemoryAlloc = 0;
362  return p;
363 }
char * memset()
static Llb_Mgr_t * p
Definition: llb3Image.c:950
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
void Extra_MmFlexStop ( Extra_MmFlex_t p)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 395 of file extraUtilMemory.c.

396 {
397  int i;
398  if ( p == NULL )
399  return;
400 //printf( "deleting flex\n" );
401  for ( i = 0; i < p->nChunks; i++ )
402  ABC_FREE( p->pChunks[i] );
403  ABC_FREE( p->pChunks );
404  ABC_FREE( p );
405 }
#define ABC_FREE(obj)
Definition: abc_global.h:232
char* Extra_MmStepEntryFetch ( Extra_MmStep_t p,
int  nBytes 
)

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

Synopsis [Creates the entry.]

Description []

SideEffects []

SeeAlso []

Definition at line 555 of file extraUtilMemory.c.

556 {
557  if ( nBytes == 0 )
558  return NULL;
559  if ( nBytes > p->nMapSize )
560  {
561 // printf( "Allocating %d bytes.\n", nBytes );
562 // return ABC_ALLOC( char, nBytes );
563  if ( p->nLargeChunks == p->nLargeChunksAlloc )
564  {
565  if ( p->nLargeChunksAlloc == 0 )
566  p->nLargeChunksAlloc = 32;
567  p->nLargeChunksAlloc *= 2;
569  }
570  p->pLargeChunks[ p->nLargeChunks++ ] = ABC_ALLOC( char, nBytes );
571  return (char *)p->pLargeChunks[ p->nLargeChunks - 1 ];
572  }
573  return Extra_MmFixedEntryFetch( p->pMap[nBytes] );
574 }
#define ABC_REALLOC(type, obj, num)
Definition: abc_global.h:233
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
Extra_MmFixed_t ** pMap
char * Extra_MmFixedEntryFetch(Extra_MmFixed_t *p)
void Extra_MmStepEntryRecycle ( Extra_MmStep_t p,
char *  pEntry,
int  nBytes 
)

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

Synopsis [Recycles the entry.]

Description []

SideEffects []

SeeAlso []

Definition at line 588 of file extraUtilMemory.c.

589 {
590  if ( nBytes == 0 )
591  return;
592  if ( nBytes > p->nMapSize )
593  {
594 // ABC_FREE( pEntry );
595  return;
596  }
597  Extra_MmFixedEntryRecycle( p->pMap[nBytes], pEntry );
598 }
void Extra_MmFixedEntryRecycle(Extra_MmFixed_t *p, char *pEntry)
Extra_MmFixed_t ** pMap
int Extra_MmStepReadMemUsage ( Extra_MmStep_t p)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 611 of file extraUtilMemory.c.

612 {
613  int i, nMemTotal = 0;
614  for ( i = 0; i < p->nMems; i++ )
615  nMemTotal += p->pMems[i]->nMemoryAlloc;
616  return nMemTotal;
617 }
Extra_MmFixed_t ** pMems
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 ABC_FREE()ed.]

SideEffects []

SeeAlso []

Definition at line 492 of file extraUtilMemory.c.

493 {
494  Extra_MmStep_t * p;
495  int i, k;
496  p = ABC_ALLOC( Extra_MmStep_t, 1 );
497  memset( p, 0, sizeof(Extra_MmStep_t) );
498  p->nMems = nSteps;
499  // start the fixed memory managers
500  p->pMems = ABC_ALLOC( Extra_MmFixed_t *, p->nMems );
501  for ( i = 0; i < p->nMems; i++ )
502  p->pMems[i] = Extra_MmFixedStart( (8<<i) );
503  // set up the mapping of the required memory size into the corresponding manager
504  p->nMapSize = (4<<p->nMems);
505  p->pMap = ABC_ALLOC( Extra_MmFixed_t *, p->nMapSize+1 );
506  p->pMap[0] = NULL;
507  for ( k = 1; k <= 4; k++ )
508  p->pMap[k] = p->pMems[0];
509  for ( i = 0; i < p->nMems; i++ )
510  for ( k = (4<<i)+1; k <= (8<<i); k++ )
511  p->pMap[k] = p->pMems[i];
512 //for ( i = 1; i < 100; i ++ )
513 //printf( "%10d: size = %10d\n", i, p->pMap[i]->nEntrySize );
514  return p;
515 }
char * memset()
static Llb_Mgr_t * p
Definition: llb3Image.c:950
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
for(p=first;p->value< newval;p=p->next)
Extra_MmFixed_t ** pMems
Extra_MmFixed_t ** pMap
Extra_MmFixed_t * Extra_MmFixedStart(int nEntrySize)
void Extra_MmStepStop ( Extra_MmStep_t p)

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

Synopsis [Stops the memory manager.]

Description []

SideEffects []

SeeAlso []

Definition at line 528 of file extraUtilMemory.c.

529 {
530  int i;
531  for ( i = 0; i < p->nMems; i++ )
532  Extra_MmFixedStop( p->pMems[i] );
533  if ( p->pLargeChunks )
534  {
535  for ( i = 0; i < p->nLargeChunks; i++ )
536  ABC_FREE( p->pLargeChunks[i] );
537  ABC_FREE( p->pLargeChunks );
538  }
539  ABC_FREE( p->pMems );
540  ABC_FREE( p->pMap );
541  ABC_FREE( p );
542 }
void Extra_MmFixedStop(Extra_MmFixed_t *p)
Extra_MmFixed_t ** pMems
#define ABC_FREE(obj)
Definition: abc_global.h:232
Extra_MmFixed_t ** pMap