LLVM: include/llvm/Support/BinaryByteStream.h Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10#ifndef LLVM_SUPPORT_BINARYBYTESTREAM_H
11#define LLVM_SUPPORT_BINARYBYTESTREAM_H
12
20#include
21#include
22#include
23
24namespace llvm {
25
26
27
28
29
31public:
37
39
43 return EC;
46 }
47
51 return EC;
54 }
55
57
59
61 const char *CharData = reinterpret_cast<const char *>(Data.data());
63 }
64
65protected:
68};
69
70
71
72
73
75public:
80
82};
83
84
85
86
87
89public:
93 : Data(Data), ImmutableStream(Data, Endian) {}
94
96 return ImmutableStream.getEndian();
97 }
98
102 }
103
107 }
108
110
112 if (Buffer.empty())
114
116 return EC;
117
119 ::memcpy(DataPtr + Offset, Buffer.data(), Buffer.size());
121 }
122
124
126
127private:
130};
131
132
133
135 std::vector<uint8_t> Data;
137
138public:
141
142 void clear() { Data.clear(); }
143
145
149 return EC;
150
153 }
154
156 Data.insert(Data.begin() + Offset, Bytes.begin(), Bytes.end());
157 }
158
162 return EC;
163
166 }
167
169
171 if (Buffer.empty())
173
174
175
176
177
178
179
182
184 if (RequiredSize > Data.size())
185 Data.resize(RequiredSize);
186
187 ::memcpy(Data.data() + Offset, Buffer.data(), Buffer.size());
189 }
190
192
193
195
197};
198
199
200
202private:
204 public:
205 StreamImpl(std::unique_ptr Buffer,
209 Buffer->getBufferEnd()),
211 FileBuffer(std::move(Buffer)) {}
212
213 Error commit() override {
214 if (FileBuffer->commit())
215 return make_error(
218 }
219
220
221 uint8_t *getBufferStart() const { return FileBuffer->getBufferStart(); }
222
223
224 uint8_t *getBufferEnd() const { return FileBuffer->getBufferEnd(); }
225
226 private:
227 std::unique_ptr FileBuffer;
228 };
229
230public:
234
236
239 return Impl.readBytes(Offset, Size, Buffer);
240 }
241
244 return Impl.readLongestContiguousChunk(Offset, Buffer);
245 }
246
248
250 return Impl.writeBytes(Offset, Data);
251 }
252
254
255
257
258
260
261private:
262 StreamImpl Impl;
263};
264
265}
266
267#endif
An implementation of WritableBinaryStream which can write at its end causing the underlying data to g...
BinaryStreamFlags getFlags() const override
Return the properties of this stream.
Error writeBytes(uint64_t Offset, ArrayRef< uint8_t > Buffer) override
Attempt to write the given bytes into the stream at the desired offset.
llvm::endianness getEndian() const override
AppendingBinaryByteStream(llvm::endianness Endian)
Error commit() override
For buffered streams, commits changes to the backing store.
MutableArrayRef< uint8_t > data()
uint64_t getLength() override
Return the number of bytes of data in this stream.
Error readBytes(uint64_t Offset, uint64_t Size, ArrayRef< uint8_t > &Buffer) override
Given an offset into the stream and a number of bytes, attempt to read the bytes and set the output A...
Error readLongestContiguousChunk(uint64_t Offset, ArrayRef< uint8_t > &Buffer) override
Given an offset into the stream, read as much as possible without copying any data.
void insert(uint64_t Offset, ArrayRef< uint8_t > Bytes)
AppendingBinaryByteStream()=default
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
bool empty() const
empty - Check if the array is empty.
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.
An implementation of BinaryStream which holds its entire data set in a single contiguous buffer.
ArrayRef< uint8_t > data() const
BinaryByteStream(ArrayRef< uint8_t > Data, llvm::endianness Endian)
Error readLongestContiguousChunk(uint64_t Offset, ArrayRef< uint8_t > &Buffer) override
Given an offset into the stream, read as much as possible without copying any data.
BinaryByteStream()=default
Error readBytes(uint64_t Offset, uint64_t Size, ArrayRef< uint8_t > &Buffer) override
Given an offset into the stream and a number of bytes, attempt to read the bytes and set the output A...
uint64_t getLength() override
Return the number of bytes of data in this stream.
llvm::endianness getEndian() const override
BinaryByteStream(StringRef Data, llvm::endianness Endian)
An interface for accessing data in a stream-like format, but which discourages copying.
Error checkOffsetForRead(uint64_t Offset, uint64_t DataSize)
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
An implementation of WritableBinaryStream backed by an llvm FileOutputBuffer.
FileBufferByteStream(std::unique_ptr< FileOutputBuffer > Buffer, llvm::endianness Endian)
uint64_t getLength() override
Return the number of bytes of data in this stream.
llvm::endianness getEndian() const override
Error commit() override
For buffered streams, commits changes to the backing store.
Error readLongestContiguousChunk(uint64_t Offset, ArrayRef< uint8_t > &Buffer) override
Given an offset into the stream, read as much as possible without copying any data.
Error writeBytes(uint64_t Offset, ArrayRef< uint8_t > Data) override
Attempt to write the given bytes into the stream at the desired offset.
uint8_t * getBufferEnd() const
Returns a pointer to the end of the buffer.
uint8_t * getBufferStart() const
Returns a pointer to the start of the buffer.
Error readBytes(uint64_t Offset, uint64_t Size, ArrayRef< uint8_t > &Buffer) override
Given an offset into the stream and a number of bytes, attempt to read the bytes and set the output A...
An implementation of BinaryStream whose data is backed by an llvm MemoryBuffer object.
std::unique_ptr< MemoryBuffer > MemBuffer
MemoryBufferByteStream(std::unique_ptr< MemoryBuffer > Buffer, llvm::endianness Endian)
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
An implementation of BinaryStream which holds its entire data set in a single contiguous buffer.
MutableArrayRef< uint8_t > data() const
uint64_t getLength() override
Return the number of bytes of data in this stream.
Error commit() override
For buffered streams, commits changes to the backing store.
llvm::endianness getEndian() const override
Error writeBytes(uint64_t Offset, ArrayRef< uint8_t > Buffer) override
Attempt to write the given bytes into the stream at the desired offset.
MutableBinaryByteStream(MutableArrayRef< uint8_t > Data, llvm::endianness Endian)
MutableBinaryByteStream()=default
Error readLongestContiguousChunk(uint64_t Offset, ArrayRef< uint8_t > &Buffer) override
Given an offset into the stream, read as much as possible without copying any data.
Error readBytes(uint64_t Offset, uint64_t Size, ArrayRef< uint8_t > &Buffer) override
Given an offset into the stream and a number of bytes, attempt to read the bytes and set the output A...
StringRef - Represent a constant reference to a string, i.e.
A BinaryStream which can be read from as well as written to.
Error checkOffsetForWrite(uint64_t Offset, uint64_t DataSize)
This is an optimization pass for GlobalISel generic memory operations.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Implement std::hash so that hash_code can be used in STL containers.