LLVM  10.0.0svn
BinaryItemStream.h
Go to the documentation of this file.
1 //===- BinaryItemStream.h ---------------------------------------*- 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_BINARYITEMSTREAM_H
10 #define LLVM_SUPPORT_BINARYITEMSTREAM_H
11 
12 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/Support/Error.h"
16 #include <cstddef>
17 #include <cstdint>
18 
19 namespace llvm {
20 
21 template <typename T> struct BinaryItemTraits {
22  static size_t length(const T &Item) = delete;
23  static ArrayRef<uint8_t> bytes(const T &Item) = delete;
24 };
25 
26 /// BinaryItemStream represents a sequence of objects stored in some kind of
27 /// external container but for which it is useful to view as a stream of
28 /// contiguous bytes. An example of this might be if you have a collection of
29 /// records and you serialize each one into a buffer, and store these serialized
30 /// records in a container. The pointers themselves are not laid out
31 /// contiguously in memory, but we may wish to read from or write to these
32 /// records as if they were.
33 template <typename T, typename Traits = BinaryItemTraits<T>>
35 public:
37  : Endian(Endian) {}
38 
39  llvm::support::endianness getEndian() const override { return Endian; }
40 
42  ArrayRef<uint8_t> &Buffer) override {
43  auto ExpectedIndex = translateOffsetIndex(Offset);
44  if (!ExpectedIndex)
45  return ExpectedIndex.takeError();
46  const auto &Item = Items[*ExpectedIndex];
47  if (auto EC = checkOffsetForRead(Offset, Size))
48  return EC;
49  if (Size > Traits::length(Item))
50  return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
51  Buffer = Traits::bytes(Item).take_front(Size);
52  return Error::success();
53  }
54 
56  ArrayRef<uint8_t> &Buffer) override {
57  auto ExpectedIndex = translateOffsetIndex(Offset);
58  if (!ExpectedIndex)
59  return ExpectedIndex.takeError();
60  Buffer = Traits::bytes(Items[*ExpectedIndex]);
61  return Error::success();
62  }
63 
64  void setItems(ArrayRef<T> ItemArray) {
65  Items = ItemArray;
66  computeItemOffsets();
67  }
68 
69  uint32_t getLength() override {
70  return ItemEndOffsets.empty() ? 0 : ItemEndOffsets.back();
71  }
72 
73 private:
74  void computeItemOffsets() {
75  ItemEndOffsets.clear();
76  ItemEndOffsets.reserve(Items.size());
77  uint32_t CurrentOffset = 0;
78  for (const auto &Item : Items) {
79  uint32_t Len = Traits::length(Item);
80  assert(Len > 0 && "no empty items");
81  CurrentOffset += Len;
82  ItemEndOffsets.push_back(CurrentOffset);
83  }
84  }
85 
86  Expected<uint32_t> translateOffsetIndex(uint32_t Offset) {
87  // Make sure the offset is somewhere in our items array.
88  if (Offset >= getLength())
89  return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
90  ++Offset;
91  auto Iter =
92  std::lower_bound(ItemEndOffsets.begin(), ItemEndOffsets.end(), Offset);
93  size_t Idx = std::distance(ItemEndOffsets.begin(), Iter);
94  assert(Idx < Items.size() && "binary search for offset failed");
95  return Idx;
96  }
97 
99  ArrayRef<T> Items;
100 
101  // Sorted vector of offsets to accelerate lookup.
102  std::vector<uint32_t> ItemEndOffsets;
103 };
104 
105 } // end namespace llvm
106 
107 #endif // LLVM_SUPPORT_BINARYITEMSTREAM_H
auto lower_bound(R &&Range, T &&Value) -> decltype(adl_begin(Range))
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1261
ArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
Definition: ArrayRef.h:211
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static ArrayRef< uint8_t > bytes(const T &Item)=delete
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.
Tagged union holding either a T or a Error.
Definition: yaml2obj.h:21
An interface for accessing data in a stream-like format, but which discourages copying.
Definition: BinaryStream.h:35
llvm::support::endianness getEndian() const override
BinaryItemStream(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...
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
static size_t length(const T &Item)=delete
uint32_t getLength() override
Return the number of bytes of data in this stream.
uint32_t Size
Definition: Profile.cpp:46
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
BinaryItemStream represents a sequence of objects stored in some kind of external container but for w...
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
void setItems(ArrayRef< T > ItemArray)