LCOV - code coverage report
Current view: top level - include/llvm/DebugInfo/MSF - StreamArray.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 49 55 89.1 %
Date: 2017-02-25 14:46:56 Functions: 28 30 93.3 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13