diff options
Diffstat (limited to 'lib/DebugInfo/MSF')
-rw-r--r-- | lib/DebugInfo/MSF/CMakeLists.txt | 2 | ||||
-rw-r--r-- | lib/DebugInfo/MSF/MappedBlockStream.cpp | 74 | ||||
-rw-r--r-- | lib/DebugInfo/MSF/StreamReader.cpp | 156 | ||||
-rw-r--r-- | lib/DebugInfo/MSF/StreamWriter.cpp | 98 |
4 files changed, 32 insertions, 298 deletions
diff --git a/lib/DebugInfo/MSF/CMakeLists.txt b/lib/DebugInfo/MSF/CMakeLists.txt index dcb2a8e0cc9c..6f38de336ee0 100644 --- a/lib/DebugInfo/MSF/CMakeLists.txt +++ b/lib/DebugInfo/MSF/CMakeLists.txt @@ -3,8 +3,6 @@ add_llvm_library(LLVMDebugInfoMSF MSFBuilder.cpp MSFCommon.cpp MSFError.cpp - StreamReader.cpp - StreamWriter.cpp ADDITIONAL_HEADER_DIRS "${LLVM_MAIN_INCLUDE_DIR}/llvm/DebugInfo/MSF" ) diff --git a/lib/DebugInfo/MSF/MappedBlockStream.cpp b/lib/DebugInfo/MSF/MappedBlockStream.cpp index e52c88a5bfb8..57953cfa338e 100644 --- a/lib/DebugInfo/MSF/MappedBlockStream.cpp +++ b/lib/DebugInfo/MSF/MappedBlockStream.cpp @@ -11,8 +11,8 @@ #include "llvm/DebugInfo/MSF/IMSFFile.h" #include "llvm/DebugInfo/MSF/MSFCommon.h" -#include "llvm/DebugInfo/MSF/MSFError.h" #include "llvm/DebugInfo/MSF/MSFStreamLayout.h" +#include "llvm/Support/BinaryStreamError.h" using namespace llvm; using namespace llvm::msf; @@ -47,22 +47,20 @@ static Interval intersect(const Interval &I1, const Interval &I2) { MappedBlockStream::MappedBlockStream(uint32_t BlockSize, uint32_t NumBlocks, const MSFStreamLayout &Layout, - const ReadableStream &MsfData) + BinaryStreamRef MsfData) : BlockSize(BlockSize), NumBlocks(NumBlocks), StreamLayout(Layout), MsfData(MsfData) {} std::unique_ptr<MappedBlockStream> MappedBlockStream::createStream(uint32_t BlockSize, uint32_t NumBlocks, const MSFStreamLayout &Layout, - const ReadableStream &MsfData) { + BinaryStreamRef MsfData) { return llvm::make_unique<MappedBlockStreamImpl<MappedBlockStream>>( BlockSize, NumBlocks, Layout, MsfData); } -std::unique_ptr<MappedBlockStream> -MappedBlockStream::createIndexedStream(const MSFLayout &Layout, - const ReadableStream &MsfData, - uint32_t StreamIndex) { +std::unique_ptr<MappedBlockStream> MappedBlockStream::createIndexedStream( + const MSFLayout &Layout, BinaryStreamRef MsfData, uint32_t StreamIndex) { assert(StreamIndex < Layout.StreamMap.size() && "Invalid stream index"); MSFStreamLayout SL; SL.Blocks = Layout.StreamMap[StreamIndex]; @@ -73,7 +71,7 @@ MappedBlockStream::createIndexedStream(const MSFLayout &Layout, std::unique_ptr<MappedBlockStream> MappedBlockStream::createDirectoryStream(const MSFLayout &Layout, - const ReadableStream &MsfData) { + BinaryStreamRef MsfData) { MSFStreamLayout SL; SL.Blocks = Layout.DirectoryBlocks; SL.Length = Layout.SB->NumDirectoryBytes; @@ -82,19 +80,17 @@ MappedBlockStream::createDirectoryStream(const MSFLayout &Layout, std::unique_ptr<MappedBlockStream> MappedBlockStream::createFpmStream(const MSFLayout &Layout, - const ReadableStream &MsfData) { + BinaryStreamRef MsfData) { MSFStreamLayout SL; initializeFpmStreamLayout(Layout, SL); return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData); } Error MappedBlockStream::readBytes(uint32_t Offset, uint32_t Size, - ArrayRef<uint8_t> &Buffer) const { + ArrayRef<uint8_t> &Buffer) { // Make sure we aren't trying to read beyond the end of the stream. - if (Size > StreamLayout.Length) - return make_error<MSFError>(msf_error_code::insufficient_buffer); - if (Offset > StreamLayout.Length - Size) - return make_error<MSFError>(msf_error_code::insufficient_buffer); + if (auto EC = checkOffset(Offset, Size)) + return EC; if (tryReadContiguously(Offset, Size, Buffer)) return Error::success(); @@ -168,11 +164,12 @@ Error MappedBlockStream::readBytes(uint32_t Offset, uint32_t Size, return Error::success(); } -Error MappedBlockStream::readLongestContiguousChunk( - uint32_t Offset, ArrayRef<uint8_t> &Buffer) const { +Error MappedBlockStream::readLongestContiguousChunk(uint32_t Offset, + ArrayRef<uint8_t> &Buffer) { // Make sure we aren't trying to read beyond the end of the stream. - if (Offset >= StreamLayout.Length) - return make_error<MSFError>(msf_error_code::insufficient_buffer); + if (auto EC = checkOffset(Offset, 1)) + return EC; + uint32_t First = Offset / BlockSize; uint32_t Last = First; @@ -197,10 +194,10 @@ Error MappedBlockStream::readLongestContiguousChunk( return Error::success(); } -uint32_t MappedBlockStream::getLength() const { return StreamLayout.Length; } +uint32_t MappedBlockStream::getLength() { return StreamLayout.Length; } bool MappedBlockStream::tryReadContiguously(uint32_t Offset, uint32_t Size, - ArrayRef<uint8_t> &Buffer) const { + ArrayRef<uint8_t> &Buffer) { if (Size == 0) { Buffer = ArrayRef<uint8_t>(); return true; @@ -241,15 +238,13 @@ bool MappedBlockStream::tryReadContiguously(uint32_t Offset, uint32_t Size, } Error MappedBlockStream::readBytes(uint32_t Offset, - MutableArrayRef<uint8_t> Buffer) const { + MutableArrayRef<uint8_t> Buffer) { uint32_t BlockNum = Offset / BlockSize; uint32_t OffsetInBlock = Offset % BlockSize; // Make sure we aren't trying to read beyond the end of the stream. - if (Buffer.size() > StreamLayout.Length) - return make_error<MSFError>(msf_error_code::insufficient_buffer); - if (Offset > StreamLayout.Length - Buffer.size()) - return make_error<MSFError>(msf_error_code::insufficient_buffer); + if (auto EC = checkOffset(Offset, Buffer.size())) + return EC; uint32_t BytesLeft = Buffer.size(); uint32_t BytesWritten = 0; @@ -319,21 +314,21 @@ void MappedBlockStream::fixCacheAfterWrite(uint32_t Offset, WritableMappedBlockStream::WritableMappedBlockStream( uint32_t BlockSize, uint32_t NumBlocks, const MSFStreamLayout &Layout, - const WritableStream &MsfData) + WritableBinaryStreamRef MsfData) : ReadInterface(BlockSize, NumBlocks, Layout, MsfData), WriteInterface(MsfData) {} std::unique_ptr<WritableMappedBlockStream> WritableMappedBlockStream::createStream(uint32_t BlockSize, uint32_t NumBlocks, const MSFStreamLayout &Layout, - const WritableStream &MsfData) { + WritableBinaryStreamRef MsfData) { return llvm::make_unique<MappedBlockStreamImpl<WritableMappedBlockStream>>( BlockSize, NumBlocks, Layout, MsfData); } std::unique_ptr<WritableMappedBlockStream> WritableMappedBlockStream::createIndexedStream(const MSFLayout &Layout, - const WritableStream &MsfData, + WritableBinaryStreamRef MsfData, uint32_t StreamIndex) { assert(StreamIndex < Layout.StreamMap.size() && "Invalid stream index"); MSFStreamLayout SL; @@ -344,7 +339,7 @@ WritableMappedBlockStream::createIndexedStream(const MSFLayout &Layout, std::unique_ptr<WritableMappedBlockStream> WritableMappedBlockStream::createDirectoryStream( - const MSFLayout &Layout, const WritableStream &MsfData) { + const MSFLayout &Layout, WritableBinaryStreamRef MsfData) { MSFStreamLayout SL; SL.Blocks = Layout.DirectoryBlocks; SL.Length = Layout.SB->NumDirectoryBytes; @@ -353,34 +348,31 @@ WritableMappedBlockStream::createDirectoryStream( std::unique_ptr<WritableMappedBlockStream> WritableMappedBlockStream::createFpmStream(const MSFLayout &Layout, - const WritableStream &MsfData) { + WritableBinaryStreamRef MsfData) { MSFStreamLayout SL; initializeFpmStreamLayout(Layout, SL); return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData); } Error WritableMappedBlockStream::readBytes(uint32_t Offset, uint32_t Size, - ArrayRef<uint8_t> &Buffer) const { + ArrayRef<uint8_t> &Buffer) { return ReadInterface.readBytes(Offset, Size, Buffer); } Error WritableMappedBlockStream::readLongestContiguousChunk( - uint32_t Offset, ArrayRef<uint8_t> &Buffer) const { + uint32_t Offset, ArrayRef<uint8_t> &Buffer) { return ReadInterface.readLongestContiguousChunk(Offset, Buffer); } -uint32_t WritableMappedBlockStream::getLength() const { +uint32_t WritableMappedBlockStream::getLength() { return ReadInterface.getLength(); } Error WritableMappedBlockStream::writeBytes(uint32_t Offset, - ArrayRef<uint8_t> Buffer) const { + ArrayRef<uint8_t> Buffer) { // Make sure we aren't trying to write beyond the end of the stream. - if (Buffer.size() > getStreamLength()) - return make_error<MSFError>(msf_error_code::insufficient_buffer); - - if (Offset > getStreamLayout().Length - Buffer.size()) - return make_error<MSFError>(msf_error_code::insufficient_buffer); + if (auto EC = checkOffset(Offset, Buffer.size())) + return EC; uint32_t BlockNum = Offset / getBlockSize(); uint32_t OffsetInBlock = Offset % getBlockSize(); @@ -410,6 +402,4 @@ Error WritableMappedBlockStream::writeBytes(uint32_t Offset, return Error::success(); } -Error WritableMappedBlockStream::commit() const { - return WriteInterface.commit(); -} +Error WritableMappedBlockStream::commit() { return WriteInterface.commit(); } diff --git a/lib/DebugInfo/MSF/StreamReader.cpp b/lib/DebugInfo/MSF/StreamReader.cpp deleted file mode 100644 index b85fd14a3b7f..000000000000 --- a/lib/DebugInfo/MSF/StreamReader.cpp +++ /dev/null @@ -1,156 +0,0 @@ -//===- StreamReader.cpp - Reads bytes and objects from a stream -----------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "llvm/DebugInfo/MSF/StreamReader.h" - -#include "llvm/DebugInfo/MSF/MSFError.h" -#include "llvm/DebugInfo/MSF/StreamRef.h" - -using namespace llvm; -using namespace llvm::msf; - -StreamReader::StreamReader(ReadableStreamRef S) : Stream(S), Offset(0) {} - -Error StreamReader::readLongestContiguousChunk(ArrayRef<uint8_t> &Buffer) { - if (auto EC = Stream.readLongestContiguousChunk(Offset, Buffer)) - return EC; - Offset += Buffer.size(); - return Error::success(); -} - -Error StreamReader::readBytes(ArrayRef<uint8_t> &Buffer, uint32_t Size) { - if (auto EC = Stream.readBytes(Offset, Size, Buffer)) - return EC; - Offset += Size; - return Error::success(); -} - -Error StreamReader::readInteger(uint8_t &Dest) { - const uint8_t *P; - if (auto EC = readObject(P)) - return EC; - Dest = *P; - return Error::success(); -} - -Error StreamReader::readInteger(uint16_t &Dest) { - const support::ulittle16_t *P; - if (auto EC = readObject(P)) - return EC; - Dest = *P; - return Error::success(); -} - -Error StreamReader::readInteger(uint32_t &Dest) { - const support::ulittle32_t *P; - if (auto EC = readObject(P)) - return EC; - Dest = *P; - return Error::success(); -} - -Error StreamReader::readInteger(uint64_t &Dest) { - const support::ulittle64_t *P; - if (auto EC = readObject(P)) - return EC; - Dest = *P; - return Error::success(); -} - -Error StreamReader::readInteger(int8_t &Dest) { - const int8_t *P; - if (auto EC = readObject(P)) - return EC; - Dest = *P; - return Error::success(); -} - -Error StreamReader::readInteger(int16_t &Dest) { - const support::little16_t *P; - if (auto EC = readObject(P)) - return EC; - Dest = *P; - return Error::success(); -} - -Error StreamReader::readInteger(int32_t &Dest) { - const support::little32_t *P; - if (auto EC = readObject(P)) - return EC; - Dest = *P; - return Error::success(); -} - -Error StreamReader::readInteger(int64_t &Dest) { - const support::little64_t *P; - if (auto EC = readObject(P)) - return EC; - Dest = *P; - return Error::success(); -} - -Error StreamReader::readZeroString(StringRef &Dest) { - uint32_t Length = 0; - // First compute the length of the string by reading 1 byte at a time. - uint32_t OriginalOffset = getOffset(); - const char *C; - do { - if (auto EC = readObject(C)) - return EC; - if (*C != '\0') - ++Length; - } while (*C != '\0'); - // Now go back and request a reference for that many bytes. - uint32_t NewOffset = getOffset(); - setOffset(OriginalOffset); - - ArrayRef<uint8_t> Data; - if (auto EC = readBytes(Data, Length)) - return EC; - Dest = StringRef(reinterpret_cast<const char *>(Data.begin()), Data.size()); - - // Now set the offset back to where it was after we calculated the length. - setOffset(NewOffset); - return Error::success(); -} - -Error StreamReader::readFixedString(StringRef &Dest, uint32_t Length) { - ArrayRef<uint8_t> Bytes; - if (auto EC = readBytes(Bytes, Length)) - return EC; - Dest = StringRef(reinterpret_cast<const char *>(Bytes.begin()), Bytes.size()); - return Error::success(); -} - -Error StreamReader::readStreamRef(ReadableStreamRef &Ref) { - return readStreamRef(Ref, bytesRemaining()); -} - -Error StreamReader::readStreamRef(ReadableStreamRef &Ref, uint32_t Length) { - if (bytesRemaining() < Length) - return make_error<MSFError>(msf_error_code::insufficient_buffer); - Ref = Stream.slice(Offset, Length); - Offset += Length; - return Error::success(); -} - -Error StreamReader::skip(uint32_t Amount) { - if (Amount > bytesRemaining()) - return make_error<MSFError>(msf_error_code::insufficient_buffer); - Offset += Amount; - return Error::success(); -} - -uint8_t StreamReader::peek() const { - ArrayRef<uint8_t> Buffer; - auto EC = Stream.readBytes(Offset, 1, Buffer); - assert(!EC && "Cannot peek an empty buffer!"); - llvm::consumeError(std::move(EC)); - return Buffer[0]; -} diff --git a/lib/DebugInfo/MSF/StreamWriter.cpp b/lib/DebugInfo/MSF/StreamWriter.cpp deleted file mode 100644 index cdae7c5acc04..000000000000 --- a/lib/DebugInfo/MSF/StreamWriter.cpp +++ /dev/null @@ -1,98 +0,0 @@ -//===- StreamWrite.cpp - Writes bytes and objects to a stream -------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "llvm/DebugInfo/MSF/StreamWriter.h" - -#include "llvm/DebugInfo/MSF/MSFError.h" -#include "llvm/DebugInfo/MSF/StreamReader.h" -#include "llvm/DebugInfo/MSF/StreamRef.h" - -using namespace llvm; -using namespace llvm::msf; - -StreamWriter::StreamWriter(WritableStreamRef S) : Stream(S), Offset(0) {} - -Error StreamWriter::writeBytes(ArrayRef<uint8_t> Buffer) { - if (auto EC = Stream.writeBytes(Offset, Buffer)) - return EC; - Offset += Buffer.size(); - return Error::success(); -} - -Error StreamWriter::writeInteger(uint8_t Int) { return writeObject(Int); } - -Error StreamWriter::writeInteger(uint16_t Int) { - return writeObject(support::ulittle16_t(Int)); -} - -Error StreamWriter::writeInteger(uint32_t Int) { - return writeObject(support::ulittle32_t(Int)); -} - -Error StreamWriter::writeInteger(uint64_t Int) { - return writeObject(support::ulittle64_t(Int)); -} - -Error StreamWriter::writeInteger(int8_t Int) { return writeObject(Int); } - -Error StreamWriter::writeInteger(int16_t Int) { - return writeObject(support::little16_t(Int)); -} - -Error StreamWriter::writeInteger(int32_t Int) { - return writeObject(support::little32_t(Int)); -} - -Error StreamWriter::writeInteger(int64_t Int) { - return writeObject(support::little64_t(Int)); -} - -Error StreamWriter::writeZeroString(StringRef Str) { - if (auto EC = writeFixedString(Str)) - return EC; - if (auto EC = writeObject('\0')) - return EC; - - return Error::success(); -} - -Error StreamWriter::writeFixedString(StringRef Str) { - ArrayRef<uint8_t> Bytes(Str.bytes_begin(), Str.bytes_end()); - if (auto EC = Stream.writeBytes(Offset, Bytes)) - return EC; - - Offset += Str.size(); - return Error::success(); -} - -Error StreamWriter::writeStreamRef(ReadableStreamRef Ref) { - if (auto EC = writeStreamRef(Ref, Ref.getLength())) - return EC; - // Don't increment Offset here, it is done by the overloaded call to - // writeStreamRef. - return Error::success(); -} - -Error StreamWriter::writeStreamRef(ReadableStreamRef Ref, uint32_t Length) { - Ref = Ref.slice(0, Length); - - StreamReader SrcReader(Ref); - // This is a bit tricky. If we just call readBytes, we are requiring that it - // return us the entire stream as a contiguous buffer. For large streams this - // will allocate a huge amount of space from the pool. Instead, iterate over - // each contiguous chunk until we've consumed the entire stream. - while (SrcReader.bytesRemaining() > 0) { - ArrayRef<uint8_t> Chunk; - if (auto EC = SrcReader.readLongestContiguousChunk(Chunk)) - return EC; - if (auto EC = writeBytes(Chunk)) - return EC; - } - return Error::success(); -} |