Movatterモバイル変換


[0]ホーム

URL:


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
9#include "llvm/Support/BinaryStreamRef.h"
10#include "llvm/Support/BinaryByteStream.h"
11
12using namespacellvm;
13
14namespace{
15
16classArrayRefImpl :publicBinaryStream {
17public:
18 ArrayRefImpl(ArrayRef<uint8_t> Data,endianness Endian) : BBS(Data,Endian) {}
19
20llvm::endiannessgetEndian() const override{return BBS.getEndian(); }
21ErrorreadBytes(uint64_tOffset,uint64_tSize,
22ArrayRef<uint8_t> &Buffer) override{
23return BBS.readBytes(Offset,Size, Buffer);
24 }
25ErrorreadLongestContiguousChunk(uint64_tOffset,
26ArrayRef<uint8_t> &Buffer) override{
27return BBS.readLongestContiguousChunk(Offset, Buffer);
28 }
29uint64_tgetLength() override{return BBS.getLength(); }
30
31private:
32BinaryByteStream BBS;
33};
34
35classMutableArrayRefImpl :publicWritableBinaryStream {
36public:
37 MutableArrayRefImpl(MutableArrayRef<uint8_t> Data,endianness Endian)
38 : BBS(Data,Endian) {}
39
40// Inherited via WritableBinaryStream
41llvm::endiannessgetEndian() const override{return BBS.getEndian(); }
42ErrorreadBytes(uint64_tOffset,uint64_tSize,
43ArrayRef<uint8_t> &Buffer) override{
44return BBS.readBytes(Offset,Size, Buffer);
45 }
46ErrorreadLongestContiguousChunk(uint64_tOffset,
47ArrayRef<uint8_t> &Buffer) override{
48return BBS.readLongestContiguousChunk(Offset, Buffer);
49 }
50uint64_tgetLength() override{return BBS.getLength(); }
51
52ErrorwriteBytes(uint64_tOffset,ArrayRef<uint8_t> Data) override{
53return BBS.writeBytes(Offset, Data);
54 }
55Errorcommit() override{return BBS.commit(); }
56
57private:
58MutableBinaryByteStream BBS;
59};
60}// namespace
61
62BinaryStreamRef::BinaryStreamRef(BinaryStream &Stream)
63 :BinaryStreamRefBase(Stream) {}
64BinaryStreamRef::BinaryStreamRef(BinaryStream &Stream,uint64_tOffset,
65 std::optional<uint64_t>Length)
66 :BinaryStreamRefBase(Stream,Offset,Length) {}
67BinaryStreamRef::BinaryStreamRef(ArrayRef<uint8_t>Data,endianness Endian)
68 :BinaryStreamRefBase(std::make_shared<ArrayRefImpl>(Data,Endian), 0,
69Data.size()) {}
70BinaryStreamRef::BinaryStreamRef(StringRefData,endianness Endian)
71 :BinaryStreamRef(ArrayRef(Data.bytes_begin(),Data.bytes_end()),Endian) {}
72
73ErrorBinaryStreamRef::readBytes(uint64_tOffset,uint64_tSize,
74ArrayRef<uint8_t> &Buffer) const{
75if (auto EC =checkOffsetForRead(Offset,Size))
76return EC;
77returnBorrowedImpl->readBytes(ViewOffset +Offset,Size, Buffer);
78}
79
80ErrorBinaryStreamRef::readLongestContiguousChunk(
81uint64_tOffset,ArrayRef<uint8_t> &Buffer) const{
82if (auto EC =checkOffsetForRead(Offset, 1))
83return EC;
84
85if (auto EC =
86BorrowedImpl->readLongestContiguousChunk(ViewOffset +Offset, Buffer))
87return 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.
91uint64_t MaxLength =getLength() -Offset;
92if (Buffer.size() > MaxLength)
93 Buffer = Buffer.slice(0, MaxLength);
94returnError::success();
95}
96
97WritableBinaryStreamRef::WritableBinaryStreamRef(WritableBinaryStream &Stream)
98 :BinaryStreamRefBase(Stream) {}
99
100WritableBinaryStreamRef::WritableBinaryStreamRef(WritableBinaryStream &Stream,
101uint64_tOffset,
102 std::optional<uint64_t>Length)
103 :BinaryStreamRefBase(Stream,Offset,Length) {}
104
105WritableBinaryStreamRef::WritableBinaryStreamRef(MutableArrayRef<uint8_t>Data,
106endianness Endian)
107 :BinaryStreamRefBase(std::make_shared<MutableArrayRefImpl>(Data,Endian),
108 0,Data.size()) {}
109
110ErrorWritableBinaryStreamRef::writeBytes(uint64_tOffset,
111ArrayRef<uint8_t>Data) const{
112if (auto EC = checkOffsetForWrite(Offset,Data.size()))
113return EC;
114
115returnBorrowedImpl->writeBytes(ViewOffset +Offset,Data);
116}
117
118WritableBinaryStreamRef::operatorBinaryStreamRef() const{
119returnBinaryStreamRef(*BorrowedImpl, ViewOffset,Length);
120}
121
122/// For buffered streams, commits changes to the backing store.
123ErrorWritableBinaryStreamRef::commit() {returnBorrowedImpl->commit(); }
BinaryByteStream.h
BinaryStreamRef.h
Size
uint64_t Size
Definition:ELFObjHandler.cpp:81
Endian
endianness Endian
Definition:SampleProfWriter.cpp:52
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition:ArrayRef.h:41
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition:ArrayRef.h:168
llvm::ArrayRef::slice
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
llvm::BinaryByteStream
An implementation of BinaryStream which holds its entire data set in a single contiguous buffer.
Definition:BinaryByteStream.h:30
llvm::BinaryStreamRefBase
Common stuff for mutable and immutable StreamRefs.
Definition:BinaryStreamRef.h:23
llvm::BinaryStreamRefBase< BinaryStreamRef, BinaryStream >::ViewOffset
uint64_t ViewOffset
Definition:BinaryStreamRef.h:142
llvm::BinaryStreamRefBase< BinaryStreamRef, BinaryStream >::BorrowedImpl
BinaryStream * BorrowedImpl
Definition:BinaryStreamRef.h:141
llvm::BinaryStreamRefBase< BinaryStreamRef, BinaryStream >::checkOffsetForRead
Error checkOffsetForRead(uint64_t Offset, uint64_t DataSize) const
Definition:BinaryStreamRef.h:132
llvm::BinaryStreamRefBase< BinaryStreamRef, BinaryStream >::getLength
uint64_t getLength() const
Definition:BinaryStreamRef.h:48
llvm::BinaryStreamRef
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
Definition:BinaryStreamRef.h:154
llvm::BinaryStreamRef::readLongestContiguousChunk
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 ...
Definition:BinaryStreamRef.cpp:80
llvm::BinaryStreamRef::readBytes
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.
Definition:BinaryStreamRef.cpp:73
llvm::BinaryStreamRef::BinaryStreamRef
BinaryStreamRef()=default
llvm::BinaryStream
An interface for accessing data in a stream-like format, but which discourages copying.
Definition:BinaryStream.h:34
llvm::BinaryStream::getEndian
virtual llvm::endianness getEndian() const =0
llvm::BinaryStream::getLength
virtual uint64_t getLength()=0
Return the number of bytes of data in this stream.
llvm::BinaryStream::readBytes
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...
llvm::BinaryStream::readLongestContiguousChunk
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.
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition:Error.h:160
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition:Error.h:337
llvm::MutableArrayRef
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition:ArrayRef.h:310
llvm::MutableBinaryByteStream
An implementation of BinaryStream which holds its entire data set in a single contiguous buffer.
Definition:BinaryByteStream.h:88
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition:StringRef.h:51
llvm::WritableBinaryStreamRef::WritableBinaryStreamRef
WritableBinaryStreamRef()=default
llvm::WritableBinaryStreamRef::writeBytes
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...
Definition:BinaryStreamRef.cpp:110
llvm::WritableBinaryStreamRef::commit
Error commit()
For buffered streams, commits changes to the backing store.
Definition:BinaryStreamRef.cpp:123
llvm::WritableBinaryStream
A BinaryStream which can be read from as well as written to.
Definition:BinaryStream.h:72
llvm::WritableBinaryStream::writeBytes
virtual Error writeBytes(uint64_t Offset, ArrayRef< uint8_t > Data)=0
Attempt to write the given bytes into the stream at the desired offset.
llvm::WritableBinaryStream::commit
virtual Error commit()=0
For buffered streams, commits changes to the backing store.
uint64_t
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::Offset
@ Offset
Definition:DWP.cpp:480
llvm::Length
@ Length
Definition:DWP.cpp:480
llvm::size
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
llvm::endianness
endianness
Definition:bit.h:70
llvm::Data
@ Data
Definition:SIMachineScheduler.h:55
std
Implement std::hash so that hash_code can be used in STL containers.
Definition:BitVector.h:858

Generated on Sun Jul 20 2025 09:48:20 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp