BinaryItemStream.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- BinaryItemStream.h ---------------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_SUPPORT_BINARYITEMSTREAM_H
  14. #define LLVM_SUPPORT_BINARYITEMSTREAM_H
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/Support/BinaryStream.h"
  17. #include "llvm/Support/BinaryStreamError.h"
  18. #include "llvm/Support/Error.h"
  19. #include <cstddef>
  20. #include <cstdint>
  21. namespace llvm {
  22. template <typename T> struct BinaryItemTraits {
  23. static size_t length(const T &Item) = delete;
  24. static ArrayRef<uint8_t> bytes(const T &Item) = delete;
  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>>
  34. class BinaryItemStream : public BinaryStream {
  35. public:
  36. explicit BinaryItemStream(llvm::support::endianness Endian)
  37. : Endian(Endian) {}
  38. llvm::support::endianness getEndian() const override { return Endian; }
  39. Error readBytes(uint64_t Offset, uint64_t Size,
  40. ArrayRef<uint8_t> &Buffer) override {
  41. auto ExpectedIndex = translateOffsetIndex(Offset);
  42. if (!ExpectedIndex)
  43. return ExpectedIndex.takeError();
  44. const auto &Item = Items[*ExpectedIndex];
  45. if (auto EC = checkOffsetForRead(Offset, Size))
  46. return EC;
  47. if (Size > Traits::length(Item))
  48. return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
  49. Buffer = Traits::bytes(Item).take_front(Size);
  50. return Error::success();
  51. }
  52. Error readLongestContiguousChunk(uint64_t Offset,
  53. ArrayRef<uint8_t> &Buffer) override {
  54. auto ExpectedIndex = translateOffsetIndex(Offset);
  55. if (!ExpectedIndex)
  56. return ExpectedIndex.takeError();
  57. Buffer = Traits::bytes(Items[*ExpectedIndex]);
  58. return Error::success();
  59. }
  60. void setItems(ArrayRef<T> ItemArray) {
  61. Items = ItemArray;
  62. computeItemOffsets();
  63. }
  64. uint64_t getLength() override {
  65. return ItemEndOffsets.empty() ? 0 : ItemEndOffsets.back();
  66. }
  67. private:
  68. void computeItemOffsets() {
  69. ItemEndOffsets.clear();
  70. ItemEndOffsets.reserve(Items.size());
  71. uint64_t CurrentOffset = 0;
  72. for (const auto &Item : Items) {
  73. uint64_t Len = Traits::length(Item);
  74. assert(Len > 0 && "no empty items");
  75. CurrentOffset += Len;
  76. ItemEndOffsets.push_back(CurrentOffset);
  77. }
  78. }
  79. Expected<uint32_t> translateOffsetIndex(uint64_t Offset) {
  80. // Make sure the offset is somewhere in our items array.
  81. if (Offset >= getLength())
  82. return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
  83. ++Offset;
  84. auto Iter = llvm::lower_bound(ItemEndOffsets, Offset);
  85. size_t Idx = std::distance(ItemEndOffsets.begin(), Iter);
  86. assert(Idx < Items.size() && "binary search for offset failed");
  87. return Idx;
  88. }
  89. llvm::support::endianness Endian;
  90. ArrayRef<T> Items;
  91. // Sorted vector of offsets to accelerate lookup.
  92. std::vector<uint64_t> ItemEndOffsets;
  93. };
  94. } // end namespace llvm
  95. #endif // LLVM_SUPPORT_BINARYITEMSTREAM_H
  96. #ifdef __GNUC__
  97. #pragma GCC diagnostic pop
  98. #endif