LLVM  10.0.0svn
MappedBlockStream.h
Go to the documentation of this file.
1 //==- MappedBlockStream.h - Discontiguous stream data in an MSF --*- C++ -*-==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_DEBUGINFO_MSF_MAPPEDBLOCKSTREAM_H
10 #define LLVM_DEBUGINFO_MSF_MAPPEDBLOCKSTREAM_H
11 
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/Support/Allocator.h"
18 #include "llvm/Support/Endian.h"
19 #include "llvm/Support/Error.h"
20 #include <cstdint>
21 #include <memory>
22 #include <vector>
23 
24 namespace llvm {
25 namespace msf {
26 
27 struct MSFLayout;
28 
29 /// MappedBlockStream represents data stored in an MSF file into chunks of a
30 /// particular size (called the Block Size), and whose chunks may not be
31 /// necessarily contiguous. The arrangement of these chunks MSF the file
32 /// is described by some other metadata contained within the MSF file. In
33 /// the case of a standard MSF Stream, the layout of the stream's blocks
34 /// is described by the MSF "directory", but in the case of the directory
35 /// itself, the layout is described by an array at a fixed location within
36 /// the MSF. MappedBlockStream provides methods for reading from and writing
37 /// to one of these streams transparently, as if it were a contiguous sequence
38 /// of bytes.
41 
42 public:
43  static std::unique_ptr<MappedBlockStream>
46 
47  static std::unique_ptr<MappedBlockStream>
48  createIndexedStream(const MSFLayout &Layout, BinaryStreamRef MsfData,
49  uint32_t StreamIndex, BumpPtrAllocator &Allocator);
50 
51  static std::unique_ptr<MappedBlockStream>
52  createFpmStream(const MSFLayout &Layout, BinaryStreamRef MsfData,
54 
55  static std::unique_ptr<MappedBlockStream>
56  createDirectoryStream(const MSFLayout &Layout, BinaryStreamRef MsfData,
58 
59  support::endianness getEndian() const override {
60  return support::little;
61  }
62 
64  ArrayRef<uint8_t> &Buffer) override;
66  ArrayRef<uint8_t> &Buffer) override;
67 
68  uint32_t getLength() override;
69 
71 
72  void invalidateCache();
73 
74  uint32_t getBlockSize() const { return BlockSize; }
75  uint32_t getNumBlocks() const { return StreamLayout.Blocks.size(); }
76  uint32_t getStreamLength() const { return StreamLayout.Length; }
77 
78 protected:
81 
82 private:
83  const MSFStreamLayout &getStreamLayout() const { return StreamLayout; }
84  void fixCacheAfterWrite(uint32_t Offset, ArrayRef<uint8_t> Data) const;
85 
87  bool tryReadContiguously(uint32_t Offset, uint32_t Size,
88  ArrayRef<uint8_t> &Buffer);
89 
90  const uint32_t BlockSize;
91  const MSFStreamLayout StreamLayout;
92  BinaryStreamRef MsfData;
93 
95 
96  // We just store the allocator by reference. We use this to allocate
97  // contiguous memory for things like arrays or strings that cross a block
98  // boundary, and this memory is expected to outlive the stream. For example,
99  // someone could create a stream, read some stuff, then close the stream, and
100  // we would like outstanding references to fields to remain valid since the
101  // entire file is mapped anyway. Because of that, the user must supply the
102  // allocator to allocate broken records from.
105 };
106 
108 public:
109  static std::unique_ptr<WritableMappedBlockStream>
110  createStream(uint32_t BlockSize, const MSFStreamLayout &Layout,
112 
113  static std::unique_ptr<WritableMappedBlockStream>
115  uint32_t StreamIndex, BumpPtrAllocator &Allocator);
116 
117  static std::unique_ptr<WritableMappedBlockStream>
118  createDirectoryStream(const MSFLayout &Layout,
119  WritableBinaryStreamRef MsfData,
120  BumpPtrAllocator &Allocator);
121 
122  static std::unique_ptr<WritableMappedBlockStream>
123  createFpmStream(const MSFLayout &Layout, WritableBinaryStreamRef MsfData,
124  BumpPtrAllocator &Allocator, bool AltFpm = false);
125 
126  support::endianness getEndian() const override {
127  return support::little;
128  }
129 
131  ArrayRef<uint8_t> &Buffer) override;
133  ArrayRef<uint8_t> &Buffer) override;
134  uint32_t getLength() override;
135 
136  Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Buffer) override;
137 
138  Error commit() override;
139 
141  return ReadInterface.getStreamLayout();
142  }
143 
144  uint32_t getBlockSize() const { return ReadInterface.getBlockSize(); }
145  uint32_t getNumBlocks() const { return ReadInterface.getNumBlocks(); }
146  uint32_t getStreamLength() const { return ReadInterface.getStreamLength(); }
147 
148 protected:
150  const MSFStreamLayout &StreamLayout,
151  WritableBinaryStreamRef MsfData,
152  BumpPtrAllocator &Allocator);
153 
154 private:
155  MappedBlockStream ReadInterface;
156  WritableBinaryStreamRef WriteInterface;
157 };
158 
159 } // end namespace pdb
160 } // end namespace llvm
161 
162 #endif // LLVM_DEBUGINFO_MSF_MAPPEDBLOCKSTREAM_H
uint32_t getLength() override
Return the number of bytes of data in this stream.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
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:77
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:80
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:35
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:140
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:33
Basic Register Allocator
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
MappedBlockStream(uint32_t BlockSize, const MSFStreamLayout &StreamLayout, BinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
uint32_t Size
Definition: Profile.cpp:46
support::endianness getEndian() const override
MappedBlockStream represents data stored in an MSF file into chunks of a particular size (called the ...
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
A BinaryStream which can be read from as well as written to.
Definition: BinaryStream.h:73