LLVM  6.0.0svn
MappedBlockStream.h
Go to the documentation of this file.
1 //==- MappedBlockStream.h - Discontiguous stream data in an MSF --*- C++ -*-==//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_DEBUGINFO_MSF_MAPPEDBLOCKSTREAM_H
11 #define LLVM_DEBUGINFO_MSF_MAPPEDBLOCKSTREAM_H
12 
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/Support/Allocator.h"
19 #include "llvm/Support/Endian.h"
20 #include "llvm/Support/Error.h"
21 #include <cstdint>
22 #include <memory>
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.
42 
43 public:
44  static std::unique_ptr<MappedBlockStream>
47 
48  static std::unique_ptr<MappedBlockStream>
49  createIndexedStream(const MSFLayout &Layout, BinaryStreamRef MsfData,
50  uint32_t StreamIndex, BumpPtrAllocator &Allocator);
51 
52  static std::unique_ptr<MappedBlockStream>
53  createFpmStream(const MSFLayout &Layout, BinaryStreamRef MsfData,
55 
56  static std::unique_ptr<MappedBlockStream>
57  createDirectoryStream(const MSFLayout &Layout, BinaryStreamRef MsfData,
59 
60  support::endianness getEndian() const override {
61  return support::little;
62  }
63 
65  ArrayRef<uint8_t> &Buffer) override;
67  ArrayRef<uint8_t> &Buffer) override;
68 
69  uint32_t getLength() override;
70 
72 
73  void invalidateCache();
74 
75  uint32_t getBlockSize() const { return BlockSize; }
76  uint32_t getNumBlocks() const { return StreamLayout.Blocks.size(); }
77  uint32_t getStreamLength() const { return StreamLayout.Length; }
78 
79 protected:
82 
83 private:
84  const MSFStreamLayout &getStreamLayout() const { return StreamLayout; }
85  void fixCacheAfterWrite(uint32_t Offset, ArrayRef<uint8_t> Data) const;
86 
88  bool tryReadContiguously(uint32_t Offset, uint32_t Size,
89  ArrayRef<uint8_t> &Buffer);
90 
91  const uint32_t BlockSize;
92  const MSFStreamLayout StreamLayout;
93  BinaryStreamRef MsfData;
94 
96 
97  // We just store the allocator by reference. We use this to allocate
98  // contiguous memory for things like arrays or strings that cross a block
99  // boundary, and this memory is expected to outlive the stream. For example,
100  // someone could create a stream, read some stuff, then close the stream, and
101  // we would like outstanding references to fields to remain valid since the
102  // entire file is mapped anyway. Because of that, the user must supply the
103  // allocator to allocate broken records from.
106 };
107 
109 public:
110  static std::unique_ptr<WritableMappedBlockStream>
111  createStream(uint32_t BlockSize, const MSFStreamLayout &Layout,
113 
114  static std::unique_ptr<WritableMappedBlockStream>
116  uint32_t StreamIndex, BumpPtrAllocator &Allocator);
117 
118  static std::unique_ptr<WritableMappedBlockStream>
119  createDirectoryStream(const MSFLayout &Layout,
120  WritableBinaryStreamRef MsfData,
121  BumpPtrAllocator &Allocator);
122 
123  static std::unique_ptr<WritableMappedBlockStream>
124  createFpmStream(const MSFLayout &Layout, WritableBinaryStreamRef MsfData,
125  BumpPtrAllocator &Allocator, bool AltFpm = false);
126 
127  support::endianness getEndian() const override {
128  return support::little;
129  }
130 
132  ArrayRef<uint8_t> &Buffer) override;
134  ArrayRef<uint8_t> &Buffer) override;
135  uint32_t getLength() override;
136 
137  Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Buffer) override;
138 
139  Error commit() override;
140 
142  return ReadInterface.getStreamLayout();
143  }
144 
145  uint32_t getBlockSize() const { return ReadInterface.getBlockSize(); }
146  uint32_t getNumBlocks() const { return ReadInterface.getNumBlocks(); }
147  uint32_t getStreamLength() const { return ReadInterface.getStreamLength(); }
148 
149 protected:
151  const MSFStreamLayout &StreamLayout,
152  WritableBinaryStreamRef MsfData,
153  BumpPtrAllocator &Allocator);
154 
155 private:
156  MappedBlockStream ReadInterface;
157  WritableBinaryStreamRef WriteInterface;
158 };
159 
160 } // end namespace pdb
161 } // end namespace llvm
162 
163 #endif // LLVM_DEBUGINFO_MSF_MAPPEDBLOCKSTREAM_H
uint32_t getLength() override
Return the number of bytes of data in this stream.
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
support::endianness getEndian() const override
Error readBytes(uint32_t Offset, uint32_t Size, ArrayRef< uint8_t > &Buffer) override
Given an offset into the stream and a number of bytes, attempt to read the bytes and set the output A...
BumpPtrAllocator & getAllocator()
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
Describes the layout of a stream in an MSF layout.
Definition: MSFCommon.h:68
static std::unique_ptr< MappedBlockStream > createIndexedStream(const MSFLayout &Layout, BinaryStreamRef MsfData, uint32_t StreamIndex, BumpPtrAllocator &Allocator)
const MSFStreamLayout & getStreamLayout() const
Error readLongestContiguousChunk(uint32_t Offset, ArrayRef< uint8_t > &Buffer) override
Given an offset into the stream, read as much as possible without copying any data.
std::vector< support::ulittle32_t > Blocks
Definition: MSFCommon.h:71
static std::unique_ptr< MappedBlockStream > createDirectoryStream(const MSFLayout &Layout, BinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
An interface for accessing data in a stream-like format, but which discourages copying.
Definition: BinaryStream.h:28
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:138
static std::unique_ptr< MappedBlockStream > createFpmStream(const MSFLayout &Layout, BinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
static std::unique_ptr< MappedBlockStream > createStream(uint32_t BlockSize, const MSFStreamLayout &Layout, BinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
static const int BlockSize
Definition: TarWriter.cpp:34
Basic Register Allocator
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
MappedBlockStream(uint32_t BlockSize, const MSFStreamLayout &StreamLayout, BinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
support::endianness getEndian() const override
MappedBlockStream represents data stored in an MSF file into chunks of a particular size (called the ...
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
A BinaryStream which can be read from as well as written to.
Definition: BinaryStream.h:63