LLVM  6.0.0svn
BinaryStreamRef.cpp
Go to the documentation of this file.
1 //===- BinaryStreamRef.cpp - ----------------------------------------------===//
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 
12 
13 using namespace llvm;
14 using namespace llvm::support;
15 
16 namespace {
17 
18 class ArrayRefImpl : public BinaryStream {
19 public:
20  ArrayRefImpl(ArrayRef<uint8_t> Data, endianness Endian) : BBS(Data, Endian) {}
21 
22  llvm::support::endianness getEndian() const override {
23  return BBS.getEndian();
24  }
25  Error readBytes(uint32_t Offset, uint32_t Size,
26  ArrayRef<uint8_t> &Buffer) override {
27  return BBS.readBytes(Offset, Size, Buffer);
28  }
29  Error readLongestContiguousChunk(uint32_t Offset,
30  ArrayRef<uint8_t> &Buffer) override {
31  return BBS.readLongestContiguousChunk(Offset, Buffer);
32  }
33  uint32_t getLength() override { return BBS.getLength(); }
34 
35 private:
36  BinaryByteStream BBS;
37 };
38 
39 class MutableArrayRefImpl : public WritableBinaryStream {
40 public:
41  MutableArrayRefImpl(MutableArrayRef<uint8_t> Data, endianness Endian)
42  : BBS(Data, Endian) {}
43 
44  // Inherited via WritableBinaryStream
45  llvm::support::endianness getEndian() const override {
46  return BBS.getEndian();
47  }
48  Error readBytes(uint32_t Offset, uint32_t Size,
49  ArrayRef<uint8_t> &Buffer) override {
50  return BBS.readBytes(Offset, Size, Buffer);
51  }
52  Error readLongestContiguousChunk(uint32_t Offset,
53  ArrayRef<uint8_t> &Buffer) override {
54  return BBS.readLongestContiguousChunk(Offset, Buffer);
55  }
56  uint32_t getLength() override { return BBS.getLength(); }
57 
58  Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Data) override {
59  return BBS.writeBytes(Offset, Data);
60  }
61  Error commit() override { return BBS.commit(); }
62 
63 private:
65 };
66 }
67 
69  : BinaryStreamRef(Stream, 0, Stream.getLength()) {}
72  : BinaryStreamRefBase(Stream, Offset, Length) {}
74  : BinaryStreamRefBase(std::make_shared<ArrayRefImpl>(Data, Endian), 0,
75  Data.size()) {}
77  : BinaryStreamRef(makeArrayRef(Data.bytes_begin(), Data.bytes_end()),
78  Endian) {}
79 
81  ArrayRef<uint8_t> &Buffer) const {
82  if (auto EC = checkOffset(Offset, Size))
83  return EC;
84  return BorrowedImpl->readBytes(ViewOffset + Offset, Size, Buffer);
85 }
86 
88  uint32_t Offset, ArrayRef<uint8_t> &Buffer) const {
89  if (auto EC = checkOffset(Offset, 1))
90  return EC;
91 
92  if (auto EC =
94  return EC;
95  // This StreamRef might refer to a smaller window over a larger stream. In
96  // that case we will have read out more bytes than we should return, because
97  // we should not read past the end of the current view.
98  uint32_t MaxLength = Length - Offset;
99  if (Buffer.size() > MaxLength)
100  Buffer = Buffer.slice(0, MaxLength);
101  return Error::success();
102 }
103 
105  : WritableBinaryStreamRef(Stream, 0, Stream.getLength()) {}
106 
108  uint32_t Offset,
110  : BinaryStreamRefBase(Stream, Offset, Length) {}
111 
113  endianness Endian)
114  : BinaryStreamRefBase(std::make_shared<MutableArrayRefImpl>(Data, Endian),
115  0, Data.size()) {}
116 
117 
119  ArrayRef<uint8_t> Data) const {
120  if (auto EC = checkOffset(Offset, Data.size()))
121  return EC;
122 
123  return BorrowedImpl->writeBytes(ViewOffset + Offset, Data);
124 }
125 
126 WritableBinaryStreamRef::operator BinaryStreamRef() const {
128 }
129 
130 /// \brief For buffered streams, commits changes to the backing store.
An implementation of BinaryStream which holds its entire data set in a single contiguous buffer...
An implementation of BinaryStream which holds its entire data set in a single contiguous buffer...
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
virtual Error readBytes(uint32_t Offset, uint32_t Size, ArrayRef< uint8_t > &Buffer)=0
Given an offset into the stream and a number of bytes, attempt to read the bytes and set the output A...
Error checkOffset(uint32_t Offset, uint32_t DataSize) const
Common stuff for mutable and immutable StreamRefs.
Definition: BitVector.h:920
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:451
virtual Error writeBytes(uint32_t Offset, ArrayRef< uint8_t > Data)=0
Attempt to write the given bytes into the stream at the desired offset.
Error readBytes(uint32_t Offset, uint32_t Size, ArrayRef< uint8_t > &Buffer) const
Given an Offset into this StreamRef and a Size, return a reference to a buffer owned by the stream...
virtual Error readLongestContiguousChunk(uint32_t Offset, ArrayRef< uint8_t > &Buffer)=0
Given an offset into the stream, read as much as possible without copying any data.
An interface for accessing data in a stream-like format, but which discourages copying.
Definition: BinaryStream.h:28
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
Error commit()
For buffered streams, commits changes to the backing store.
static ErrorSuccess success()
Create a success value.
Definition: Error.h:313
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array...
Definition: ArrayRef.h:179
virtual Error commit()=0
For buffered streams, commits changes to the backing store.
Error readLongestContiguousChunk(uint32_t Offset, ArrayRef< uint8_t > &Buffer) const
Given an Offset into this BinaryStreamRef, return a reference to the largest buffer the stream could ...
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...
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
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