abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
mem.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "mem.h"

Go to the source code of this file.

Data Structures

struct  Mem_Fixed_t_
 DECLARATIONS ///. More...
 
struct  Mem_Flex_t_
 
struct  Mem_Step_t_
 

Functions

Mem_Fixed_tMem_FixedStart (int nEntrySize)
 FUNCTION DEFINITIONS ///. More...
 
void Mem_FixedStop (Mem_Fixed_t *p, int fVerbose)
 
char * Mem_FixedEntryFetch (Mem_Fixed_t *p)
 
void Mem_FixedEntryRecycle (Mem_Fixed_t *p, char *pEntry)
 
void Mem_FixedRestart (Mem_Fixed_t *p)
 
int Mem_FixedReadMemUsage (Mem_Fixed_t *p)
 
int Mem_FixedReadMaxEntriesUsed (Mem_Fixed_t *p)
 
Mem_Flex_tMem_FlexStart ()
 
void Mem_FlexStop (Mem_Flex_t *p, int fVerbose)
 
char * Mem_FlexEntryFetch (Mem_Flex_t *p, int nBytes)
 
void Mem_FlexRestart (Mem_Flex_t *p)
 
int Mem_FlexReadMemUsage (Mem_Flex_t *p)
 
Mem_Step_tMem_StepStart (int nSteps)
 
void Mem_StepStop (Mem_Step_t *p, int fVerbose)
 
char * Mem_StepEntryFetch (Mem_Step_t *p, int nBytes)
 
void Mem_StepEntryRecycle (Mem_Step_t *p, char *pEntry, int nBytes)
 
int Mem_StepReadMemUsage (Mem_Step_t *p)
 

Function Documentation

char* Mem_FixedEntryFetch ( Mem_Fixed_t p)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 168 of file mem.c.

169 {
170  char * pTemp;
171  int i;
172 
173  // check if there are still free entries
174  if ( p->nEntriesUsed == p->nEntriesAlloc )
175  { // need to allocate more entries
176  assert( p->pEntriesFree == NULL );
177  if ( p->nChunks == p->nChunksAlloc )
178  {
179  p->nChunksAlloc *= 2;
180  p->pChunks = ABC_REALLOC( char *, p->pChunks, p->nChunksAlloc );
181  }
182  p->pEntriesFree = ABC_ALLOC( char, p->nEntrySize * p->nChunkSize );
183  p->nMemoryAlloc += p->nEntrySize * p->nChunkSize;
184  // transform these entries into a linked list
185  pTemp = p->pEntriesFree;
186  for ( i = 1; i < p->nChunkSize; i++ )
187  {
188  *((char **)pTemp) = pTemp + p->nEntrySize;
189  pTemp += p->nEntrySize;
190  }
191  // set the last link
192  *((char **)pTemp) = NULL;
193  // add the chunk to the chunk storage
194  p->pChunks[ p->nChunks++ ] = p->pEntriesFree;
195  // add to the number of entries allocated
196  p->nEntriesAlloc += p->nChunkSize;
197  }
198  // incrememt the counter of used entries
199  p->nEntriesUsed++;
200  if ( p->nEntriesMax < p->nEntriesUsed )
201  p->nEntriesMax = p->nEntriesUsed;
202  // return the first entry in the free entry list
203  pTemp = p->pEntriesFree;
204  p->pEntriesFree = *((char **)pTemp);
205  return pTemp;
206 }
static Llb_Mgr_t * p
Definition: llb3Image.c:950
#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 Mem_FixedEntryRecycle ( Mem_Fixed_t p,
char *  pEntry 
)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 219 of file mem.c.

220 {
221  // decrement the counter of used entries
222  p->nEntriesUsed--;
223  // add the entry to the linked list of free entries
224  *((char **)pEntry) = p->pEntriesFree;
225  p->pEntriesFree = pEntry;
226 }
static Llb_Mgr_t * p
Definition: llb3Image.c:950
int Mem_FixedReadMaxEntriesUsed ( Mem_Fixed_t p)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 293 of file mem.c.

294 {
295  return p->nEntriesMax;
296 }
static Llb_Mgr_t * p
Definition: llb3Image.c:950
int Mem_FixedReadMemUsage ( Mem_Fixed_t p)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 277 of file mem.c.

278 {
279  return p->nMemoryAlloc;
280 }
static Llb_Mgr_t * p
Definition: llb3Image.c:950
void Mem_FixedRestart ( Mem_Fixed_t p)

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

