ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
fraigMem.c
Go to the documentation of this file.
1
18
19#include "fraigInt.h"
20
22
23
27
29{
30 // information about individual entries
31 int nEntrySize; // the size of one entry
32 int nEntriesAlloc; // the total number of entries allocated
33 int nEntriesUsed; // the number of entries in use
34 int nEntriesMax; // the max number of entries in use
35 char * pEntriesFree; // the linked list of free entries
36
37 // this is where the memory is stored
38 int nChunkSize; // the size of one chunk
39 int nChunksAlloc; // the maximum number of memory chunks
40 int nChunks; // the current number of memory chunks
41 char ** pChunks; // the allocated memory
42
43 // statistics
44 int nMemoryUsed; // memory used in the allocated entries
45 int nMemoryAlloc; // memory allocated
46};
47
51
64{
66
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}
90
102void Fraig_MemFixedStop( Fraig_MemFixed_t * p, int fVerbose )
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",
112 p->nEntriesUsed, p->nEntriesMax, p->nEntrySize * p->nEntriesUsed, p->nMemoryAlloc );
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}
119
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}
170
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}
190
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}
228
241{
242 return p->nMemoryAlloc;
243}
244
248
249
251
#define ABC_ALLOC(type, num)
Definition abc_global.h:264
#define ABC_REALLOC(type, obj, num)
Definition abc_global.h:268
#define ABC_FREE(obj)
Definition abc_global.h:267
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
Cube * p
Definition exorList.c:222
struct Fraig_MemFixed_t_ Fraig_MemFixed_t
STRUCTURE DEFINITIONS ///.
Definition fraigInt.h:101
int Fraig_MemFixedReadMemUsage(Fraig_MemFixed_t *p)
Definition fraigMem.c:240
Fraig_MemFixed_t * Fraig_MemFixedStart(int nEntrySize)
FUNCTION DEFINITIONS ///.
Definition fraigMem.c:63
void Fraig_MemFixedStop(Fraig_MemFixed_t *p, int fVerbose)
Definition fraigMem.c:102
void Fraig_MemFixedEntryRecycle(Fraig_MemFixed_t *p, char *pEntry)
Definition fraigMem.c:182
char * Fraig_MemFixedEntryFetch(Fraig_MemFixed_t *p)
Definition fraigMem.c:131
void Fraig_MemFixedRestart(Fraig_MemFixed_t *p)
Definition fraigMem.c:202
DECLARATIONS ///.
Definition fraigMem.c:29
char * pEntriesFree
Definition fraigMem.c:35
char ** pChunks
Definition fraigMem.c:41
#define assert(ex)
Definition util_old.h:213
char * memset()