LLVM 20.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"
18#include "llvm/Support/Endian.h"
19#include "llvm/Support/Error.h"
20#include <cstdint>
21#include <memory>
22#include <vector>
23
24namespace llvm {
25namespace 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
40public:
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 llvm::endianness getEndian() const override {
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
76protected:
79
80private:
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
106public:
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,
119
120 static std::unique_ptr<WritableMappedBlockStream>
122 BumpPtrAllocator &Allocator, bool AltFpm = false);
123
124 llvm::endianness getEndian() const override {
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
146protected:
148 const MSFStreamLayout &StreamLayout,
151
152private:
153 MappedBlockStream ReadInterface;
154 WritableBinaryStreamRef WriteInterface;
155};
156
157} // namespace msf
158} // end namespace llvm
159
160#endif // LLVM_DEBUGINFO_MSF_MAPPEDBLOCKSTREAM_H
This file defines the BumpPtrAllocator interface.
This file defines the DenseMap class.
uint64_t Size
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.
Definition: BinaryStream.h:34
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:66
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:307
A BinaryStream which can be read from as well as written to.
Definition: BinaryStream.h:72
Describes the layout of a stream in an MSF layout.
Definition: MSFCommon.h:77
std::vector< support::ulittle32_t > Blocks
Definition: MSFCommon.h:80
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)
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...
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.
BumpPtrAllocator & getAllocator()
static std::unique_ptr< MappedBlockStream > createStream(uint32_t BlockSize, const MSFStreamLayout &Layout, BinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
llvm::endianness getEndian() const override
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)
uint64_t getLength() override
Return the number of bytes of data in this stream.
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)
Error commit() override
For buffered streams, commits changes to the backing store.
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...
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)
uint64_t getLength() override
Return the number of bytes of data in this stream.
const MSFStreamLayout & getStreamLayout() const
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.
Error writeBytes(uint64_t Offset, ArrayRef< uint8_t > Buffer) override
Attempt to write the given bytes into the stream at the desired offset.
llvm::endianness getEndian() const override
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
endianness
Definition: bit.h:70