LLVM 18.0.0git
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
12using namespace llvm;
13using namespace llvm::support;
14
15namespace {
16
17class ArrayRefImpl : public BinaryStream {
18public:
19 ArrayRefImpl(ArrayRef<uint8_t> Data, endianness Endian) : BBS(Data, Endian) {}
20
21 llvm::support::endianness getEndian() const override {
22 return BBS.getEndian();
23 }
25 ArrayRef<uint8_t> &Buffer) override {
26 return BBS.readBytes(Offset, Size, Buffer);
27 }
29 ArrayRef<uint8_t> &Buffer) override {
30 return BBS.readLongestContiguousChunk(Offset, Buffer);
31 }
32 uint64_t getLength() override { return BBS.getLength(); }
33
34private:
36};
37
38class MutableArrayRefImpl : public WritableBinaryStream {
39public:
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 }
48 ArrayRef<uint8_t> &Buffer) override {
49 return BBS.readBytes(Offset, Size, Buffer);
50 }
52 ArrayRef<uint8_t> &Buffer) override {
53 return BBS.readLongestContiguousChunk(Offset, Buffer);
54 }
55 uint64_t getLength() override { return BBS.getLength(); }
56
58 return BBS.writeBytes(Offset, Data);
59 }
60 Error commit() override { return BBS.commit(); }
61
62private:
64};
65} // namespace
66
68 : BinaryStreamRefBase(Stream) {}
70 std::optional<uint64_t> Length)
71 : BinaryStreamRefBase(Stream, Offset, Length) {}
73 : BinaryStreamRefBase(std::make_shared<ArrayRefImpl>(Data, Endian), 0,
74 Data.size()) {}
76 : BinaryStreamRef(ArrayRef(Data.bytes_begin(), Data.bytes_end()), Endian) {}
77
79 ArrayRef<uint8_t> &Buffer) const {
80 if (auto EC = checkOffsetForRead(Offset, Size))
81 return EC;
82 return BorrowedImpl->readBytes(ViewOffset + Offset, Size, Buffer);
83}
84
86 uint64_t Offset, ArrayRef<uint8_t> &Buffer) const {
87 if (auto EC = checkOffsetForRead(Offset, 1))
88 return EC;
89
90 if (auto EC =
92 return EC;
93 // This StreamRef might refer to a smaller window over a larger stream. In
94 // that case we will have read out more bytes than we should return, because
95 // we should not read past the end of the current view.
96 uint64_t MaxLength = getLength() - Offset;
97 if (Buffer.size() > MaxLength)
98 Buffer = Buffer.slice(0, MaxLength);
99 return Error::success();
100}
101
103 : BinaryStreamRefBase(Stream) {}
104
107 std::optional<uint64_t> Length)
108 : BinaryStreamRefBase(Stream, Offset, Length) {}
109
111 endianness Endian)
112 : BinaryStreamRefBase(std::make_shared<MutableArrayRefImpl>(Data, Endian),
113 0, Data.size()) {}
114
116 ArrayRef<uint8_t> Data) const {
117 if (auto EC = checkOffsetForWrite(Offset, Data.size()))
118 return EC;
119
121}
122
123WritableBinaryStreamRef::operator BinaryStreamRef() const {
124 return BinaryStreamRef(*BorrowedImpl, ViewOffset, Length);
125}
126
127/// For buffered streams, commits changes to the backing store.
uint64_t Size
endianness Endian
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
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:195
An implementation of BinaryStream which holds its entire data set in a single contiguous buffer.
Common stuff for mutable and immutable StreamRefs.
Error checkOffsetForRead(uint64_t Offset, uint64_t DataSize) const
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
Error readLongestContiguousChunk(uint64_t Offset, ArrayRef< uint8_t > &Buffer) const
Given an Offset into this BinaryStreamRef, return a reference to the largest buffer the stream could ...
Error readBytes(uint64_t Offset, uint64_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.
An interface for accessing data in a stream-like format, but which discourages copying.
Definition: BinaryStream.h:35
virtual uint64_t getLength()=0
Return the number of bytes of data in this stream.
virtual Error readBytes(uint64_t Offset, uint64_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...
virtual llvm::support::endianness getEndian() const =0
virtual Error readLongestContiguousChunk(uint64_t Offset, ArrayRef< uint8_t > &Buffer)=0
Given an offset into the stream, read as much as possible without copying any data.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:334
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:307
An implementation of BinaryStream which holds its entire data set in a single contiguous buffer.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Error writeBytes(uint64_t Offset, ArrayRef< uint8_t > Data) const
Given an Offset into this WritableBinaryStreamRef and some input data, writes the data to the underly...
Error commit()
For buffered streams, commits changes to the backing store.
A BinaryStream which can be read from as well as written to.
Definition: BinaryStream.h:73
virtual Error writeBytes(uint64_t Offset, ArrayRef< uint8_t > Data)=0
Attempt to write the given bytes into the stream at the desired offset.
virtual Error commit()=0
For buffered streams, commits changes to the backing store.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:440
@ Length
Definition: DWP.cpp:440
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1685
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858