Line data Source code
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 :
10 : #include "llvm/DebugInfo/Msf/StreamWriter.h"
11 :
12 : #include "llvm/DebugInfo/Msf/MsfError.h"
13 : #include "llvm/DebugInfo/Msf/StreamReader.h"
14 : #include "llvm/DebugInfo/Msf/StreamRef.h"
15 :
16 : using namespace llvm;
17 : using namespace llvm::msf;
18 :
19 3 : StreamWriter::StreamWriter(WritableStreamRef S) : Stream(S), Offset(0) {}
20 :
21 37 : Error StreamWriter::writeBytes(ArrayRef<uint8_t> Buffer) {
22 111 : if (auto EC = Stream.writeBytes(Offset, Buffer))
23 0 : return EC;
24 37 : Offset += Buffer.size();
25 : return Error::success();
26 : }
27 :
28 0 : Error StreamWriter::writeInteger(uint16_t Int) {
29 0 : return writeObject(support::ulittle16_t(Int));
30 : }
31 :
32 15 : Error StreamWriter::writeInteger(uint32_t Int) {
33 30 : return writeObject(support::ulittle32_t(Int));
34 : }
35 :
36 3 : Error StreamWriter::writeZeroString(StringRef Str) {
37 9 : if (auto EC = writeFixedString(Str))
38 0 : return EC;
39 12 : if (auto EC = writeObject('\0'))
40 0 : return EC;
41 :
42 : return Error::success();
43 : }
44 :
45 3 : Error StreamWriter::writeFixedString(StringRef Str) {
46 9 : ArrayRef<uint8_t> Bytes(Str.bytes_begin(), Str.bytes_end());
47 9 : if (auto EC = Stream.writeBytes(Offset, Bytes))
48 0 : return EC;
49 :
50 3 : Offset += Str.size();
51 : return Error::success();
52 : }
53 :
54 0 : Error StreamWriter::writeStreamRef(ReadableStreamRef Ref) {
55 0 : if (auto EC = writeStreamRef(Ref, Ref.getLength()))
56 0 : return EC;
57 : // Don't increment Offset here, it is done by the overloaded call to
58 : // writeStreamRef.
59 : return Error::success();
60 : }
61 :
62 0 : Error StreamWriter::writeStreamRef(ReadableStreamRef Ref, uint32_t Length) {
63 0 : Ref = Ref.slice(0, Length);
64 :
65 0 : StreamReader SrcReader(Ref);
66 : // This is a bit tricky. If we just call readBytes, we are requiring that it
67 : // return us the entire stream as a contiguous buffer. For large streams this
68 : // will allocate a huge amount of space from the pool. Instead, iterate over
69 : // each contiguous chunk until we've consumed the entire stream.
70 0 : while (SrcReader.bytesRemaining() > 0) {
71 0 : ArrayRef<uint8_t> Chunk;
72 0 : if (auto EC = SrcReader.readLongestContiguousChunk(Chunk))
73 0 : return EC;
74 0 : if (auto EC = writeBytes(Chunk))
75 0 : return EC;
76 : }
77 : return Error::success();
78 : }
|