utilMem.c 8.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
/**CFile****************************************************************

  FileName    [utilMem.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Memory recycling utilities.]

  Synopsis    [Memory recycling utilities.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 20, 2005.]

  Revision    [$Id: utilMem.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]

***********************************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

#include "abc_global.h"

ABC_NAMESPACE_IMPL_START

////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////
 

typedef struct Vec_Mem_t_       Vec_Mem_t;
struct Vec_Mem_t_ 
{
    int              nCap;
    int              nSize;
    void **          pArray;
};

void * s_vAllocs = NULL;
void * s_vFrees  = NULL;
int    s_fInterrupt = 0;

#define ABC_MEM_ALLOC(type, num)     ((type *) malloc(sizeof(type) * (num)))
#define ABC_MEM_CALLOC(type, num)     ((type *) calloc((num), sizeof(type)))
#define ABC_MEM_FALLOC(type, num)     ((type *) memset(malloc(sizeof(type) * (num)), 0xff, sizeof(type) * (num)))
#define ABC_MEM_FREE(obj)             ((obj) ? (free((char *) (obj)), (obj) = 0) : 0)
#define ABC_MEM_REALLOC(type, obj, num)    \
        ((obj) ? ((type *) realloc((char *)(obj), sizeof(type) * (num))) : \
         ((type *) malloc(sizeof(type) * (num))))

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

/**Function*************************************************************

  Synopsis    [Allocates a vector with the given capacity.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline Vec_Mem_t * Vec_MemAlloc( int nCap )
{
    Vec_Mem_t * p;
    p = ABC_MEM_ALLOC( Vec_Mem_t, 1 );
    if ( nCap > 0 && nCap < 8 )
        nCap = 8;
    p->nSize  = 0;
    p->nCap   = nCap;
    p->pArray = p->nCap? ABC_MEM_ALLOC( void *, p->nCap ) : NULL;
    return p;
}

/**Function*************************************************************

  Synopsis    [Frees the vector.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Vec_MemFree( Vec_Mem_t * p )
{
    ABC_MEM_FREE( p->pArray );
    ABC_MEM_FREE( p );
}

/**Function*************************************************************

  Synopsis    [Resizes the vector to the given capacity.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Vec_MemGrow( Vec_Mem_t * p, int nCapMin )
{
    if ( p->nCap >= nCapMin )
        return;
    p->pArray = ABC_MEM_REALLOC( void *, p->pArray, nCapMin ); 
    p->nCap   = nCapMin;
}

/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Vec_MemPush( Vec_Mem_t * p, void * Entry )
{
    if ( p->nSize == p->nCap )
    {
        if ( p->nCap < 16 )
            Vec_MemGrow( p, 16 );
        else
            Vec_MemGrow( p, 2 * p->nCap );
    }
    p->pArray[p->nSize++] = Entry;
}

/**Function*************************************************************

  Synopsis    [Sorting the entries by their integer value.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static void Vec_MemSort( Vec_Mem_t * p, int (*Vec_MemSortCompare)() )
{
    if ( p->nSize < 2 )
        return;
    qsort( (void *)p->pArray, p->nSize, sizeof(void *), 
            (int (*)(const void *, const void *)) Vec_MemSortCompare );
}


/**Function*************************************************************

  Synopsis    [Remembers an allocated pointer.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void * Util_MemRecAlloc( void * pMem )
{
    if ( s_vAllocs )
        Vec_MemPush( (Vec_Mem_t *)s_vAllocs, pMem );
    return pMem;
}

/**Function*************************************************************

  Synopsis    [Remembers a deallocated pointer.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void * Util_MemRecFree( void * pMem )
{
    if ( s_vFrees )
        Vec_MemPush( (Vec_Mem_t *)s_vFrees, pMem );
    return pMem;
}

/**Function*************************************************************

  Synopsis    [Procedure used for sorting the nodes in decreasing order of levels.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Util_ComparePointers( void ** pp1, void ** pp2 )
{
    if ( *pp1 < *pp2 )
        return -1;
    if ( *pp1 > *pp2 ) 
        return 1;
    return 0; 
}

/**Function*************************************************************

  Synopsis    [Finds entries that do not appear in both lists.]

  Description [Assumes that the vectors are sorted in the increasing order.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline Vec_Mem_t * Vec_MemTwoMerge( Vec_Mem_t * vArr1, Vec_Mem_t * vArr2 )
{
    Vec_Mem_t * vArr = Vec_MemAlloc( vArr1->nSize + vArr2->nSize ); 
    void ** pBeg  = vArr->pArray;
    void ** pBeg1 = vArr1->pArray;
    void ** pBeg2 = vArr2->pArray;
    void ** pEnd1 = vArr1->pArray + vArr1->nSize;
    void ** pEnd2 = vArr2->pArray + vArr2->nSize;
    while ( pBeg1 < pEnd1 && pBeg2 < pEnd2 )
    {
        if ( *pBeg1 == *pBeg2 )
            pBeg1++, pBeg2++;
        else if ( *pBeg1 < *pBeg2 )
        {
            free( *pBeg1 );
            *pBeg++ = *pBeg1++;
        }
        else 
            assert( 0 );
//            *pBeg++ = *pBeg2++;
    }
    while ( pBeg1 < pEnd1 )
        *pBeg++ = *pBeg1++;
//    while ( pBeg2 < pEnd2 )
//        *pBeg++ = *pBeg2++;
    assert( pBeg2 >= pEnd2 );
    vArr->nSize = pBeg - vArr->pArray;
    assert( vArr->nSize <= vArr->nCap );
    assert( vArr->nSize >= vArr1->nSize );
    assert( vArr->nSize >= vArr2->nSize );
    return vArr;
}

/**Function*************************************************************

  Synopsis    [Recycles the accumulated memory.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Util_MemRecRecycle()
{
    Vec_Mem_t * vMerge;
    assert( s_vAllocs == NULL );
    assert( s_vFrees == NULL );
    Vec_MemSort( (Vec_Mem_t *)s_vAllocs, (int (*)())Util_ComparePointers );
    Vec_MemSort( (Vec_Mem_t *)s_vFrees, (int (*)())Util_ComparePointers );
    vMerge = (Vec_Mem_t *)Vec_MemTwoMerge( (Vec_Mem_t *)s_vAllocs, (Vec_Mem_t *)s_vFrees );
    Vec_MemFree( vMerge );
}

/**Function*************************************************************

  Synopsis    [Starts memory structures.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Util_MemRecStart()
{
    assert( s_vAllocs == NULL && s_vFrees == NULL );
    s_vAllocs = Vec_MemAlloc( 1000 );
    s_vFrees = Vec_MemAlloc( 1000 );
}

/**Function*************************************************************

  Synopsis    [Quits memory structures.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Util_MemRecQuit()
{
    assert( s_vAllocs != NULL && s_vFrees != NULL );
    Vec_MemFree( (Vec_Mem_t *)s_vAllocs );
    Vec_MemFree( (Vec_Mem_t *)s_vFrees );
}

/**Function*************************************************************

  Synopsis    [Starts memory structures.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Util_MemRecIsSet()
{
    return s_vAllocs != NULL && s_vFrees != NULL;
}

////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


ABC_NAMESPACE_IMPL_END