satMem.c 16.3 KB
Newer Older
Alan Mishchenko committed
1 2 3 4
/**CFile****************************************************************

  FileName    [satMem.c]

5 6
  SystemName  [ABC: Logic synthesis and verification system.]

Alan Mishchenko committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20
  PackageName [SAT solver.]

  Synopsis    [Memory management.]

  Author      [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - January 1, 2004.]

  Revision    [$Id: satMem.c,v 1.0 2004/01/01 1:00:00 alanmi Exp $]

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

Alan Mishchenko committed
21 22 23 24 25
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

Alan Mishchenko committed
26 27
#include "satMem.h"

28 29 30
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
31 32 33 34 35 36 37 38 39 40 41
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

struct Sat_MmFixed_t_
{
    // information about individual entries
    int           nEntrySize;    // the size of one entry
    int           nEntriesAlloc; // the total number of entries allocated
    int           nEntriesUsed;  // the number of entries in use
    int           nEntriesMax;   // the max number of entries in use
42
    char *        pEntriesFree;  // the linked list of free entries
Alan Mishchenko committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

    // this is where the memory is stored
    int           nChunkSize;    // the size of one chunk
    int           nChunksAlloc;  // the maximum number of memory chunks 
    int           nChunks;       // the current number of memory chunks 
    char **       pChunks;       // the allocated memory

    // statistics
    int           nMemoryUsed;   // memory used in the allocated entries
    int           nMemoryAlloc;  // memory allocated
};

struct Sat_MmFlex_t_
{
    // information about individual entries
    int           nEntriesUsed;  // the number of entries allocated
59 60
    char *        pCurrent;      // the current pointer to free memory
    char *        pEnd;          // the first entry outside the free memory
Alan Mishchenko committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74

    // this is where the memory is stored
    int           nChunkSize;    // the size of one chunk
    int           nChunksAlloc;  // the maximum number of memory chunks 
    int           nChunks;       // the current number of memory chunks 
    char **       pChunks;       // the allocated memory

    // statistics
    int           nMemoryUsed;   // memory used in the allocated entries
    int           nMemoryAlloc;  // memory allocated
};

struct Sat_MmStep_t_
{
Alan Mishchenko committed
75 76 77 78 79 80 81 82
    int               nMems;     // the number of fixed memory managers employed
    Sat_MmFixed_t **  pMems;     // memory managers: 2^1 words, 2^2 words, etc
    int               nMapSize;  // the size of the memory array
    Sat_MmFixed_t **  pMap;      // maps the number of bytes into its memory manager
    // additional memory chunks
    int           nChunksAlloc;  // the maximum number of memory chunks 
    int           nChunks;       // the current number of memory chunks 
    char **       pChunks;       // the allocated memory
Alan Mishchenko committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
};

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

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

  Synopsis    [Allocates memory pieces of fixed size.]

  Description [The size of the chunk is computed as the minimum of
  1024 entries and 64K. Can only work with entry size at least 4 byte long.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Sat_MmFixed_t * Sat_MmFixedStart( int nEntrySize )
{
    Sat_MmFixed_t * p;

Alan Mishchenko committed
105
    p = ABC_ALLOC( Sat_MmFixed_t, 1 );
Alan Mishchenko committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    memset( p, 0, sizeof(Sat_MmFixed_t) );

    p->nEntrySize    = nEntrySize;
    p->nEntriesAlloc = 0;
    p->nEntriesUsed  = 0;
    p->pEntriesFree  = NULL;

    if ( nEntrySize * (1 << 10) < (1<<16) )
        p->nChunkSize = (1 << 10);
    else
        p->nChunkSize = (1<<16) / nEntrySize;
    if ( p->nChunkSize < 8 )
        p->nChunkSize = 8;

    p->nChunksAlloc  = 64;
    p->nChunks       = 0;
Alan Mishchenko committed
122
    p->pChunks       = ABC_ALLOC( char *, p->nChunksAlloc );
Alan Mishchenko committed
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

    p->nMemoryUsed   = 0;
    p->nMemoryAlloc  = 0;
    return p;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Sat_MmFixedStop( Sat_MmFixed_t * p, int fVerbose )
{
    int i;
    if ( p == NULL )
        return;
    if ( fVerbose )
    {
        printf( "Fixed memory manager: Entry = %5d. Chunk = %5d. Chunks used = %5d.\n",
            p->nEntrySize, p->nChunkSize, p->nChunks );
        printf( "   Entries used = %8d. Entries peak = %8d. Memory used = %8d. Memory alloc = %8d.\n",
            p->nEntriesUsed, p->nEntriesMax, p->nEntrySize * p->nEntriesUsed, p->nMemoryAlloc );
    }
    for ( i = 0; i < p->nChunks; i++ )
Alan Mishchenko committed
153 154 155
        ABC_FREE( p->pChunks[i] );
    ABC_FREE( p->pChunks );
    ABC_FREE( p );
Alan Mishchenko committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Sat_MmFixedEntryFetch( Sat_MmFixed_t * p )
{
    char * pTemp;
    int i;

174
    // check if there are still free entries
Alan Mishchenko committed
175 176 177 178 179 180
    if ( p->nEntriesUsed == p->nEntriesAlloc )
    { // need to allocate more entries
        assert( p->pEntriesFree == NULL );
        if ( p->nChunks == p->nChunksAlloc )
        {
            p->nChunksAlloc *= 2;
Alan Mishchenko committed
181
            p->pChunks = ABC_REALLOC( char *, p->pChunks, p->nChunksAlloc ); 
Alan Mishchenko committed
182
        }
Alan Mishchenko committed
183
        p->pEntriesFree = ABC_ALLOC( char, p->nEntrySize * p->nChunkSize );
Alan Mishchenko committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
        p->nMemoryAlloc += p->nEntrySize * p->nChunkSize;
        // transform these entries into a linked list
        pTemp = p->pEntriesFree;
        for ( i = 1; i < p->nChunkSize; i++ )
        {
            *((char **)pTemp) = pTemp + p->nEntrySize;
            pTemp += p->nEntrySize;
        }
        // set the last link
        *((char **)pTemp) = NULL;
        // add the chunk to the chunk storage
        p->pChunks[ p->nChunks++ ] = p->pEntriesFree;
        // add to the number of entries allocated
        p->nEntriesAlloc += p->nChunkSize;
    }
    // incrememt the counter of used entries
    p->nEntriesUsed++;
    if ( p->nEntriesMax < p->nEntriesUsed )
        p->nEntriesMax = p->nEntriesUsed;
203
    // return the first entry in the free entry list
Alan Mishchenko committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    pTemp = p->pEntriesFree;
    p->pEntriesFree = *((char **)pTemp);
    return pTemp;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Sat_MmFixedEntryRecycle( Sat_MmFixed_t * p, char * pEntry )
{
    // decrement the counter of used entries
    p->nEntriesUsed--;
224
    // add the entry to the linked list of free entries
Alan Mishchenko committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
    *((char **)pEntry) = p->pEntriesFree;
    p->pEntriesFree = pEntry;
}

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

  Synopsis    []

  Description [Relocates all the memory except the first chunk.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Sat_MmFixedRestart( Sat_MmFixed_t * p )
{
    int i;
    char * pTemp;
244 245 246
    if ( p->nChunks == 0 )
        return;
    assert( p->nChunks > 0 );
Alan Mishchenko committed
247 248 249

    // deallocate all chunks except the first one
    for ( i = 1; i < p->nChunks; i++ )
Alan Mishchenko committed
250
        ABC_FREE( p->pChunks[i] );
Alan Mishchenko committed
251 252 253 254 255 256 257 258 259 260
    p->nChunks = 1;
    // transform these entries into a linked list
    pTemp = p->pChunks[0];
    for ( i = 1; i < p->nChunkSize; i++ )
    {
        *((char **)pTemp) = pTemp + p->nEntrySize;
        pTemp += p->nEntrySize;
    }
    // set the last link
    *((char **)pTemp) = NULL;
261
    // set the free entry list
Alan Mishchenko committed
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
    p->pEntriesFree  = p->pChunks[0];
    // set the correct statistics
    p->nMemoryAlloc  = p->nEntrySize * p->nChunkSize;
    p->nMemoryUsed   = 0;
    p->nEntriesAlloc = p->nChunkSize;
    p->nEntriesUsed  = 0;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Sat_MmFixedReadMemUsage( Sat_MmFixed_t * p )
{
    return p->nMemoryAlloc;
}



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

  Synopsis    [Allocates entries of flexible size.]

  Description [Can only work with entry size at least 4 byte long.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Sat_MmFlex_t * Sat_MmFlexStart()
{
    Sat_MmFlex_t * p;

Alan Mishchenko committed
303
    p = ABC_ALLOC( Sat_MmFlex_t, 1 );
Alan Mishchenko committed
304 305 306 307 308 309
    memset( p, 0, sizeof(Sat_MmFlex_t) );

    p->nEntriesUsed  = 0;
    p->pCurrent      = NULL;
    p->pEnd          = NULL;

Alan Mishchenko committed
310
    p->nChunkSize    = (1 << 16);
Alan Mishchenko committed
311 312
    p->nChunksAlloc  = 64;
    p->nChunks       = 0;
Alan Mishchenko committed
313
    p->pChunks       = ABC_ALLOC( char *, p->nChunksAlloc );
Alan Mishchenko committed
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 342 343

    p->nMemoryUsed   = 0;
    p->nMemoryAlloc  = 0;
    return p;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Sat_MmFlexStop( Sat_MmFlex_t * p, int fVerbose )
{
    int i;
    if ( p == NULL )
        return;
    if ( fVerbose )
    {
        printf( "Flexible memory manager: Chunk size = %d. Chunks used = %d.\n",
            p->nChunkSize, p->nChunks );
        printf( "   Entries used = %d. Memory used = %d. Memory alloc = %d.\n",
            p->nEntriesUsed, p->nMemoryUsed, p->nMemoryAlloc );
    }
    for ( i = 0; i < p->nChunks; i++ )
Alan Mishchenko committed
344 345 346
        ABC_FREE( p->pChunks[i] );
    ABC_FREE( p->pChunks );
    ABC_FREE( p );
Alan Mishchenko committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Sat_MmFlexEntryFetch( Sat_MmFlex_t * p, int nBytes )
{
    char * pTemp;
363
    // check if there are still free entries
Alan Mishchenko committed
364 365 366 367 368
    if ( p->pCurrent == NULL || p->pCurrent + nBytes > p->pEnd )
    { // need to allocate more entries
        if ( p->nChunks == p->nChunksAlloc )
        {
            p->nChunksAlloc *= 2;
Alan Mishchenko committed
369
            p->pChunks = ABC_REALLOC( char *, p->pChunks, p->nChunksAlloc ); 
Alan Mishchenko committed
370 371 372 373 374 375 376
        }
        if ( nBytes > p->nChunkSize )
        {
            // resize the chunk size if more memory is requested than it can give
            // (ideally, this should never happen)
            p->nChunkSize = 2 * nBytes;
        }
Alan Mishchenko committed
377
        p->pCurrent = ABC_ALLOC( char, p->nChunkSize );
Alan Mishchenko committed
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
        p->pEnd     = p->pCurrent + p->nChunkSize;
        p->nMemoryAlloc += p->nChunkSize;
        // add the chunk to the chunk storage
        p->pChunks[ p->nChunks++ ] = p->pCurrent;
    }
    assert( p->pCurrent + nBytes <= p->pEnd );
    // increment the counter of used entries
    p->nEntriesUsed++;
    // keep track of the memory used
    p->nMemoryUsed += nBytes;
    // return the next entry
    pTemp = p->pCurrent;
    p->pCurrent += nBytes;
    return pTemp;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Sat_MmFlexReadMemUsage( Sat_MmFlex_t * p )
{
    return p->nMemoryAlloc;
}





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

  Synopsis    [Starts the hierarchical memory manager.]

  Description [This manager can allocate entries of any size.
  Iternally they are mapped into the entries with the number of bytes
  equal to the power of 2. The smallest entry size is 8 bytes. The
  next one is 16 bytes etc. So, if the user requests 6 bytes, he gets 
  8 byte entry. If we asks for 25 bytes, he gets 32 byte entry etc.
  The input parameters "nSteps" says how many fixed memory managers
  are employed internally. Calling this procedure with nSteps equal
  to 10 results in 10 hierarchically arranged internal memory managers, 
  which can allocate up to 4096 (1Kb) entries. Requests for larger 
Alan Mishchenko committed
427
  entries are handed over to malloc() and then ABC_FREE()ed.]
Alan Mishchenko committed
428 429 430 431 432 433 434 435 436 437
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Sat_MmStep_t * Sat_MmStepStart( int nSteps )
{
    Sat_MmStep_t * p;
    int i, k;
Alan Mishchenko committed
438
    p = ABC_ALLOC( Sat_MmStep_t, 1 );
Alan Mishchenko committed
439 440
    p->nMems = nSteps;
    // start the fixed memory managers
Alan Mishchenko committed
441
    p->pMems = ABC_ALLOC( Sat_MmFixed_t *, p->nMems );
Alan Mishchenko committed
442 443 444 445
    for ( i = 0; i < p->nMems; i++ )
        p->pMems[i] = Sat_MmFixedStart( (8<<i) );
    // set up the mapping of the required memory size into the corresponding manager
    p->nMapSize = (4<<p->nMems);
Alan Mishchenko committed
446
    p->pMap = ABC_ALLOC( Sat_MmFixed_t *, p->nMapSize+1 );
Alan Mishchenko committed
447 448 449 450 451 452 453 454
    p->pMap[0] = NULL;
    for ( k = 1; k <= 4; k++ )
        p->pMap[k] = p->pMems[0];
    for ( i = 0; i < p->nMems; i++ )
        for ( k = (4<<i)+1; k <= (8<<i); k++ )
            p->pMap[k] = p->pMems[i];
//for ( i = 1; i < 100; i ++ )
//printf( "%10d: size = %10d\n", i, p->pMap[i]->nEntrySize );
Alan Mishchenko committed
455 456
    p->nChunksAlloc  = 64;
    p->nChunks       = 0;
Alan Mishchenko committed
457
    p->pChunks       = ABC_ALLOC( char *, p->nChunksAlloc );
Alan Mishchenko committed
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
    return p;
}

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

  Synopsis    [Stops the memory manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Sat_MmStepStop( Sat_MmStep_t * p, int fVerbose )
{
    int i;
Alan Mishchenko committed
475 476 477
    if ( p->nChunksAlloc )
    {
        for ( i = 0; i < p->nChunks; i++ )
Alan Mishchenko committed
478 479
            ABC_FREE( p->pChunks[i] );
        ABC_FREE( p->pChunks );
Alan Mishchenko committed
480
    }
Alan Mishchenko committed
481 482
    for ( i = 0; i < p->nMems; i++ )
        Sat_MmFixedStop( p->pMems[i], fVerbose );
Alan Mishchenko committed
483 484 485
    ABC_FREE( p->pMems );
    ABC_FREE( p->pMap );
    ABC_FREE( p );
Alan Mishchenko committed
486 487 488 489
}

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

490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
  Synopsis    [Stops the memory manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Sat_MmStepRestart( Sat_MmStep_t * p )
{
    int i;
    if ( p->nChunksAlloc )
    {
        for ( i = 0; i < p->nChunks; i++ )
            ABC_FREE( p->pChunks[i] );
        p->nChunks = 0;
    }
    for ( i = 0; i < p->nMems; i++ )
        Sat_MmFixedRestart( p->pMems[i] );
}

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

Alan Mishchenko committed
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
  Synopsis    [Creates the entry.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Sat_MmStepEntryFetch( Sat_MmStep_t * p, int nBytes )
{
    if ( nBytes == 0 )
        return NULL;
    if ( nBytes > p->nMapSize )
    {
Alan Mishchenko committed
529 530 531
        if ( p->nChunks == p->nChunksAlloc )
        {
            p->nChunksAlloc *= 2;
Alan Mishchenko committed
532
            p->pChunks = ABC_REALLOC( char *, p->pChunks, p->nChunksAlloc ); 
Alan Mishchenko committed
533
        }
Alan Mishchenko committed
534
        p->pChunks[ p->nChunks++ ] = ABC_ALLOC( char, nBytes );
Alan Mishchenko committed
535
        return p->pChunks[p->nChunks-1];
Alan Mishchenko committed
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
    }
    return Sat_MmFixedEntryFetch( p->pMap[nBytes] );
}


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

  Synopsis    [Recycles the entry.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Sat_MmStepEntryRecycle( Sat_MmStep_t * p, char * pEntry, int nBytes )
{
    if ( nBytes == 0 )
        return;
    if ( nBytes > p->nMapSize )
    {
Alan Mishchenko committed
558
//        ABC_FREE( pEntry );
Alan Mishchenko committed
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
        return;
    }
    Sat_MmFixedEntryRecycle( p->pMap[nBytes], pEntry );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Sat_MmStepReadMemUsage( Sat_MmStep_t * p )
{
    int i, nMemTotal = 0;
    for ( i = 0; i < p->nMems; i++ )
        nMemTotal += p->pMems[i]->nMemoryAlloc;
    return nMemTotal;
}
582 583
ABC_NAMESPACE_IMPL_END