klee
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TreeStream.cpp
Go to the documentation of this file.
1 //===-- TreeStream.cpp ----------------------------------------------------===//
2 //
3 // The KLEE Symbolic Virtual Machine
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
11 
12 #include <cassert>
13 #include <iomanip>
14 #include <fstream>
15 #include <iterator>
16 #include <map>
17 
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include <string.h>
21 
22 using namespace klee;
23 
25 
26 TreeStreamWriter::TreeStreamWriter(const std::string &_path)
27  : lastID(0),
28  bufferCount(0),
29  path(_path),
30  output(new std::ofstream(path.c_str(),
31  std::ios::out | std::ios::binary)),
32  ids(1) {
33  if (!output->good()) {
34  delete output;
35  output = 0;
36  }
37 }
38 
40  flush();
41  if (output)
42  delete output;
43 }
44 
46  return !!output;
47 }
48 
50  return open(TreeOStream(*this, 0));
51 }
52 
54  assert(output && os.writer==this);
55  flushBuffer();
56  unsigned id = ids++;
57  output->write(reinterpret_cast<const char*>(&os.id), 4);
58  unsigned tag = id | (1<<31);
59  output->write(reinterpret_cast<const char*>(&tag), 4);
60  return TreeOStream(*this, id);
61 }
62 
63 void TreeStreamWriter::write(TreeOStream &os, const char *s, unsigned size) {
64 #if 1
65  if (bufferCount &&
66  (os.id!=lastID || size+bufferCount>bufferSize))
67  flushBuffer();
68  if (bufferCount) { // (os.id==lastID && size+bufferCount<=bufferSize)
69  memcpy(&buffer[bufferCount], s, size);
70  bufferCount += size;
71  } else if (size<bufferSize) {
72  lastID = os.id;
73  memcpy(buffer, s, size);
74  bufferCount = size;
75  } else {
76  output->write(reinterpret_cast<const char*>(&os.id), 4);
77  output->write(reinterpret_cast<const char*>(&size), 4);
78  output->write(buffer, size);
79  }
80 #else
81  output->write(reinterpret_cast<const char*>(&os.id), 4);
82  output->write(reinterpret_cast<const char*>(&size), 4);
83  output->write(s, size);
84 #endif
85 }
86 
88  if (bufferCount) {
89  output->write(reinterpret_cast<const char*>(&lastID), 4);
90  output->write(reinterpret_cast<const char*>(&bufferCount), 4);
91  output->write(buffer, bufferCount);
92  bufferCount = 0;
93  }
94 }
95 
97  flushBuffer();
98  output->flush();
99 }
100 
102  std::vector<unsigned char> &out) {
103  assert(streamID>0 && streamID<ids);
104  flush();
105 
106  std::ifstream is(path.c_str(),
107  std::ios::in | std::ios::binary);
108  assert(is.good());
109  DEBUG_WITH_TYPE("TreeStreamWriter",
110  llvm::errs() << "finding chain for: " << streamID << "\n");
111 
112  std::map<unsigned,unsigned> parents;
113  std::vector<unsigned> roots;
114  for (;;) {
115  assert(is.good());
116  unsigned id;
117  unsigned tag;
118  is.read(reinterpret_cast<char*>(&id), 4);
119  is.read(reinterpret_cast<char*>(&tag), 4);
120  if (tag&(1<<31)) { // fork
121  unsigned child = tag ^ (1<<31);
122 
123  if (child==streamID) {
124  roots.push_back(child);
125  while (id) {
126  roots.push_back(id);
127  std::map<unsigned, unsigned>::iterator it = parents.find(id);
128  assert(it!=parents.end());
129  id = it->second;
130  }
131  break;
132  } else {
133  parents.insert(std::make_pair(child,id));
134  }
135  } else {
136  unsigned size = tag;
137  while (size--) is.get();
138  }
139  }
140  DEBUG(llvm::errs() << "roots: ";
141  for (size_t i = 0, e = roots.size(); i < e; ++i) {
142  llvm::errs() << roots[i] << " ";
143  }
144  llvm::errs() << "\n";);
145  is.seekg(0, std::ios::beg);
146  for (;;) {
147  unsigned id;
148  unsigned tag;
149  is.read(reinterpret_cast<char*>(&id), 4);
150  is.read(reinterpret_cast<char*>(&tag), 4);
151  if (!is.good()) break;
152  if (tag&(1<<31)) { // fork
153  unsigned child = tag ^ (1<<31);
154  if (id==roots.back() && roots.size()>1 && child==roots[roots.size()-2])
155  roots.pop_back();
156  } else {
157  unsigned size = tag;
158  if (id==roots.back()) {
159  while (size--) out.push_back(is.get());
160  } else {
161  while (size--) is.get();
162  }
163  }
164  }
165 }
166 
168 
170  : writer(0),
171  id(0) {
172 }
173 
175  : writer(&_writer),
176  id(_id) {
177 }
178 
180 }
181 
182 unsigned TreeOStream::getID() const {
183  assert(writer);
184  return id;
185 }
186 
187 void TreeOStream::write(const char *buffer, unsigned size) {
188  assert(writer);
189  writer->write(*this, buffer, size);
190 }
191 
192 TreeOStream &TreeOStream::operator<<(const std::string &s) {
193  assert(writer);
194  write(s.c_str(), s.size());
195  return *this;
196 }
197 
199  assert(writer);
200  writer->flush();
201 }
TreeStreamWriter(const std::string &_path)
Definition: TreeStream.cpp:26
TreeStreamWriter * writer
Definition: TreeStream.h:57
void write(TreeOStream &os, const char *s, unsigned size)
Definition: TreeStream.cpp:63
char buffer[bufferSize]
Definition: TreeStream.h:27
friend class TreeOStream
Definition: TreeStream.h:24
std::ofstream * output
Definition: TreeStream.h:31
unsigned TreeStreamID
Definition: TreeStream.h:18
void readStream(TreeStreamID id, std::vector< unsigned char > &out)
Definition: TreeStream.cpp:101
void write(const char *buffer, unsigned size)
Definition: TreeStream.cpp:187
unsigned getID() const
Definition: TreeStream.cpp:182
TreeOStream & operator<<(const std::string &s)
Definition: TreeStream.cpp:192
TreeOStream open()
Definition: TreeStream.cpp:49
static const unsigned bufferSize
Definition: TreeStream.h:22