[llvm] r306122 - Add a BinarySubstreamRef, and a method to read one.

Zachary Turner via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 23 09:38:40 PDT 2017


Author: zturner
Date: Fri Jun 23 11:38:40 2017
New Revision: 306122

URL: http://llvm.org/viewvc/llvm-project?rev=306122&view=rev
Log:
Add a BinarySubstreamRef, and a method to read one.

This is essentially just a BinaryStreamRef packaged with an
offset and the logic for reading one is no different than the
logic for reading a BinaryStreamRef, except that we save the
current offset.

Modified:
    llvm/trunk/include/llvm/Support/BinaryStreamReader.h
    llvm/trunk/include/llvm/Support/BinaryStreamRef.h
    llvm/trunk/lib/Support/BinaryStreamReader.cpp

Modified: llvm/trunk/include/llvm/Support/BinaryStreamReader.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/BinaryStreamReader.h?rev=306122&r1=306121&r2=306122&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/BinaryStreamReader.h (original)
+++ llvm/trunk/include/llvm/Support/BinaryStreamReader.h Fri Jun 23 11:38:40 2017
@@ -137,6 +137,15 @@ public:
   /// returns an appropriate error code.
   Error readStreamRef(BinaryStreamRef &Ref, uint32_t Length);
 
+  /// Read \p Length bytes from the underlying stream into \p Stream.  This is
+  /// equivalent to calling getUnderlyingStream().slice(Offset, Length).
+  /// Updates the stream's offset to point after the newly read object.  Never
+  /// causes a copy.
+  ///
+  /// \returns a success error code if the data was successfully read, otherwise
+  /// returns an appropriate error code.
+  Error readSubstream(BinarySubstreamRef &Stream, uint32_t Size);
+
   /// Get a pointer to an object of type T from the underlying stream, as if by
   /// memcpy, and store the result into \p Dest.  It is up to the caller to
   /// ensure that objects of type T can be safely treated in this manner.

Modified: llvm/trunk/include/llvm/Support/BinaryStreamRef.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/BinaryStreamRef.h?rev=306122&r1=306121&r2=306122&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/BinaryStreamRef.h (original)
+++ llvm/trunk/include/llvm/Support/BinaryStreamRef.h Fri Jun 23 11:38:40 2017
@@ -166,6 +166,11 @@ public:
                                    ArrayRef<uint8_t> &Buffer) const;
 };
 
+struct BinarySubstreamRef {
+  uint32_t Offset;            // Offset in the parent stream
+  BinaryStreamRef StreamData; // Stream Data
+};
+
 class WritableBinaryStreamRef
     : public BinaryStreamRefBase<WritableBinaryStreamRef,
                                  WritableBinaryStream> {

Modified: llvm/trunk/lib/Support/BinaryStreamReader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/BinaryStreamReader.cpp?rev=306122&r1=306121&r2=306122&view=diff
==============================================================================
--- llvm/trunk/lib/Support/BinaryStreamReader.cpp (original)
+++ llvm/trunk/lib/Support/BinaryStreamReader.cpp Fri Jun 23 11:38:40 2017
@@ -109,6 +109,12 @@ Error BinaryStreamReader::readStreamRef(
   return Error::success();
 }
 
+Error BinaryStreamReader::readSubstream(BinarySubstreamRef &Stream,
+                                        uint32_t Size) {
+  Stream.Offset = getOffset();
+  return readStreamRef(Stream.StreamData, Size);
+}
+
 Error BinaryStreamReader::skip(uint32_t Amount) {
   if (Amount > bytesRemaining())
     return make_error<BinaryStreamError>(stream_error_code::stream_too_short);




More information about the llvm-commits mailing list