ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
mem.c
Go to the documentation of this file.
1
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <assert.h>
25
26#include "mem.h"
27
29
30
34
36{
37 // information about individual entries
38 int nEntrySize; // the size of one entry
39 int nEntriesAlloc; // the total number of entries allocated
40 int nEntriesUsed; // the number of entries in use
41 int nEntriesMax; // the max number of entries in use
42 char * pEntriesFree; // the linked list of free entries
43
44 // this is where the memory is stored
45 int nChunkSize; // the size of one chunk
46 int nChunksAlloc; // the maximum number of memory chunks
47 int nChunks; // the current number of memory chunks
48 char ** pChunks; // the allocated memory
49
50 // statistics
51 int nMemoryUsed; // memory used in the allocated entries
52 int nMemoryAlloc; // memory allocated
53};
54
56{
57 // information about individual entries
58 int nEntriesUsed; // the number of entries allocated
59 char * pCurrent; // the current pointer to free memory
60 char * pEnd; // the first entry outside the free memory
61
62 // this is where the memory is stored
63 int nChunkSize; // the size of one chunk
64 int nChunksAlloc; // the maximum number of memory chunks
65 int nChunks; // the current number of memory chunks
66 char ** pChunks; // the allocated memory
67
68 // statistics
69 int nMemoryUsed; // memory used in the allocated entries
70 int nMemoryAlloc; // memory allocated
71};
72
74{
75 int nMems; // the number of fixed memory managers employed
76 Mem_Fixed_t ** pMems; // memory managers: 2^1 words, 2^2 words, etc
77 int nMapSize; // the size of the memory array
78 Mem_Fixed_t ** pMap; // maps the number of bytes into its memory manager
79 int nLargeChunksAlloc; // the maximum number of large memory chunks
80 int nLargeChunks; // the current number of large memory chunks
81 void ** pLargeChunks; // the allocated large memory chunks
82};
83
87
100Mem_Fixed_t * Mem_FixedStart( int nEntrySize )
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}
127
139void Mem_FixedStop( Mem_Fixed_t * p, int fVerbose )
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}
156
169{
170 Mem_FlexStop( p, 0 );
171}
172
185{
186 char * pTemp;
187 int i;
188
189 // check if there are still free entries
190 if ( p->nEntriesUsed == p->nEntriesAlloc )
191 { // need to allocate more entries
192 assert( p->pEntriesFree == NULL );
193 if ( p->nChunks == p->nChunksAlloc )
194 {
195 p->nChunksAlloc *= 2;
196 p->pChunks = ABC_REALLOC( char *, p->pChunks, p->nChunksAlloc );
197 }
198 p->pEntriesFree = ABC_ALLOC( char, p->nEntrySize * p->nChunkSize );
199 p->nMemoryAlloc += p->nEntrySize * p->nChunkSize;
200 // transform these entries into a linked list
201 pTemp = p->pEntriesFree;
202 for ( i = 1; i < p->nChunkSize; i++ )
203 {
204 *((char **)pTemp) = pTemp + p->nEntrySize;
205 pTemp += p->nEntrySize;
206 }
207 // set the last link
208 *((char **)pTemp) = NULL;
209 // add the chunk to the chunk storage
210 p->pChunks[ p->nChunks++ ] = p->pEntriesFree;
211 // add to the number of entries allocated
212 p->nEntriesAlloc += p->nChunkSize;
213 }
214 // incrememt the counter of used entries
215 p->nEntriesUsed++;
216 if ( p->nEntriesMax < p->nEntriesUsed )
217 p->nEntriesMax = p->nEntriesUsed;
218 // return the first entry in the free entry list
219 pTemp = p->pEntriesFree;
220 p->pEntriesFree = *((char **)pTemp);
221 return pTemp;
222}
223
235void Mem_FixedEntryRecycle( Mem_Fixed_t * p, char * pEntry )
236{
237 // decrement the counter of used entries
238 p->nEntriesUsed--;
239 // add the entry to the linked list of free entries
240 *((char **)pEntry) = p->pEntriesFree;
241 p->pEntriesFree = pEntry;
242}
243
256{
257 int i;
258 char * pTemp;
259
260 // deallocate all chunks except the first one
261 for ( i = 1; i < p->nChunks; i++ )
262 ABC_FREE( p->pChunks[i] );
263 p->nChunks = 1;
264 // transform these entries into a linked list
265 pTemp = p->pChunks[0];
266 for ( i = 1; i < p->nChunkSize; i++ )
267 {
268 *((char **)pTemp) = pTemp + p->nEntrySize;
269 pTemp += p->nEntrySize;
270 }
271 // set the last link
272 *((char **)pTemp) = NULL;
273 // set the free entry list
274 p->pEntriesFree = p->pChunks[0];
275 // set the correct statistics
276 p->nMemoryAlloc = p->nEntrySize * p->nChunkSize;
277 p->nMemoryUsed = 0;
278 p->nEntriesAlloc = p->nChunkSize;
279 p->nEntriesUsed = 0;
280}
281
294{
295 return p->nMemoryAlloc;
296}
297
310{
311 return p->nEntriesMax;
312}
313
314
315
328{
329 Mem_Flex_t * p;
330
331 p = ABC_ALLOC( Mem_Flex_t, 1 );
332 memset( p, 0, sizeof(Mem_Flex_t) );
333
334 p->nEntriesUsed = 0;
335 p->pCurrent = NULL;
336 p->pEnd = NULL;
337
338 p->nChunkSize = (1 << 12);
339 p->nChunksAlloc = 64;
340 p->nChunks = 0;
341 p->pChunks = ABC_ALLOC( char *, p->nChunksAlloc );
342
343 p->nMemoryUsed = 0;
344 p->nMemoryAlloc = 0;
345 return p;
346}
347
359void Mem_FlexStop( Mem_Flex_t * p, int fVerbose )
360{
361 int i;
362 if ( p == NULL )
363 return;
364 if ( fVerbose )
365 {
366 printf( "Flexible memory manager: Chunk size = %d. Chunks used = %d.\n",
367 p->nChunkSize, p->nChunks );
368 printf( " Entries used = %d. Memory used = %d. Memory alloc = %d.\n",
369 p->nEntriesUsed, p->nMemoryUsed, p->nMemoryAlloc );
370 }
371 for ( i = 0; i < p->nChunks; i++ )
372 ABC_FREE( p->pChunks[i] );
373 ABC_FREE( p->pChunks );
374 ABC_FREE( p );
375}
376
388char * Mem_FlexEntryFetch( Mem_Flex_t * p, int nBytes )
389{
390 char * pTemp;
391 // check if there are still free entries
392 if ( p->pCurrent == NULL || p->pCurrent + nBytes > p->pEnd )
393 { // need to allocate more entries
394 if ( p->nChunks == p->nChunksAlloc )
395 {
396 p->nChunksAlloc *= 2;
397 p->pChunks = ABC_REALLOC( char *, p->pChunks, p->nChunksAlloc );
398 }
399 if ( nBytes > p->nChunkSize )
400 {
401 // resize the chunk size if more memory is requested than it can give
402 // (ideally, this should never happen)
403 p->nChunkSize = 2 * nBytes;
404 }
405 p->pCurrent = ABC_ALLOC( char, p->nChunkSize );
406 p->pEnd = p->pCurrent + p->nChunkSize;
407 p->nMemoryAlloc += p->nChunkSize;
408 // add the chunk to the chunk storage
409 p->pChunks[ p->nChunks++ ] = p->pCurrent;
410 }
411 assert( p->pCurrent + nBytes <= p->pEnd );
412 // increment the counter of used entries
413 p->nEntriesUsed++;
414 // keep track of the memory used
415 p->nMemoryUsed += nBytes;
416 // return the next entry
417 pTemp = p->pCurrent;
418 p->pCurrent += nBytes;
419 return pTemp;
420}
421
434{
435 int i;
436 if ( p->nChunks == 0 )
437 return;
438 // deallocate all chunks except the first one
439 for ( i = 1; i < p->nChunks; i++ )
440 ABC_FREE( p->pChunks[i] );
441 p->nChunks = 1;
442 p->nMemoryAlloc = p->nChunkSize;
443 // transform these entries into a linked list
444 p->pCurrent = p->pChunks[0];
445 p->pEnd = p->pCurrent + p->nChunkSize;
446 p->nEntriesUsed = 0;
447 p->nMemoryUsed = 0;
448}
449
462{
463 return p->nMemoryUsed;
464}
465
466
467
468
469
491{
492 Mem_Step_t * p;
493 int i, k;
494 p = ABC_ALLOC( Mem_Step_t, 1 );
495 memset( p, 0, sizeof(Mem_Step_t) );
496 p->nMems = nSteps;
497 // start the fixed memory managers
498 p->pMems = ABC_ALLOC( Mem_Fixed_t *, p->nMems );
499 for ( i = 0; i < p->nMems; i++ )
500 p->pMems[i] = Mem_FixedStart( (8<<i) );
501 // set up the mapping of the required memory size into the corresponding manager
502 p->nMapSize = (4<<p->nMems);
503 p->pMap = ABC_ALLOC( Mem_Fixed_t *, p->nMapSize+1 );
504 p->pMap[0] = NULL;
505 for ( k = 1; k <= 4; k++ )
506 p->pMap[k] = p->pMems[0];
507 for ( i = 0; i < p->nMems; i++ )
508 for ( k = (4<<i)+1; k <= (8<<i); k++ )
509 p->pMap[k] = p->pMems[i];
510//for ( i = 1; i < 100; i ++ )
511//printf( "%10d: size = %10d\n", i, p->pMap[i]->nEntrySize );
512 return p;
513}
514
526void Mem_StepStop( Mem_Step_t * p, int fVerbose )
527{
528 int i;
529 for ( i = 0; i < p->nMems; i++ )
530 Mem_FixedStop( p->pMems[i], fVerbose );
531 if ( p->pLargeChunks )
532 {
533 for ( i = 0; i < p->nLargeChunks; i++ )
534 ABC_FREE( p->pLargeChunks[i] );
535 ABC_FREE( p->pLargeChunks );
536 }
537 ABC_FREE( p->pMems );
538 ABC_FREE( p->pMap );
539 ABC_FREE( p );
540}
541
553char * Mem_StepEntryFetch( Mem_Step_t * p, int nBytes )
554{
555 if ( nBytes == 0 )
556 return NULL;
557 if ( nBytes > p->nMapSize )
558 {
559// printf( "Allocating %d bytes.\n", nBytes );
560// return ABC_ALLOC( char, nBytes );
561 if ( p->nLargeChunks == p->nLargeChunksAlloc )
562 {
563 if ( p->nLargeChunksAlloc == 0 )
564 p->nLargeChunksAlloc = 32;
565 p->nLargeChunksAlloc *= 2;
566 p->pLargeChunks = (void **)ABC_REALLOC( char *, p->pLargeChunks, p->nLargeChunksAlloc );
567 }
568 p->pLargeChunks[ p->nLargeChunks++ ] = ABC_ALLOC( char, nBytes );
569 return (char *)p->pLargeChunks[ p->nLargeChunks - 1 ];
570 }
571 return Mem_FixedEntryFetch( p->pMap[nBytes] );
572}
573
574
586void Mem_StepEntryRecycle( Mem_Step_t * p, char * pEntry, int nBytes )
587{
588 if ( nBytes == 0 )
589 return;
590 if ( nBytes > p->nMapSize )
591 {
592// ABC_FREE( pEntry );
593 return;
594 }
595 Mem_FixedEntryRecycle( p->pMap[nBytes], pEntry );
596}
597
610{
611 int i, nMemTotal = 0;
612 for ( i = 0; i < p->nMems; i++ )
613 nMemTotal += p->pMems[i]->nMemoryAlloc;
614 return nMemTotal;
615}
616
621
#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
char * Mem_StepEntryFetch(Mem_Step_t *p, int nBytes)
Definition mem.c:553
int Mem_FixedReadMemUsage(Mem_Fixed_t *p)
Definition mem.c:293
void Mem_FixedStop(Mem_Fixed_t *p, int fVerbose)
Definition mem.c:139
Mem_Flex_t * Mem_FlexStart()
Definition mem.c:327
void Mem_StepStop(Mem_Step_t *p, int fVerbose)
Definition mem.c:526
int Mem_FixedReadMaxEntriesUsed(Mem_Fixed_t *p)
Definition mem.c:309
char * Mem_FlexEntryFetch(Mem_Flex_t *p, int nBytes)
Definition mem.c:388
int Mem_FlexReadMemUsage(Mem_Flex_t *p)
Definition mem.c:461
char * Mem_FixedEntryFetch(Mem_Fixed_t *p)
Definition mem.c:184
void Mem_FixedRestart(Mem_Fixed_t *p)
Definition mem.c:255
Mem_Step_t * Mem_StepStart(int nSteps)
Definition mem.c:490
void Mem_FlexStop2(Mem_Flex_t *p)
Definition mem.c:168
Mem_Fixed_t * Mem_FixedStart(int nEntrySize)
FUNCTION DEFINITIONS ///.
Definition mem.c:100
void Mem_StepEntryRecycle(Mem_Step_t *p, char *pEntry, int nBytes)
Definition mem.c:586
void Mem_FlexRestart(Mem_Flex_t *p)
Definition mem.c:433
void Mem_FlexStop(Mem_Flex_t *p, int fVerbose)
Definition mem.c:359
int Mem_StepReadMemUsage(Mem_Step_t *p)
Definition mem.c:609
void Mem_FixedEntryRecycle(Mem_Fixed_t *p, char *pEntry)
Definition mem.c:235
struct Mem_Step_t_ Mem_Step_t
Definition mem.h:35
typedefABC_NAMESPACE_HEADER_START struct Mem_Fixed_t_ Mem_Fixed_t
DECLARATIONS ///.
Definition mem.h:33
struct Mem_Flex_t_ Mem_Flex_t
Definition mem.h:34
DECLARATIONS ///.
Definition mem.c:36
int nEntriesMax
Definition mem.c:41
int nChunkSize
Definition mem.c:45
char * pEntriesFree
Definition mem.c:42
int nEntriesAlloc
Definition mem.c:39
int nEntriesUsed
Definition mem.c:40
int nMemoryUsed
Definition mem.c:51
int nMemoryAlloc
Definition mem.c:52
int nChunks
Definition mem.c:47
int nEntrySize
Definition mem.c:38
char ** pChunks
Definition mem.c:48
int nChunksAlloc
Definition mem.c:46
char * pCurrent
Definition mem.c:59
char * pEnd
Definition mem.c:60
int nEntriesUsed
Definition mem.c:58
int nChunks
Definition mem.c:65
char ** pChunks
Definition mem.c:66
int nMemoryAlloc
Definition mem.c:70
int nChunkSize
Definition mem.c:63
int nMemoryUsed
Definition mem.c:69
int nChunksAlloc
Definition mem.c:64
Mem_Fixed_t ** pMems
Definition mem.c:76
Mem_Fixed_t ** pMap
Definition mem.c:78
int nMapSize
Definition mem.c:77
int nLargeChunksAlloc
Definition mem.c:79
void ** pLargeChunks
Definition mem.c:81
int nLargeChunks
Definition mem.c:80
int nMems
Definition mem.c:75
#define assert(ex)
Definition util_old.h:213
char * memset()