blob: cdae7c5acc04323327b1b03831f298cb877568bb [file] [log] [blame]
Zachary Turner5acb4ac2016-06-10 05:09:12 +00001//===- 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 Turnera3225b02016-07-29 20:56:36 +000010#include "llvm/DebugInfo/MSF/StreamWriter.h"
Zachary Turner5acb4ac2016-06-10 05:09:12 +000011
Zachary Turnera3225b02016-07-29 20:56:36 +000012#include "llvm/DebugInfo/MSF/MSFError.h"
13#include "llvm/DebugInfo/MSF/StreamReader.h"
14#include "llvm/DebugInfo/MSF/StreamRef.h"
Zachary Turner5acb4ac2016-06-10 05:09:12 +000015
16using namespace llvm;
Zachary Turnerbac69d32016-07-22 19:56:05 +000017using namespace llvm::msf;
Zachary Turner5acb4ac2016-06-10 05:09:12 +000018
Zachary Turnerd66889c2016-07-28 19:12:28 +000019StreamWriter::StreamWriter(WritableStreamRef S) : Stream(S), Offset(0) {}
Zachary Turner5acb4ac2016-06-10 05:09:12 +000020
21Error 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 Turner7251ede2016-11-02 17:05:19 +000028Error StreamWriter::writeInteger(uint8_t Int) { return writeObject(Int); }
29
Zachary Turner5acb4ac2016-06-10 05:09:12 +000030Error StreamWriter::writeInteger(uint16_t Int) {
31 return writeObject(support::ulittle16_t(Int));
32}
33
34Error StreamWriter::writeInteger(uint32_t Int) {
35 return writeObject(support::ulittle32_t(Int));
36}
37
Zachary Turner7251ede2016-11-02 17:05:19 +000038Error StreamWriter::writeInteger(uint64_t Int) {
39 return writeObject(support::ulittle64_t(Int));
40}
41
42Error StreamWriter::writeInteger(int8_t Int) { return writeObject(Int); }
43
44Error StreamWriter::writeInteger(int16_t Int) {
45 return writeObject(support::little16_t(Int));
46}
47
48Error StreamWriter::writeInteger(int32_t Int) {
49 return writeObject(support::little32_t(Int));
50}
51
52Error StreamWriter::writeInteger(int64_t Int) {
53 return writeObject(support::little64_t(Int));
54}
55
Zachary Turner5acb4ac2016-06-10 05:09:12 +000056Error 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
65Error 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 Turnerd66889c2016-07-28 19:12:28 +000074Error StreamWriter::writeStreamRef(ReadableStreamRef Ref) {
Zachary Turner5acb4ac2016-06-10 05:09:12 +000075 if (auto EC = writeStreamRef(Ref, Ref.getLength()))
76 return EC;
Zachary Turnerd218c262016-07-22 15:46:37 +000077 // Don't increment Offset here, it is done by the overloaded call to
78 // writeStreamRef.
Zachary Turner5acb4ac2016-06-10 05:09:12 +000079 return Error::success();
80}
81
Zachary Turnerd66889c2016-07-28 19:12:28 +000082Error StreamWriter::writeStreamRef(ReadableStreamRef Ref, uint32_t Length) {
Zachary Turner5acb4ac2016-06-10 05:09:12 +000083 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}