ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
utilMem.c
Go to the documentation of this file.
1
20
21#include <stdio.h>
22#include <string.h>
23#include <stdlib.h>
24#include <assert.h>
25
26#include "abc_global.h"
27
29
33
34
35typedef struct Vec_Mem_t_ Vec_Mem_t;
37{
38 int nCap;
39 int nSize;
40 void ** pArray;
41};
42
43void * s_vAllocs = NULL;
44void * s_vFrees = NULL;
46
47#define ABC_MEM_ALLOC(type, num) ((type *) malloc(sizeof(type) * (num)))
48#define ABC_MEM_CALLOC(type, num) ((type *) calloc((num), sizeof(type)))
49#define ABC_MEM_FALLOC(type, num) ((type *) memset(malloc(sizeof(type) * (num)), 0xff, sizeof(type) * (num)))
50#define ABC_MEM_FREE(obj) ((obj) ? (free((char *) (obj)), (obj) = 0) : 0)
51#define ABC_MEM_REALLOC(type, obj, num) \
52 ((obj) ? ((type *) realloc((char *)(obj), sizeof(type) * (num))) : \
53 ((type *) malloc(sizeof(type) * (num))))
54
58
70static inline Vec_Mem_t * Vec_MemAlloc( int nCap )
71{
72 Vec_Mem_t * p;
74 if ( nCap > 0 && nCap < 8 )
75 nCap = 8;
76 p->nSize = 0;
77 p->nCap = nCap;
78 p->pArray = p->nCap? ABC_MEM_ALLOC( void *, p->nCap ) : NULL;
79 return p;
80}
81
93static inline void Vec_MemFree( Vec_Mem_t * p )
94{
95 ABC_MEM_FREE( p->pArray );
96 ABC_MEM_FREE( p );
97}
98
110static inline void Vec_MemGrow( Vec_Mem_t * p, int nCapMin )
111{
112 if ( p->nCap >= nCapMin )
113 return;
114 p->pArray = ABC_MEM_REALLOC( void *, p->pArray, nCapMin );
115 p->nCap = nCapMin;
116}
117
129static inline void Vec_MemPush( Vec_Mem_t * p, void * Entry )
130{
131 if ( p->nSize == p->nCap )
132 {
133 if ( p->nCap < 16 )
134 Vec_MemGrow( p, 16 );
135 else
136 Vec_MemGrow( p, 2 * p->nCap );
137 }
138 p->pArray[p->nSize++] = Entry;
139}
140
152static void Vec_MemSort( Vec_Mem_t * p, int (*Vec_MemSortCompare)() )
153{
154 if ( p->nSize < 2 )
155 return;
156 qsort( (void *)p->pArray, (size_t)p->nSize, sizeof(void *),
157 (int (*)(const void *, const void *)) Vec_MemSortCompare );
158}
159
160
172void * Util_MemRecAlloc( void * pMem )
173{
174 if ( s_vAllocs )
175 Vec_MemPush( (Vec_Mem_t *)s_vAllocs, pMem );
176 return pMem;
177}
178
190void * Util_MemRecFree( void * pMem )
191{
192 if ( s_vFrees )
193 Vec_MemPush( (Vec_Mem_t *)s_vFrees, pMem );
194 return pMem;
195}
196
208int Util_ComparePointers( void ** pp1, void ** pp2 )
209{
210 if ( *pp1 < *pp2 )
211 return -1;
212 if ( *pp1 > *pp2 )
213 return 1;
214 return 0;
215}
216
228static inline Vec_Mem_t * Vec_MemTwoMerge( Vec_Mem_t * vArr1, Vec_Mem_t * vArr2 )
229{
230 Vec_Mem_t * vArr = Vec_MemAlloc( vArr1->nSize + vArr2->nSize );
231 void ** pBeg = vArr->pArray;
232 void ** pBeg1 = vArr1->pArray;
233 void ** pBeg2 = vArr2->pArray;
234 void ** pEnd1 = vArr1->pArray + vArr1->nSize;
235 void ** pEnd2 = vArr2->pArray + vArr2->nSize;
236 while ( pBeg1 < pEnd1 && pBeg2 < pEnd2 )
237 {
238 if ( *pBeg1 == *pBeg2 )
239 pBeg1++, pBeg2++;
240 else if ( *pBeg1 < *pBeg2 )
241 {
242 free( *pBeg1 );
243 *pBeg++ = *pBeg1++;
244 }
245 else
246 assert( 0 );
247// *pBeg++ = *pBeg2++;
248 }
249 while ( pBeg1 < pEnd1 )
250 *pBeg++ = *pBeg1++;
251// while ( pBeg2 < pEnd2 )
252// *pBeg++ = *pBeg2++;
253 assert( pBeg2 >= pEnd2 );
254 vArr->nSize = pBeg - vArr->pArray;
255 assert( vArr->nSize <= vArr->nCap );
256 assert( vArr->nSize >= vArr1->nSize );
257 assert( vArr->nSize >= vArr2->nSize );
258 return vArr;
259}
260
273{
274 Vec_Mem_t * vMerge;
275 assert( s_vAllocs == NULL );
276 assert( s_vFrees == NULL );
277 Vec_MemSort( (Vec_Mem_t *)s_vAllocs, (int (*)())Util_ComparePointers );
278 Vec_MemSort( (Vec_Mem_t *)s_vFrees, (int (*)())Util_ComparePointers );
279 vMerge = (Vec_Mem_t *)Vec_MemTwoMerge( (Vec_Mem_t *)s_vAllocs, (Vec_Mem_t *)s_vFrees );
280 Vec_MemFree( vMerge );
281}
282
295{
296 assert( s_vAllocs == NULL && s_vFrees == NULL );
297 s_vAllocs = Vec_MemAlloc( 1000 );
298 s_vFrees = Vec_MemAlloc( 1000 );
299}
300
313{
314 assert( s_vAllocs != NULL && s_vFrees != NULL );
315 Vec_MemFree( (Vec_Mem_t *)s_vAllocs );
316 Vec_MemFree( (Vec_Mem_t *)s_vFrees );
317}
318
331{
332 return s_vAllocs != NULL && s_vFrees != NULL;
333}
334
338
339
341
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
Cube * p
Definition exorList.c:222
int nCap
Definition utilMem.c:38
int nSize
Definition utilMem.c:39
void ** pArray
Definition utilMem.c:40
#define ABC_MEM_REALLOC(type, obj, num)
Definition utilMem.c:51
void * s_vAllocs
INCLUDES ///.
Definition utilMem.c:43
void Util_MemRecRecycle()
Definition utilMem.c:272
void * s_vFrees
Definition utilMem.c:44
void Util_MemRecQuit()
Definition utilMem.c:312
void * Util_MemRecAlloc(void *pMem)
BASIC TYPES ///.
Definition utilMem.c:172
void * Util_MemRecFree(void *pMem)
Definition utilMem.c:190
int Util_ComparePointers(void **pp1, void **pp2)
Definition utilMem.c:208
#define ABC_MEM_FREE(obj)
Definition utilMem.c:50
typedefABC_NAMESPACE_IMPL_START struct Vec_Mem_t_ Vec_Mem_t
DECLARATIONS ///.
Definition utilMem.c:35
int Util_MemRecIsSet()
Definition utilMem.c:330
void Util_MemRecStart()
Definition utilMem.c:294
int s_fInterrupt
Definition utilMem.c:45
#define ABC_MEM_ALLOC(type, num)
Definition utilMem.c:47
#define assert(ex)
Definition util_old.h:213
VOID_HACK free()