LLVM  14.0.0git
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 /// MappedBlockStream represents data stored in an MSF file into chunks of a
28 /// particular size (called the Block Size), and whose chunks may not be
29 /// necessarily contiguous. The arrangement of these chunks MSF the file
30 /// is described by some other metadata contained within the MSF file. In
31 /// the case of a standard MSF Stream, the layout of the stream's blocks
32 /// is described by the MSF "directory", but in the case of the directory
33 /// itself, the layout is described by an array at a fixed location within
34 /// the MSF. MappedBlockStream provides methods for reading from and writing
35 /// to one of these streams transparently, as if it were a contiguous sequence
36 /// of bytes.
39 
40 public:
41  static std::unique_ptr<MappedBlockStream>
44 
45  static std::unique_ptr<MappedBlockStream>
46  createIndexedStream(const MSFLayout &Layout, BinaryStreamRef MsfData,
47  uint32_t StreamIndex, BumpPtrAllocator &Allocator);
48 
49  static std::unique_ptr<MappedBlockStream>
50  createFpmStream(const MSFLayout &Layout, BinaryStreamRef MsfData,
52 
53  static std::unique_ptr<MappedBlockStream>
54  createDirectoryStream(const MSFLayout &Layout, BinaryStreamRef MsfData,
56 
57  support::endianness getEndian() const override {
58  return support::little;
59  }
60 
62  ArrayRef<uint8_t> &Buffer) override;
64  ArrayRef<uint8_t> &Buffer) override;
65 
66  uint64_t getLength() override;
67 
69 
70  void invalidateCache();
71 
72  uint32_t getBlockSize() const { return BlockSize; }
73  uint32_t getNumBlocks() const { return StreamLayout.Blocks.size(); }
74  uint32_t getStreamLength() const { return StreamLayout.Length; }
75 
76 protected:
79 
80 private:
81  const MSFStreamLayout &getStreamLayout() const { return StreamLayout; }
82  void fixCacheAfterWrite(uint64_t Offset, ArrayRef<uint8_t> Data) const;
83 
85  bool tryReadContiguously(uint64_t Offset, uint64_t Size,
86  ArrayRef<uint8_t> &Buffer);
87 
88  const uint32_t BlockSize;
89  const MSFStreamLayout StreamLayout;
90  BinaryStreamRef MsfData;
91 
92  using CacheEntry = MutableArrayRef<uint8_t>;
93 
94  // We just store the allocator by reference. We use this to allocate
95  // contiguous memory for things like arrays or strings that cross a block
96  // boundary, and this memory is expected to outlive the stream. For example,
97  // someone could create a stream, read some stuff, then close the stream, and
98  // we would like outstanding references to fields to remain valid since the
99  // entire file is mapped anyway. Because of that, the user must supply the
100  // allocator to allocate broken records from.
103 };
104 
106 public:
107  static std::unique_ptr<WritableMappedBlockStream>
110 
111  static std::unique_ptr<WritableMappedBlockStream>
113  uint32_t StreamIndex, BumpPtrAllocator &Allocator);
114 
115  static std::unique_ptr<WritableMappedBlockStream>
116  createDirectoryStream(const MSFLayout &Layout,
117  WritableBinaryStreamRef MsfData,
119 
120  static std::unique_ptr<WritableMappedBlockStream>
121  createFpmStream(const MSFLayout &Layout, WritableBinaryStreamRef MsfData,
122  BumpPtrAllocator &Allocator, bool AltFpm = false);
123 
124  support::endianness getEndian() const override {
125  return support::little;
126  }
127 
129  ArrayRef<uint8_t> &Buffer) override;
131  ArrayRef<uint8_t> &Buffer) override;
132  uint64_t getLength() override;
133 
135 
136  Error commit() override;
137 
139  return ReadInterface.getStreamLayout();
140  }
141 
142  uint32_t getBlockSize() const { return ReadInterface.getBlockSize(); }
143  uint32_t getNumBlocks() const { return ReadInterface.getNumBlocks(); }
144  uint32_t getStreamLength() const { return ReadInterface.getStreamLength(); }
145 
146 protected:
148  const MSFStreamLayout &StreamLayout,
149  WritableBinaryStreamRef MsfData,
151 
152 private:
153  MappedBlockStream ReadInterface;
154  WritableBinaryStreamRef WriteInterface;
155 };
156 
157 } // end namespace pdb
158 } // end namespace llvm
159 
160 #endif // LLVM_DEBUGINFO_MSF_MAPPEDBLOCKSTREAM_H
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm::msf::WritableMappedBlockStream::readLongestContiguousChunk
Error readLongestContiguousChunk(uint64_t Offset, ArrayRef< uint8_t > &Buffer) override
Given an offset into the stream, read as much as possible without copying any data.
Definition: MappedBlockStream.cpp:378
llvm::msf::WritableMappedBlockStream::WritableMappedBlockStream
WritableMappedBlockStream(uint32_t BlockSize, const MSFStreamLayout &StreamLayout, WritableBinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
Definition: MappedBlockStream.cpp:310
llvm::BinaryStream
An interface for accessing data in a stream-like format, but which discourages copying.
Definition: BinaryStream.h:35
llvm::msf::MSFStreamLayout::Blocks
std::vector< support::ulittle32_t > Blocks
Definition: MSFCommon.h:80
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
BlockSize
static const int BlockSize
Definition: TarWriter.cpp:33
llvm::msf::MappedBlockStream::createDirectoryStream
static std::unique_ptr< MappedBlockStream > createDirectoryStream(const MSFLayout &Layout, BinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
Definition: MappedBlockStream.cpp:71
llvm::msf::WritableMappedBlockStream::getLength
uint64_t getLength() override
Return the number of bytes of data in this stream.
Definition: MappedBlockStream.cpp:383
BinaryStream.h
llvm::msf::WritableMappedBlockStream::getStreamLayout
const MSFStreamLayout & getStreamLayout() const
Definition: MappedBlockStream.h:138
llvm::msf::WritableMappedBlockStream::getNumBlocks
uint32_t getNumBlocks() const
Definition: MappedBlockStream.h:143
Allocator.h
Error.h
DenseMap.h
llvm::msf::WritableMappedBlockStream
Definition: MappedBlockStream.h:105
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::msf::MappedBlockStream::readBytes
Error readBytes(uint64_t Offset, uint64_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...
Definition: MappedBlockStream.cpp:88
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::msf::MappedBlockStream::invalidateCache
void invalidateCache()
Definition: MappedBlockStream.cpp:272
llvm::msf::WritableMappedBlockStream::commit
Error commit() override
For buffered streams, commits changes to the backing store.
Definition: MappedBlockStream.cpp:421
llvm::MutableArrayRef< uint8_t >
llvm::msf::WritableMappedBlockStream::createFpmStream
static std::unique_ptr< WritableMappedBlockStream > createFpmStream(const MSFLayout &Layout, WritableBinaryStreamRef MsfData, BumpPtrAllocator &Allocator, bool AltFpm=false)
Definition: MappedBlockStream.cpp:348
llvm::msf::MappedBlockStream
MappedBlockStream represents data stored in an MSF file into chunks of a particular size (called the ...
Definition: MappedBlockStream.h:37
llvm::support::little
@ little
Definition: Endian.h:27
llvm::msf::MSFLayout
Definition: MSFCommon.h:51
llvm::msf::MappedBlockStream::getStreamLength
uint32_t getStreamLength() const
Definition: MappedBlockStream.h:74
llvm::msf::WritableMappedBlockStream::createStream
static std::unique_ptr< WritableMappedBlockStream > createStream(uint32_t BlockSize, const MSFStreamLayout &Layout, WritableBinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
Definition: MappedBlockStream.cpp:317
llvm::msf::WritableMappedBlockStream::readBytes
Error readBytes(uint64_t Offset, uint64_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...
Definition: MappedBlockStream.cpp:373
uint64_t
llvm::msf::WritableMappedBlockStream::createIndexedStream
static std::unique_ptr< WritableMappedBlockStream > createIndexedStream(const MSFLayout &Layout, WritableBinaryStreamRef MsfData, uint32_t StreamIndex, BumpPtrAllocator &Allocator)
Definition: MappedBlockStream.cpp:326
llvm::BumpPtrAllocatorImpl
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:67
llvm::msf::WritableMappedBlockStream::writeBytes
Error writeBytes(uint64_t Offset, ArrayRef< uint8_t > Buffer) override
Attempt to write the given bytes into the stream at the desired offset.
Definition: MappedBlockStream.cpp:387
llvm::msf::MappedBlockStream::getEndian
support::endianness getEndian() const override
Definition: MappedBlockStream.h:57
llvm::DenseMap
Definition: DenseMap.h:714
llvm::msf::MappedBlockStream::getAllocator
BumpPtrAllocator & getAllocator()
Definition: MappedBlockStream.h:68
ArrayRef.h
llvm::msf::MappedBlockStream::getNumBlocks
uint32_t getNumBlocks() const
Definition: MappedBlockStream.h:73
llvm::ArrayRef< uint8_t >
BinaryStreamRef.h
uint32_t
MSFCommon.h
llvm::msf::MSFStreamLayout::Length
uint32_t Length
Definition: MSFCommon.h:79
llvm::msf::MappedBlockStream::getBlockSize
uint32_t getBlockSize() const
Definition: MappedBlockStream.h:72
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::msf::WritableMappedBlockStream::getEndian
support::endianness getEndian() const override
Definition: MappedBlockStream.h:124
llvm::msf::MappedBlockStream::MappedBlockStream
MappedBlockStream(uint32_t BlockSize, const MSFStreamLayout &StreamLayout, BinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
Definition: MappedBlockStream.cpp:45
llvm::msf::MappedBlockStream::createIndexedStream
static std::unique_ptr< MappedBlockStream > createIndexedStream(const MSFLayout &Layout, BinaryStreamRef MsfData, uint32_t StreamIndex, BumpPtrAllocator &Allocator)
Definition: MappedBlockStream.cpp:59
llvm::msf::WritableMappedBlockStream::getStreamLength
uint32_t getStreamLength() const
Definition: MappedBlockStream.h:144
llvm::msf::MappedBlockStream::createStream
static std::unique_ptr< MappedBlockStream > createStream(uint32_t BlockSize, const MSFStreamLayout &Layout, BinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
Definition: MappedBlockStream.cpp:52
Allocator
Basic Register Allocator
Definition: RegAllocBasic.cpp:146
llvm::msf::MappedBlockStream::readLongestContiguousChunk
Error readLongestContiguousChunk(uint64_t Offset, ArrayRef< uint8_t > &Buffer) override
Given an offset into the stream, read as much as possible without copying any data.
Definition: MappedBlockStream.cpp:166
llvm::msf::WritableMappedBlockStream::createDirectoryStream
static std::unique_ptr< WritableMappedBlockStream > createDirectoryStream(const MSFLayout &Layout, WritableBinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
Definition: MappedBlockStream.cpp:338
llvm::msf::MappedBlockStream::createFpmStream
static std::unique_ptr< MappedBlockStream > createFpmStream(const MSFLayout &Layout, BinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
Definition: MappedBlockStream.cpp:81
llvm::support::endianness
endianness
Definition: Endian.h:27
Endian.h
llvm::WritableBinaryStream
A BinaryStream which can be read from as well as written to.
Definition: BinaryStream.h:73
llvm::msf::MappedBlockStream::getLength
uint64_t getLength() override
Return the number of bytes of data in this stream.
Definition: MappedBlockStream.cpp:196
llvm::WritableBinaryStreamRef
Definition: BinaryStreamRef.h:221
llvm::BinaryStreamRef
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
Definition: BinaryStreamRef.h:156
llvm::msf::WritableMappedBlockStream::getBlockSize
uint32_t getBlockSize() const
Definition: MappedBlockStream.h:142
llvm::msf::MSFStreamLayout
Describes the layout of a stream in an MSF layout.
Definition: MSFCommon.h:77