1 //===- MappedBlockStream.h - Discontiguous stream data in an MSF -*- C++
4 // The LLVM Compiler Infrastructure
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef LLVM_DEBUGINFO_MSF_MAPPEDBLOCKSTREAM_H
12 #define LLVM_DEBUGINFO_MSF_MAPPEDBLOCKSTREAM_H
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/DebugInfo/MSF/MSFStreamLayout.h"
18 #include "llvm/DebugInfo/MSF/StreamInterface.h"
19 #include "llvm/Support/Allocator.h"
20 #include "llvm/Support/Endian.h"
21 #include "llvm/Support/Error.h"
30 /// MappedBlockStream represents data stored in an MSF file into chunks of a
31 /// particular size (called the Block Size), and whose chunks may not be
32 /// necessarily contiguous. The arrangement of these chunks MSF the file
33 /// is described by some other metadata contained within the MSF file. In
34 /// the case of a standard MSF Stream, the layout of the stream's blocks
35 /// is described by the MSF "directory", but in the case of the directory
36 /// itself, the layout is described by an array at a fixed location within
37 /// the MSF. MappedBlockStream provides methods for reading from and writing
38 /// to one of these streams transparently, as if it were a contiguous sequence
40 class MappedBlockStream : public ReadableStream {
41 friend class WritableMappedBlockStream;
43 static std::unique_ptr<MappedBlockStream>
44 createStream(uint32_t BlockSize, uint32_t NumBlocks,
45 const MSFStreamLayout &Layout, const ReadableStream &MsfData);
47 static std::unique_ptr<MappedBlockStream>
48 createIndexedStream(const MSFLayout &Layout, const ReadableStream &MsfData,
49 uint32_t StreamIndex);
51 static std::unique_ptr<MappedBlockStream>
52 createFpmStream(const MSFLayout &Layout, const ReadableStream &MsfData);
54 static std::unique_ptr<MappedBlockStream>
55 createDirectoryStream(const MSFLayout &Layout, const ReadableStream &MsfData);
57 Error readBytes(uint32_t Offset, uint32_t Size,
58 ArrayRef<uint8_t> &Buffer) const override;
59 Error readLongestContiguousChunk(uint32_t Offset,
60 ArrayRef<uint8_t> &Buffer) const override;
62 uint32_t getLength() const override;
64 uint32_t getNumBytesCopied() const;
66 llvm::BumpPtrAllocator &getAllocator() { return Pool; }
68 void invalidateCache();
70 uint32_t getBlockSize() const { return BlockSize; }
71 uint32_t getNumBlocks() const { return NumBlocks; }
72 uint32_t getStreamLength() const { return StreamLayout.Length; }
75 MappedBlockStream(uint32_t BlockSize, uint32_t NumBlocks,
76 const MSFStreamLayout &StreamLayout,
77 const ReadableStream &MsfData);
80 const MSFStreamLayout &getStreamLayout() const { return StreamLayout; }
81 void fixCacheAfterWrite(uint32_t Offset, ArrayRef<uint8_t> Data) const;
83 Error readBytes(uint32_t Offset, MutableArrayRef<uint8_t> Buffer) const;
84 bool tryReadContiguously(uint32_t Offset, uint32_t Size,
85 ArrayRef<uint8_t> &Buffer) const;
87 const uint32_t BlockSize;
88 const uint32_t NumBlocks;
89 const MSFStreamLayout StreamLayout;
90 const ReadableStream &MsfData;
92 typedef MutableArrayRef<uint8_t> CacheEntry;
93 mutable llvm::BumpPtrAllocator Pool;
94 mutable DenseMap<uint32_t, std::vector<CacheEntry>> CacheMap;
97 class WritableMappedBlockStream : public WritableStream {
99 static std::unique_ptr<WritableMappedBlockStream>
100 createStream(uint32_t BlockSize, uint32_t NumBlocks,
101 const MSFStreamLayout &Layout, const WritableStream &MsfData);
103 static std::unique_ptr<WritableMappedBlockStream>
104 createIndexedStream(const MSFLayout &Layout, const WritableStream &MsfData,
105 uint32_t StreamIndex);
107 static std::unique_ptr<WritableMappedBlockStream>
108 createDirectoryStream(const MSFLayout &Layout, const WritableStream &MsfData);
110 static std::unique_ptr<WritableMappedBlockStream>
111 createFpmStream(const MSFLayout &Layout, const WritableStream &MsfData);
113 Error readBytes(uint32_t Offset, uint32_t Size,
114 ArrayRef<uint8_t> &Buffer) const override;
115 Error readLongestContiguousChunk(uint32_t Offset,
116 ArrayRef<uint8_t> &Buffer) const override;
117 uint32_t getLength() const override;
119 Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Buffer) const override;
121 Error commit() const override;
123 const MSFStreamLayout &getStreamLayout() const {
124 return ReadInterface.getStreamLayout();
126 uint32_t getBlockSize() const { return ReadInterface.getBlockSize(); }
127 uint32_t getNumBlocks() const { return ReadInterface.getNumBlocks(); }
128 uint32_t getStreamLength() const { return ReadInterface.getStreamLength(); }
131 WritableMappedBlockStream(uint32_t BlockSize, uint32_t NumBlocks,
132 const MSFStreamLayout &StreamLayout,
133 const WritableStream &MsfData);
136 MappedBlockStream ReadInterface;
138 const WritableStream &WriteInterface;
141 } // end namespace pdb
142 } // end namespace llvm
144 #endif // LLVM_DEBUGINFO_MSF_MAPPEDBLOCKSTREAM_H