LLVM  4.0.0
StreamWriter.cpp
Go to the documentation of this file.
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 
11 
15 
16 using namespace llvm;
17 using namespace llvm::msf;
18 
20 
22  if (auto EC = Stream.writeBytes(Offset, Buffer))
23  return EC;
24  Offset += Buffer.size();
25  return Error::success();
26 }
27 
28 Error StreamWriter::writeInteger(uint8_t Int) { return writeObject(Int); }
29 
31  return writeObject(support::ulittle16_t(Int));
32 }
33 
35  return writeObject(support::ulittle32_t(Int));
36 }
37 
39  return writeObject(support::ulittle64_t(Int));
40 }
41 
42 Error StreamWriter::writeInteger(int8_t Int) { return writeObject(Int); }
43 
45  return writeObject(support::little16_t(Int));
46 }
47 
49  return writeObject(support::little32_t(Int));
50 }
51 
53  return writeObject(support::little64_t(Int));
54 }
55 
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 
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 
75  if (auto EC = writeStreamRef(Ref, Ref.getLength()))
76  return EC;
77  // Don't increment Offset here, it is done by the overloaded call to
78  // writeStreamRef.
79  return Error::success();
80 }
81 
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 }
RefType slice(uint32_t Offset, uint32_t Len) const
Definition: StreamRef.h:47
Error writeZeroString(StringRef Str)
const unsigned char * bytes_end() const
Definition: StringRef.h:110
Error writeFixedString(StringRef Str)
Error writeStreamRef(ReadableStreamRef Ref)
Error writeObject(const T &Obj)
Definition: StreamWriter.h:49
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:135
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:141
Error writeBytes(ArrayRef< uint8_t > Buffer)
uint32_t getLength() const
Definition: StreamRef.h:29
uint32_t Offset
Error writeInteger(uint8_t Int)
Error readLongestContiguousChunk(ArrayRef< uint8_t > &Buffer)
Error writeBytes(uint32_t Offset, ArrayRef< uint8_t > Data) const
Definition: StreamRef.h:123
static ErrorSuccess success()
Create a success value.
uint32_t bytesRemaining() const
Definition: StreamReader.h:108
const unsigned char * bytes_begin() const
Definition: StringRef.h:107
Lightweight error class with error context and mandatory checking.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47