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 2643 : StreamWriter::StreamWriter(WritableStreamRef S) : Stream(S), Offset(0) {}
20 :
21 34975 : Error StreamWriter::writeBytes(ArrayRef<uint8_t> Buffer) {
22 104925 : if (auto EC = Stream.writeBytes(Offset, Buffer))
23 0 : return EC;
24 34975 : Offset += Buffer.size();
25 104925 : return Error::success();
26 : }
27 :
28 6942 : Error StreamWriter::writeZeroString(StringRef Str) {
29 20826 : if (auto EC = writeFixedString(Str))
30 0 : return EC;
31 27768 : if (auto EC = writeObject('\0'))
32 0 : return EC;
33 :
34 20826 : return Error::success();
35 : }
36 :
37 6946 : Error StreamWriter::writeFixedString(StringRef Str) {
38 20838 : ArrayRef<uint8_t> Bytes(Str.bytes_begin(), Str.bytes_end());
39 20838 : if (auto EC = Stream.writeBytes(Offset, Bytes))
40 0 : return EC;
41 :
42 6946 : Offset += Str.size();
43 20838 : return Error::success();
44 : }
45 :
46 22 : Error StreamWriter::writeStreamRef(ReadableStreamRef Ref) {
47 66 : if (auto EC = writeStreamRef(Ref, Ref.getLength()))
48 0 : return EC;
49 : // Don't increment Offset here, it is done by the overloaded call to
50 : // writeStreamRef.
51 66 : return Error::success();
52 : }
53 :
54 22 : Error StreamWriter::writeStreamRef(ReadableStreamRef Ref, uint32_t Length) {
55 22 : Ref = Ref.slice(0, Length);
56 :
57 22 : StreamReader SrcReader(Ref);
58 : // This is a bit tricky. If we just call readBytes, we are requiring that it
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.
62 240 : while (SrcReader.bytesRemaining() > 0) {
63 218 : ArrayRef<uint8_t> Chunk;
64 654 : if (auto EC = SrcReader.readLongestContiguousChunk(Chunk))
65 0 : return EC;
66 654 : if (auto EC = writeBytes(Chunk))
67 0 : return EC;
68 : }
69 66 : return Error::success();
70 : }
|