blob: 54415b96c0bdd388cd689187cfcf492d63608a4b [file] [log] [blame]
NAKAMURA Takumi05a75e42017-02-25 17:04:23 +00001//===- StreamWrite.cpp - Writes bytes and objects to a stream -------------===//
Zachary Turner5acb4ac2016-06-10 05:09:12 +00002//
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 Turnerd2684b72017-02-25 00:33:34 +000010#include "llvm/DebugInfo/MSF/BinaryStreamWriter.h"
Zachary Turner5acb4ac2016-06-10 05:09:12 +000011
Zachary Turnerd2684b72017-02-25 00:33:34 +000012#include "llvm/DebugInfo/MSF/BinaryStreamReader.h"
13#include "llvm/DebugInfo/MSF/BinaryStreamRef.h"
NAKAMURA Takumi05a75e42017-02-25 17:04:23 +000014#include "llvm/DebugInfo/MSF/MSFError.h"
Zachary Turner5acb4ac2016-06-10 05:09:12 +000015
16using namespace llvm;
NAKAMURA Takumi05a75e42017-02-25 17:04:23 +000017using namespace llvm::msf;
Zachary Turner5acb4ac2016-06-10 05:09:12 +000018
NAKAMURA Takumi05a75e42017-02-25 17:04:23 +000019StreamWriter::StreamWriter(WritableStreamRef S) : Stream(S), Offset(0) {}
Zachary Turner5acb4ac2016-06-10 05:09:12 +000020
NAKAMURA Takumi05a75e42017-02-25 17:04:23 +000021Error StreamWriter::writeBytes(ArrayRef<uint8_t> Buffer) {
Zachary Turner5acb4ac2016-06-10 05:09:12 +000022 if (auto EC = Stream.writeBytes(Offset, Buffer))
23 return EC;
24 Offset += Buffer.size();
25 return Error::success();
26}
27
NAKAMURA Takumi05a75e42017-02-25 17:04:23 +000028Error StreamWriter::writeZeroString(StringRef Str) {
Zachary Turner5acb4ac2016-06-10 05:09:12 +000029 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 Takumi05a75e42017-02-25 17:04:23 +000037Error 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 Turner5acb4ac2016-06-10 05:09:12 +000044}
45
NAKAMURA Takumi05a75e42017-02-25 17:04:23 +000046Error 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 Turner5acb4ac2016-06-10 05:09:12 +000052}
53
NAKAMURA Takumi05a75e42017-02-25 17:04:23 +000054Error StreamWriter::writeStreamRef(ReadableStreamRef Ref, uint32_t Length) {
55 Ref = Ref.slice(0, Length);
56
57 StreamReader SrcReader(Ref);
Zachary Turner5acb4ac2016-06-10 05:09:12 +000058 // This is a bit tricky. If we just call readBytes, we are requiring that it
NAKAMURA Takumi05a75e42017-02-25 17:04:23 +000059 // 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 Turner5acb4ac2016-06-10 05:09:12 +000062 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}