cyclegraph: don't export CycleNode::edges directly, use accessors
[model-checker.git] / cyclegraph.cc
index e1d5b3c50ef1a243eac7c2955e55d0b4a6b0e67d..0528029a12e100f239b754b62492f639d8759762 100644 (file)
@@ -6,6 +6,7 @@
 
 /** Initializes a CycleGraph object. */
 CycleGraph::CycleGraph() :
+       discovered(new HashTable<CycleNode *, CycleNode *, uintptr_t, 4, model_malloc, model_calloc, model_free>(16)),
        hasCycles(false),
        oldCycles(false),
        hasRMWViolation(false),
@@ -106,9 +107,8 @@ void CycleGraph::addRMWEdge(const ModelAction *from, const ModelAction *rmw) {
         * (2) the fromnode is the new node and therefore it should not
         * have any outgoing edges.
         */
-       std::vector<CycleNode *> * edges=fromnode->getEdges();
-       for(unsigned int i=0;i<edges->size();i++) {
-               CycleNode * tonode=(*edges)[i];
+       for (unsigned int i = 0; i < fromnode->getNumEdges(); i++) {
+               CycleNode *tonode = fromnode->getEdge(i);
                if (tonode!=rmwnode) {
                        if (rmwnode->addEdge(tonode))
                                rollbackvector.push_back(rmwnode);
@@ -125,36 +125,35 @@ void CycleGraph::addRMWEdge(const ModelAction *from, const ModelAction *rmw) {
                // careful...avoid it..
                hasCycles=checkReachable(rmwnode, fromnode);
        }
-       if(fromnode->addEdge(rmwnode))
+       if (fromnode->addEdge(rmwnode))
                rollbackvector.push_back(fromnode);
 }
 
 #if SUPPORT_MOD_ORDER_DUMP
 void CycleGraph::dumpNodes(FILE *file) {
-  for(unsigned int i=0;i<nodeList.size();i++) {
+       for (unsigned int i=0;i<nodeList.size();i++) {
                CycleNode *cn=nodeList[i];
-               std::vector<CycleNode *> * edges=cn->getEdges();
                const ModelAction *action=cn->getAction();
                fprintf(file, "N%u [label=\"%u, T%u\"];\n",action->get_seq_number(),action->get_seq_number(), action->get_tid());
                if (cn->getRMW()!=NULL) {
                        fprintf(file, "N%u -> N%u[style=dotted];\n", action->get_seq_number(), cn->getRMW()->getAction()->get_seq_number());
                }
-               for(unsigned int j=0;j<edges->size();j++) {
-                 CycleNode *dst=(*edges)[j];
+               for (unsigned int j = 0; j < cn->getNumEdges(); j++) {
+                       CycleNode *dst = cn->getEdge(j);
                        const ModelAction *dstaction=dst->getAction();
-      fprintf(file, "N%u -> N%u;\n", action->get_seq_number(), dstaction->get_seq_number());
-         }
+                       fprintf(file, "N%u -> N%u;\n", action->get_seq_number(), dstaction->get_seq_number());
+               }
        }
 }
 
 void CycleGraph::dumpGraphToFile(const char *filename) {
        char buffer[200];
-  sprintf(buffer, "%s.dot",filename);
-  FILE *file=fopen(buffer, "w");
-  fprintf(file, "digraph %s {\n",filename);
+       sprintf(buffer, "%s.dot",filename);
+       FILE *file=fopen(buffer, "w");
+       fprintf(file, "digraph %s {\n",filename);
        dumpNodes(file);
-  fprintf(file,"}\n");
-  fclose(file);        
+       fprintf(file,"}\n");
+       fclose(file);   
 }
 #endif
 
@@ -182,20 +181,20 @@ bool CycleGraph::checkReachable(const ModelAction *from, const ModelAction *to)
  */
 bool CycleGraph::checkReachable(CycleNode *from, CycleNode *to) {
        std::vector<CycleNode *, ModelAlloc<CycleNode *> > queue;
-       HashTable<CycleNode *, CycleNode *, uintptr_t, 4, model_malloc, model_calloc, model_free> discovered(64);
+       discovered->reset();
 
        queue.push_back(from);
-       discovered.put(from, from);
+       discovered->put(from, from);
        while(!queue.empty()) {
                CycleNode * node=queue.back();
                queue.pop_back();
                if (node==to)
                        return true;
 
-               for(unsigned int i=0;i<node->getEdges()->size();i++) {
-                       CycleNode *next=(*node->getEdges())[i];
-                       if (!discovered.contains(next)) {
-                               discovered.put(next,next);
+               for (unsigned int i = 0; i < node->getNumEdges(); i++) {
+                       CycleNode *next = node->getEdge(i);
+                       if (!discovered->contains(next)) {
+                               discovered->put(next,next);
                                queue.push_back(next);
                        }
                }
@@ -205,12 +204,12 @@ bool CycleGraph::checkReachable(CycleNode *from, CycleNode *to) {
 
 bool CycleGraph::checkPromise(const ModelAction *fromact, Promise *promise) {
        std::vector<CycleNode *, ModelAlloc<CycleNode *> > queue;
-       HashTable<CycleNode *, CycleNode *, uintptr_t, 4, model_malloc, model_calloc, model_free> discovered(64);
+       discovered->reset();
        CycleNode *from = actionToNode.get(fromact);
 
 
        queue.push_back(from);
-       discovered.put(from, from);
+       discovered->put(from, from);
        while(!queue.empty()) {
                CycleNode * node=queue.back();
                queue.pop_back();
@@ -219,10 +218,10 @@ bool CycleGraph::checkPromise(const ModelAction *fromact, Promise *promise) {
                        return true;
                }
 
-               for(unsigned int i=0;i<node->getEdges()->size();i++) {
-                       CycleNode *next=(*node->getEdges())[i];
-                       if (!discovered.contains(next)) {
-                               discovered.put(next,next);
+               for (unsigned int i = 0; i < node->getNumEdges(); i++) {
+                       CycleNode *next = node->getEdge(i);
+                       if (!discovered->contains(next)) {
+                               discovered->put(next,next);
                                queue.push_back(next);
                        }
                }
@@ -280,9 +279,19 @@ CycleNode::CycleNode(const ModelAction *modelaction) :
 {
 }
 
-/** @returns a vector of the edges from a CycleNode. */
-std::vector<CycleNode *> * CycleNode::getEdges() {
-       return &edges;
+/**
+ * @param i The index of the edge to return
+ * @returns The a CycleNode edge indexed by i
+ */
+CycleNode * CycleNode::getEdge(unsigned int i) const
+{
+       return edges[i];
+}
+
+/** @returns The number of edges leaving this CycleNode */
+unsigned int CycleNode::getNumEdges() const
+{
+       return edges.size();
 }
 
 /**