Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:43

0001 //==- MappedBlockStream.h - Discontiguous stream data in an MSF --*- 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_DEBUGINFO_MSF_MAPPEDBLOCKSTREAM_H
0010 #define LLVM_DEBUGINFO_MSF_MAPPEDBLOCKSTREAM_H
0011 
0012 #include "llvm/ADT/ArrayRef.h"
0013 #include "llvm/ADT/DenseMap.h"
0014 #include "llvm/DebugInfo/MSF/MSFCommon.h"
0015 #include "llvm/Support/Allocator.h"
0016 #include "llvm/Support/BinaryStream.h"
0017 #include "llvm/Support/BinaryStreamRef.h"
0018 #include "llvm/Support/Endian.h"
0019 #include "llvm/Support/Error.h"
0020 #include <cstdint>
0021 #include <memory>
0022 #include <vector>
0023 
0024 namespace llvm {
0025 namespace msf {
0026 
0027 /// MappedBlockStream represents data stored in an MSF file into chunks of a
0028 /// particular size (called the Block Size), and whose chunks may not be
0029 /// necessarily contiguous.  The arrangement of these chunks MSF the file
0030 /// is described by some other metadata contained within the MSF file.  In
0031 /// the case of a standard MSF Stream, the layout of the stream's blocks
0032 /// is described by the MSF "directory", but in the case of the directory
0033 /// itself, the layout is described by an array at a fixed location within
0034 /// the MSF.  MappedBlockStream provides methods for reading from and writing
0035 /// to one of these streams transparently, as if it were a contiguous sequence
0036 /// of bytes.
0037 class MappedBlockStream : public BinaryStream {
0038   friend class WritableMappedBlockStream;
0039 
0040 public:
0041   static std::unique_ptr<MappedBlockStream>
0042   createStream(uint32_t BlockSize, const MSFStreamLayout &Layout,
0043                BinaryStreamRef MsfData, BumpPtrAllocator &Allocator);
0044 
0045   static std::unique_ptr<MappedBlockStream>
0046   createIndexedStream(const MSFLayout &Layout, BinaryStreamRef MsfData,
0047                       uint32_t StreamIndex, BumpPtrAllocator &Allocator);
0048 
0049   static std::unique_ptr<MappedBlockStream>
0050   createFpmStream(const MSFLayout &Layout, BinaryStreamRef MsfData,
0051                   BumpPtrAllocator &Allocator);
0052 
0053   static std::unique_ptr<MappedBlockStream>
0054   createDirectoryStream(const MSFLayout &Layout, BinaryStreamRef MsfData,
0055                         BumpPtrAllocator &Allocator);
0056 
0057   llvm::endianness getEndian() const override {
0058     return llvm::endianness::little;
0059   }
0060 
0061   Error readBytes(uint64_t Offset, uint64_t Size,
0062                   ArrayRef<uint8_t> &Buffer) override;
0063   Error readLongestContiguousChunk(uint64_t Offset,
0064                                    ArrayRef<uint8_t> &Buffer) override;
0065 
0066   uint64_t getLength() override;
0067 
0068   BumpPtrAllocator &getAllocator() { return Allocator; }
0069 
0070   void invalidateCache();
0071 
0072   uint32_t getBlockSize() const { return BlockSize; }
0073   uint32_t getNumBlocks() const { return StreamLayout.Blocks.size(); }
0074   uint32_t getStreamLength() const { return StreamLayout.Length; }
0075 
0076 protected:
0077   MappedBlockStream(uint32_t BlockSize, const MSFStreamLayout &StreamLayout,
0078                     BinaryStreamRef MsfData, BumpPtrAllocator &Allocator);
0079 
0080 private:
0081   const MSFStreamLayout &getStreamLayout() const { return StreamLayout; }
0082   void fixCacheAfterWrite(uint64_t Offset, ArrayRef<uint8_t> Data) const;
0083 
0084   Error readBytes(uint64_t Offset, MutableArrayRef<uint8_t> Buffer);
0085   bool tryReadContiguously(uint64_t Offset, uint64_t Size,
0086                            ArrayRef<uint8_t> &Buffer);
0087 
0088   const uint32_t BlockSize;
0089   const MSFStreamLayout StreamLayout;
0090   BinaryStreamRef MsfData;
0091 
0092   using CacheEntry = MutableArrayRef<uint8_t>;
0093 
0094   // We just store the allocator by reference.  We use this to allocate
0095   // contiguous memory for things like arrays or strings that cross a block
0096   // boundary, and this memory is expected to outlive the stream.  For example,
0097   // someone could create a stream, read some stuff, then close the stream, and
0098   // we would like outstanding references to fields to remain valid since the
0099   // entire file is mapped anyway.  Because of that, the user must supply the
0100   // allocator to allocate broken records from.
0101   BumpPtrAllocator &Allocator;
0102   DenseMap<uint32_t, std::vector<CacheEntry>> CacheMap;
0103 };
0104 
0105 class WritableMappedBlockStream : public WritableBinaryStream {
0106 public:
0107   static std::unique_ptr<WritableMappedBlockStream>
0108   createStream(uint32_t BlockSize, const MSFStreamLayout &Layout,
0109                WritableBinaryStreamRef MsfData, BumpPtrAllocator &Allocator);
0110 
0111   static std::unique_ptr<WritableMappedBlockStream>
0112   createIndexedStream(const MSFLayout &Layout, WritableBinaryStreamRef MsfData,
0113                       uint32_t StreamIndex, BumpPtrAllocator &Allocator);
0114 
0115   static std::unique_ptr<WritableMappedBlockStream>
0116   createDirectoryStream(const MSFLayout &Layout,
0117                         WritableBinaryStreamRef MsfData,
0118                         BumpPtrAllocator &Allocator);
0119 
0120   static std::unique_ptr<WritableMappedBlockStream>
0121   createFpmStream(const MSFLayout &Layout, WritableBinaryStreamRef MsfData,
0122                   BumpPtrAllocator &Allocator, bool AltFpm = false);
0123 
0124   llvm::endianness getEndian() const override {
0125     return llvm::endianness::little;
0126   }
0127 
0128   Error readBytes(uint64_t Offset, uint64_t Size,
0129                   ArrayRef<uint8_t> &Buffer) override;
0130   Error readLongestContiguousChunk(uint64_t Offset,
0131                                    ArrayRef<uint8_t> &Buffer) override;
0132   uint64_t getLength() override;
0133 
0134   Error writeBytes(uint64_t Offset, ArrayRef<uint8_t> Buffer) override;
0135 
0136   Error commit() override;
0137 
0138   const MSFStreamLayout &getStreamLayout() const {
0139     return ReadInterface.getStreamLayout();
0140   }
0141 
0142   uint32_t getBlockSize() const { return ReadInterface.getBlockSize(); }
0143   uint32_t getNumBlocks() const { return ReadInterface.getNumBlocks(); }
0144   uint32_t getStreamLength() const { return ReadInterface.getStreamLength(); }
0145 
0146 protected:
0147   WritableMappedBlockStream(uint32_t BlockSize,
0148                             const MSFStreamLayout &StreamLayout,
0149                             WritableBinaryStreamRef MsfData,
0150                             BumpPtrAllocator &Allocator);
0151 
0152 private:
0153   MappedBlockStream ReadInterface;
0154   WritableBinaryStreamRef WriteInterface;
0155 };
0156 
0157 } // namespace msf
0158 } // end namespace llvm
0159 
0160 #endif // LLVM_DEBUGINFO_MSF_MAPPEDBLOCKSTREAM_H