Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 1 | //===- StreamWrite.cpp - Writes bytes and objects to a 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 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/MSF/StreamWriter.h" |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 11 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 12 | #include "llvm/DebugInfo/MSF/MSFError.h" |
| 13 | #include "llvm/DebugInfo/MSF/StreamReader.h" |
| 14 | #include "llvm/DebugInfo/MSF/StreamRef.h" |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 15 | |
| 16 | using namespace llvm; |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 17 | using namespace llvm::msf; |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 18 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +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 | |
| 21 | Error StreamWriter::writeBytes(ArrayRef<uint8_t> Buffer) { |
| 22 | if (auto EC = Stream.writeBytes(Offset, Buffer)) |
| 23 | return EC; |
| 24 | Offset += Buffer.size(); |
| 25 | return Error::success(); |
| 26 | } |
| 27 | |
Zachary Turner | 7251ede | 2016-11-02 17:05:19 +0000 | [diff] [blame] | 28 | Error StreamWriter::writeInteger(uint8_t Int) { return writeObject(Int); } |
| 29 | |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 30 | Error StreamWriter::writeInteger(uint16_t Int) { |
| 31 | return writeObject(support::ulittle16_t(Int)); |
| 32 | } |
| 33 | |
| 34 | Error StreamWriter::writeInteger(uint32_t Int) { |
| 35 | return writeObject(support::ulittle32_t(Int)); |
| 36 | } |
| 37 | |
Zachary Turner | 7251ede | 2016-11-02 17:05:19 +0000 | [diff] [blame] | 38 | Error StreamWriter::writeInteger(uint64_t Int) { |
| 39 | return writeObject(support::ulittle64_t(Int)); |
| 40 | } |
| 41 | |
| 42 | Error StreamWriter::writeInteger(int8_t Int) { return writeObject(Int); } |
| 43 | |
| 44 | Error StreamWriter::writeInteger(int16_t Int) { |
| 45 | return writeObject(support::little16_t(Int)); |
| 46 | } |
| 47 | |
| 48 | Error StreamWriter::writeInteger(int32_t Int) { |
| 49 | return writeObject(support::little32_t(Int)); |
| 50 | } |
| 51 | |
| 52 | Error StreamWriter::writeInteger(int64_t Int) { |
| 53 | return writeObject(support::little64_t(Int)); |
| 54 | } |
| 55 | |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 56 | Error StreamWriter::writeZeroString(StringRef Str) { |
| 57 | if (auto EC = writeFixedString(Str)) |
| 58 | return EC; |
| 59 | if (auto EC = writeObject('\0')) |
| 60 | return EC; |
| 61 | |
| 62 | return Error::success(); |
| 63 | } |
| 64 | |
| 65 | Error StreamWriter::writeFixedString(StringRef Str) { |
| 66 | ArrayRef<uint8_t> Bytes(Str.bytes_begin(), Str.bytes_end()); |
| 67 | if (auto EC = Stream.writeBytes(Offset, Bytes)) |
| 68 | return EC; |
| 69 | |
| 70 | Offset += Str.size(); |
| 71 | return Error::success(); |
| 72 | } |
| 73 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 74 | Error StreamWriter::writeStreamRef(ReadableStreamRef Ref) { |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 75 | if (auto EC = writeStreamRef(Ref, Ref.getLength())) |
| 76 | return EC; |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 77 | // Don't increment Offset here, it is done by the overloaded call to |
| 78 | // writeStreamRef. |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 79 | return Error::success(); |
| 80 | } |
| 81 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 82 | Error StreamWriter::writeStreamRef(ReadableStreamRef Ref, uint32_t Length) { |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 83 | Ref = Ref.slice(0, Length); |
| 84 | |
| 85 | StreamReader SrcReader(Ref); |
| 86 | // This is a bit tricky. If we just call readBytes, we are requiring that it |
| 87 | // return us the entire stream as a contiguous buffer. For large streams this |
| 88 | // will allocate a huge amount of space from the pool. Instead, iterate over |
| 89 | // each contiguous chunk until we've consumed the entire stream. |
| 90 | while (SrcReader.bytesRemaining() > 0) { |
| 91 | ArrayRef<uint8_t> Chunk; |
| 92 | if (auto EC = SrcReader.readLongestContiguousChunk(Chunk)) |
| 93 | return EC; |
| 94 | if (auto EC = writeBytes(Chunk)) |
| 95 | return EC; |
| 96 | } |
| 97 | return Error::success(); |
| 98 | } |