reoUnits.c 5.01 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/**CFile****************************************************************

  FileName    [reoUnits.c]

  PackageName [REO: A specialized DD reordering engine.]

  Synopsis    [Procedures which support internal data structures.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - October 15, 2002.]

  Revision    [$Id: reoUnits.c,v 1.0 2002/15/10 03:00:00 alanmi Exp $]

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

#include "reo.h"

21 22 23
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
24 25 26 27 28 29 30 31 32 33 34 35
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

static void reoUnitsAddToFreeUnitList( reo_man * p );

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

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

36
  Synopsis    [Extract the next unit from the free unit list.]
Alan Mishchenko committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
reo_unit * reoUnitsGetNextUnit(reo_man * p )
{
    reo_unit * pUnit;
    // check there are stil units to extract
    if ( p->pUnitFreeList == NULL )
        reoUnitsAddToFreeUnitList( p );
    // extract the next unit from the linked list
    pUnit            = p->pUnitFreeList;
    p->pUnitFreeList = pUnit->Next;
    p->nUnitsUsed++;
    return pUnit;
}

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

60
  Synopsis    [Returns the unit to the free unit list.]
Alan Mishchenko committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
void reoUnitsRecycleUnit( reo_man * p, reo_unit * pUnit )
{
    pUnit->Next      = p->pUnitFreeList;
    p->pUnitFreeList = pUnit;
    p->nUnitsUsed--;
}

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

78
  Synopsis    [Returns the list of units to the free unit list.]
Alan Mishchenko committed
79 80 81 82 83 84 85 86 87 88 89

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
void reoUnitsRecycleUnitList( reo_man * p, reo_plane * pPlane )
{
    reo_unit * pUnit;
Alan Mishchenko committed
90
    reo_unit * pTail = NULL; // Suppress "might be used uninitialized"
Alan Mishchenko committed
91 92 93 94 95 96 97 98 99

    if ( pPlane->pHead == NULL )
        return;

    // find the tail
    for ( pUnit = pPlane->pHead; pUnit; pUnit = pUnit->Next )
        pTail = pUnit;
    pTail->Next = p->pUnitFreeList;
    p->pUnitFreeList    = pPlane->pHead;
100 101
    memset( pPlane, 0, sizeof(reo_plane) );
//    pPlane->pHead = NULL;
Alan Mishchenko committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
}

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

  Synopsis    [Stops the unit dispenser.]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
void reoUnitsStopDispenser( reo_man * p )
{
    int i;
    for ( i = 0; i < p->nMemChunks; i++ )
Alan Mishchenko committed
119
        ABC_FREE( p->pMemChunks[i] );
Alan Mishchenko committed
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
//    printf("\nThe number of chunks used is %d, each of them %d units\n", p->nMemChunks, REO_CHUNK_SIZE );
    p->nMemChunks = 0;
}

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

  Synopsis    [Adds one unit to the list of units which constitutes the plane.]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
void reoUnitsAddUnitToPlane( reo_plane * pPlane, reo_unit * pUnit )
{
    if ( pPlane->pHead == NULL )
    {
        pPlane->pHead = pUnit;
        pUnit->Next   = NULL;
    }
    else
    {
        pUnit->Next   = pPlane->pHead;
        pPlane->pHead = pUnit;
    }
    pPlane->statsNodes++;
}


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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
void reoUnitsAddToFreeUnitList( reo_man * p )
{
    int c;
    // check that we still have chunks left
    if ( p->nMemChunks == p->nMemChunksAlloc )
    {
        printf( "reoUnitsAddToFreeUnitList(): Memory manager ran out of memory!\n" );
        fflush( stdout );
        return;
    }
    // allocate the next chunk
    assert( p->pUnitFreeList == NULL );
Alan Mishchenko committed
174
    p->pUnitFreeList = ABC_ALLOC( reo_unit, REO_CHUNK_SIZE );
Alan Mishchenko committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188
    // split chunks into list-connected units
    for ( c = 0; c < REO_CHUNK_SIZE-1; c++ )
        (p->pUnitFreeList + c)->Next = p->pUnitFreeList + c + 1;
    // set the last pointer to NULL
    (p->pUnitFreeList + REO_CHUNK_SIZE-1)->Next = NULL;
    // add the chunk to the array of chunks
    p->pMemChunks[p->nMemChunks++] = p->pUnitFreeList;
}


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

189 190
ABC_NAMESPACE_IMPL_END