LLVM  6.0.0svn
BinaryStreamWriter.cpp
Go to the documentation of this file.
1 //===- BinaryStreamWriter.cpp - Writes objects to a BinaryStream ----------===//
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 
19  : Stream(Ref) {}
20 
22  : Stream(Stream) {}
23 
26  : Stream(Data, Endian) {}
27 
29  if (auto EC = Stream.writeBytes(Offset, Buffer))
30  return EC;
31  Offset += Buffer.size();
32  return Error::success();
33 }
34 
36  if (auto EC = writeFixedString(Str))
37  return EC;
38  if (auto EC = writeObject('\0'))
39  return EC;
40 
41  return Error::success();
42 }
43 
45  return writeBytes(ArrayRef<uint8_t>(Str.bytes_begin(), Str.bytes_end()));
46 }
47 
49  return writeStreamRef(Ref, Ref.getLength());
50 }
51 
53  BinaryStreamReader SrcReader(Ref.slice(0, Length));
54  // This is a bit tricky. If we just call readBytes, we are requiring that it
55  // return us the entire stream as a contiguous buffer. There is no guarantee
56  // this can be satisfied by returning a reference straight from the buffer, as
57  // an implementation may not store all data in a single contiguous buffer. So
58  // we iterate over each contiguous chunk, writing each one in succession.
59  while (SrcReader.bytesRemaining() > 0) {
60  ArrayRef<uint8_t> Chunk;
61  if (auto EC = SrcReader.readLongestContiguousChunk(Chunk))
62  return EC;
63  if (auto EC = writeBytes(Chunk))
64  return EC;
65  }
66  return Error::success();
67 }
68 
69 std::pair<BinaryStreamWriter, BinaryStreamWriter>
71  assert(getLength() >= Off);
72 
74 
75  WritableBinaryStreamRef Second = First.drop_front(Off);
76  First = First.keep_front(Off);
77  BinaryStreamWriter W1{First};
78  BinaryStreamWriter W2{Second};
79  return std::make_pair(W1, W2);
80 }
81 
83  uint32_t NewOffset = alignTo(Offset, Align);
84  if (NewOffset > getLength())
85  return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
86  while (Offset < NewOffset)
87  if (auto EC = writeInteger('\0'))
88  return EC;
89  return Error::success();
90 }
Error writeObject(const T &Obj)
Writes the object Obj to the underlying stream, as if by using memcpy.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
Error writeBytes(ArrayRef< uint8_t > Buffer)
Write the bytes specified in Buffer to the underlying stream.
RefType slice(uint32_t Offset, uint32_t Len) const
Return a new BinaryStreamRef with the first Offset elements removed, and retaining exactly Len elemen...
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
const unsigned char * bytes_end() const
Definition: StringRef.h:113
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:677
Error writeFixedString(StringRef Str)
Write the the string Str to the underlying stream without a null terminator.
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
Error writeStreamRef(BinaryStreamRef Ref)
Efficiently reads all data from Ref, and writes it to this stream.
RefType drop_front(uint32_t N) const
Return a new BinaryStreamRef with the first N elements removed.
WritableBinaryStreamRef Stream
Provides write only access to a subclass of WritableBinaryStream.
Error writeInteger(T Value)
Write the the integer Value to the underlying stream in the specified endianness. ...
Error writeCString(StringRef Str)
Write the the string Str to the underlying stream followed by a null terminator.
uint32_t getLength() const
static ErrorSuccess success()
Create a success value.
Definition: Error.h:313
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
std::pair< BinaryStreamWriter, BinaryStreamWriter > split(uint32_t Off) const
Splits the Writer into two Writers at a given offset.
const unsigned char * bytes_begin() const
Definition: StringRef.h:110
Error padToAlignment(uint32_t Align)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Error writeBytes(uint32_t Offset, ArrayRef< uint8_t > Data) const
Given an Offset into this WritableBinaryStreamRef and some input data, writes the data to the underly...
RefType keep_front(uint32_t N) const
Return a new BinaryStreamRef with only the first N elements remaining.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
Provides read only access to a subclass of BinaryStream.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
A BinaryStream which can be read from as well as written to.
Definition: BinaryStream.h:63