LLVM 20.0.0git
BinaryStreamRef.h
Go to the documentation of this file.
1//===- BinaryStreamRef.h - A copyable reference to a stream -----*- C++ -*-===//
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
9#ifndef LLVM_SUPPORT_BINARYSTREAMREF_H
10#define LLVM_SUPPORT_BINARYSTREAMREF_H
11
12#include "llvm/ADT/ArrayRef.h"
15#include "llvm/Support/Error.h"
16#include <cstdint>
17#include <memory>
18#include <optional>
19
20namespace llvm {
21
22/// Common stuff for mutable and immutable StreamRefs.
23template <class RefType, class StreamType> class BinaryStreamRefBase {
24protected:
26 explicit BinaryStreamRefBase(StreamType &BorrowedImpl)
28 if (!(BorrowedImpl.getFlags() & BSF_Append))
29 Length = BorrowedImpl.getLength();
30 }
31
32 BinaryStreamRefBase(std::shared_ptr<StreamType> SharedImpl, uint64_t Offset,
33 std::optional<uint64_t> Length)
37 std::optional<uint64_t> Length)
41
44
45public:
46 llvm::endianness getEndian() const { return BorrowedImpl->getEndian(); }
47
49 if (Length)
50 return *Length;
51
52 return BorrowedImpl ? (BorrowedImpl->getLength() - ViewOffset) : 0;
53 }
54
55 /// Return a new BinaryStreamRef with the first \p N elements removed. If
56 /// this BinaryStreamRef is length-tracking, then the resulting one will be
57 /// too.
58 RefType drop_front(uint64_t N) const {
59 if (!BorrowedImpl)
60 return RefType();
61
62 N = std::min(N, getLength());
63 RefType Result(static_cast<const RefType &>(*this));
64 if (N == 0)
65 return Result;
66
67 Result.ViewOffset += N;
68 if (Result.Length)
69 *Result.Length -= N;
70 return Result;
71 }
72
73 /// Return a new BinaryStreamRef with the last \p N elements removed. If
74 /// this BinaryStreamRef is length-tracking and \p N is greater than 0, then
75 /// this BinaryStreamRef will no longer length-track.
76 RefType drop_back(uint64_t N) const {
77 if (!BorrowedImpl)
78 return RefType();
79
80 RefType Result(static_cast<const RefType &>(*this));
81 N = std::min(N, getLength());
82
83 if (N == 0)
84 return Result;
85
86 // Since we're dropping non-zero bytes from the end, stop length-tracking
87 // by setting the length of the resulting StreamRef to an explicit value.
88 if (!Result.Length)
89 Result.Length = getLength();
90
91 *Result.Length -= N;
92 return Result;
93 }
94
95 /// Return a new BinaryStreamRef with only the first \p N elements remaining.
96 RefType keep_front(uint64_t N) const {
97 assert(N <= getLength());
98 return drop_back(getLength() - N);
99 }
100
101 /// Return a new BinaryStreamRef with only the last \p N elements remaining.
102 RefType keep_back(uint64_t N) const {
103 assert(N <= getLength());
104 return drop_front(getLength() - N);
105 }
106
107 /// Return a new BinaryStreamRef with the first and last \p N elements
108 /// removed.
109 RefType drop_symmetric(uint64_t N) const {
110 return drop_front(N).drop_back(N);
111 }
112
113 /// Return a new BinaryStreamRef with the first \p Offset elements removed,
114 /// and retaining exactly \p Len elements.
115 RefType slice(uint64_t Offset, uint64_t Len) const {
116 return drop_front(Offset).keep_front(Len);
117 }
118
119 bool valid() const { return BorrowedImpl != nullptr; }
120
121 friend bool operator==(const RefType &LHS, const RefType &RHS) {
122 if (LHS.BorrowedImpl != RHS.BorrowedImpl)
123 return false;
124 if (LHS.ViewOffset != RHS.ViewOffset)
125 return false;
126 if (LHS.Length != RHS.Length)
127 return false;
128 return true;
129 }
130
131protected:
133 if (Offset > getLength())
134 return make_error<BinaryStreamError>(stream_error_code::invalid_offset);
135 if (getLength() < DataSize + Offset)
136 return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
137 return Error::success();
138 }
139
140 std::shared_ptr<StreamType> SharedImpl;
141 StreamType *BorrowedImpl = nullptr;
143 std::optional<uint64_t> Length;
144};
145
146/// BinaryStreamRef is to BinaryStream what ArrayRef is to an Array. It
147/// provides copy-semantics and read only access to a "window" of the underlying
148/// BinaryStream. Note that BinaryStreamRef is *not* a BinaryStream. That is to
149/// say, it does not inherit and override the methods of BinaryStream. In
150/// general, you should not pass around pointers or references to BinaryStreams
151/// and use inheritance to achieve polymorphism. Instead, you should pass
152/// around BinaryStreamRefs by value and achieve polymorphism that way.
154 : public BinaryStreamRefBase<BinaryStreamRef, BinaryStream> {
157 BinaryStreamRef(std::shared_ptr<BinaryStream> Impl, uint64_t ViewOffset,
158 std::optional<uint64_t> Length)
160
161public:
162 BinaryStreamRef() = default;
165 std::optional<uint64_t> Length);
168
173
174 // Use BinaryStreamRef.slice() instead.
176 uint64_t Length) = delete;
177
178 /// Given an Offset into this StreamRef and a Size, return a reference to a
179 /// buffer owned by the stream.
180 ///
181 /// \returns a success error code if the entire range of data is within the
182 /// bounds of this BinaryStreamRef's view and the implementation could read
183 /// the data, and an appropriate error code otherwise.
185 ArrayRef<uint8_t> &Buffer) const;
186
187 /// Given an Offset into this BinaryStreamRef, return a reference to the
188 /// largest buffer the stream could support without necessitating a copy.
189 ///
190 /// \returns a success error code if implementation could read the data,
191 /// and an appropriate error code otherwise.
193 ArrayRef<uint8_t> &Buffer) const;
194};
195
197 uint64_t Offset = 0; // Offset in the parent stream
199
201 BinaryStreamRef SubSub = StreamData.slice(Off, Size);
202 return {Off + Offset, SubSub};
203 }
205 return slice(N, size() - N);
206 }
208
209 std::pair<BinarySubstreamRef, BinarySubstreamRef> split(uint64_t Off) const {
210 return std::make_pair(keep_front(Off), drop_front(Off));
211 }
212
213 uint64_t size() const { return StreamData.getLength(); }
214 bool empty() const { return size() == 0; }
215};
216
218 : public BinaryStreamRefBase<WritableBinaryStreamRef,
219 WritableBinaryStream> {
221 WritableBinaryStreamRef(std::shared_ptr<WritableBinaryStream> Impl,
222 uint64_t ViewOffset, std::optional<uint64_t> Length)
224
225 Error checkOffsetForWrite(uint64_t Offset, uint64_t DataSize) const {
226 if (!(BorrowedImpl->getFlags() & BSF_Append))
227 return checkOffsetForRead(Offset, DataSize);
228
229 if (Offset > getLength())
230 return make_error<BinaryStreamError>(stream_error_code::invalid_offset);
231 return Error::success();
232 }
233
234public:
238 std::optional<uint64_t> Length);
244
247
248 // Use WritableBinaryStreamRef.slice() instead.
250 uint64_t Length) = delete;
251
252 /// Given an Offset into this WritableBinaryStreamRef and some input data,
253 /// writes the data to the underlying stream.
254 ///
255 /// \returns a success error code if the data could fit within the underlying
256 /// stream at the specified location and the implementation could write the
257 /// data, and an appropriate error code otherwise.
259
260 /// Conver this WritableBinaryStreamRef to a read-only BinaryStreamRef.
261 operator BinaryStreamRef() const;
262
263 /// For buffered streams, commits changes to the backing store.
264 Error commit();
265};
266
267} // end namespace llvm
268
269#endif // LLVM_SUPPORT_BINARYSTREAMREF_H
uint64_t Size
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
endianness Endian
Value * RHS
Value * LHS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Common stuff for mutable and immutable StreamRefs.
friend bool operator==(const RefType &LHS, const RefType &RHS)
std::optional< uint64_t > Length
RefType drop_back(uint64_t N) const
Return a new BinaryStreamRef with the last N elements removed.
BinaryStreamRefBase & operator=(const BinaryStreamRefBase &Other)=default
RefType drop_front(uint64_t N) const
Return a new BinaryStreamRef with the first N elements removed.
BinaryStreamRefBase & operator=(BinaryStreamRefBase &&Other)=default
BinaryStreamRefBase(StreamType &BorrowedImpl, uint64_t Offset, std::optional< uint64_t > Length)
std::shared_ptr< StreamType > SharedImpl
Error checkOffsetForRead(uint64_t Offset, uint64_t DataSize) const
RefType drop_symmetric(uint64_t N) const
Return a new BinaryStreamRef with the first and last N elements removed.
BinaryStreamRefBase(std::shared_ptr< StreamType > SharedImpl, uint64_t Offset, std::optional< uint64_t > Length)
RefType slice(uint64_t Offset, uint64_t Len) const
Return a new BinaryStreamRef with the first Offset elements removed, and retaining exactly Len elemen...
BinaryStreamRefBase(StreamType &BorrowedImpl)
llvm::endianness getEndian() const
RefType keep_back(uint64_t N) const
Return a new BinaryStreamRef with only the last N elements remaining.
RefType keep_front(uint64_t N) const
Return a new BinaryStreamRef with only the first N elements remaining.
BinaryStreamRefBase(BinaryStreamRefBase &&Other)=default
uint64_t getLength() const
BinaryStreamRefBase(const BinaryStreamRefBase &Other)=default
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
BinaryStreamRef(BinaryStreamRef &&Other)=default
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 ...
BinaryStreamRef(const BinaryStreamRef &Other)=default
BinaryStreamRef(BinaryStreamRef &S, uint64_t Offset, uint64_t Length)=delete
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.
BinaryStreamRef & operator=(BinaryStreamRef &&Other)=default
BinaryStreamRef & operator=(const BinaryStreamRef &Other)=default
An interface for accessing data in a stream-like format, but which discourages copying.
Definition: BinaryStream.h:34
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:307
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
WritableBinaryStreamRef & operator=(const WritableBinaryStreamRef &Other)=default
WritableBinaryStreamRef(const WritableBinaryStreamRef &Other)=default
WritableBinaryStreamRef(WritableBinaryStreamRef &&Other)=default
WritableBinaryStreamRef(WritableBinaryStreamRef &S, uint64_t Offset, uint64_t Length)=delete
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...
WritableBinaryStreamRef & operator=(WritableBinaryStreamRef &&Other)=default
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
BinaryStreamFlags getFlags() const override
Return the properties of this stream.
Definition: BinaryStream.h:85
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
@ BSF_Append
Definition: BinaryStream.h:23
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
@ Other
Any other memory.
endianness
Definition: bit.h:70
#define N
std::pair< BinarySubstreamRef, BinarySubstreamRef > split(uint64_t Off) const
BinarySubstreamRef keep_front(uint64_t N) const
BinarySubstreamRef slice(uint64_t Off, uint64_t Size) const
BinaryStreamRef StreamData
BinarySubstreamRef drop_front(uint64_t N) const