LLVM 20.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;
13
14namespace {
15
16class ArrayRefImpl : public BinaryStream {
17public:
18 ArrayRefImpl(ArrayRef<uint8_t> Data, endianness Endian) : BBS(Data, Endian) {}
19
20 llvm::endianness getEndian() const override { return BBS.getEndian(); }
22 ArrayRef<uint8_t> &Buffer) override {
23 return BBS.readBytes(Offset, Size, Buffer);
24 }
26 ArrayRef<uint8_t> &Buffer) override {
27 return BBS.readLongestContiguousChunk(Offset, Buffer);
28 }
29 uint64_t getLength() override { return BBS.getLength(); }
30
31private:
33};
34
35class MutableArrayRefImpl : public WritableBinaryStream {
36public:
37 MutableArrayRefImpl(MutableArrayRef<uint8_t> Data, endianness Endian)
38 : BBS(Data, Endian) {}
39
40 // Inherited via WritableBinaryStream
41 llvm::endianness getEndian() const override { return BBS.getEndian(); }
43 ArrayRef<uint8_t> &Buffer) override {
44 return BBS.readBytes(Offset, Size, Buffer);
45 }
47 ArrayRef<uint8_t> &Buffer) override {
48 return BBS.readLongestContiguousChunk(Offset, Buffer);
49 }
50 uint64_t getLength() override { return BBS.getLength(); }
51
53 return BBS.writeBytes(Offset, Data);
54 }
55 Error commit() override { return BBS.commit(); }
56
57private:
59};
60} // namespace
61
63 : BinaryStreamRefBase(Stream) {}
65 std::optional<uint64_t> Length)
66 : BinaryStreamRefBase(Stream, Offset, Length) {}
68 : BinaryStreamRefBase(std::make_shared<ArrayRefImpl>(Data, Endian), 0,
69 Data.size()) {}
71 : BinaryStreamRef(ArrayRef(Data.bytes_begin(), Data.bytes_end()), Endian) {}
72
74 ArrayRef<uint8_t> &Buffer) const {
75 if (auto EC = checkOffsetForRead(Offset, Size))
76 return EC;
77 return BorrowedImpl->readBytes(ViewOffset + Offset, Size, Buffer);
78}
79
81 uint64_t Offset, ArrayRef<uint8_t> &Buffer) const {
82 if (auto EC = checkOffsetForRead(Offset, 1))
83 return EC;
84
85 if (auto EC =
87 return EC;
88 // This StreamRef might refer to a smaller window over a larger stream. In
89 // that case we will have read out more bytes than we should return, because
90 // we should not read past the end of the current view.
91 uint64_t MaxLength = getLength() - Offset;
92 if (Buffer.size() > MaxLength)
93 Buffer = Buffer.slice(0, MaxLength);
94 return Error::success();
95}
96
98 : BinaryStreamRefBase(Stream) {}
99
102 std::optional<uint64_t> Length)
103 : BinaryStreamRefBase(Stream, Offset, Length) {}
104
106 endianness Endian)
107 : BinaryStreamRefBase(std::make_shared<MutableArrayRefImpl>(Data, Endian),
108 0, Data.size()) {}
109
111 ArrayRef<uint8_t> Data) const {
112 if (auto EC = checkOffsetForWrite(Offset, Data.size()))
113 return EC;
114
116}
117
118WritableBinaryStreamRef::operator BinaryStreamRef() const {
119 return BinaryStreamRef(*BorrowedImpl, ViewOffset, Length);
120}
121
122/// 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:168
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:198
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:34
virtual llvm::endianness getEndian() const =0
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 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:337
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:310
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:51
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:72
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:480
@ Length
Definition: DWP.cpp:480
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:1697
endianness
Definition: bit.h:70
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858