zcov: / lib/Support/TreeStream.cpp


Files: 1 Branches Taken: 2.5% 2 / 80
Generated: 2009-05-17 22:47 Branches Executed: 5.0% 4 / 80
Line Coverage: 5.9% 6 / 101


Programs: 1 Runs 371


       1                 : /* -*- mode: c++; c-basic-offset: 2; -*- */
       2                 : 
       3                 : #include "klee/Internal/ADT/TreeStream.h"
       4                 : 
       5                 : #include <cassert>
       6                 : #include <iostream>
       7                 : #include <iomanip>
       8                 : #include <fstream>
       9                 : #include <iterator>
      10                 : #include <map>
      11                 : 
      12                 : #include <string.h>
      13                 : 
      14                 : using namespace klee;
      15                 : 
      16                 : ///
      17                 : 
      18                0: TreeStreamWriter::TreeStreamWriter(const std::string &_path) 
      19                 :   : lastID(0),
      20                 :     bufferCount(0),
      21                 :     path(_path),
      22                 :     output(new std::ofstream(path.c_str(), 
      23                 :                              std::ios::out | std::ios::binary)),
      24                0:     ids(1) {
                        0: branch 0 not taken
                        0: branch 1 not taken
                        0: branch 2 not taken
                        0: branch 3 not taken
      25                0:   if (!output->good()) {
                        0: branch 0 not taken
                        0: branch 1 not taken
                        0: branch 3 not taken
                        0: branch 4 not taken
      26                0:     delete output;
      27                0:     output = 0;
      28                 :   }
      29                0: }
      30                 : 
      31                0: TreeStreamWriter::~TreeStreamWriter() {
      32                0:   flush();
                        0: branch 0 not taken
                        0: branch 1 not taken
                        0: branch 2 not taken
                        0: branch 3 not taken
      33                0:   if (output)
                        0: branch 0 not taken
                        0: branch 1 not taken
                        0: branch 3 not taken
                        0: branch 4 not taken
      34                0:     delete output;
      35                0: }
      36                 : 
      37                0: bool TreeStreamWriter::good() {
      38                0:   return !!output;
      39                 : }
      40                 : 
      41                0: TreeOStream TreeStreamWriter::open() {
      42                0:   return open(TreeOStream(*this, 0));
      43                 : }
      44                 : 
      45                0: TreeOStream TreeStreamWriter::open(const TreeOStream &os) {
                        0: branch 0 not taken
                        0: branch 1 not taken
                        0: branch 2 not taken
                        0: branch 3 not taken
      46                0:   assert(output && os.writer==this);
      47                0:   flushBuffer();
      48                0:   unsigned id = ids++;
      49                0:   output->write(reinterpret_cast<const char*>(&os.id), 4);
      50                0:   unsigned tag = id | (1<<31);
      51                0:   output->write(reinterpret_cast<const char*>(&tag), 4);
      52                0:   return TreeOStream(*this, id);
      53                 : }
      54                 : 
      55                0: void TreeStreamWriter::write(TreeOStream &os, const char *s, unsigned size) {
      56                 : #if 1
                        0: branch 0 not taken
                        0: branch 1 not taken
                        0: branch 2 not taken
                        0: branch 3 not taken
                        0: branch 4 not taken
                        0: branch 5 not taken
      57                0:   if (bufferCount && 
      58                 :       (os.id!=lastID || size+bufferCount>bufferSize))
      59                0:     flushBuffer();
                        0: branch 0 not taken
                        0: branch 1 not taken
      60                0:   if (bufferCount) { // (os.id==lastID && size+bufferCount<=bufferSize)
      61                0:     memcpy(&buffer[bufferCount], s, size);
      62                0:     bufferCount += size;
                        0: branch 0 not taken
                        0: branch 1 not taken
      63                0:   } else if (size<bufferSize) {
      64                0:     lastID = os.id;
      65                0:     memcpy(buffer, s, size);
      66                0:     bufferCount = size;
      67                 :   } else {
      68                0:     output->write(reinterpret_cast<const char*>(&os.id), 4);
      69                0:     output->write(reinterpret_cast<const char*>(&size), 4);
      70                0:     output->write(buffer, size);
      71                 :   }
      72                 : #else
      73                 :   output->write(reinterpret_cast<const char*>(&os.id), 4);
      74                 :   output->write(reinterpret_cast<const char*>(&size), 4);
      75                 :   output->write(s, size);
      76                 : #endif
      77                0: }
      78                 : 
      79                0: void TreeStreamWriter::flushBuffer() {
                        0: branch 0 not taken
                        0: branch 1 not taken
      80                0:   if (bufferCount) {    
      81                0:     output->write(reinterpret_cast<const char*>(&lastID), 4);
      82                0:     output->write(reinterpret_cast<const char*>(&bufferCount), 4);
      83                0:     output->write(buffer, bufferCount);
      84                0:     bufferCount = 0;
      85                 :   }
      86                0: }
      87                 : 
      88                0: void TreeStreamWriter::flush() {
      89                0:   flushBuffer();
      90                0:   output->flush();
      91                0: }
      92                 : 
      93                 : void TreeStreamWriter::readStream(TreeStreamID streamID,
      94                0:                                   std::vector<unsigned char> &out) {
                        0: branch 0 not taken
                        0: branch 1 not taken
                        0: branch 2 not taken
                        0: branch 3 not taken
      95                0:   assert(streamID>0 && streamID<ids);
      96                0:   flush();
      97                 :   
      98                 :   std::ifstream is(path.c_str(),
      99                0:                    std::ios::in | std::ios::binary);
                        0: branch 0 not taken
                        0: branch 1 not taken
     100                0:   assert(is.good());
     101                 : #if 0
     102                 :   std::cout << "finding chain for: " << streamID << "\n";
     103                 : #endif
     104                 :   
     105                 :   std::map<unsigned,unsigned> parents;
     106                 :   std::vector<unsigned> roots;
     107                 :   for (;;) {
                        0: branch 0 not taken
                        0: branch 1 not taken
     108                0:     assert(is.good());
     109                 :     unsigned id;
     110                 :     unsigned tag;
     111                0:     is.read(reinterpret_cast<char*>(&id), 4);
     112                0:     is.read(reinterpret_cast<char*>(&tag), 4);
                        0: branch 0 not taken
                        0: branch 1 not taken
     113                0:     if (tag&(1<<31)) { // fork
     114                0:       unsigned child = tag ^ (1<<31);
     115                 : 
                        0: branch 0 not taken
                        0: branch 1 not taken
     116                0:       if (child==streamID) {
     117                 :         roots.push_back(child);
                        0: branch 0 not taken
                        0: branch 1 not taken
     118                0:         while (id) {
     119                 :           roots.push_back(id);
     120                 :           std::map<unsigned, unsigned>::iterator it = parents.find(id);
                        0: branch 0 not taken
                        0: branch 1 not taken
     121                0:           assert(it!=parents.end());
     122                0:           id = it->second;
     123                 :         } 
     124                 :         break;
     125                 :       } else {
     126                0:         parents.insert(std::make_pair(child,id));
     127                 :       }
     128                 :     } else {
     129                0:       unsigned size = tag;
                        0: branch 1 not taken
                        0: branch 2 not taken
     130                0:       while (size--) is.get();
     131                 :     }
     132                 :   }
     133                 : #if 0
     134                 :   std::cout << "roots: ";
     135                 :   std::copy(roots.begin(), roots.end(), std::ostream_iterator<unsigned>(std::cout, " "));
     136                 :   std::cout << "\n"; 
     137                 : #endif
     138                0:   is.seekg(0, std::ios::beg);
     139                 :   for (;;) {
     140                 :     unsigned id;
     141                 :     unsigned tag;
     142                0:     is.read(reinterpret_cast<char*>(&id), 4);
     143                0:     is.read(reinterpret_cast<char*>(&tag), 4);
                        0: branch 0 not taken
                        0: branch 1 not taken
     144                0:     if (!is.good()) break;
                        0: branch 0 not taken
                        0: branch 1 not taken
     145                0:     if (tag&(1<<31)) { // fork
     146                0:       unsigned child = tag ^ (1<<31);
                        0: branch 0 not taken
                        0: branch 1 not taken
                        0: branch 2 not taken
                        0: branch 3 not taken
                        0: branch 4 not taken
                        0: branch 5 not taken
                        0: branch 6 not taken
                        0: branch 7 not taken
     147                0:       if (id==roots.back() && roots.size()>1 && child==roots[roots.size()-2])
     148                 :         roots.pop_back();
     149                 :     } else {
     150                0:       unsigned size = tag;
                        0: branch 0 not taken
                        0: branch 1 not taken
     151                0:       if (id==roots.back()) {
                        0: branch 1 not taken
                        0: branch 2 not taken
     152                0:         while (size--) out.push_back(is.get());
     153                 :       } else {
                        0: branch 1 not taken
                        0: branch 2 not taken
     154                0:         while (size--) is.get();
     155                 :       }
     156                 :     }
     157                0:   }  
     158                0: }
     159                 : 
     160                 : ///
     161                 : 
     162              206: TreeOStream::TreeOStream()
     163                 :   : writer(0),
     164              206:     id(0) {
     165              206: }
     166                 : 
     167                0: TreeOStream::TreeOStream(TreeStreamWriter &_writer, unsigned _id)
     168                 :   : writer(&_writer),
     169                0:     id(_id) {
     170                0: }
     171                 : 
     172             2906: TreeOStream::~TreeOStream() {
     173             2906: }
     174                 : 
     175                0: unsigned TreeOStream::getID() const {
                        0: branch 0 not taken
                        0: branch 1 not taken
     176                0:   assert(writer);
     177                0:   return id;
     178                 : }
     179                 : 
     180                0: void TreeOStream::write(const char *buffer, unsigned size) {
                        0: branch 0 not taken
                        0: branch 1 not taken
     181                0:   assert(writer);
     182                0:   writer->write(*this, buffer, size);
     183                0: }
     184                 : 
     185                0: TreeOStream &TreeOStream::operator<<(const std::string &s) {
                        0: branch 0 not taken
                        0: branch 1 not taken
     186                0:   assert(writer);
     187                0:   write(s.c_str(), s.size());
     188                0:   return *this;
     189                 : }
     190                 : 
     191                0: void TreeOStream::flush() {
                        0: branch 0 not taken
                        0: branch 1 not taken
     192                0:   assert(writer);
     193                0:   writer->flush();
                      103: branch 0 taken
                        0: branch 1 not taken
                      103: branch 2 taken
                        0: branch 3 not taken
     194              206: }

Generated: 2009-05-17 22:47 by zcov