46 uint32_t MinBlockCount,
bool CanGrow) {
49 "The requested block size is unsupported");
51 MSFBuilder Builder(BlockSize, Allocator);
52 if (
auto EC = Builder.growFreeBlocks(
56 Builder.FreeBlocks.reset(Builder.BlockMapAddr);
57 Builder.IsGrowable = CanGrow;
62 if (Addr == BlockMapAddr)
65 if (
auto EC = growFreeBlocks(
uint64_t(Addr) + 1))
71 "Requested block map address is already in use");
72 FreeBlocks[BlockMapAddr] =
true;
73 FreeBlocks[Addr] =
false;
83 for (
auto B : DirectoryBlocks)
85 for (
auto B : DirBlocks) {
88 "Attempt to reuse an allocated block");
90 FreeBlocks[
B] =
false;
93 DirectoryBlocks = DirBlocks;
97Error MSFBuilder::growFreeBlocks(uint64_t MinBlockCount,
99 const uint64_t OldBlockCount = FreeBlocks.
size();
100 uint64_t EndForFreeBlocks = OldBlockCount + MinNewFreeBlocks;
101 uint64_t NewBlockCount = std::max(MinBlockCount, EndForFreeBlocks);
102 if (NewBlockCount == OldBlockCount)
107 "Cannot grow the number of blocks");
109 const uint64_t FirstFpm = OldBlockCount == 0
113 constexpr uint64_t MaxBlockCount = std::numeric_limits<int>::max();
116 for (uint64_t Fpm = FirstFpm;
117 Fpm < NewBlockCount && NewBlockCount <= MaxBlockCount;
119 EndForFreeBlocks += 2;
120 NewBlockCount = std::max({NewBlockCount, Fpm + 2, EndForFreeBlocks});
122 if (NewBlockCount > MaxBlockCount)
124 "The requested number of blocks is too large");
129 FreeBlocks.
reset(Fpm, Fpm + 2);
133Error MSFBuilder::allocateBlocks(uint32_t NumBlocks,
138 uint32_t NumFreeBlocks = FreeBlocks.count();
139 if (NumFreeBlocks < NumBlocks) {
140 if (
auto EC = growFreeBlocks(FreeBlocks.size(), NumBlocks - NumFreeBlocks))
145 int Block = FreeBlocks.find_first();
149 uint32_t NextBlock =
static_cast<uint32_t
>(
Block);
150 Blocks[
I++] = NextBlock;
151 FreeBlocks.reset(NextBlock);
153 }
while (--NumBlocks > 0);
173 if (ReqBlocks != Blocks.
size())
176 "Incorrect number of blocks for requested stream size");
177 for (
auto Block : Blocks) {
178 if (
Block >= FreeBlocks.size()) {
180 return std::move(EC);
183 if (!FreeBlocks.test(
Block))
186 "Attempt to re-use an already allocated block");
189 for (
auto Block : Blocks) {
190 FreeBlocks.reset(
Block);
192 StreamData.push_back(std::make_pair(
Size, Blocks));
193 return StreamData.size() - 1;
198 std::vector<uint32_t> NewBlocks;
199 NewBlocks.resize(ReqBlocks);
200 if (
auto EC = allocateBlocks(ReqBlocks, NewBlocks))
201 return std::move(EC);
202 StreamData.push_back(std::make_pair(
Size, NewBlocks));
203 return StreamData.size() - 1;
214 if (NewBlocks > OldBlocks) {
215 uint32_t AddedBlocks = NewBlocks - OldBlocks;
217 std::vector<uint32_t> AddedBlockList;
218 AddedBlockList.resize(AddedBlocks);
219 if (
auto EC = allocateBlocks(AddedBlocks, AddedBlockList))
221 auto &CurrentBlocks = StreamData[Idx].second;
223 }
else if (OldBlocks > NewBlocks) {
226 uint32_t RemovedBlocks = OldBlocks - NewBlocks;
228 auto RemovedBlockList = CurrentBlocks.drop_front(NewBlocks);
229 for (
auto P : RemovedBlockList)
230 FreeBlocks[
P] =
true;
231 StreamData[Idx].second = CurrentBlocks.drop_back(RemovedBlocks);
234 StreamData[Idx].first =
Size;
241 return StreamData[StreamIdx].first;
245 return StreamData[StreamIdx].second;
248uint32_t MSFBuilder::computeDirectoryByteSize()
const {
255 for (
const auto &
D : StreamData) {
257 assert(ExpectedNumBlocks ==
D.second.size() &&
258 "Unexpected number of blocks");
279 if (NumDirectoryBlocks > DirectoryBlocks.size()) {
282 std::vector<uint32_t> ExtraBlocks;
283 uint32_t NumExtraBlocks = NumDirectoryBlocks - DirectoryBlocks.size();
284 ExtraBlocks.resize(NumExtraBlocks);
285 if (
auto EC = allocateBlocks(NumExtraBlocks, ExtraBlocks))
286 return std::move(EC);
288 }
else if (NumDirectoryBlocks < DirectoryBlocks.size()) {
289 uint32_t NumUnnecessaryBlocks = DirectoryBlocks.size() - NumDirectoryBlocks;
292 FreeBlocks[
B] =
true;
293 DirectoryBlocks.resize(NumDirectoryBlocks);
307 if (!StreamData.empty()) {
310 L.StreamMap.resize(StreamData.size());
312 Sizes[
I] = StreamData[
I].first;
314 Allocator.Allocate<
ulittle32_t>(StreamData[
I].second.size());
321 L.FreePageMap = FreeBlocks;
337 while (BI < Layout.SB->NumBlocks) {
357 return L.takeError();
359 Layout = std::move(*L);
379 formatv(
"File size {0,1:N} too large for current PDB page size {1}",
383 uint64_t NumDirectoryBlocks =
385 uint64_t DirectoryBlockMapSize =
387 if (DirectoryBlockMapSize > Layout.
SB->
BlockSize) {
389 formatv(
"The directory block map ({0} bytes) "
390 "doesn't fit in a block ({1} bytes)",
391 DirectoryBlockMapSize,
396 if (
auto EC = OutFileOrError.takeError())
397 return std::move(EC);
404 return std::move(EC);
412 return std::move(EC);
415 Layout, Buffer, Allocator);
418 return std::move(EC);
421 return std::move(EC);
423 for (
const auto &Blocks : Layout.
StreamMap) {
425 return std::move(EC);
428 return std::move(Buffer);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static const uint32_t kFreePageMap1Block
static void commitFpm(WritableBinaryStream &MsfBuffer, const MSFLayout &Layout, BumpPtrAllocator &Allocator)
static const uint32_t kSuperBlockBlock
static const uint32_t kFreePageMap0Block
static const uint32_t kDefaultBlockMapAddr
static const uint32_t kNumReservedPages
static const uint32_t kDefaultFreePageMap
static const int BlockSize
Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
Get the array size.
Provides write only access to a subclass of WritableBinaryStream.
Error writeArray(ArrayRef< T > Array)
Writes an array of objects of type T to the underlying stream, as if by using memcpy.
Error writeInteger(T Value)
Write the integer Value to the underlying stream in the specified endianness.
uint64_t bytesRemaining() const
void setOffset(uint64_t Off)
Error writeObject(const T &Obj)
Writes the object Obj to the underlying stream, as if by using memcpy.
bool test(unsigned Idx) const
Returns true if bit Idx is set.
BitVector & reset()
Reset all bits in the bitvector.
void resize(unsigned N, bool t=false)
Grow or shrink the bitvector.
size_type size() const
Returns the number of bits in this bitvector.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
An implementation of WritableBinaryStream backed by an llvm FileOutputBuffer.
static LLVM_ABI Expected< std::unique_ptr< FileOutputBuffer > > create(StringRef FilePath, size_t Size, unsigned Flags=0)
Factory method to create an OutputBuffer object which manages a read/write buffer of the specified si...
Represent a constant reference to a string, i.e.
The TimeTraceScope is a helper class to call the begin and end functions of the time trace profiler.
A BinaryStream which can be read from as well as written to.
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.
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.
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)
uint32_t getMinimumBlockCount()
uint64_t blockToOffset(uint64_t BlockNumber, uint64_t BlockSize)
uint64_t getMaxFileSizeFromBlockSize(uint32_t Size)
Given the specified block size, returns the maximum possible file size.
bool isValidBlockSize(uint32_t Size)
@ stream_directory_overflow
uint64_t bytesToBlocks(uint64_t NumBytes, uint64_t BlockSize)
static const char Magic[]
detail::packed_endian_specific_integral< uint32_t, llvm::endianness::little, unaligned > ulittle32_t
This is an optimization pass for GlobalISel generic memory operations.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
auto uninitialized_copy(R &&Src, IterTy Dst)
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
MutableArrayRef(T &OneElt) -> MutableArrayRef< T >
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
ArrayRef(const T &OneElt) -> ArrayRef< T >
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
ArrayRef< support::ulittle32_t > StreamSizes
ArrayRef< support::ulittle32_t > DirectoryBlocks
std::vector< ArrayRef< support::ulittle32_t > > StreamMap
support::ulittle32_t NumBlocks
support::ulittle32_t BlockSize
support::ulittle32_t Unknown1
char MagicBytes[sizeof(Magic)]
support::ulittle32_t NumDirectoryBytes
support::ulittle32_t BlockMapAddr
support::ulittle32_t FreeBlockMapBlock