summaryrefslogtreecommitdiffstats
path: root/src/misc/nm/nmApi.c
blob: 72ec24e652e39968ebaa52e4cab6bd1adaf1faa4 (plain)
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
/**CFile****************************************************************

  FileName    [nmApi.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Name manager.]

  Synopsis    [APIs of the name manager.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "nmInt.h"

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

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

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

  Synopsis    [Allocates the name manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Nm_Man_t * Nm_ManCreate( int nSize )
{
    Nm_Man_t * p;
    // allocate the table
    p = ALLOC( Nm_Man_t, 1 );
    memset( p, 0, sizeof(Nm_Man_t) );
    // set the parameters
    p->nSizeFactor   = 3; // determined how much larger the table should be compared to data in it
    p->nGrowthFactor = 3; // determined how much the table grows after resizing
    // allocate and clean the bins
    p->nBins = Cudd_PrimeNm(p->nSizeFactor*nSize);
    p->pBinsI2N = ALLOC( Nm_Entry_t *, p->nBins );
    p->pBinsN2I = ALLOC( Nm_Entry_t *, p->nBins );
    memset( p->pBinsI2N, 0, sizeof(Nm_Entry_t *) * p->nBins );
    memset( p->pBinsN2I, 0, sizeof(Nm_Entry_t *) * p->nBins );
    // start the memory manager
    p->pMem = Extra_MmFlexStart();
    return p;
}

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

  Synopsis    [Deallocates the name manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Nm_ManFree( Nm_Man_t * p )
{
    Extra_MmFlexStop( p->pMem, 0 );
    FREE( p->pBinsI2N );
    FREE( p->pBinsN2I );
    FREE( p );
}

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

  Synopsis    [Returns the number of objects with names.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Nm_ManNumEntries( Nm_Man_t * p )
{
    return p->nEntries;
}

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

  Synopsis    [Creates a new entry in the name manager.]

  Description [Returns 1 if the entry with the given object ID
  already exists in the name manager.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Nm_ManStoreIdName( Nm_Man_t * p, int ObjId, char * pName, char * pSuffix )
{
    Nm_Entry_t * pEntry, * pEntry2;
    int RetValue, nEntrySize;
    if ( pEntry = Nm_ManTableLookupId(p, ObjId) )
    {
        if ( strcmp(pEntry->Name, pName) == 0 )
            printf( "Nm_ManStoreIdName(): Entry with the same ID and name already exists.\n" );
        else
            printf( "Nm_ManStoreIdName(): Entry with the same ID and different name already exists.\n" );
        return NULL;
    }
    if ( pSuffix == NULL && (pEntry = Nm_ManTableLookupName(p, pName, &pEntry2)) && pEntry2 )
    {
        printf( "Nm_ManStoreIdName(): Two entries with the same name already exist.\n" );
        return NULL;
    }
    // create the entry
    nEntrySize = sizeof(Nm_Entry_t) + strlen(pName) + (pSuffix?strlen(pSuffix):0) + 1;
    nEntrySize = (nEntrySize / 4 + ((nEntrySize % 4) > 0)) * 4;
    pEntry = (Nm_Entry_t *)Extra_MmFlexEntryFetch( p->pMem, nEntrySize );
    pEntry->ObjId = ObjId;
    sprintf( pEntry->Name, "%s%s", pName, pSuffix? pSuffix : "" );
    // add the entry to the hash table
    RetValue = Nm_ManTableAdd( p, pEntry );
    assert( RetValue == 1 );
    return pEntry->Name;
}


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

  Synopsis    [Finds a unique name for the node.]

  Description [If the name exists, tries appending numbers to it until 
  it becomes unique.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Nm_ManCreateUniqueName( Nm_Man_t * p, int ObjId )
{
    static char NameStr[1000];
    Nm_Entry_t * pEntry;
    int i;
    if ( pEntry = Nm_ManTableLookupId(p, ObjId) )
        return pEntry->Name;
    sprintf( NameStr, "[%d]", ObjId );
    for ( i = 1; Nm_ManTableLookupName(p, NameStr, NULL); i++ )
        sprintf( NameStr, "[%d]_%d", ObjId, i );
    return Nm_ManStoreIdName( p, ObjId, NameStr, NULL );
}

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

  Synopsis    [Returns name of the object if the ID is known.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Nm_ManFindNameById( Nm_Man_t * p, int ObjId )
{
    Nm_Entry_t * pEntry;
    if ( pEntry = Nm_ManTableLookupId(p, ObjId) )
        return pEntry->Name;
    return NULL;
}

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

  Synopsis    [Returns ID of the object if its name is known.]

  Description [This procedure may return two IDs because POs and latches 
  may have the same name (the only allowed case of name duplication).]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Nm_ManFindIdByName( Nm_Man_t * p, char * pName, int * pSecond )
{
    Nm_Entry_t * pEntry, * pEntry2;
    if ( pEntry = Nm_ManTableLookupName(p, pName, &pEntry2) )
    {
        if ( pSecond )
            *pSecond = pEntry2? pEntry2->ObjId : -1;
        return pEntry->ObjId;
    }
    return -1;
}


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

  Synopsis    [Prints distribution of entries in the bins.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Nm_ManPrintTables( Nm_Man_t * p )
{
    int i, Counter;

    // rehash the entries from the old table
    Counter = 0;
    printf( "Int2Name: " );
    for ( i = 0; i < p->nBins; i++ )
    {
        if ( Counter == 0 && p->pBinsI2N[i] == NULL )
            continue;
        if ( p->pBinsI2N[i] )
            Counter++;
        else
        {
            printf( "%d ", Counter );
            Counter = 0;
        }
    }
    printf( "\n" );

    // rehash the entries from the old table
    Counter = 0;
    printf( "Name2Int: " );
    for ( i = 0; i < p->nBins; i++ )
    {
        if ( Counter == 0 && p->pBinsN2I[i] == NULL )
            continue;
        if ( p->pBinsN2I[i] )
            Counter++;
        else
        {
            printf( "%d ", Counter );
            Counter = 0;
        }
    }
    printf( "\n" );
}

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