LLVM 22.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(); }
21 Error readBytes(uint64_t Offset, uint64_t Size,
22 ArrayRef<uint8_t> &Buffer) override {
23 return BBS.readBytes(Offset, Size, Buffer);
24 }
25 Error readLongestContiguousChunk(uint64_t Offset,
26 ArrayRef<uint8_t> &Buffer) override {
27 return BBS.readLongestContiguousChunk(Offset, Buffer);
28 }
29 uint64_t getLength() override { return BBS.getLength(); }
30
31private:
32 BinaryByteStream BBS;
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(); }
42 Error readBytes(uint64_t Offset, uint64_t Size,
43 ArrayRef<uint8_t> &Buffer) override {
44 return BBS.readBytes(Offset, Size, Buffer);
45 }
46 Error readLongestContiguousChunk(uint64_t Offset,
47 ArrayRef<uint8_t> &Buffer) override {
48 return BBS.readLongestContiguousChunk(Offset, Buffer);
49 }
50 uint64_t getLength() override { return BBS.getLength(); }
51
52 Error writeBytes(uint64_t Offset, ArrayRef<uint8_t> Data) override {
53 return BBS.writeBytes(Offset, Data);
54 }
55 Error commit() override { return BBS.commit(); }
56
57private:
58 MutableBinaryByteStream BBS;
59};
60} // namespace
61
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 =
86 BorrowedImpl->readLongestContiguousChunk(ViewOffset + Offset, Buffer))
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
99
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
115 return BorrowedImpl->writeBytes(ViewOffset + Offset, Data);
116}
117
118WritableBinaryStreamRef::operator BinaryStreamRef() const {
120}
121
122/// For buffered streams, commits changes to the backing store.
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:147
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:191
Error checkOffsetForRead(uint64_t Offset, uint64_t DataSize) const
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
LLVM_ABI 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 ...
LLVM_ABI 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.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition ArrayRef.h:303
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
LLVM_ABI 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...
LLVM_ABI Error commit()
For buffered streams, commits changes to the backing store.
A BinaryStream which can be read from as well as written to.
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
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:1657
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
endianness
Definition bit.h:71
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:851