LLVM 24.0.0git
MSFBuilder.h
Go to the documentation of this file.
1//===- MSFBuilder.h - MSF Directory & Metadata Builder ----------*- 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_MSFBUILDER_H
10#define LLVM_DEBUGINFO_MSF_MSFBUILDER_H
11
12#include "llvm/ADT/ArrayRef.h"
13#include "llvm/ADT/BitVector.h"
14#include "llvm/ADT/StringRef.h"
17#include "llvm/Support/Error.h"
18#include <cstdint>
19#include <utility>
20#include <vector>
21
22namespace llvm {
24namespace msf {
25
26struct MSFLayout;
27
28class MSFBuilder {
29public:
30 /// Create a new `MSFBuilder`.
31 ///
32 /// \param BlockSize The internal block size used by the PDB file. See
33 /// isValidBlockSize() for a list of valid block sizes.
34 ///
35 /// \param MinBlockCount Causes the builder to reserve up front space for
36 /// at least `MinBlockCount` blocks. This is useful when using `MSFBuilder`
37 /// to read an existing MSF that you want to write back out later. The
38 /// original MSF file's SuperBlock contains the exact number of blocks used
39 /// by the file, so is a good hint as to how many blocks the new MSF file
40 /// will contain. Furthermore, it is actually necessary in this case. To
41 /// preserve stability of the file's layout, it is helpful to try to keep
42 /// all streams mapped to their original block numbers. To ensure that this
43 /// is possible, space for all blocks must be allocated beforehand so that
44 /// streams can be assigned to them.
45 ///
46 /// \param CanGrow Whether operations may increase the block count after
47 /// initial preallocation. When false, requests for blocks beyond the
48 /// preallocated range or for more free blocks than remain return an error.
49 ///
50 /// \returns an llvm::Error if the block size is invalid or the initial block
51 /// count, including any reserved Free Page Map blocks, is too large.
53 uint32_t BlockSize,
54 uint32_t MinBlockCount = 0,
55 bool CanGrow = true);
56
57 /// Request the block map to be at a specific block address. This is useful
58 /// when editing a MSF and you want the layout to be as stable as possible.
62 LLVM_ABI void setUnknown1(uint32_t Unk1);
63
64 /// Add a stream to the MSF file with the given size, occupying the given
65 /// list of blocks. This is useful when reading a MSF file and you want a
66 /// particular stream to occupy the original set of blocks. If the given
67 /// blocks are already allocated, outside the preallocated range when
68 /// `CanGrow` is false, or insufficient or excessive for the given stream
69 /// size, this function will return an Error.
71 ArrayRef<uint32_t> Blocks);
72
73 /// Add a stream to the MSF file with the given size, occupying any available
74 /// blocks that the builder decides to use. This is useful when building a
75 /// new PDB file from scratch and you don't care what blocks a stream occupies
76 /// but you just want it to work.
78
79 /// Update the size of an existing stream. This will allocate or deallocate
80 /// blocks as needed to match the requested size. This can fail if `CanGrow`
81 /// was set to false when initializing the `MSFBuilder`.
83
84 /// Get the total number of streams in the MSF layout. This should return 1
85 /// for every call to `addStream`.
87
88 /// Get the size of a stream by index.
89 LLVM_ABI uint32_t getStreamSize(uint32_t StreamIdx) const;
90
91 /// Get the list of blocks allocated to a particular stream.
93
94 /// Get the total number of blocks that will be allocated to actual data in
95 /// this MSF file.
97
98 /// Get the total number of blocks that exist in the MSF file but are not
99 /// allocated to any valid data.
101
102 /// Get the total number of blocks in the MSF file. In practice this is equal
103 /// to `getNumUsedBlocks() + getNumFreeBlocks()`.
105
106 /// Check whether a particular block is allocated or free.
107 LLVM_ABI bool isBlockFree(uint32_t Idx) const;
108
109 /// Finalize the layout and build the headers and structures that describe the
110 /// MSF layout and can be written directly to the MSF file.
112
113 /// Write the MSF layout to the underlying file.
115 MSFLayout &Layout);
116
117 BumpPtrAllocator &getAllocator() { return Allocator; }
118
119private:
121
122 // Grow to the requested size and leave at least MinNewFreeBlocks newly free.
123 Error growFreeBlocks(uint64_t MinBlockCount, uint32_t MinNewFreeBlocks = 0);
124 Error allocateBlocks(uint32_t NumBlocks, MutableArrayRef<uint32_t> Blocks);
125 uint32_t computeDirectoryByteSize() const;
126
127 using BlockList = std::vector<uint32_t>;
128
130
131 bool IsGrowable = true;
132 uint32_t FreePageMap;
133 uint32_t Unknown1 = 0;
135 uint32_t BlockMapAddr;
136 BitVector FreeBlocks;
137 std::vector<uint32_t> DirectoryBlocks;
138 std::vector<std::pair<uint32_t, BlockList>> StreamData;
139};
140
141} // end namespace msf
142} // end namespace llvm
143
144#endif // LLVM_DEBUGINFO_MSF_MSFBUILDER_H
uint64_t uint64_t
This file defines the BumpPtrAllocator interface.
This file implements the BitVector class.
#define LLVM_ABI
Definition Compiler.h:211
Basic Register Allocator
static const int BlockSize
Definition TarWriter.cpp:33
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
An implementation of WritableBinaryStream backed by an llvm FileOutputBuffer.
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:294
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
LLVM_ABI uint32_t getNumStreams() const
Get the total number of streams in the MSF layout.
LLVM_ABI Error setBlockMapAddr(uint32_t Addr)
Request the block map to be at a specific block address.
LLVM_ABI ArrayRef< uint32_t > getStreamBlocks(uint32_t StreamIdx) const
Get the list of blocks allocated to a particular stream.
LLVM_ABI Error setDirectoryBlocksHint(ArrayRef< uint32_t > DirBlocks)
LLVM_ABI uint32_t getTotalBlockCount() const
Get the total number of blocks in the MSF file.
LLVM_ABI uint32_t getNumFreeBlocks() const
Get the total number of blocks that exist in the MSF file but are not allocated to any valid data.
BumpPtrAllocator & getAllocator()
Definition MSFBuilder.h:117
LLVM_ABI Error setStreamSize(uint32_t Idx, uint32_t Size)
Update the size of an existing stream.
LLVM_ABI Expected< FileBufferByteStream > commit(StringRef Path, MSFLayout &Layout)
Write the MSF layout to the underlying file.
LLVM_ABI Expected< MSFLayout > generateLayout()
Finalize the layout and build the headers and structures that describe the MSF layout and can be writ...
LLVM_ABI bool isBlockFree(uint32_t Idx) const
Check whether a particular block is allocated or free.
LLVM_ABI void setFreePageMap(uint32_t Fpm)
LLVM_ABI uint32_t getStreamSize(uint32_t StreamIdx) const
Get the size of a stream by index.
static LLVM_ABI Expected< MSFBuilder > create(BumpPtrAllocator &Allocator, uint32_t BlockSize, uint32_t MinBlockCount=0, bool CanGrow=true)
Create a new MSFBuilder.
LLVM_ABI uint32_t getNumUsedBlocks() const
Get the total number of blocks that will be allocated to actual data in this MSF file.
LLVM_ABI void setUnknown1(uint32_t Unk1)
LLVM_ABI Expected< uint32_t > addStream(uint32_t Size, ArrayRef< uint32_t > Blocks)
Add a stream to the MSF file with the given size, occupying the given list of blocks.
This is an optimization pass for GlobalISel generic memory operations.
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:391