NAKAMURA Takumi | 05a75e4 | 2017-02-25 17:04:23 +0000 | [diff] [blame] | 1 | //===- StreamWrite.cpp - Writes bytes and objects to a stream -------------===// |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 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 | |
Zachary Turner | d2684b7 | 2017-02-25 00:33:34 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/MSF/BinaryStreamWriter.h" |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 11 | |
Zachary Turner | d2684b7 | 2017-02-25 00:33:34 +0000 | [diff] [blame] | 12 | #include "llvm/DebugInfo/MSF/BinaryStreamReader.h" |
| 13 | #include "llvm/DebugInfo/MSF/BinaryStreamRef.h" |
NAKAMURA Takumi | 05a75e4 | 2017-02-25 17:04:23 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/MSF/MSFError.h" |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 15 | |
| 16 | using namespace llvm; |
NAKAMURA Takumi | 05a75e4 | 2017-02-25 17:04:23 +0000 | [diff] [blame] | 17 | using namespace llvm::msf; |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 18 | |
NAKAMURA Takumi | 05a75e4 | 2017-02-25 17:04:23 +0000 | [diff] [blame] | 19 | StreamWriter::StreamWriter(WritableStreamRef S) : Stream(S), Offset(0) {} |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 20 | |
NAKAMURA Takumi | 05a75e4 | 2017-02-25 17:04:23 +0000 | [diff] [blame] | 21 | Error StreamWriter::writeBytes(ArrayRef<uint8_t> Buffer) { |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 22 | if (auto EC = Stream.writeBytes(Offset, Buffer)) |
| 23 | return EC; |
| 24 | Offset += Buffer.size(); |
| 25 | return Error::success(); |
| 26 | } |
| 27 | |
NAKAMURA Takumi | 05a75e4 | 2017-02-25 17:04:23 +0000 | [diff] [blame] | 28 | Error StreamWriter::writeZeroString(StringRef Str) { |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 29 | if (auto EC = writeFixedString(Str)) |
| 30 | return EC; |
| 31 | if (auto EC = writeObject('\0')) |
| 32 | return EC; |
| 33 | |
| 34 | return Error::success(); |
| 35 | } |
| 36 | |
NAKAMURA Takumi | 05a75e4 | 2017-02-25 17:04:23 +0000 | [diff] [blame] | 37 | Error StreamWriter::writeFixedString(StringRef Str) { |
| 38 | ArrayRef<uint8_t> Bytes(Str.bytes_begin(), Str.bytes_end()); |
| 39 | if (auto EC = Stream.writeBytes(Offset, Bytes)) |
| 40 | return EC; |
| 41 | |
| 42 | Offset += Str.size(); |
| 43 | return Error::success(); |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 44 | } |
| 45 | |
NAKAMURA Takumi | 05a75e4 | 2017-02-25 17:04:23 +0000 | [diff] [blame] | 46 | Error StreamWriter::writeStreamRef(ReadableStreamRef Ref) { |
| 47 | if (auto EC = writeStreamRef(Ref, Ref.getLength())) |
| 48 | return EC; |
| 49 | // Don't increment Offset here, it is done by the overloaded call to |
| 50 | // writeStreamRef. |
| 51 | return Error::success(); |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 52 | } |
| 53 | |
NAKAMURA Takumi | 05a75e4 | 2017-02-25 17:04:23 +0000 | [diff] [blame] | 54 | Error StreamWriter::writeStreamRef(ReadableStreamRef Ref, uint32_t Length) { |
| 55 | Ref = Ref.slice(0, Length); |
| 56 | |
| 57 | StreamReader SrcReader(Ref); |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 58 | // This is a bit tricky. If we just call readBytes, we are requiring that it |
NAKAMURA Takumi | 05a75e4 | 2017-02-25 17:04:23 +0000 | [diff] [blame] | 59 | // return us the entire stream as a contiguous buffer. For large streams this |
| 60 | // will allocate a huge amount of space from the pool. Instead, iterate over |
| 61 | // each contiguous chunk until we've consumed the entire stream. |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 62 | while (SrcReader.bytesRemaining() > 0) { |
| 63 | ArrayRef<uint8_t> Chunk; |
| 64 | if (auto EC = SrcReader.readLongestContiguousChunk(Chunk)) |
| 65 | return EC; |
| 66 | if (auto EC = writeBytes(Chunk)) |
| 67 | return EC; |
| 68 | } |
| 69 | return Error::success(); |
| 70 | } |