VIS

src/hrc/hrcMemUtil.c

Go to the documentation of this file.
00001 
00036 #include "hrcInt.h"
00037 
00038 static char rcsid[] UNUSED = "$Id: hrcMemUtil.c,v 1.3 2005/04/16 04:23:47 fabio Exp $";
00039 
00040 /*---------------------------------------------------------------------------*/
00041 /* Constant declarations                                                     */
00042 /*---------------------------------------------------------------------------*/
00043 
00044 
00045 /*---------------------------------------------------------------------------*/
00046 /* Type declarations                                                         */
00047 /*---------------------------------------------------------------------------*/
00048 
00049 
00050 /*---------------------------------------------------------------------------*/
00051 /* Stucture declarations                                                     */
00052 /*---------------------------------------------------------------------------*/
00053 
00054 
00055 /*---------------------------------------------------------------------------*/
00056 /* Variable declarations                                                     */
00057 /*---------------------------------------------------------------------------*/
00058 
00059 
00060 /*---------------------------------------------------------------------------*/
00061 /* Macro declarations                                                        */
00062 /*---------------------------------------------------------------------------*/
00063 
00064 
00067 /*---------------------------------------------------------------------------*/
00068 /* Static function prototypes                                                */
00069 /*---------------------------------------------------------------------------*/
00070 
00071 static void ModelFree(Hrc_Model_t *model);
00072 static void SubcktFree(Hrc_Subckt_t *subckt);
00073 static void LatchFree(Hrc_Latch_t *latch);
00074 
00078 /*---------------------------------------------------------------------------*/
00079 /* Definition of exported functions                                          */
00080 /*---------------------------------------------------------------------------*/
00093 Hrc_Manager_t *
00094 Hrc_ManagerAlloc(
00095   void)
00096 {
00097   Hrc_Manager_t *manager = ALLOC(Hrc_Manager_t, 1);
00098   
00099   manager->rootNode = NIL(Hrc_Node_t);
00100   manager->currentNode = NIL(Hrc_Node_t);
00101   manager->modelTable = st_init_table(strcmp, st_strhash);
00102   return manager;
00103 }
00104 
00121 Hrc_Model_t *
00122 Hrc_ModelAlloc(
00123   Hrc_Manager_t *manager,
00124   char *modelName)
00125 {
00126   Hrc_Model_t *model;
00127   
00128   if(!st_is_member(manager->modelTable,modelName)){
00129     model = ALLOC(Hrc_Model_t, 1);
00130     model->modelName = util_strsav(modelName);
00131     st_insert(manager->modelTable, model->modelName, (char *) model);
00132     model->subcktTable = st_init_table(strcmp, st_strhash);
00133     model->masterNode = Hrc_NodeAlloc(manager, model->modelName,
00134                                       NIL(char), NIL(Hrc_Node_t));
00135     return model;
00136   }
00137   else {
00138     return NIL(Hrc_Model_t);
00139   }
00140 }
00141 
00157 Hrc_Node_t *
00158 Hrc_NodeAlloc(
00159   Hrc_Manager_t *manager,
00160   char *modelName,
00161   char *instanceName,
00162   Hrc_Node_t *parentNode)
00163 {
00164   Hrc_Node_t *node = ALLOC(Hrc_Node_t, 1);
00165   /* The manager field is needed only for allocating the parent node. For
00166      allocating any other node in the hierarchy, the manager can be obtained
00167      from the parent node. */
00168   node->manager = manager;
00169   node->modelName = util_strsav(modelName);
00170   node->instanceName = util_strsav(instanceName);
00171   node->parentNode = parentNode;
00172   node->formalInputs = array_alloc(Var_Variable_t *, 0);
00173   node->formalOutputs = array_alloc(Var_Variable_t *, 0);
00174   /* The arrays actualInputs and actualOutputs do not need to be allocated
00175      because 1) they are never needed for the master node of a model, and
00176      2) they are written over whenever a new node is added to the hierarchy
00177      by using Hrc_NodeAddChild() */
00178   node->actualInputs = NIL(array_t);
00179   node->actualOutputs = NIL(array_t);
00180   node->nameTables = array_alloc(Tbl_Table_t *, 0);
00181   node->childTable = st_init_table(strcmp, st_strhash);
00182   node->latchTable = st_init_table(strcmp, st_strhash);
00183   node->varTable = st_init_table(strcmp, st_strhash);
00184   node->applInfoTable = st_init_table(strcmp, st_strhash);
00185   node->undef = NIL(void);
00186   return node;
00187 }
00188 
00208 Hrc_Node_t *
00209 Hrc_NodeDup(
00210   Hrc_Node_t *node,
00211   char *instanceName)
00212 {
00213   Hrc_Node_t *dupNode = ALLOC(Hrc_Node_t, 1);
00214   st_generator *gen;
00215   int i, index;
00216   Var_Variable_t *var, *dupVar;
00217   Tbl_Table_t *table, *dupTable;
00218   Hrc_Latch_t *latch, *dupLatch;
00219   st_table *varToDupVar;
00220   char *name, *dupName;
00221   char *key, *dupKey;
00222   ApplInfo_t *applInfo;
00223   
00224   dupNode->manager = node->manager;
00225   dupNode->modelName = util_strsav(node->modelName);
00226   dupNode->instanceName = util_strsav(instanceName);
00227   dupNode->parentNode = node->parentNode;
00228   dupNode->formalInputs = array_alloc(Var_Variable_t *, 0);
00229   dupNode->formalOutputs = array_alloc(Var_Variable_t *, 0);
00230   dupNode->actualInputs = NIL(array_t);
00231   dupNode->actualOutputs = NIL(array_t);
00232   /*
00233   dupNode->actualInputs = array_alloc(Var_Variable_t *, 0);
00234   Hrc_NodeForEachActualInput(node, i, var) {
00235     dupVar = Var_VariableDup(var, dupNode);
00236     array_insert_last(Var_Variable_t *, dupNode->actualInputs, dupVar);
00237   }
00238   dupNode->actualOutputs = array_alloc(Var_Variable_t *, 0);
00239   Hrc_NodeForEachActualOutput(node, i, var) {
00240     dupVar = Var_VariableDup(var, dupNode);
00241     array_insert_last(Var_Variable_t *, dupNode->actualOutputs, dupVar);
00242   }
00243   */
00244   dupNode->varTable = st_init_table(strcmp, st_strhash);
00245   varToDupVar = st_init_table(st_ptrcmp, st_ptrhash);
00246   Hrc_NodeForEachVariable(node, gen, name, var) {
00247     dupVar = Var_VariableDup(var, dupNode);
00248     dupName = Var_VariableReadName(dupVar);
00249     st_insert(dupNode->varTable, dupName, dupVar);
00250     st_insert(varToDupVar, var, dupVar);
00251   }
00252   Hrc_NodeForEachFormalInput(node, i, var) {
00253     st_lookup(varToDupVar, var, &dupVar);
00254     array_insert_last(Var_Variable_t *, dupNode->formalInputs, dupVar);
00255   }
00256   Hrc_NodeForEachFormalOutput(node, i, var) {
00257     st_lookup(varToDupVar, var, &dupVar);
00258     array_insert_last(Var_Variable_t *, dupNode->formalOutputs, dupVar);
00259   }
00260   dupNode->nameTables = array_alloc(Tbl_Table_t *, 0);
00261   Hrc_NodeForEachNameTable(node, i, table) {
00262     dupTable = Tbl_TableSoftDup(table);
00263     /*dupTable = Tbl_TableHardDup(table);*/
00264     Tbl_TableForEachInputVar(dupTable, index, var) {
00265       st_lookup(varToDupVar, var, &dupVar);
00266       Tbl_TableSubstituteVar(dupTable, var, dupVar);
00267     }
00268     Tbl_TableForEachOutputVar(dupTable, index, var) {
00269       st_lookup(varToDupVar, var, &dupVar);
00270       Tbl_TableSubstituteVar(dupTable, var, dupVar);
00271     }
00272     array_insert_last(Tbl_Table_t *, dupNode->nameTables, dupTable);
00273   }
00274   dupNode->latchTable = st_init_table(strcmp, st_strhash);
00275   Hrc_NodeForEachLatch(node, gen, name, latch) {
00276     dupLatch = ALLOC(Hrc_Latch_t, 1);
00277     st_lookup(varToDupVar, latch->latchInput, &(dupLatch->latchInput));
00278     st_lookup(varToDupVar, latch->latchOutput, &(dupLatch->latchOutput));
00279     dupLatch->resetTable = Tbl_TableSoftDup(latch->resetTable);
00280     /*dupLatch->resetTable = Tbl_TableHardDup(latch->resetTable);*/
00281     Tbl_TableForEachInputVar(dupLatch->resetTable, index, var) {
00282       st_lookup(varToDupVar, var, &dupVar);
00283       Tbl_TableSubstituteVar(dupLatch->resetTable, var, dupVar);
00284     }
00285     Tbl_TableForEachOutputVar(dupLatch->resetTable, index, var) {
00286       st_lookup(varToDupVar, var, &dupVar);
00287       Tbl_TableSubstituteVar(dupLatch->resetTable, var, dupVar);
00288     }
00289     dupLatch->undef = latch->undef;
00290     st_insert(dupNode->latchTable, Var_VariableReadName(dupLatch->latchOutput), dupLatch);
00291   }
00292   dupNode->childTable = st_copy(node->childTable);
00293   dupNode->applInfoTable = st_init_table(strcmp, st_strhash);
00294   st_foreach_item(node->applInfoTable, gen, &key, &applInfo) {
00295     dupKey = util_strsav(key);
00296     st_insert(dupNode->applInfoTable, dupKey, applInfo);
00297   }
00298   dupNode->undef = node->undef;
00299   st_free_table(varToDupVar);
00300   return dupNode;
00301 }
00302 
00303     
00319 Hrc_Latch_t *
00320 Hrc_LatchCreate(
00321   Hrc_Model_t *model,
00322   Var_Variable_t *latchInput,
00323   Var_Variable_t *latchOutput)
00324 {
00325   Hrc_Latch_t *latch;
00326   char *outputName = Var_VariableReadName(latchOutput);
00327   
00328   if(!st_is_member(model->masterNode->latchTable, outputName)) {
00329     latch = ALLOC(Hrc_Latch_t, 1);
00330     latch->latchInput = latchInput;
00331     latch->latchOutput = latchOutput;
00332     latch->resetTable = NIL(Tbl_Table_t);
00333     latch->undef = NIL(void);
00334     st_insert(model->masterNode->latchTable, outputName, (char *) latch);
00335     return latch;
00336   }
00337   else {
00338     return NIL(Hrc_Latch_t);
00339   }
00340 }
00341 
00354 void
00355 Hrc_ManagerFree(
00356   Hrc_Manager_t *manager)
00357 {
00358   st_generator *gen;
00359   char *name;
00360   Hrc_Model_t *model;
00361   
00362   if(manager->rootNode != NIL(Hrc_Node_t)) {
00363     HrcNodeFreeRecursively(manager->rootNode);
00364   }
00365   Hrc_ManagerForEachModel(manager, gen, name, model){
00366     ModelFree(model);
00367   }
00368   st_free_table(manager->modelTable);
00369   FREE(manager);
00370 }
00371 
00385 boolean
00386 Hrc_ModelDelete(
00387   Hrc_Manager_t *manager,
00388   char *modelName)
00389 {
00390   Hrc_Model_t *model;
00391   
00392   if(st_delete(manager->modelTable, &(modelName), &model)) {
00393     ModelFree(model);
00394     return TRUE;
00395   }
00396   else {
00397     return FALSE;
00398   }
00399 }
00400 
00401 /*---------------------------------------------------------------------------*/
00402 /* Definition of internal functions                                          */
00403 /*---------------------------------------------------------------------------*/
00421 void
00422 HrcNodeFreeRecursively(
00423   Hrc_Node_t *node)
00424 {
00425   st_generator *gen;
00426   char *name;
00427   Hrc_Node_t *childNode;
00428   
00429   if(node == node->manager->rootNode) {
00430     node->manager->rootNode = NIL(Hrc_Node_t);
00431   }
00432   if(node == node->manager->currentNode) {
00433     node->manager->currentNode = NIL(Hrc_Node_t);
00434   }
00435   HrcNodeFreeInternalMemory(node);
00436   Hrc_NodeForEachChild(node, gen, name, childNode) {
00437     HrcNodeFreeRecursively(childNode);
00438   }
00439   st_free_table(node->childTable);
00440   FREE(node);
00441 }
00442 
00456 void
00457 HrcNodeFreeInternalMemory(
00458   Hrc_Node_t *node)
00459 {
00460   char *name;
00461   st_generator *gen;
00462   int i;
00463   Tbl_Table_t *table;
00464   Hrc_Latch_t *latch;
00465   Var_Variable_t *var;
00466   ApplInfo_t *applInfo;
00467   char *key;
00468   
00469   FREE(node->modelName);
00470   FREE(node->instanceName);
00471   array_free(node->formalInputs);
00472   array_free(node->formalOutputs);
00473   /* In a master node of a model, actualInputs and actualOutputs are NULL.
00474      That is why the following two checks are needed. */
00475   if(node->actualInputs != NIL(array_t)) {
00476     array_free(node->actualInputs);
00477   }
00478   if(node->actualOutputs != NIL(array_t)) {
00479     array_free(node->actualOutputs);
00480   }
00481   Hrc_NodeForEachNameTable(node, i, table) {
00482     Tbl_TableFree(table);
00483   }
00484   array_free(node->nameTables);
00485   Hrc_NodeForEachLatch(node, gen, name, latch) {
00486     LatchFree(latch);
00487   }
00488   st_free_table(node->latchTable);
00489   Hrc_NodeForEachVariable(node, gen, name, var) {
00490     Var_VariableFree(var);
00491   }
00492   st_free_table(node->varTable);
00493   if(node->applInfoTable) {
00494     st_foreach_item(node->applInfoTable, gen, &key, &applInfo) {
00495       FREE(key);
00496       (*applInfo->freeFn)(applInfo->data);
00497       FREE(applInfo);
00498     }
00499     st_free_table(node->applInfoTable);
00500   }
00501 }
00502 
00503 
00504 /*---------------------------------------------------------------------------*/
00505 /* Definition of static functions                                            */
00506 /*---------------------------------------------------------------------------*/
00520 static void
00521 ModelFree(
00522   Hrc_Model_t *model)
00523 {
00524   st_generator *gen;
00525   char *name;
00526   Hrc_Subckt_t *subckt;
00527   
00528   FREE(model->modelName);
00529   HrcNodeFreeInternalMemory(model->masterNode);
00530   st_free_table(model->masterNode->childTable);
00531   FREE(model->masterNode);
00532   st_foreach_item(model->subcktTable, gen, &name, &subckt) {
00533     SubcktFree(subckt);
00534   }
00535   st_free_table(model->subcktTable);
00536   FREE(model);
00537 }
00538 
00551 static void
00552 SubcktFree(
00553   Hrc_Subckt_t *subckt)
00554 {
00555   FREE(subckt->instanceName);
00556   array_free(subckt->actualInputVars);
00557   array_free(subckt->actualOutputVars);
00558   FREE(subckt);
00559 }
00560 
00573 static void
00574 LatchFree(
00575   Hrc_Latch_t *latch)
00576 {
00577   if(latch->resetTable != NIL(Tbl_Table_t)){    
00578     Tbl_TableFree(latch->resetTable);
00579   }
00580   FREE(latch);
00581 }        
00582 
00583 
00584 
00585 
00586 
00587 
00588 
00589 
00590 
00591 
00592 
00593 
00594 
00595 
00596 
00597 
00598 
00599 
00600 
00601 
00602 
00603 
00604 
00605 
00606