LLVM  9.0.0svn
BinaryStreamRef.cpp
Go to the documentation of this file.
1 //===- BinaryStreamRef.cpp - ----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
11 
12 using namespace llvm;
13 using namespace llvm::support;
14 
15 namespace {
16 
17 class ArrayRefImpl : public BinaryStream {
18 public:
19  ArrayRefImpl(ArrayRef<uint8_t> Data, endianness Endian) : BBS(Data, Endian) {}
20 
21  llvm::support::endianness getEndian() const override {
22  return BBS.getEndian();
23  }
24  Error readBytes(uint32_t Offset, uint32_t Size,
25  ArrayRef<uint8_t> &Buffer) override {
26  return BBS.readBytes(Offset, Size, Buffer);
27  }
28  Error readLongestContiguousChunk(uint32_t Offset,
29  ArrayRef<uint8_t> &Buffer) override {
30  return BBS.readLongestContiguousChunk(Offset, Buffer);
31  }
32  uint32_t getLength() override { return BBS.getLength(); }
33 
34 private:
35  BinaryByteStream BBS;
36 };
37 
38 class MutableArrayRefImpl : public WritableBinaryStream {
39 public:
40  MutableArrayRefImpl(MutableArrayRef<uint8_t> Data, endianness Endian)
41  : BBS(Data, Endian) {}
42 
43  // Inherited via WritableBinaryStream
44  llvm::support::endianness getEndian() const override {
45  return BBS.getEndian();
46  }
47  Error readBytes(uint32_t Offset, uint32_t Size,
48  ArrayRef<uint8_t> &Buffer) override {
49  return BBS.readBytes(Offset, Size, Buffer);
50  }
51  Error readLongestContiguousChunk(uint32_t Offset,
52  ArrayRef<uint8_t> &Buffer) override {
53  return BBS.readLongestContiguousChunk(Offset, Buffer);
54  }
55  uint32_t getLength() override { return BBS.getLength(); }
56 
57  Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Data) override {
58  return BBS.writeBytes(Offset, Data);
59  }
60  Error commit() override { return BBS.commit(); }
61 
62 private:
64 };
65 }
66 
68  : BinaryStreamRefBase(Stream) {}
71  : BinaryStreamRefBase(Stream, Offset, Length) {}
73  : BinaryStreamRefBase(std::make_shared<ArrayRefImpl>(Data, Endian), 0,
74  Data.size()) {}
76  : BinaryStreamRef(makeArrayRef(Data.bytes_begin(), Data.bytes_end()),
77  Endian) {}
78 
80  ArrayRef<uint8_t> &Buffer) const {
81  if (auto EC = checkOffsetForRead(Offset, Size))
82  return EC;
83  return BorrowedImpl->readBytes(ViewOffset + Offset, Size, Buffer);
84 }
85 
87  uint32_t Offset, ArrayRef<uint8_t> &Buffer) const {
88  if (auto EC = checkOffsetForRead(Offset, 1))
89  return EC;
90 
91  if (auto EC =
93  return EC;
94  // This StreamRef might refer to a smaller window over a larger stream. In
95  // that case we will have read out more bytes than we should return, because
96  // we should not read past the end of the current view.
97  uint32_t MaxLength = getLength() - Offset;
98  if (Buffer.size() > MaxLength)
99  Buffer = Buffer.slice(0, MaxLength);
100  return Error::success();
101 }
102 
104  : BinaryStreamRefBase(Stream) {}
105 
107  uint32_t Offset,
109  : BinaryStreamRefBase(Stream, Offset, Length) {}
110 
112  endianness Endian)
113  : BinaryStreamRefBase(std::make_shared<MutableArrayRefImpl>(Data, Endian),
114  0, Data.size()) {}
115 
116 
118  ArrayRef<uint8_t> Data) const {
119  if (auto EC = checkOffsetForWrite(Offset, Data.size()))
120  return EC;
121 
122  return BorrowedImpl->writeBytes(ViewOffset + Offset, Data);
123 }
124 
125 WritableBinaryStreamRef::operator BinaryStreamRef() const {
127 }
128 
129 /// 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...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
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...
Common stuff for mutable and immutable StreamRefs.
Definition: BitVector.h:937
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:450
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:35
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:148
Error commit()
For buffered streams, commits changes to the backing store.
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:1166
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:178
virtual Error commit()=0
For buffered streams, commits changes to the backing store.
uint32_t Size
Definition: Profile.cpp:46
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...
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Error checkOffsetForRead(uint32_t Offset, uint32_t DataSize) const
A BinaryStream which can be read from as well as written to.
Definition: BinaryStream.h:73