abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
fraigMem.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [fraigMem.c]
4 
5  PackageName [FRAIG: Functionally reduced AND-INV graphs.]
6 
7  Synopsis [Fixed-size-entry memory manager for the FRAIG package.]
8 
9  Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
10 
11  Affiliation [UC Berkeley]
12 
13  Date [Ver. 2.0. Started - October 1, 2004]
14 
15  Revision [$Id: fraigMem.c,v 1.4 2005/07/08 01:01:31 alanmi Exp $]
16 
17 ***********************************************************************/
18 
19 #include "fraigInt.h"
20 
22 
23 
24 ////////////////////////////////////////////////////////////////////////
25 /// DECLARATIONS ///
26 ////////////////////////////////////////////////////////////////////////
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 
48 ////////////////////////////////////////////////////////////////////////
49 /// FUNCTION DEFINITIONS ///
50 ////////////////////////////////////////////////////////////////////////
51 
52 /**Function*************************************************************
53 
54  Synopsis [Starts the internal memory manager.]
55 
56  Description [Can only work with entry size at least 4 byte long.]
57 
58  SideEffects []
59 
60  SeeAlso []
61 
62 ***********************************************************************/
64 {
66 
67  p = ABC_ALLOC( Fraig_MemFixed_t, 1 );
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 
91 /**Function*************************************************************
92 
93  Synopsis [Stops the internal memory manager.]
94 
95  Description []
96 
97  SideEffects []
98 
99  SeeAlso []
100 
101 ***********************************************************************/
102 void 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",
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 
120 /**Function*************************************************************
121 
122  Synopsis [Extracts one entry from the memory manager.]
123 
124  Description []
125 
126  SideEffects []
127 
128  SeeAlso []
129 
130 ***********************************************************************/
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 
171 /**Function*************************************************************
172 
173  Synopsis [Returns one entry into the memory manager.]
174 
175  Description []
176 
177  SideEffects []
178 
179  SeeAlso []
180 
181 ***********************************************************************/
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 
191 /**Function*************************************************************
192 
193  Synopsis [Frees all associated memory and resets the manager.]
194 
195  Description [Relocates all the memory except the first chunk.]
196 
197  SideEffects []
198 
199  SeeAlso []
200 
201 ***********************************************************************/
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 
229 /**Function*************************************************************
230 
231  Synopsis [Reports the memory usage.]
232 
233  Description []
234 
235  SideEffects []
236 
237  SeeAlso []
238 
239 ***********************************************************************/
241 {
242  return p->nMemoryAlloc;
243 }
244 
245 ////////////////////////////////////////////////////////////////////////
246 /// END OF FILE ///
247 ////////////////////////////////////////////////////////////////////////
248 
249 
251 
char * pEntriesFree
Definition: fraigMem.c:35
char * memset()
void Fraig_MemFixedRestart(Fraig_MemFixed_t *p)
Definition: fraigMem.c:202
static Llb_Mgr_t * p
Definition: llb3Image.c:950
int Fraig_MemFixedReadMemUsage(Fraig_MemFixed_t *p)
Definition: fraigMem.c:240
DECLARATIONS ///.
Definition: fraigMem.c:28
#define ABC_REALLOC(type, obj, num)
Definition: abc_global.h:233
char * Fraig_MemFixedEntryFetch(Fraig_MemFixed_t *p)
Definition: fraigMem.c:131
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
char ** pChunks
Definition: fraigMem.c:41
void Fraig_MemFixedStop(Fraig_MemFixed_t *p, int fVerbose)
Definition: fraigMem.c:102
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
#define ABC_FREE(obj)
Definition: abc_global.h:232
void Fraig_MemFixedEntryRecycle(Fraig_MemFixed_t *p, char *pEntry)
Definition: fraigMem.c:182
#define assert(ex)
Definition: util_old.h:213
Fraig_MemFixed_t * Fraig_MemFixedStart(int nEntrySize)
FUNCTION DEFINITIONS ///.
Definition: fraigMem.c:63