]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/DebugInfo/MSF/MappedBlockStream.h
Merge ^/head r311812 through r311939.
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / DebugInfo / MSF / MappedBlockStream.h
1 //===- MappedBlockStream.h - Discontiguous stream data in an MSF -*- C++
2 //-*-===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef LLVM_DEBUGINFO_MSF_MAPPEDBLOCKSTREAM_H
12 #define LLVM_DEBUGINFO_MSF_MAPPEDBLOCKSTREAM_H
13
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"
22 #include <cstdint>
23 #include <vector>
24
25 namespace llvm {
26 namespace msf {
27
28 struct MSFLayout;
29
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
39 /// of bytes.
40 class MappedBlockStream : public ReadableStream {
41   friend class WritableMappedBlockStream;
42 public:
43   static std::unique_ptr<MappedBlockStream>
44   createStream(uint32_t BlockSize, uint32_t NumBlocks,
45                const MSFStreamLayout &Layout, const ReadableStream &MsfData);
46
47   static std::unique_ptr<MappedBlockStream>
48   createIndexedStream(const MSFLayout &Layout, const ReadableStream &MsfData,
49                       uint32_t StreamIndex);
50
51   static std::unique_ptr<MappedBlockStream>
52   createFpmStream(const MSFLayout &Layout, const ReadableStream &MsfData);
53
54   static std::unique_ptr<MappedBlockStream>
55   createDirectoryStream(const MSFLayout &Layout, const ReadableStream &MsfData);
56
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;
61
62   uint32_t getLength() const override;
63
64   uint32_t getNumBytesCopied() const;
65
66   llvm::BumpPtrAllocator &getAllocator() { return Pool; }
67
68   void invalidateCache();
69
70   uint32_t getBlockSize() const { return BlockSize; }
71   uint32_t getNumBlocks() const { return NumBlocks; }
72   uint32_t getStreamLength() const { return StreamLayout.Length; }
73
74 protected:
75   MappedBlockStream(uint32_t BlockSize, uint32_t NumBlocks,
76                     const MSFStreamLayout &StreamLayout,
77                     const ReadableStream &MsfData);
78
79 private:
80   const MSFStreamLayout &getStreamLayout() const { return StreamLayout; }
81   void fixCacheAfterWrite(uint32_t Offset, ArrayRef<uint8_t> Data) const;
82
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;
86
87   const uint32_t BlockSize;
88   const uint32_t NumBlocks;
89   const MSFStreamLayout StreamLayout;
90   const ReadableStream &MsfData;
91
92   typedef MutableArrayRef<uint8_t> CacheEntry;
93   mutable llvm::BumpPtrAllocator Pool;
94   mutable DenseMap<uint32_t, std::vector<CacheEntry>> CacheMap;
95 };
96
97 class WritableMappedBlockStream : public WritableStream {
98 public:
99   static std::unique_ptr<WritableMappedBlockStream>
100   createStream(uint32_t BlockSize, uint32_t NumBlocks,
101                const MSFStreamLayout &Layout, const WritableStream &MsfData);
102
103   static std::unique_ptr<WritableMappedBlockStream>
104   createIndexedStream(const MSFLayout &Layout, const WritableStream &MsfData,
105                       uint32_t StreamIndex);
106
107   static std::unique_ptr<WritableMappedBlockStream>
108   createDirectoryStream(const MSFLayout &Layout, const WritableStream &MsfData);
109
110   static std::unique_ptr<WritableMappedBlockStream>
111   createFpmStream(const MSFLayout &Layout, const WritableStream &MsfData);
112
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;
118
119   Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Buffer) const override;
120
121   Error commit() const override;
122
123   const MSFStreamLayout &getStreamLayout() const {
124     return ReadInterface.getStreamLayout();
125   }
126   uint32_t getBlockSize() const { return ReadInterface.getBlockSize(); }
127   uint32_t getNumBlocks() const { return ReadInterface.getNumBlocks(); }
128   uint32_t getStreamLength() const { return ReadInterface.getStreamLength(); }
129
130 protected:
131   WritableMappedBlockStream(uint32_t BlockSize, uint32_t NumBlocks,
132                             const MSFStreamLayout &StreamLayout,
133                             const WritableStream &MsfData);
134
135 private:
136   MappedBlockStream ReadInterface;
137
138   const WritableStream &WriteInterface;
139 };
140
141 } // end namespace pdb
142 } // end namespace llvm
143
144 #endif // LLVM_DEBUGINFO_MSF_MAPPEDBLOCKSTREAM_H