Synopsis []

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

SideEffects []

SeeAlso []

Definition at line 239 of file mem.c.

240 {
241  int i;
242  char * pTemp;
243 
244  // deallocate all chunks except the first one
245  for ( i = 1; i < p->nChunks; i++ )
246  ABC_FREE( p->pChunks[i] );
247  p->nChunks = 1;
248  // transform these entries into a linked list
249  pTemp = p->pChunks[0];
250  for ( i = 1; i < p->nChunkSize; i++ )
251  {
252  *((char **)pTemp) = pTemp + p->nEntrySize;
253  pTemp += p->nEntrySize;
254  }
255  // set the last link
256  *((char **)pTemp) = NULL;
257  // set the free entry list
258  p->pEntriesFree = p->pChunks[0];
259  // set the correct statistics
260  p->nMemoryAlloc = p->nEntrySize * p->nChunkSize;
261  p->nMemoryUsed = 0;
262  p->nEntriesAlloc = p->nChunkSize;
263  p->nEntriesUsed = 0;
264 }
static Llb_Mgr_t * p
Definition: llb3Image.c:950
#define ABC_FREE(obj)
Definition: abc_global.h:232
Mem_Fixed_t* Mem_FixedStart ( 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 100 of file mem.c.

101 {
102  Mem_Fixed_t * p;
103 
104  p = ABC_ALLOC( Mem_Fixed_t, 1 );
105  memset( p, 0, sizeof(Mem_Fixed_t) );
106 
107  p->nEntrySize = nEntrySize;
108  p->nEntriesAlloc = 0;
109  p->nEntriesUsed = 0;
110  p->pEntriesFree = NULL;
111 
112  if ( nEntrySize * (1 << 10) < (1<<16) )
113  p->nChunkSize = (1 << 10);
114  else
115  p->nChunkSize = (1<<16) / nEntrySize;
116  if ( p->nChunkSize < 8 )
117  p->nChunkSize = 8;
118 
119  p->nChunksAlloc = 64;
120  p->nChunks = 0;
121  p->pChunks = ABC_ALLOC( char *, p->nChunksAlloc );
122 
123  p->nMemoryUsed = 0;
124  p->nMemoryAlloc = 0;
125  return p;
126 }
char * memset()
static Llb_Mgr_t * p
Definition: llb3Image.c:950
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
typedefABC_NAMESPACE_HEADER_START struct Mem_Fixed_t_ Mem_Fixed_t
DECLARATIONS ///.
Definition: mem.h:33
void Mem_FixedStop ( Mem_Fixed_t p,
int  fVerbose 
)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 139 of file mem.c.

140 {
141  int i;
142  if ( p == NULL )
143  return;
144  if ( fVerbose )
145  {
146  printf( "Fixed memory manager: Entry = %5d. Chunk = %5d. Chunks used = %5d.\n",
147  p->nEntrySize, p->nChunkSize, p->nChunks );
148  printf( " Entries used = %8d. Entries peak = %8d. Memory used = %8d. Memory alloc = %8d.\n",
149  p->nEntriesUsed, p->nEntriesMax, p->nEntrySize * p->nEntriesUsed, p->nMemoryAlloc );
150  }
151  for ( i = 0; i < p->nChunks; i++ )
152  ABC_FREE( p->pChunks[i] );
153  ABC_FREE( p->pChunks );
154  ABC_FREE( p );
155 }
static Llb_Mgr_t * p
Definition: llb3Image.c:950
#define ABC_FREE(obj)
Definition: abc_global.h:232
char* Mem_FlexEntryFetch ( Mem_Flex_t p,
int  nBytes 
)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 372 of file mem.c.

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

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 445 of file mem.c.

446 {
447  return p->nMemoryUsed;
448 }
int nMemoryUsed
Definition: mem.c:69
void Mem_FlexRestart ( Mem_Flex_t p)

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

Synopsis []

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

SideEffects []

SeeAlso []

Definition at line 417 of file mem.c.

418 {
419  int i;
420  if ( p->nChunks == 0 )
421  return;
422  // deallocate all chunks except the first one
423  for ( i = 1; i < p->nChunks; i++ )
424  ABC_FREE( p->pChunks[i] );
425  p->nChunks = 1;
426  p->nMemoryAlloc = p->nChunkSize;
427  // transform these entries into a linked list
428  p->pCurrent = p->pChunks[0];
429  p->pEnd = p->pCurrent + p->nChunkSize;
430  p->nEntriesUsed = 0;
431  p->nMemoryUsed = 0;
432 }
int nChunks
Definition: mem.c:65
int nMemoryUsed
Definition: mem.c:69
char * pCurrent
Definition: mem.c:59
int nEntriesUsed
Definition: mem.c:58
char ** pChunks
Definition: mem.c:66
int nMemoryAlloc
Definition: mem.c:70
#define ABC_FREE(obj)
Definition: abc_global.h:232
int nChunkSize
Definition: mem.c:63
char * pEnd
Definition: mem.c:60
Mem_Flex_t* Mem_FlexStart ( )

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

Synopsis [Allocates entries of flexible size.]

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

SideEffects []

SeeAlso []

Definition at line 311 of file mem.c.

312 {
313  Mem_Flex_t * p;
314 
315  p = ABC_ALLOC( Mem_Flex_t, 1 );
316  memset( p, 0, sizeof(Mem_Flex_t) );
317 
318  p->nEntriesUsed = 0;
319  p->pCurrent = NULL;
320  p->pEnd = NULL;
321 
322  p->nChunkSize = (1 << 12);
323  p->nChunksAlloc = 64;
324  p->nChunks = 0;
325  p->pChunks = ABC_ALLOC( char *, p->nChunksAlloc );
326 
327  p->nMemoryUsed = 0;
328  p->nMemoryAlloc = 0;
329  return p;
330 }
char * memset()
int nChunks
Definition: mem.c:65
static Llb_Mgr_t * p
Definition: llb3Image.c:950
int nMemoryUsed
Definition: mem.c:69
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
char * pCurrent
Definition: mem.c:59
int nEntriesUsed
Definition: mem.c:58
char ** pChunks
Definition: mem.c:66
int nMemoryAlloc
Definition: mem.c:70
int nChunksAlloc
Definition: mem.c:64
int nChunkSize
Definition: mem.c:63
char * pEnd
Definition: mem.c:60
void Mem_FlexStop ( Mem_Flex_t p,
int  fVerbose 
)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 343 of file mem.c.

344 {
345  int i;
346  if ( p == NULL )
347  return;
348  if ( fVerbose )
349  {
350  printf( "Flexible memory manager: Chunk size = %d. Chunks used = %d.\n",
351  p->nChunkSize, p->nChunks );
352  printf( " Entries used = %d. Memory used = %d. Memory alloc = %d.\n",
354  }
355  for ( i = 0; i < p->nChunks; i++ )
356  ABC_FREE( p->pChunks[i] );
357  ABC_FREE( p->pChunks );
358  ABC_FREE( p );
359 }
int nChunks
Definition: mem.c:65
int nMemoryUsed
Definition: mem.c:69
int nEntriesUsed
Definition: mem.c:58
char ** pChunks
Definition: mem.c:66
int nMemoryAlloc
Definition: mem.c:70
#define ABC_FREE(obj)
Definition: abc_global.h:232
int nChunkSize
Definition: mem.c:63
char* Mem_StepEntryFetch ( Mem_Step_t p,
int  nBytes 
)

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

Synopsis [Creates the entry.]

Description []

SideEffects []

SeeAlso []

Definition at line 537 of file mem.c.

538 {
539  if ( nBytes == 0 )
540  return NULL;
541  if ( nBytes > p->nMapSize )
542  {
543 // printf( "Allocating %d bytes.\n", nBytes );
544 // return ABC_ALLOC( char, nBytes );
545  if ( p->nLargeChunks == p->nLargeChunksAlloc )
546  {
547  if ( p->nLargeChunksAlloc == 0 )
548  p->nLargeChunksAlloc = 32;
549  p->nLargeChunksAlloc *= 2;
550  p->pLargeChunks = (void **)ABC_REALLOC( char *, p->pLargeChunks, p->nLargeChunksAlloc );
551  }
552  p->pLargeChunks[ p->nLargeChunks++ ] = ABC_ALLOC( char, nBytes );
553  return (char *)p->pLargeChunks[ p->nLargeChunks - 1 ];
554  }
555  return Mem_FixedEntryFetch( p->pMap[nBytes] );
556 }
#define ABC_REALLOC(type, obj, num)
Definition: abc_global.h:233
Mem_Fixed_t ** pMap
Definition: mem.c:78
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
int nLargeChunks
Definition: mem.c:80
void ** pLargeChunks
Definition: mem.c:81
int nMapSize
Definition: mem.c:77
int nLargeChunksAlloc
Definition: mem.c:79
char * Mem_FixedEntryFetch(Mem_Fixed_t *p)
Definition: mem.c:168
void Mem_StepEntryRecycle ( Mem_Step_t p,
char *  pEntry,
int  nBytes 
)

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

Synopsis [Recycles the entry.]

Description []

SideEffects []

SeeAlso []

Definition at line 570 of file mem.c.

571 {
572  if ( nBytes == 0 )
573  return;
574  if ( nBytes > p->nMapSize )
575  {
576 // ABC_FREE( pEntry );
577  return;
578  }
579  Mem_FixedEntryRecycle( p->pMap[nBytes], pEntry );
580 }
Mem_Fixed_t ** pMap
Definition: mem.c:78
void Mem_FixedEntryRecycle(Mem_Fixed_t *p, char *pEntry)
Definition: mem.c:219
int nMapSize
Definition: mem.c:77
int Mem_StepReadMemUsage ( Mem_Step_t p)

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 593 of file mem.c.

594 {
595  int i, nMemTotal = 0;
596  for ( i = 0; i < p->nMems; i++ )
597  nMemTotal += p->pMems[i]->nMemoryAlloc;
598  return nMemTotal;
599 }
int nMems
Definition: mem.c:75
Mem_Fixed_t ** pMems
Definition: mem.c:76
Mem_Step_t* Mem_StepStart ( 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 474 of file mem.c.

475 {
476  Mem_Step_t * p;
477  int i, k;
478  p = ABC_ALLOC( Mem_Step_t, 1 );
479  memset( p, 0, sizeof(Mem_Step_t) );
480  p->nMems = nSteps;
481  // start the fixed memory managers
482  p->pMems = ABC_ALLOC( Mem_Fixed_t *, p->nMems );
483  for ( i = 0; i < p->nMems; i++ )
484  p->pMems[i] = Mem_FixedStart( (8<<i) );
485  // set up the mapping of the required memory size into the corresponding manager
486  p->nMapSize = (4<<p->nMems);
487  p->pMap = ABC_ALLOC( Mem_Fixed_t *, p->nMapSize+1 );
488  p->pMap[0] = NULL;
489  for ( k = 1; k <= 4; k++ )
490  p->pMap[k] = p->pMems[0];
491  for ( i = 0; i < p->nMems; i++ )
492  for ( k = (4<<i)+1; k <= (8<<i); k++ )
493  p->pMap[k] = p->pMems[i];
494 //for ( i = 1; i < 100; i ++ )
495 //printf( "%10d: size = %10d\n", i, p->pMap[i]->nEntrySize );
496  return p;
497 }
char * memset()
static Llb_Mgr_t * p
Definition: llb3Image.c:950
Mem_Fixed_t ** pMap
Definition: mem.c:78
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
int nMems
Definition: mem.c:75
for(p=first;p->value< newval;p=p->next)
Mem_Fixed_t * Mem_FixedStart(int nEntrySize)
FUNCTION DEFINITIONS ///.
Definition: mem.c:100
typedefABC_NAMESPACE_HEADER_START struct Mem_Fixed_t_ Mem_Fixed_t
DECLARATIONS ///.
Definition: mem.h:33
Mem_Fixed_t ** pMems
Definition: mem.c:76
int nMapSize
Definition: mem.c:77
void Mem_StepStop ( Mem_Step_t p,
int  fVerbose 
)

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

Synopsis [Stops the memory manager.]

Description []

SideEffects []

SeeAlso []

Definition at line 510 of file mem.c.

511 {
512  int i;
513  for ( i = 0; i < p->nMems; i++ )
514  Mem_FixedStop( p->pMems[i], fVerbose );
515  if ( p->pLargeChunks )
516  {
517  for ( i = 0; i < p->nLargeChunks; i++ )
518  ABC_FREE( p->pLargeChunks[i] );
519  ABC_FREE( p->pLargeChunks );
520  }
521  ABC_FREE( p->pMems );
522  ABC_FREE( p->pMap );
523  ABC_FREE( p );
524 }
Mem_Fixed_t ** pMap
Definition: mem.c:78
void Mem_FixedStop(Mem_Fixed_t *p, int fVerbose)
Definition: mem.c:139
int nLargeChunks
Definition: mem.c:80
int nMems
Definition: mem.c:75
void ** pLargeChunks
Definition: mem.c:81
Mem_Fixed_t ** pMems
Definition: mem.c:76
#define ABC_FREE(obj)
Definition: abc_global.h:232