mem2.h 8.51 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 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/**CFile****************************************************************

  FileName    [mem2.h]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Memory management.]

  Synopsis    [External declarations.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#ifndef ABC__aig__mem__mem2_h
#define ABC__aig__mem__mem2_h

#include "misc/vec/vec.h"

ABC_NAMESPACE_HEADER_START


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

typedef struct Mmr_Flex_t_     Mmr_Flex_t;     
typedef struct Mmr_Fixed_t_    Mmr_Fixed_t;    
typedef struct Mmr_Step_t_     Mmr_Step_t;     

struct Mmr_Flex_t_
{
    int           nPageBase;     // log2 page size in words
    int           PageMask;      // page mask
    int           nEntries;      // entries allocated
Alan Mishchenko committed
42
    int           nEntriesMax;   // max number of enries used
Alan Mishchenko committed
43 44 45 46 47 48 49 50 51 52
    int           iNext;         // next word to be used
    Vec_Ptr_t     vPages;        // memory pages
};

struct Mmr_Fixed_t_
{
    int           nPageBase;     // log2 page size in words
    int           PageMask;      // page mask
    int           nEntryWords;   // entry size in words
    int           nEntries;      // entries allocated
Alan Mishchenko committed
53
    int           nEntriesMax;   // max number of enries used
Alan Mishchenko committed
54 55 56 57 58 59
    Vec_Ptr_t     vPages;        // memory pages
    Vec_Int_t     vFrees;        // free entries
};

struct Mmr_Step_t_
{
Alan Mishchenko committed
60 61 62 63 64 65
    int           nBits;         // the number of bits
    int           uMask;         // the number of managers minus 1
    int           nEntries;      // the number of entries
    int           nEntriesMax;   // the max number of entries
    int           nEntriesAll;   // the total number of entries
    Mmr_Fixed_t   pMems[0];      // memory managers: 2^0 words, 2^1 words, etc
Alan Mishchenko committed
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
};

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////
 
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline Mmr_Flex_t * Mmr_FlexStart( int nPageBase )
{
    Mmr_Flex_t * p;
    p = ABC_CALLOC( Mmr_Flex_t, 1 );
    p->nPageBase = nPageBase;
    p->PageMask  = (1 << nPageBase) - 1;
    p->iNext     = (1 << nPageBase);
    return p;
}
static inline void Mmr_FlexStop( Mmr_Flex_t * p )
{
    word * pPage;
    int i;
Alan Mishchenko committed
96 97
    if ( 0 && Vec_PtrSize(&p->vPages) )
        printf( "Using %3d pages of %6d words each with %6d entries (max = %6d). Total memory = %5.2f MB.\n", 
Alan Mishchenko committed
98
            Vec_PtrSize(&p->vPages), p->nPageBase ? 1 << p->nPageBase : 0, p->nEntries, p->nEntriesMax, 
Alan Mishchenko committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
            1.0 * Vec_PtrSize(&p->vPages) * (1 << p->nPageBase) * 8 / (1 << 20) );
    Vec_PtrForEachEntry( word *, &p->vPages, pPage, i )
        ABC_FREE( pPage );
    ABC_FREE( p->vPages.pArray );
    ABC_FREE( p );
}
static inline word * Mmr_FlexEntry( Mmr_Flex_t * p, int h )
{
    assert( h > 0 && h < p->iNext );
    return (word *)Vec_PtrEntry(&p->vPages, (h >> p->nPageBase)) + (h & p->PageMask);
}
static inline int Mmr_FlexFetch( Mmr_Flex_t * p, int nWords )
{
    int hEntry;
    assert( nWords > 0 && nWords < p->PageMask );
    if ( p->iNext + nWords >= p->PageMask )
    {
        Vec_PtrPush( &p->vPages, ABC_FALLOC( word, p->PageMask + 1 ) );
        p->iNext = 1;
    }
    hEntry = ((Vec_PtrSize(&p->vPages) - 1) << p->nPageBase) | p->iNext;
    p->iNext += nWords;
    p->nEntries++;
Alan Mishchenko committed
122
    p->nEntriesMax = Abc_MaxInt( p->nEntriesMax, p->nEntries );
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
    return hEntry;
}
static inline void Mmr_FlexRelease( Mmr_Flex_t * p, int h )
{
    assert( h > 0 && h < p->iNext );
    if ( (h >> p->nPageBase) && Vec_PtrEntry(&p->vPages, (h >> p->nPageBase) - 1) )
    {
        word * pPage = (word *)Vec_PtrEntry(&p->vPages, (h >> p->nPageBase) - 1);
        Vec_PtrWriteEntry( &p->vPages, (h >> p->nPageBase) - 1, NULL );
        ABC_FREE( pPage );
    }
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
148
static inline void Mmr_FixedCreate( Mmr_Fixed_t * p, int nPageBase, int nEntryWords )
Alan Mishchenko committed
149 150 151 152 153
{
    assert( nEntryWords > 0 && nEntryWords < (1 << nPageBase) );
    p->nPageBase   = nPageBase;
    p->PageMask    = (1 << nPageBase) - 1;
    p->nEntryWords = nEntryWords;
Alan Mishchenko committed
154 155 156 157 158
}
static inline Mmr_Fixed_t * Mmr_FixedStart( int nPageBase, int nEntryWords )
{
    Mmr_Fixed_t * p = ABC_CALLOC( Mmr_Fixed_t, 1 );
    Mmr_FixedCreate( p, nPageBase, nEntryWords );
Alan Mishchenko committed
159 160
    return p;
}
Alan Mishchenko committed
161
static inline void Mmr_FixedStop( Mmr_Fixed_t * p, int fFreeLast )
Alan Mishchenko committed
162 163 164
{
    word * pPage;
    int i;
Alan Mishchenko committed
165 166
    if ( 0 && Vec_PtrSize(&p->vPages) )
        printf( "Using %3d pages of %6d words each with %6d entries (max = %6d) of size %d. Total memory = %5.2f MB.\n", 
Alan Mishchenko committed
167
            Vec_PtrSize(&p->vPages), p->nPageBase ? 1 << p->nPageBase : 0, p->nEntries, p->nEntriesMax, p->nEntryWords,
Alan Mishchenko committed
168 169 170 171 172
            1.0 * Vec_PtrSize(&p->vPages) * (1 << p->nPageBase) * 8 / (1 << 20) );
    Vec_PtrForEachEntry( word *, &p->vPages, pPage, i )
        ABC_FREE( pPage );
    ABC_FREE( p->vPages.pArray );
    ABC_FREE( p->vFrees.pArray );
Alan Mishchenko committed
173 174
    if ( fFreeLast )
        ABC_FREE( p );
Alan Mishchenko committed
175 176 177
}
static inline word * Mmr_FixedEntry( Mmr_Fixed_t * p, int h )
{
Alan Mishchenko committed
178
    assert( h > 0 && h < (Vec_PtrSize(&p->vPages) << p->nPageBase) );
Alan Mishchenko committed
179 180 181 182 183 184 185 186 187 188 189 190 191
    return (word *)Vec_PtrEntry(&p->vPages, (h >> p->nPageBase)) + (h & p->PageMask);
}
static inline int Mmr_FixedFetch( Mmr_Fixed_t * p )
{
    if ( Vec_IntSize(&p->vFrees) == 0 )
    {
        int i, hEntry = Vec_PtrSize(&p->vPages) << p->nPageBase;
        Vec_PtrPush( &p->vPages, ABC_FALLOC( word, p->PageMask + 1 ) );
        for ( i = 1; i + p->nEntryWords <= p->PageMask; i += p->nEntryWords )
            Vec_IntPush( &p->vFrees, hEntry | i );
        Vec_IntReverseOrder( &p->vFrees );
    }
    p->nEntries++;
Alan Mishchenko committed
192
    p->nEntriesMax = Abc_MaxInt( p->nEntriesMax, p->nEntries );
Alan Mishchenko committed
193 194 195 196
    return Vec_IntPop( &p->vFrees );
}
static inline void Mmr_FixedRecycle( Mmr_Fixed_t * p, int h )
{
Alan Mishchenko committed
197
    p->nEntries--;
Alan Mishchenko committed
198 199 200
    memset( Mmr_FixedEntry(p, h), 0xFF, sizeof(word) * p->nEntryWords );
    Vec_IntPush( &p->vFrees, h );
}
Alan Mishchenko committed
201 202 203 204
static inline int Mmr_FixedMemory( Mmr_Fixed_t * p )
{
    return Vec_PtrSize(&p->vPages) * (p->PageMask + 1);
}
Alan Mishchenko committed
205 206 207 208 209 210 211 212 213 214 215 216 217

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
218
static inline Mmr_Step_t * Mmr_StepStart( int nPageBase, int nWordBase )
Alan Mishchenko committed
219
{
Alan Mishchenko committed
220
    char * pMemory = ABC_CALLOC( char, sizeof(Mmr_Step_t) + sizeof(Mmr_Fixed_t) * (1 << nWordBase) );
Alan Mishchenko committed
221
    Mmr_Step_t * p = (Mmr_Step_t *)pMemory;
Alan Mishchenko committed
222 223 224 225 226
    int i;
    p->nBits = nWordBase;
    p->uMask = (1 << nWordBase) - 1;
    for ( i = 1; i <= p->uMask; i++ )
        Mmr_FixedCreate( p->pMems + i, nPageBase, i );
Alan Mishchenko committed
227 228 229 230 231
    return p;
}
static inline void Mmr_StepStop( Mmr_Step_t * p )
{
    int i;
Alan Mishchenko committed
232 233
    for ( i = 0; i <= p->uMask; i++ )
        Mmr_FixedStop( p->pMems + i, 0 );
Alan Mishchenko committed
234 235
    ABC_FREE( p );
}
Alan Mishchenko committed
236
static inline word * Mmr_StepEntry( Mmr_Step_t * p, int h )
Alan Mishchenko committed
237
{
Alan Mishchenko committed
238 239
    assert( (h & p->uMask) > 0 );
    return Mmr_FixedEntry( p->pMems + (h & p->uMask), (h >> p->nBits) );
Alan Mishchenko committed
240 241 242
}
static inline int Mmr_StepFetch( Mmr_Step_t * p, int nWords )
{
Alan Mishchenko committed
243 244 245 246 247
    assert( nWords > 0 && nWords <= p->uMask );
    p->nEntries++;
    p->nEntriesAll++;
    p->nEntriesMax = Abc_MaxInt( p->nEntriesMax, p->nEntries );
    return (Mmr_FixedFetch(p->pMems + nWords) << p->nBits) | nWords;
Alan Mishchenko committed
248
}
Alan Mishchenko committed
249
static inline void Mmr_StepRecycle( Mmr_Step_t * p, int h )
Alan Mishchenko committed
250
{
Alan Mishchenko committed
251 252
    p->nEntries--;
    Mmr_FixedRecycle( p->pMems + (h & p->uMask), (h >> p->nBits) );
Alan Mishchenko committed
253
}
Alan Mishchenko committed
254 255 256 257 258 259 260
static inline int Mmr_StepMemory( Mmr_Step_t * p )
{
    int i, Mem = 0;
    for ( i = 1; i <= p->uMask; i++ )
        Mem += Mmr_FixedMemory( p->pMems + i );
    return Mem;
}
Alan Mishchenko committed
261 262 263 264 265 266 267 268 269 270 271


ABC_NAMESPACE_HEADER_END

#endif

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