ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
aigMem.c
Go to the documentation of this file.
1
20
21#include "aig.h"
22
24
25
29
31{
32 // information about individual entries
33 int nEntrySize; // the size of one entry
34 int nEntriesAlloc; // the total number of entries allocated
35 int nEntriesUsed; // the number of entries in use
36 int nEntriesMax; // the max number of entries in use
37 char * pEntriesFree; // the linked list of free entries
38
39 // this is where the memory is stored
40 int nChunkSize; // the size of one chunk
41 int nChunksAlloc; // the maximum number of memory chunks
42 int nChunks; // the current number of memory chunks
43 char ** pChunks; // the allocated memory
44
45 // statistics
46 int nMemoryUsed; // memory used in the allocated entries
47 int nMemoryAlloc; // memory allocated
48};
49
51{
52 // information about individual entries
53 int nEntriesUsed; // the number of entries allocated
54 char * pCurrent; // the current pointer to free memory
55 char * pEnd; // the first entry outside the free memory
56
57 // this is where the memory is stored
58 int nChunkSize; // the size of one chunk
59 int nChunksAlloc; // the maximum number of memory chunks
60 int nChunks; // the current number of memory chunks
61 char ** pChunks; // the allocated memory
62
63 // statistics
64 int nMemoryUsed; // memory used in the allocated entries
65 int nMemoryAlloc; // memory allocated
66};
67
69{
70 int nMems; // the number of fixed memory managers employed
71 Aig_MmFixed_t ** pMems; // memory managers: 2^1 words, 2^2 words, etc
72 int nMapSize; // the size of the memory array
73 Aig_MmFixed_t ** pMap; // maps the number of bytes into its memory manager
74 // additional memory chunks
75 int nChunksAlloc; // the maximum number of memory chunks
76 int nChunks; // the current number of memory chunks
77 char ** pChunks; // the allocated memory
78};
79
83
96Aig_MmFixed_t * Aig_MmFixedStart( int nEntrySize, int nEntriesMax )
97{
99
100 p = ABC_ALLOC( Aig_MmFixed_t, 1 );
101 memset( p, 0, sizeof(Aig_MmFixed_t) );
102
103 p->nEntrySize = nEntrySize;
104 p->nEntriesAlloc = 0;
105 p->nEntriesUsed = 0;
106 p->pEntriesFree = NULL;
107
108 p->nChunkSize = nEntriesMax / 8;
109 if ( p->nChunkSize < 8 )
110 p->nChunkSize = 8;
111
112 p->nChunksAlloc = 64;
113 p->nChunks = 0;
114 p->pChunks = ABC_ALLOC( char *, p->nChunksAlloc );
115
116 p->nMemoryUsed = 0;
117 p->nMemoryAlloc = 0;
118 return p;
119}
120
132void Aig_MmFixedStop( Aig_MmFixed_t * p, int fVerbose )
133{
134 int i;
135 if ( p == NULL )
136 return;
137 if ( fVerbose )
138 {
139 printf( "Fixed memory manager: Entry = %5d. Chunk = %5d. Chunks used = %5d.\n",
140 p->nEntrySize, p->nChunkSize, p->nChunks );
141 printf( " Entries used = %8d. Entries peak = %8d. Memory used = %8d. Memory alloc = %8d.\n",
142 p->nEntriesUsed, p->nEntriesMax, p->nEntrySize * p->nEntriesUsed, p->nMemoryAlloc );
143 }
144 for ( i = 0; i < p->nChunks; i++ )
145 ABC_FREE( p->pChunks[i] );
146 ABC_FREE( p->pChunks );
147 ABC_FREE( p );
148}
149
162{
163 char * pTemp;
164 int i;
165
166 // check if there are still free entries
167 if ( p->nEntriesUsed == p->nEntriesAlloc )
168 { // need to allocate more entries
169 assert( p->pEntriesFree == NULL );
170 if ( p->nChunks == p->nChunksAlloc )
171 {
172 p->nChunksAlloc *= 2;
173 p->pChunks = ABC_REALLOC( char *, p->pChunks, p->nChunksAlloc );
174 }
175 p->pEntriesFree = ABC_ALLOC( char, p->nEntrySize * p->nChunkSize );
176 p->nMemoryAlloc += p->nEntrySize * p->nChunkSize;
177 // transform these entries into a linked list
178 pTemp = p->pEntriesFree;
179 for ( i = 1; i < p->nChunkSize; i++ )
180 {
181 *((char **)pTemp) = pTemp + p->nEntrySize;
182 pTemp += p->nEntrySize;
183 }
184 // set the last link
185 *((char **)pTemp) = NULL;
186 // add the chunk to the chunk storage
187 p->pChunks[ p->nChunks++ ] = p->pEntriesFree;
188 // add to the number of entries allocated
189 p->nEntriesAlloc += p->nChunkSize;
190 }
191 // incrememt the counter of used entries
192 p->nEntriesUsed++;
193 if ( p->nEntriesMax < p->nEntriesUsed )
194 p->nEntriesMax = p->nEntriesUsed;
195 // return the first entry in the free entry list
196 pTemp = p->pEntriesFree;
197 p->pEntriesFree = *((char **)pTemp);
198 return pTemp;
199}
200
213{
214 // decrement the counter of used entries
215 p->nEntriesUsed--;
216 // add the entry to the linked list of free entries
217 *((char **)pEntry) = p->pEntriesFree;
218 p->pEntriesFree = pEntry;
219}
220
233{
234 int i;
235 char * pTemp;
236 if ( p->nChunks == 0 )
237 return;
238 // deallocate all chunks except the first one
239 for ( i = 1; i < p->nChunks; i++ )
240 ABC_FREE( p->pChunks[i] );
241 p->nChunks = 1;
242 // transform these entries into a linked list
243 pTemp = p->pChunks[0];
244 for ( i = 1; i < p->nChunkSize; i++ )
245 {
246 *((char **)pTemp) = pTemp + p->nEntrySize;
247 pTemp += p->nEntrySize;
248 }
249 // set the last link
250 *((char **)pTemp) = NULL;
251 // set the free entry list
252 p->pEntriesFree = p->pChunks[0];
253 // set the correct statistics
254 p->nMemoryAlloc = p->nEntrySize * p->nChunkSize;
255 p->nMemoryUsed = 0;
256 p->nEntriesAlloc = p->nChunkSize;
257 p->nEntriesUsed = 0;
258}
259
272{
273 return p->nMemoryAlloc;
274}
275
288{
289 return p->nEntriesMax;
290}
291
292
293
306{
307 Aig_MmFlex_t * p;
308
309 p = ABC_ALLOC( Aig_MmFlex_t, 1 );
310 memset( p, 0, sizeof(Aig_MmFlex_t) );
311
312 p->nEntriesUsed = 0;
313 p->pCurrent = NULL;
314 p->pEnd = NULL;
315
316 p->nChunkSize = (1 << 18);
317 p->nChunksAlloc = 64;
318 p->nChunks = 0;
319 p->pChunks = ABC_ALLOC( char *, p->nChunksAlloc );
320
321 p->nMemoryUsed = 0;
322 p->nMemoryAlloc = 0;
323 return p;
324}
325
337void Aig_MmFlexStop( Aig_MmFlex_t * p, int fVerbose )
338{
339 int i;
340 if ( p == NULL )
341 return;
342 if ( fVerbose )
343 {
344 printf( "Flexible memory manager: Chunk size = %d. Chunks used = %d.\n",
345 p->nChunkSize, p->nChunks );
346 printf( " Entries used = %d. Memory used = %d. Memory alloc = %d.\n",
347 p->nEntriesUsed, p->nMemoryUsed, p->nMemoryAlloc );
348 }
349 for ( i = 0; i < p->nChunks; i++ )
350 ABC_FREE( p->pChunks[i] );
351 ABC_FREE( p->pChunks );
352 ABC_FREE( p );
353}
354
366char * Aig_MmFlexEntryFetch( Aig_MmFlex_t * p, int nBytes )
367{
368 char * pTemp;
369#ifdef ABC_MEMALIGN
370 // extend size to max alignment
371 nBytes += (ABC_MEMALIGN - nBytes % ABC_MEMALIGN) % ABC_MEMALIGN;
372#endif
373 // check if there are still free entries
374 if ( p->pCurrent == NULL || p->pCurrent + nBytes > p->pEnd )
375 { // need to allocate more entries
376 if ( p->nChunks == p->nChunksAlloc )
377 {
378 p->nChunksAlloc *= 2;
379 p->pChunks = ABC_REALLOC( char *, p->pChunks, p->nChunksAlloc );
380 }
381 if ( nBytes > p->nChunkSize )
382 {
383 // resize the chunk size if more memory is requested than it can give
384 // (ideally, this should never happen)
385 p->nChunkSize = 2 * nBytes;
386 }
387 p->pCurrent = ABC_ALLOC( char, p->nChunkSize );
388 p->pEnd = p->pCurrent + p->nChunkSize;
389 p->nMemoryAlloc += p->nChunkSize;
390 // add the chunk to the chunk storage
391 p->pChunks[ p->nChunks++ ] = p->pCurrent;
392 }
393 assert( p->pCurrent + nBytes <= p->pEnd );
394 // increment the counter of used entries
395 p->nEntriesUsed++;
396 // keep track of the memory used
397 p->nMemoryUsed += nBytes;
398 // return the next entry
399 pTemp = p->pCurrent;
400 p->pCurrent += nBytes;
401 return pTemp;
402}
403
416{
417 int i;
418 if ( p->nChunks == 0 )
419 return;
420 // deallocate all chunks except the first one
421 for ( i = 1; i < p->nChunks; i++ )
422 ABC_FREE( p->pChunks[i] );
423 p->nChunks = 1;
424 p->nMemoryAlloc = p->nChunkSize;
425 // transform these entries into a linked list
426 p->pCurrent = p->pChunks[0];
427 p->pEnd = p->pCurrent + p->nChunkSize;
428 p->nEntriesUsed = 0;
429 p->nMemoryUsed = 0;
430}
431
444{
445 return p->nMemoryUsed;
446}
447
448
449
450
451
473{
474 Aig_MmStep_t * p;
475 int i, k;
476 p = ABC_ALLOC( Aig_MmStep_t, 1 );
477 memset( p, 0, sizeof(Aig_MmStep_t) );
478 p->nMems = nSteps;
479 // start the fixed memory managers
480 p->pMems = ABC_ALLOC( Aig_MmFixed_t *, p->nMems );
481 for ( i = 0; i < p->nMems; i++ )
482 p->pMems[i] = Aig_MmFixedStart( (8<<i), (1<<13) );
483 // set up the mapping of the required memory size into the corresponding manager
484 p->nMapSize = (4<<p->nMems);
485 p->pMap = ABC_ALLOC( Aig_MmFixed_t *, p->nMapSize+1 );
486 p->pMap[0] = NULL;
487 for ( k = 1; k <= 4; k++ )
488 p->pMap[k] = p->pMems[0];
489 for ( i = 0; i < p->nMems; i++ )
490 for ( k = (4<<i)+1; k <= (8<<i); k++ )
491 p->pMap[k] = p->pMems[i];
492//for ( i = 1; i < 100; i ++ )
493//printf( "%10d: size = %10d\n", i, p->pMap[i]->nEntrySize );
494 p->nChunksAlloc = 64;
495 p->nChunks = 0;
496 p->pChunks = ABC_ALLOC( char *, p->nChunksAlloc );
497 return p;
498}
499
511void Aig_MmStepStop( Aig_MmStep_t * p, int fVerbose )
512{
513 int i;
514 for ( i = 0; i < p->nMems; i++ )
515 Aig_MmFixedStop( p->pMems[i], fVerbose );
516 if ( p->nChunksAlloc )
517 {
518 for ( i = 0; i < p->nChunks; i++ )
519 ABC_FREE( p->pChunks[i] );
520 ABC_FREE( p->pChunks );
521 }
522 ABC_FREE( p->pMems );
523 ABC_FREE( p->pMap );
524 ABC_FREE( p );
525}
526
538char * Aig_MmStepEntryFetch( Aig_MmStep_t * p, int nBytes )
539{
540 if ( nBytes == 0 )
541 return NULL;
542#ifdef ABC_MEMALIGN
543 // extend size to max alignment
544 nBytes += (ABC_MEMALIGN - nBytes % ABC_MEMALIGN) % ABC_MEMALIGN;
545#endif
546 if ( nBytes > p->nMapSize )
547 {
548 if ( p->nChunks == p->nChunksAlloc )
549 {
550 p->nChunksAlloc *= 2;
551 p->pChunks = ABC_REALLOC( char *, p->pChunks, p->nChunksAlloc );
552 }
553 p->pChunks[ p->nChunks++ ] = ABC_ALLOC( char, nBytes );
554 return p->pChunks[p->nChunks-1];
555 }
556 return Aig_MmFixedEntryFetch( p->pMap[nBytes] );
557}
558
559
571void Aig_MmStepEntryRecycle( Aig_MmStep_t * p, char * pEntry, int nBytes )
572{
573 if ( nBytes == 0 )
574 return;
575#ifdef ABC_MEMALIGN
576 // extend size to max alignment
577 nBytes += (ABC_MEMALIGN - nBytes % ABC_MEMALIGN) % ABC_MEMALIGN;
578#endif
579 if ( nBytes > p->nMapSize )
580 {
581// ABC_FREE( pEntry );
582 return;
583 }
584 Aig_MmFixedEntryRecycle( p->pMap[nBytes], pEntry );
585}
586
599{
600 int i, nMemTotal = 0;
601 for ( i = 0; i < p->nMems; i++ )
602 nMemTotal += p->pMems[i]->nMemoryAlloc;
603 return nMemTotal;
604}
605
610
#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
int Aig_MmStepReadMemUsage(Aig_MmStep_t *p)
Definition aigMem.c:598
void Aig_MmFlexRestart(Aig_MmFlex_t *p)
Definition aigMem.c:415
void Aig_MmFlexStop(Aig_MmFlex_t *p, int fVerbose)
Definition aigMem.c:337
int Aig_MmFixedReadMemUsage(Aig_MmFixed_t *p)
Definition aigMem.c:271
char * Aig_MmStepEntryFetch(Aig_MmStep_t *p, int nBytes)
Definition aigMem.c:538
char * Aig_MmFixedEntryFetch(Aig_MmFixed_t *p)
Definition aigMem.c:161
char * Aig_MmFlexEntryFetch(Aig_MmFlex_t *p, int nBytes)
Definition aigMem.c:366
int Aig_MmFixedReadMaxEntriesUsed(Aig_MmFixed_t *p)
Definition aigMem.c:287
Aig_MmFixed_t * Aig_MmFixedStart(int nEntrySize, int nEntriesMax)
FUNCTION DEFINITIONS ///.
Definition aigMem.c:96
void Aig_MmStepStop(Aig_MmStep_t *p, int fVerbose)
Definition aigMem.c:511
void Aig_MmFixedRestart(Aig_MmFixed_t *p)
Definition aigMem.c:232
Aig_MmFlex_t * Aig_MmFlexStart()
Definition aigMem.c:305
void Aig_MmFixedEntryRecycle(Aig_MmFixed_t *p, char *pEntry)
Definition aigMem.c:212
int Aig_MmFlexReadMemUsage(Aig_MmFlex_t *p)
Definition aigMem.c:443
Aig_MmStep_t * Aig_MmStepStart(int nSteps)
Definition aigMem.c:472
void Aig_MmFixedStop(Aig_MmFixed_t *p, int fVerbose)
Definition aigMem.c:132
void Aig_MmStepEntryRecycle(Aig_MmStep_t *p, char *pEntry, int nBytes)
Definition aigMem.c:571
struct Aig_MmFlex_t_ Aig_MmFlex_t
Definition aig.h:53
Aig_MmFixed_t * Aig_MmFixedStart(int nEntrySize, int nEntriesMax)
FUNCTION DEFINITIONS ///.
Definition aigMem.c:96
struct Aig_MmFixed_t_ Aig_MmFixed_t
Definition aig.h:52
struct Aig_MmStep_t_ Aig_MmStep_t
Definition aig.h:54
Cube * p
Definition exorList.c:222
DECLARATIONS ///.
Definition aigMem.c:31
int nEntriesMax
Definition aigMem.c:36
int nMemoryAlloc
Definition aigMem.c:47
int nChunkSize
Definition aigMem.c:40
char ** pChunks
Definition aigMem.c:43
int nChunksAlloc
Definition aigMem.c:41
int nMemoryUsed
Definition aigMem.c:46
int nEntrySize
Definition aigMem.c:33
int nEntriesUsed
Definition aigMem.c:35
char * pEntriesFree
Definition aigMem.c:37
int nEntriesAlloc
Definition aigMem.c:34
int nChunkSize
Definition aigMem.c:58
int nMemoryUsed
Definition aigMem.c:64
int nChunks
Definition aigMem.c:60
char * pEnd
Definition aigMem.c:55
char * pCurrent
Definition aigMem.c:54
int nEntriesUsed
Definition aigMem.c:53
int nChunksAlloc
Definition aigMem.c:59
int nMemoryAlloc
Definition aigMem.c:65
char ** pChunks
Definition aigMem.c:61
int nChunksAlloc
Definition aigMem.c:75
Aig_MmFixed_t ** pMems
Definition aigMem.c:71
Aig_MmFixed_t ** pMap
Definition aigMem.c:73
int nChunks
Definition aigMem.c:76
int nMapSize
Definition aigMem.c:72
char ** pChunks
Definition aigMem.c:77
#define assert(ex)
Definition util_old.h:213
char * memset()