Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:27

0001 //===- BinaryItemStream.h ---------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_SUPPORT_BINARYITEMSTREAM_H
0010 #define LLVM_SUPPORT_BINARYITEMSTREAM_H
0011 
0012 #include "llvm/ADT/ArrayRef.h"
0013 #include "llvm/Support/BinaryStream.h"
0014 #include "llvm/Support/BinaryStreamError.h"
0015 #include "llvm/Support/Error.h"
0016 #include <cstddef>
0017 #include <cstdint>
0018 
0019 namespace llvm {
0020 
0021 template <typename T> struct BinaryItemTraits {
0022   static size_t length(const T &Item) = delete;
0023   static ArrayRef<uint8_t> bytes(const T &Item) = delete;
0024 };
0025 
0026 /// BinaryItemStream represents a sequence of objects stored in some kind of
0027 /// external container but for which it is useful to view as a stream of
0028 /// contiguous bytes.  An example of this might be if you have a collection of
0029 /// records and you serialize each one into a buffer, and store these serialized
0030 /// records in a container.  The pointers themselves are not laid out
0031 /// contiguously in memory, but we may wish to read from or write to these
0032 /// records as if they were.
0033 template <typename T, typename Traits = BinaryItemTraits<T>>
0034 class BinaryItemStream : public BinaryStream {
0035 public:
0036   explicit BinaryItemStream(llvm::endianness Endian) : Endian(Endian) {}
0037 
0038   llvm::endianness getEndian() const override { return Endian; }
0039 
0040   Error readBytes(uint64_t Offset, uint64_t Size,
0041                   ArrayRef<uint8_t> &Buffer) override {
0042     auto ExpectedIndex = translateOffsetIndex(Offset);
0043     if (!ExpectedIndex)
0044       return ExpectedIndex.takeError();
0045     const auto &Item = Items[*ExpectedIndex];
0046     if (auto EC = checkOffsetForRead(Offset, Size))
0047       return EC;
0048     if (Size > Traits::length(Item))
0049       return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
0050     Buffer = Traits::bytes(Item).take_front(Size);
0051     return Error::success();
0052   }
0053 
0054   Error readLongestContiguousChunk(uint64_t Offset,
0055                                    ArrayRef<uint8_t> &Buffer) override {
0056     auto ExpectedIndex = translateOffsetIndex(Offset);
0057     if (!ExpectedIndex)
0058       return ExpectedIndex.takeError();
0059     Buffer = Traits::bytes(Items[*ExpectedIndex]);
0060     return Error::success();
0061   }
0062 
0063   void setItems(ArrayRef<T> ItemArray) {
0064     Items = ItemArray;
0065     computeItemOffsets();
0066   }
0067 
0068   uint64_t getLength() override {
0069     return ItemEndOffsets.empty() ? 0 : ItemEndOffsets.back();
0070   }
0071 
0072 private:
0073   void computeItemOffsets() {
0074     ItemEndOffsets.clear();
0075     ItemEndOffsets.reserve(Items.size());
0076     uint64_t CurrentOffset = 0;
0077     for (const auto &Item : Items) {
0078       uint64_t Len = Traits::length(Item);
0079       assert(Len > 0 && "no empty items");
0080       CurrentOffset += Len;
0081       ItemEndOffsets.push_back(CurrentOffset);
0082     }
0083   }
0084 
0085   Expected<uint32_t> translateOffsetIndex(uint64_t Offset) {
0086     // Make sure the offset is somewhere in our items array.
0087     if (Offset >= getLength())
0088       return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
0089     ++Offset;
0090     auto Iter = llvm::lower_bound(ItemEndOffsets, Offset);
0091     size_t Idx = std::distance(ItemEndOffsets.begin(), Iter);
0092     assert(Idx < Items.size() && "binary search for offset failed");
0093     return Idx;
0094   }
0095 
0096   llvm::endianness Endian;
0097   ArrayRef<T> Items;
0098 
0099   // Sorted vector of offsets to accelerate lookup.
0100   std::vector<uint64_t> ItemEndOffsets;
0101 };
0102 
0103 } // end namespace llvm
0104 
0105 #endif // LLVM_SUPPORT_BINARYITEMSTREAM_H