Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:43

0001 //===- MSFBuilder.h - MSF Directory & Metadata Builder ----------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_DEBUGINFO_MSF_MSFBUILDER_H
0010 #define LLVM_DEBUGINFO_MSF_MSFBUILDER_H
0011 
0012 #include "llvm/ADT/ArrayRef.h"
0013 #include "llvm/ADT/BitVector.h"
0014 #include "llvm/ADT/StringRef.h"
0015 #include "llvm/Support/Allocator.h"
0016 #include "llvm/Support/Error.h"
0017 #include <cstdint>
0018 #include <utility>
0019 #include <vector>
0020 
0021 namespace llvm {
0022 class FileBufferByteStream;
0023 namespace msf {
0024 
0025 struct MSFLayout;
0026 
0027 class MSFBuilder {
0028 public:
0029   /// Create a new `MSFBuilder`.
0030   ///
0031   /// \param BlockSize The internal block size used by the PDB file.  See
0032   /// isValidBlockSize() for a list of valid block sizes.
0033   ///
0034   /// \param MinBlockCount Causes the builder to reserve up front space for
0035   /// at least `MinBlockCount` blocks.  This is useful when using `MSFBuilder`
0036   /// to read an existing MSF that you want to write back out later.  The
0037   /// original MSF file's SuperBlock contains the exact number of blocks used
0038   /// by the file, so is a good hint as to how many blocks the new MSF file
0039   /// will contain.  Furthermore, it is actually necessary in this case.  To
0040   /// preserve stability of the file's layout, it is helpful to try to keep
0041   /// all streams mapped to their original block numbers.  To ensure that this
0042   /// is possible, space for all blocks must be allocated beforehand so that
0043   /// streams can be assigned to them.
0044   ///
0045   /// \param CanGrow If true, any operation which results in an attempt to
0046   /// locate a free block when all available blocks have been exhausted will
0047   /// allocate a new block, thereby growing the size of the final MSF file.
0048   /// When false, any such attempt will result in an error.  This is especially
0049   /// useful in testing scenarios when you know your test isn't going to do
0050   /// anything to increase the size of the file, so having an Error returned if
0051   /// it were to happen would catch a programming error
0052   ///
0053   /// \returns an llvm::Error representing whether the operation succeeded or
0054   /// failed.  Currently the only way this can fail is if an invalid block size
0055   /// is specified, or `MinBlockCount` does not leave enough room for the
0056   /// mandatory reserved blocks required by an MSF file.
0057   static Expected<MSFBuilder> create(BumpPtrAllocator &Allocator,
0058                                      uint32_t BlockSize,
0059                                      uint32_t MinBlockCount = 0,
0060                                      bool CanGrow = true);
0061 
0062   /// Request the block map to be at a specific block address.  This is useful
0063   /// when editing a MSF and you want the layout to be as stable as possible.
0064   Error setBlockMapAddr(uint32_t Addr);
0065   Error setDirectoryBlocksHint(ArrayRef<uint32_t> DirBlocks);
0066   void setFreePageMap(uint32_t Fpm);
0067   void setUnknown1(uint32_t Unk1);
0068 
0069   /// Add a stream to the MSF file with the given size, occupying the given
0070   /// list of blocks.  This is useful when reading a MSF file and you want a
0071   /// particular stream to occupy the original set of blocks.  If the given
0072   /// blocks are already allocated, or if the number of blocks specified is
0073   /// incorrect for the given stream size, this function will return an Error.
0074   Expected<uint32_t> addStream(uint32_t Size, ArrayRef<uint32_t> Blocks);
0075 
0076   /// Add a stream to the MSF file with the given size, occupying any available
0077   /// blocks that the builder decides to use.  This is useful when building a
0078   /// new PDB file from scratch and you don't care what blocks a stream occupies
0079   /// but you just want it to work.
0080   Expected<uint32_t> addStream(uint32_t Size);
0081 
0082   /// Update the size of an existing stream.  This will allocate or deallocate
0083   /// blocks as needed to match the requested size.  This can fail if `CanGrow`
0084   /// was set to false when initializing the `MSFBuilder`.
0085   Error setStreamSize(uint32_t Idx, uint32_t Size);
0086 
0087   /// Get the total number of streams in the MSF layout.  This should return 1
0088   /// for every call to `addStream`.
0089   uint32_t getNumStreams() const;
0090 
0091   /// Get the size of a stream by index.
0092   uint32_t getStreamSize(uint32_t StreamIdx) const;
0093 
0094   /// Get the list of blocks allocated to a particular stream.
0095   ArrayRef<uint32_t> getStreamBlocks(uint32_t StreamIdx) const;
0096 
0097   /// Get the total number of blocks that will be allocated to actual data in
0098   /// this MSF file.
0099   uint32_t getNumUsedBlocks() const;
0100 
0101   /// Get the total number of blocks that exist in the MSF file but are not
0102   /// allocated to any valid data.
0103   uint32_t getNumFreeBlocks() const;
0104 
0105   /// Get the total number of blocks in the MSF file.  In practice this is equal
0106   /// to `getNumUsedBlocks() + getNumFreeBlocks()`.
0107   uint32_t getTotalBlockCount() const;
0108 
0109   /// Check whether a particular block is allocated or free.
0110   bool isBlockFree(uint32_t Idx) const;
0111 
0112   /// Finalize the layout and build the headers and structures that describe the
0113   /// MSF layout and can be written directly to the MSF file.
0114   Expected<MSFLayout> generateLayout();
0115 
0116   /// Write the MSF layout to the underlying file.
0117   Expected<FileBufferByteStream> commit(StringRef Path, MSFLayout &Layout);
0118 
0119   BumpPtrAllocator &getAllocator() { return Allocator; }
0120 
0121 private:
0122   MSFBuilder(uint32_t BlockSize, uint32_t MinBlockCount, bool CanGrow,
0123              BumpPtrAllocator &Allocator);
0124 
0125   Error allocateBlocks(uint32_t NumBlocks, MutableArrayRef<uint32_t> Blocks);
0126   uint32_t computeDirectoryByteSize() const;
0127 
0128   using BlockList = std::vector<uint32_t>;
0129 
0130   BumpPtrAllocator &Allocator;
0131 
0132   bool IsGrowable;
0133   uint32_t FreePageMap;
0134   uint32_t Unknown1 = 0;
0135   uint32_t BlockSize;
0136   uint32_t BlockMapAddr;
0137   BitVector FreeBlocks;
0138   std::vector<uint32_t> DirectoryBlocks;
0139   std::vector<std::pair<uint32_t, BlockList>> StreamData;
0140 };
0141 
0142 } // end namespace msf
0143 } // end namespace llvm
0144 
0145 #endif // LLVM_DEBUGINFO_MSF_MSFBUILDER_H