LLVM 22.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"
19#include "llvm/Support/Endian.h"
20#include "llvm/Support/Error.h"
21#include <cstdint>
22#include <memory>
23#include <vector>
24
25namespace llvm {
26namespace msf {
27
28/// MappedBlockStream represents data stored in an MSF file into chunks of a
29/// particular size (called the Block Size), and whose chunks may not be
30/// necessarily contiguous. The arrangement of these chunks MSF the file
31/// is described by some other metadata contained within the MSF file. In
32/// the case of a standard MSF Stream, the layout of the stream's blocks
33/// is described by the MSF "directory", but in the case of the directory
34/// itself, the layout is described by an array at a fixed location within
35/// the MSF. MappedBlockStream provides methods for reading from and writing
36/// to one of these streams transparently, as if it were a contiguous sequence
37/// of bytes.
40
41public:
42 static std::unique_ptr<MappedBlockStream>
43 createStream(uint32_t BlockSize, const MSFStreamLayout &Layout,
44 BinaryStreamRef MsfData, BumpPtrAllocator &Allocator);
45
46 static std::unique_ptr<MappedBlockStream>
47 createIndexedStream(const MSFLayout &Layout, BinaryStreamRef MsfData,
48 uint32_t StreamIndex, BumpPtrAllocator &Allocator);
49
50 static std::unique_ptr<MappedBlockStream>
51 createFpmStream(const MSFLayout &Layout, BinaryStreamRef MsfData,
52 BumpPtrAllocator &Allocator);
53
54 static std::unique_ptr<MappedBlockStream>
55 createDirectoryStream(const MSFLayout &Layout, BinaryStreamRef MsfData,
56 BumpPtrAllocator &Allocator);
57
58 llvm::endianness getEndian() const override {
60 }
61
63 ArrayRef<uint8_t> &Buffer) override;
64 Error readLongestContiguousChunk(uint64_t Offset,
65 ArrayRef<uint8_t> &Buffer) override;
66
67 uint64_t getLength() override;
68
69 BumpPtrAllocator &getAllocator() { return Allocator; }
70
71 void invalidateCache();
72
73 uint32_t getBlockSize() const { return BlockSize; }
74 uint32_t getNumBlocks() const { return StreamLayout.Blocks.size(); }
75 uint32_t getStreamLength() const { return StreamLayout.Length; }
76
77protected:
80
81private:
82 const MSFStreamLayout &getStreamLayout() const { return StreamLayout; }
83 void fixCacheAfterWrite(uint64_t Offset, ArrayRef<uint8_t> Data) const;
84
86 bool tryReadContiguously(uint64_t Offset, uint64_t Size,
87 ArrayRef<uint8_t> &Buffer);
88
89 const uint32_t BlockSize;
90 const MSFStreamLayout StreamLayout;
91 BinaryStreamRef MsfData;
92
93 using CacheEntry = MutableArrayRef<uint8_t>;
94
95 // We just store the allocator by reference. We use this to allocate
96 // contiguous memory for things like arrays or strings that cross a block
97 // boundary, and this memory is expected to outlive the stream. For example,
98 // someone could create a stream, read some stuff, then close the stream, and
99 // we would like outstanding references to fields to remain valid since the
100 // entire file is mapped anyway. Because of that, the user must supply the
101 // allocator to allocate broken records from.
104};
105
107public:
108 static std::unique_ptr<WritableMappedBlockStream>
111
112 static std::unique_ptr<WritableMappedBlockStream>
114 uint32_t StreamIndex, BumpPtrAllocator &Allocator);
115
116 static std::unique_ptr<WritableMappedBlockStream>
117 createDirectoryStream(const MSFLayout &Layout,
120
121 static std::unique_ptr<WritableMappedBlockStream>
123 BumpPtrAllocator &Allocator, bool AltFpm = false);
124
125 llvm::endianness getEndian() const override {
127 }
128
129 Error readBytes(uint64_t Offset, uint64_t Size,
130 ArrayRef<uint8_t> &Buffer) override;
131 Error readLongestContiguousChunk(uint64_t Offset,
132 ArrayRef<uint8_t> &Buffer) override;
133 uint64_t getLength() override;
134
135 Error writeBytes(uint64_t Offset, ArrayRef<uint8_t> Buffer) override;
136
137 Error commit() override;
138
140 return ReadInterface.getStreamLayout();
141 }
142
143 uint32_t getBlockSize() const { return ReadInterface.getBlockSize(); }
144 uint32_t getNumBlocks() const { return ReadInterface.getNumBlocks(); }
145 uint32_t getStreamLength() const { return ReadInterface.getStreamLength(); }
146
147protected:
149 const MSFStreamLayout &StreamLayout,
152
153private:
154 MappedBlockStream ReadInterface;
155 WritableBinaryStreamRef WriteInterface;
156};
157
158} // namespace msf
159} // end namespace llvm
160
161#endif // LLVM_DEBUGINFO_MSF_MAPPEDBLOCKSTREAM_H
This file defines the BumpPtrAllocator interface.
#define LLVM_ABI
Definition Compiler.h:213
This file defines the DenseMap class.
Basic Register Allocator
static const int BlockSize
Definition TarWriter.cpp:33
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
An interface for accessing data in a stream-like format, but which discourages copying.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition ArrayRef.h:303
A BinaryStream which can be read from as well as written to.
Describes the layout of a stream in an MSF layout.
Definition MSFCommon.h:78
MappedBlockStream represents data stored in an MSF file into chunks of a particular size (called the ...
static std::unique_ptr< MappedBlockStream > createIndexedStream(const MSFLayout &Layout, BinaryStreamRef MsfData, uint32_t StreamIndex, BumpPtrAllocator &Allocator)
BumpPtrAllocator & getAllocator()
static std::unique_ptr< MappedBlockStream > createStream(uint32_t BlockSize, const MSFStreamLayout &Layout, BinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
llvm::endianness getEndian() const override
MappedBlockStream(uint32_t BlockSize, const MSFStreamLayout &StreamLayout, BinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
static std::unique_ptr< MappedBlockStream > createDirectoryStream(const MSFLayout &Layout, BinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
static std::unique_ptr< MappedBlockStream > createFpmStream(const MSFLayout &Layout, BinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
static std::unique_ptr< WritableMappedBlockStream > createIndexedStream(const MSFLayout &Layout, WritableBinaryStreamRef MsfData, uint32_t StreamIndex, BumpPtrAllocator &Allocator)
static std::unique_ptr< WritableMappedBlockStream > createStream(uint32_t BlockSize, const MSFStreamLayout &Layout, WritableBinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
WritableMappedBlockStream(uint32_t BlockSize, const MSFStreamLayout &StreamLayout, WritableBinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
static std::unique_ptr< WritableMappedBlockStream > createFpmStream(const MSFLayout &Layout, WritableBinaryStreamRef MsfData, BumpPtrAllocator &Allocator, bool AltFpm=false)
static std::unique_ptr< WritableMappedBlockStream > createDirectoryStream(const MSFLayout &Layout, WritableBinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
const MSFStreamLayout & getStreamLayout() const
llvm::endianness getEndian() const override
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
BumpPtrAllocatorImpl BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
endianness
Definition bit.h:71