LLVM  6.0.0svn
BinaryByteStream.h
Go to the documentation of this file.
1 //===- BinaryByteStream.h ---------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //===----------------------------------------------------------------------===//
8 // A BinaryStream which stores data in a single continguous memory buffer.
9 //===----------------------------------------------------------------------===//
10 
11 #ifndef LLVM_SUPPORT_BINARYBYTESTREAM_H
12 #define LLVM_SUPPORT_BINARYBYTESTREAM_H
13 
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/Error.h"
21 #include <algorithm>
22 #include <cstdint>
23 #include <cstring>
24 #include <memory>
25 
26 namespace llvm {
27 
28 /// \brief An implementation of BinaryStream which holds its entire data set
29 /// in a single contiguous buffer. BinaryByteStream guarantees that no read
30 /// operation will ever incur a copy. Note that BinaryByteStream does not
31 /// own the underlying buffer.
33 public:
34  BinaryByteStream() = default;
36  : Endian(Endian), Data(Data) {}
38  : Endian(Endian), Data(Data.bytes_begin(), Data.bytes_end()) {}
39 
40  llvm::support::endianness getEndian() const override { return Endian; }
41 
43  ArrayRef<uint8_t> &Buffer) override {
44  if (auto EC = checkOffset(Offset, Size))
45  return EC;
46  Buffer = Data.slice(Offset, Size);
47  return Error::success();
48  }
49 
51  ArrayRef<uint8_t> &Buffer) override {
52  if (auto EC = checkOffset(Offset, 1))
53  return EC;
54  Buffer = Data.slice(Offset);
55  return Error::success();
56  }
57 
58  uint32_t getLength() override { return Data.size(); }
59 
60  ArrayRef<uint8_t> data() const { return Data; }
61 
62  StringRef str() const {
63  const char *CharData = reinterpret_cast<const char *>(Data.data());
64  return StringRef(CharData, Data.size());
65  }
66 
67 protected:
70 };
71 
72 /// \brief An implementation of BinaryStream whose data is backed by an llvm
73 /// MemoryBuffer object. MemoryBufferByteStream owns the MemoryBuffer in
74 /// question. As with BinaryByteStream, reading from a MemoryBufferByteStream
75 /// will never cause a copy.
77 public:
78  MemoryBufferByteStream(std::unique_ptr<MemoryBuffer> Buffer,
80  : BinaryByteStream(Buffer->getBuffer(), Endian),
81  MemBuffer(std::move(Buffer)) {}
82 
83  std::unique_ptr<MemoryBuffer> MemBuffer;
84 };
85 
86 /// \brief An implementation of BinaryStream which holds its entire data set
87 /// in a single contiguous buffer. As with BinaryByteStream, the mutable
88 /// version also guarantees that no read operation will ever incur a copy,
89 /// and similarly it does not own the underlying buffer.
91 public:
92  MutableBinaryByteStream() = default;
95  : Data(Data), ImmutableStream(Data, Endian) {}
96 
98  return ImmutableStream.getEndian();
99  }
100 
102  ArrayRef<uint8_t> &Buffer) override {
103  return ImmutableStream.readBytes(Offset, Size, Buffer);
104  }
105 
107  ArrayRef<uint8_t> &Buffer) override {
108  return ImmutableStream.readLongestContiguousChunk(Offset, Buffer);
109  }
110 
111  uint32_t getLength() override { return ImmutableStream.getLength(); }
112 
114  if (Buffer.empty())
115  return Error::success();
116 
117  if (auto EC = checkOffset(Offset, Buffer.size()))
118  return EC;
119 
120  uint8_t *DataPtr = const_cast<uint8_t *>(Data.data());
121  ::memcpy(DataPtr + Offset, Buffer.data(), Buffer.size());
122  return Error::success();
123  }
124 
125  Error commit() override { return Error::success(); }
126 
127  MutableArrayRef<uint8_t> data() const { return Data; }
128 
129 private:
131  BinaryByteStream ImmutableStream;
132 };
133 
134 /// \brief An implementation of WritableBinaryStream backed by an llvm
135 /// FileOutputBuffer.
137 private:
138  class StreamImpl : public MutableBinaryByteStream {
139  public:
140  StreamImpl(std::unique_ptr<FileOutputBuffer> Buffer,
143  MutableArrayRef<uint8_t>(Buffer->getBufferStart(),
144  Buffer->getBufferEnd()),
145  Endian),
146  FileBuffer(std::move(Buffer)) {}
147 
148  Error commit() override {
149  if (FileBuffer->commit())
150  return make_error<BinaryStreamError>(
152  return Error::success();
153  }
154 
155  private:
156  std::unique_ptr<FileOutputBuffer> FileBuffer;
157  };
158 
159 public:
160  FileBufferByteStream(std::unique_ptr<FileOutputBuffer> Buffer,
162  : Impl(std::move(Buffer), Endian) {}
163 
165  return Impl.getEndian();
166  }
167 
169  ArrayRef<uint8_t> &Buffer) override {
170  return Impl.readBytes(Offset, Size, Buffer);
171  }
172 
174  ArrayRef<uint8_t> &Buffer) override {
175  return Impl.readLongestContiguousChunk(Offset, Buffer);
176  }
177 
178  uint32_t getLength() override { return Impl.getLength(); }
179 
181  return Impl.writeBytes(Offset, Data);
182  }
183 
184  Error commit() override { return Impl.commit(); }
185 
186 private:
187  StreamImpl Impl;
188 };
189 
190 } // end namespace llvm
191 
192 #endif // LLVM_SUPPORT_BYTESTREAM_H
std::unique_ptr< MemoryBuffer > MemBuffer
An implementation of BinaryStream which holds its entire data set in a single contiguous buffer...
An implementation of BinaryStream which holds its entire data set in a single contiguous buffer...
Error checkOffset(uint32_t Offset, uint32_t DataSize)
Definition: BinaryStream.h:49
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
MutableArrayRef< uint8_t > data() const
Error readBytes(uint32_t Offset, uint32_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...
llvm::support::endianness getEndian() const override
Error readLongestContiguousChunk(uint32_t Offset, ArrayRef< uint8_t > &Buffer) override
Given an offset into the stream, read as much as possible without copying any data.
uint32_t getLength() override
Return the number of bytes of data in this stream.
Definition: BitVector.h:920
Error readBytes(uint32_t Offset, uint32_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...
ArrayRef< uint8_t > data() const
MutableBinaryByteStream(MutableArrayRef< uint8_t > Data, llvm::support::endianness Endian)
Error commit() override
For buffered streams, commits changes to the backing store.
An interface for accessing data in a stream-like format, but which discourages copying.
Definition: BinaryStream.h:28
Error writeBytes(uint32_t Offset, ArrayRef< uint8_t > Data) override
Attempt to write the given bytes into the stream at the desired offset.
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
ArrayRef< uint8_t > Data
Error readLongestContiguousChunk(uint32_t Offset, ArrayRef< uint8_t > &Buffer) override
Given an offset into the stream, read as much as possible without copying any data.
An implementation of BinaryStream whose data is backed by an llvm MemoryBuffer object.
const T * data() const
Definition: ArrayRef.h:146
llvm::support::endianness getEndian() const override
static ErrorSuccess success()
Create a success value.
Definition: Error.h:313
llvm::support::endianness getEndian() const override
Error commit() override
For buffered streams, commits changes to the backing store.
BinaryByteStream(StringRef Data, llvm::support::endianness Endian)
llvm::support::endianness Endian
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:179
StringRef str() const
BinaryByteStream(ArrayRef< uint8_t > Data, llvm::support::endianness Endian)
uint32_t getLength() override
Return the number of bytes of data in this stream.
MemoryBufferByteStream(std::unique_ptr< MemoryBuffer > Buffer, llvm::support::endianness Endian)
Error readBytes(uint32_t Offset, uint32_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...
FileBufferByteStream(std::unique_ptr< FileOutputBuffer > Buffer, llvm::support::endianness Endian)
Error readLongestContiguousChunk(uint32_t Offset, ArrayRef< uint8_t > &Buffer) override
Given an offset into the stream, read as much as possible without copying any data.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
An implementation of WritableBinaryStream backed by an llvm FileOutputBuffer.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
uint32_t getLength() override
Return the number of bytes of data in this stream.
Error writeBytes(uint32_t Offset, ArrayRef< uint8_t > Buffer) override
Attempt to write the given bytes into the stream at the desired offset.
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:144
A BinaryStream which can be read from as well as written to.
Definition: BinaryStream.h:63