Line data Source code
1 : //===- StreamArray.h - Array backed by an arbitrary stream ----------------===//
2 : //
3 : // The LLVM Compiler Infrastructure
4 : //
5 : // This file is distributed under the University of Illinois Open Source
6 : // License. See LICENSE.TXT for details.
7 : //
8 : //===----------------------------------------------------------------------===//
9 :
10 : #ifndef LLVM_DEBUGINFO_MSF_STREAMARRAY_H
11 : #define LLVM_DEBUGINFO_MSF_STREAMARRAY_H
12 :
13 : #include "llvm/DebugInfo/Msf/StreamRef.h"
14 : #include "llvm/Support/Error.h"
15 :
16 : #include <functional>
17 : #include <type_traits>
18 :
19 : namespace llvm {
20 : namespace msf {
21 :
22 : /// VarStreamArrayExtractor is intended to be specialized to provide customized
23 : /// extraction logic. On input it receives a StreamRef pointing to the
24 : /// beginning of the next record, but where the length of the record is not yet
25 : /// known. Upon completion, it should return an appropriate Error instance if
26 : /// a record could not be extracted, or if one could be extracted it should
27 : /// return success and set Len to the number of bytes this record occupied in
28 : /// the underlying stream, and it should fill out the fields of the value type
29 : /// Item appropriately to represent the current record.
30 : ///
31 : /// You can specialize this template for your own custom value types to avoid
32 : /// having to specify a second template argument to VarStreamArray (documented
33 : /// below).
34 : template <typename T> struct VarStreamArrayExtractor {
35 : // Method intentionally deleted. You must provide an explicit specialization
36 : // with the following method implemented.
37 : Error operator()(ReadableStreamRef Stream, uint32_t &Len,
38 : T &Item) const = delete;
39 : };
40 :
41 : /// VarStreamArray represents an array of variable length records backed by a
42 : /// stream. This could be a contiguous sequence of bytes in memory, it could
43 : /// be a file on disk, or it could be a PDB stream where bytes are stored as
44 : /// discontiguous blocks in a file. Usually it is desirable to treat arrays
45 : /// as contiguous blocks of memory, but doing so with large PDB files, for
46 : /// example, could mean allocating huge amounts of memory just to allow
47 : /// re-ordering of stream data to be contiguous before iterating over it. By
48 : /// abstracting this out, we need not duplicate this memory, and we can
49 : /// iterate over arrays in arbitrarily formatted streams. Elements are parsed
50 : /// lazily on iteration, so there is no upfront cost associated with building
51 : /// a VarStreamArray, no matter how large it may be.
52 : ///
53 : /// You create a VarStreamArray by specifying a ValueType and an Extractor type.
54 : /// If you do not specify an Extractor type, it expects you to specialize
55 : /// VarStreamArrayExtractor<T> for your ValueType.
56 : ///
57 : /// By default an Extractor is default constructed in the class, but in some
58 : /// cases you might find it useful for an Extractor to maintain state across
59 : /// extractions. In this case you can provide your own Extractor through a
60 : /// secondary constructor. The following examples show various ways of
61 : /// creating a VarStreamArray.
62 : ///
63 : /// // Will use VarStreamArrayExtractor<MyType> as the extractor.
64 : /// VarStreamArray<MyType> MyTypeArray;
65 : ///
66 : /// // Will use a default-constructed MyExtractor as the extractor.
67 : /// VarStreamArray<MyType, MyExtractor> MyTypeArray2;
68 : ///
69 : /// // Will use the specific instance of MyExtractor provided.
70 : /// // MyExtractor need not be default-constructible in this case.
71 : /// MyExtractor E(SomeContext);
72 : /// VarStreamArray<MyType, MyExtractor> MyTypeArray3(E);
73 : ///
74 : template <typename ValueType, typename Extractor> class VarStreamArrayIterator;
75 :
76 : template <typename ValueType,
77 : typename Extractor = VarStreamArrayExtractor<ValueType>>
78 : class VarStreamArray {
79 : friend class VarStreamArrayIterator<ValueType, Extractor>;
80 :
81 : public:
82 : typedef VarStreamArrayIterator<ValueType, Extractor> Iterator;
83 :
84 728 : VarStreamArray() {}
85 4 : explicit VarStreamArray(const Extractor &E) : E(E) {}
86 :
87 : explicit VarStreamArray(ReadableStreamRef Stream) : Stream(Stream) {}
88 : VarStreamArray(ReadableStreamRef Stream, const Extractor &E)
89 : : Stream(Stream), E(E) {}
90 :
91 : VarStreamArray(const VarStreamArray<ValueType, Extractor> &Other)
92 : : Stream(Other.Stream), E(Other.E) {}
93 :
94 : Iterator begin(bool *HadError = nullptr) const {
95 370 : return Iterator(*this, E, HadError);
96 : }
97 :
98 740 : Iterator end() const { return Iterator(E); }
99 :
100 : const Extractor &getExtractor() const { return E; }
101 :
102 : ReadableStreamRef getUnderlyingStream() const { return Stream; }
103 :
104 : private:
105 : ReadableStreamRef Stream;
106 : Extractor E;
107 : };
108 :
109 : template <typename ValueType, typename Extractor> class VarStreamArrayIterator {
110 : typedef VarStreamArrayIterator<ValueType, Extractor> IterType;
111 : typedef VarStreamArray<ValueType, Extractor> ArrayType;
112 :
113 : public:
114 370 : VarStreamArrayIterator(const ArrayType &Array, const Extractor &E,
115 : bool *HadError = nullptr)
116 733 : : IterRef(Array.Stream), Array(&Array), HadError(HadError), Extract(E) {
117 370 : if (IterRef.getLength() == 0)
118 2 : moveToEnd();
119 : else {
120 736 : auto EC = Extract(IterRef, ThisLen, ThisValue);
121 368 : if (EC) {
122 0 : consumeError(std::move(EC));
123 : markError();
124 : }
125 : }
126 370 : }
127 0 : VarStreamArrayIterator() {}
128 1103 : explicit VarStreamArrayIterator(const Extractor &E) : Extract(E) {}
129 794 : ~VarStreamArrayIterator() {}
130 :
131 3669 : bool operator==(const IterType &R) const {
132 3669 : if (Array && R.Array) {
133 : // Both have a valid array, make sure they're same.
134 : assert(Array == R.Array);
135 0 : return IterRef == R.IterRef;
136 : }
137 :
138 : // Both iterators are at the end.
139 3669 : if (!Array && !R.Array)
140 : return true;
141 :
142 : // One is not at the end and one is.
143 3299 : return false;
144 : }
145 :
146 3669 : bool operator!=(const IterType &R) { return !(*this == R); }
147 :
148 : const ValueType &operator*() const {
149 : assert(Array && !HasError);
150 : return ThisValue;
151 : }
152 :
153 3299 : IterType &operator++() {
154 : // We are done with the current record, discard it so that we are
155 : // positioned at the next record.
156 6598 : IterRef = IterRef.drop_front(ThisLen);
157 3299 : if (IterRef.getLength() == 0) {
158 : // There is nothing after the current record, we must make this an end
159 : // iterator.
160 366 : moveToEnd();
161 : } else {
162 : // There is some data after the current record.
163 5866 : auto EC = Extract(IterRef, ThisLen, ThisValue);
164 2933 : if (EC) {
165 6 : consumeError(std::move(EC));
166 : markError();
167 2931 : } else if (ThisLen == 0) {
168 : // An empty record? Make this an end iterator.
169 0 : moveToEnd();
170 : }
171 : }
172 3299 : return *this;
173 : }
174 :
175 : IterType operator++(int) {
176 : IterType Original = *this;
177 : ++*this;
178 : return Original;
179 : }
180 :
181 : private:
182 : void moveToEnd() {
183 370 : Array = nullptr;
184 368 : ThisLen = 0;
185 : }
186 : void markError() {
187 2 : moveToEnd();
188 2 : HasError = true;
189 2 : if (HadError != nullptr)
190 0 : *HadError = true;
191 : }
192 :
193 : ValueType ThisValue;
194 : ReadableStreamRef IterRef;
195 : const ArrayType *Array{nullptr};
196 : uint32_t ThisLen{0};
197 : bool HasError{false};
198 : bool *HadError{nullptr};
199 : Extractor Extract;
200 : };
201 :
202 : template <typename T> class FixedStreamArrayIterator;
203 :
204 : template <typename T> class FixedStreamArray {
205 : friend class FixedStreamArrayIterator<T>;
206 :
207 : public:
208 136 : FixedStreamArray() : Stream() {}
209 : FixedStreamArray(ReadableStreamRef Stream) : Stream(Stream) {
210 : assert(Stream.getLength() % sizeof(T) == 0);
211 : }
212 :
213 1150 : const T &operator[](uint32_t Index) const {
214 : assert(Index < size());
215 1150 : uint32_t Off = Index * sizeof(T);
216 1150 : ArrayRef<uint8_t> Data;
217 3450 : if (auto EC = Stream.readBytes(Off, sizeof(T), Data)) {
218 : assert(false && "Unexpected failure reading from stream");
219 : // This should never happen since we asserted that the stream length was
220 : // an exact multiple of the element size.
221 0 : consumeError(std::move(EC));
222 : }
223 1150 : return *reinterpret_cast<const T *>(Data.data());
224 : }
225 :
226 501 : uint32_t size() const { return Stream.getLength() / sizeof(T); }
227 :
228 : FixedStreamArrayIterator<T> begin() const {
229 37 : return FixedStreamArrayIterator<T>(*this, 0);
230 : }
231 : FixedStreamArrayIterator<T> end() const {
232 37 : return FixedStreamArrayIterator<T>(*this, size());
233 : }
234 :
235 : ReadableStreamRef getUnderlyingStream() const { return Stream; }
236 :
237 : private:
238 : ReadableStreamRef Stream;
239 : };
240 :
241 : template <typename T> class FixedStreamArrayIterator {
242 : public:
243 : FixedStreamArrayIterator(const FixedStreamArray<T> &Array, uint32_t Index)
244 : : Array(Array), Index(Index) {}
245 :
246 : bool operator==(const FixedStreamArrayIterator<T> &R) {
247 : assert(&Array == &R.Array);
248 : return Index == R.Index;
249 : }
250 :
251 : bool operator!=(const FixedStreamArrayIterator<T> &R) {
252 471 : return !(*this == R);
253 : }
254 :
255 434 : const T &operator*() const { return Array[Index]; }
256 :
257 : FixedStreamArrayIterator<T> &operator++() {
258 : assert(Index < Array.size());
259 434 : ++Index;
260 : return *this;
261 : }
262 :
263 : FixedStreamArrayIterator<T> operator++(int) {
264 : FixedStreamArrayIterator<T> Original = *this;
265 : ++*this;
266 : return Original;
267 : }
268 :
269 : private:
270 : const FixedStreamArray<T> &Array;
271 : uint32_t Index;
272 : };
273 :
274 : } // namespace msf
275 : } // namespace llvm
276 :
277 : #endif // LLVM_DEBUGINFO_MSF_STREAMARRAY_H
|