#include "fraigInt.h"
Go to the source code of this file.
Data Structures | |
struct | Fraig_MemFixed_t_ |
Functions | |
Fraig_MemFixed_t * | Fraig_MemFixedStart (int nEntrySize) |
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) |
char* Fraig_MemFixedEntryFetch | ( | Fraig_MemFixed_t * | p | ) |
Function*************************************************************
Synopsis [Extracts one entry from the memory manager.]
Description []
SideEffects []
SeeAlso []
Definition at line 128 of file fraigMem.c.
00129 { 00130 char * pTemp; 00131 int i; 00132 00133 // check if there are still free entries 00134 if ( p->nEntriesUsed == p->nEntriesAlloc ) 00135 { // need to allocate more entries 00136 assert( p->pEntriesFree == NULL ); 00137 if ( p->nChunks == p->nChunksAlloc ) 00138 { 00139 p->nChunksAlloc *= 2; 00140 p->pChunks = REALLOC( char *, p->pChunks, p->nChunksAlloc ); 00141 } 00142 p->pEntriesFree = ALLOC( char, p->nEntrySize * p->nChunkSize ); 00143 p->nMemoryAlloc += p->nEntrySize * p->nChunkSize; 00144 // transform these entries into a linked list 00145 pTemp = p->pEntriesFree; 00146 for ( i = 1; i < p->nChunkSize; i++ ) 00147 { 00148 *((char **)pTemp) = pTemp + p->nEntrySize; 00149 pTemp += p->nEntrySize; 00150 } 00151 // set the last link 00152 *((char **)pTemp) = NULL; 00153 // add the chunk to the chunk storage 00154 p->pChunks[ p->nChunks++ ] = p->pEntriesFree; 00155 // add to the number of entries allocated 00156 p->nEntriesAlloc += p->nChunkSize; 00157 } 00158 // incrememt the counter of used entries 00159 p->nEntriesUsed++; 00160 if ( p->nEntriesMax < p->nEntriesUsed ) 00161 p->nEntriesMax = p->nEntriesUsed; 00162 // return the first entry in the free entry list 00163 pTemp = p->pEntriesFree; 00164 p->pEntriesFree = *((char **)pTemp); 00165 return pTemp; 00166 }
void Fraig_MemFixedEntryRecycle | ( | Fraig_MemFixed_t * | p, | |
char * | pEntry | |||
) |
Function*************************************************************
Synopsis [Returns one entry into the memory manager.]
Description []
SideEffects []
SeeAlso []
Definition at line 179 of file fraigMem.c.
00180 { 00181 // decrement the counter of used entries 00182 p->nEntriesUsed--; 00183 // add the entry to the linked list of free entries 00184 *((char **)pEntry) = p->pEntriesFree; 00185 p->pEntriesFree = pEntry; 00186 }
int Fraig_MemFixedReadMemUsage | ( | Fraig_MemFixed_t * | p | ) |
Function*************************************************************
Synopsis [Reports the memory usage.]
Description []
SideEffects []
SeeAlso []
Definition at line 237 of file fraigMem.c.
00238 { 00239 return p->nMemoryAlloc; 00240 }
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 199 of file fraigMem.c.
00200 { 00201 int i; 00202 char * pTemp; 00203 00204 // deallocate all chunks except the first one 00205 for ( i = 1; i < p->nChunks; i++ ) 00206 free( p->pChunks[i] ); 00207 p->nChunks = 1; 00208 // transform these entries into a linked list 00209 pTemp = p->pChunks[0]; 00210 for ( i = 1; i < p->nChunkSize; i++ ) 00211 { 00212 *((char **)pTemp) = pTemp + p->nEntrySize; 00213 pTemp += p->nEntrySize; 00214 } 00215 // set the last link 00216 *((char **)pTemp) = NULL; 00217 // set the free entry list 00218 p->pEntriesFree = p->pChunks[0]; 00219 // set the correct statistics 00220 p->nMemoryAlloc = p->nEntrySize * p->nChunkSize; 00221 p->nMemoryUsed = 0; 00222 p->nEntriesAlloc = p->nChunkSize; 00223 p->nEntriesUsed = 0; 00224 }
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 60 of file fraigMem.c.
00061 { 00062 Fraig_MemFixed_t * p; 00063 00064 p = ALLOC( Fraig_MemFixed_t, 1 ); 00065 memset( p, 0, sizeof(Fraig_MemFixed_t) ); 00066 00067 p->nEntrySize = nEntrySize; 00068 p->nEntriesAlloc = 0; 00069 p->nEntriesUsed = 0; 00070 p->pEntriesFree = NULL; 00071 00072 if ( nEntrySize * (1 << 10) < (1<<16) ) 00073 p->nChunkSize = (1 << 10); 00074 else 00075 p->nChunkSize = (1<<16) / nEntrySize; 00076 if ( p->nChunkSize < 8 ) 00077 p->nChunkSize = 8; 00078 00079 p->nChunksAlloc = 64; 00080 p->nChunks = 0; 00081 p->pChunks = ALLOC( char *, p->nChunksAlloc ); 00082 00083 p->nMemoryUsed = 0; 00084 p->nMemoryAlloc = 0; 00085 return p; 00086 }
void Fraig_MemFixedStop | ( | Fraig_MemFixed_t * | p, | |
int | fVerbose | |||
) |
Function*************************************************************
Synopsis [Stops the internal memory manager.]
Description []
SideEffects []
SeeAlso []
Definition at line 99 of file fraigMem.c.
00100 { 00101 int i; 00102 if ( p == NULL ) 00103 return; 00104 if ( fVerbose ) 00105 { 00106 printf( "Fixed memory manager: Entry = %5d. Chunk = %5d. Chunks used = %5d.\n", 00107 p->nEntrySize, p->nChunkSize, p->nChunks ); 00108 printf( " Entries used = %8d. Entries peak = %8d. Memory used = %8d. Memory alloc = %8d.\n", 00109 p->nEntriesUsed, p->nEntriesMax, p->nEntrySize * p->nEntriesUsed, p->nMemoryAlloc ); 00110 } 00111 for ( i = 0; i < p->nChunks; i++ ) 00112 free( p->pChunks[i] ); 00113 free( p->pChunks ); 00114 free( p ); 00115 }