Eugene Zelenko | 2db0cfa | 2017-06-23 21:57:40 +0000 | [diff] [blame] | 1 | //===- MSFBuilder.cpp -----------------------------------------------------===// |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Eugene Zelenko | 2db0cfa | 2017-06-23 21:57:40 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/ArrayRef.h" |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 11 | #include "llvm/DebugInfo/MSF/MSFBuilder.h" |
| 12 | #include "llvm/DebugInfo/MSF/MSFError.h" |
Eugene Zelenko | 2db0cfa | 2017-06-23 21:57:40 +0000 | [diff] [blame] | 13 | #include "llvm/Support/Endian.h" |
| 14 | #include "llvm/Support/Error.h" |
| 15 | #include <algorithm> |
| 16 | #include <cassert> |
| 17 | #include <cstdint> |
| 18 | #include <cstring> |
| 19 | #include <memory> |
| 20 | #include <utility> |
| 21 | #include <vector> |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace llvm; |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 24 | using namespace llvm::msf; |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 25 | using namespace llvm::support; |
| 26 | |
Eugene Zelenko | 2db0cfa | 2017-06-23 21:57:40 +0000 | [diff] [blame] | 27 | static const uint32_t kSuperBlockBlock = 0; |
| 28 | static const uint32_t kFreePageMap0Block = 1; |
| 29 | static const uint32_t kFreePageMap1Block = 2; |
| 30 | static const uint32_t kNumReservedPages = 3; |
Zachary Turner | b927e02 | 2016-07-15 22:17:19 +0000 | [diff] [blame] | 31 | |
Eugene Zelenko | 2db0cfa | 2017-06-23 21:57:40 +0000 | [diff] [blame] | 32 | static const uint32_t kDefaultFreePageMap = kFreePageMap0Block; |
| 33 | static const uint32_t kDefaultBlockMapAddr = kNumReservedPages; |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 34 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 35 | MSFBuilder::MSFBuilder(uint32_t BlockSize, uint32_t MinBlockCount, bool CanGrow, |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 36 | BumpPtrAllocator &Allocator) |
Rui Ueyama | 5d6714e | 2016-09-30 20:52:12 +0000 | [diff] [blame] | 37 | : Allocator(Allocator), IsGrowable(CanGrow), |
| 38 | FreePageMap(kDefaultFreePageMap), BlockSize(BlockSize), |
NAKAMURA Takumi | 1657f2a | 2017-11-01 13:47:55 +0000 | [diff] [blame^] | 39 | BlockMapAddr(kDefaultBlockMapAddr), FreeBlocks(MinBlockCount, true) { |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 40 | FreeBlocks[kSuperBlockBlock] = false; |
Zachary Turner | b927e02 | 2016-07-15 22:17:19 +0000 | [diff] [blame] | 41 | FreeBlocks[kFreePageMap0Block] = false; |
| 42 | FreeBlocks[kFreePageMap1Block] = false; |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 43 | FreeBlocks[BlockMapAddr] = false; |
| 44 | } |
| 45 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 46 | Expected<MSFBuilder> MSFBuilder::create(BumpPtrAllocator &Allocator, |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 47 | uint32_t BlockSize, |
| 48 | uint32_t MinBlockCount, bool CanGrow) { |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 49 | if (!isValidBlockSize(BlockSize)) |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 50 | return make_error<MSFError>(msf_error_code::invalid_format, |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 51 | "The requested block size is unsupported"); |
| 52 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 53 | return MSFBuilder(BlockSize, |
Zachary Turner | b927e02 | 2016-07-15 22:17:19 +0000 | [diff] [blame] | 54 | std::max(MinBlockCount, msf::getMinimumBlockCount()), |
| 55 | CanGrow, Allocator); |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 58 | Error MSFBuilder::setBlockMapAddr(uint32_t Addr) { |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 59 | if (Addr == BlockMapAddr) |
| 60 | return Error::success(); |
| 61 | |
| 62 | if (Addr >= FreeBlocks.size()) { |
| 63 | if (!IsGrowable) |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 64 | return make_error<MSFError>(msf_error_code::insufficient_buffer, |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 65 | "Cannot grow the number of blocks"); |
Rui Ueyama | 4ee7f3c | 2016-08-02 21:56:37 +0000 | [diff] [blame] | 66 | FreeBlocks.resize(Addr + 1, true); |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | if (!isBlockFree(Addr)) |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 70 | return make_error<MSFError>( |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 71 | msf_error_code::block_in_use, |
| 72 | "Requested block map address is already in use"); |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 73 | FreeBlocks[BlockMapAddr] = true; |
| 74 | FreeBlocks[Addr] = false; |
| 75 | BlockMapAddr = Addr; |
| 76 | return Error::success(); |
| 77 | } |
| 78 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 79 | void MSFBuilder::setFreePageMap(uint32_t Fpm) { FreePageMap = Fpm; } |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 80 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 81 | void MSFBuilder::setUnknown1(uint32_t Unk1) { Unknown1 = Unk1; } |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 82 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 83 | Error MSFBuilder::setDirectoryBlocksHint(ArrayRef<uint32_t> DirBlocks) { |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 84 | for (auto B : DirectoryBlocks) |
| 85 | FreeBlocks[B] = true; |
| 86 | for (auto B : DirBlocks) { |
| 87 | if (!isBlockFree(B)) { |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 88 | return make_error<MSFError>(msf_error_code::unspecified, |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 89 | "Attempt to reuse an allocated block"); |
| 90 | } |
| 91 | FreeBlocks[B] = false; |
| 92 | } |
| 93 | |
| 94 | DirectoryBlocks = DirBlocks; |
| 95 | return Error::success(); |
| 96 | } |
| 97 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 98 | Error MSFBuilder::allocateBlocks(uint32_t NumBlocks, |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 99 | MutableArrayRef<uint32_t> Blocks) { |
| 100 | if (NumBlocks == 0) |
| 101 | return Error::success(); |
| 102 | |
| 103 | uint32_t NumFreeBlocks = FreeBlocks.count(); |
| 104 | if (NumFreeBlocks < NumBlocks) { |
| 105 | if (!IsGrowable) |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 106 | return make_error<MSFError>(msf_error_code::insufficient_buffer, |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 107 | "There are no free Blocks in the file"); |
| 108 | uint32_t AllocBlocks = NumBlocks - NumFreeBlocks; |
Zachary Turner | 9fb9d71 | 2017-08-02 22:31:39 +0000 | [diff] [blame] | 109 | uint32_t OldBlockCount = FreeBlocks.size(); |
| 110 | uint32_t NewBlockCount = AllocBlocks + OldBlockCount; |
| 111 | uint32_t NextFpmBlock = alignTo(OldBlockCount, BlockSize) + 1; |
| 112 | FreeBlocks.resize(NewBlockCount, true); |
| 113 | // If we crossed over an fpm page, we actually need to allocate 2 extra |
| 114 | // blocks for each FPM group crossed and mark both blocks from the group as |
| 115 | // used. We may not actually use them since there are many more FPM blocks |
| 116 | // present than are required to represent all blocks in a given PDB, but we |
| 117 | // need to make sure they aren't allocated to a stream or something else. |
| 118 | // At the end when committing the PDB, we'll go through and mark the |
| 119 | // extraneous ones unused. |
| 120 | while (NextFpmBlock < NewBlockCount) { |
| 121 | NewBlockCount += 2; |
| 122 | FreeBlocks.resize(NewBlockCount, true); |
| 123 | FreeBlocks.reset(NextFpmBlock, NextFpmBlock + 2); |
| 124 | NextFpmBlock += BlockSize; |
| 125 | } |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | int I = 0; |
| 129 | int Block = FreeBlocks.find_first(); |
| 130 | do { |
| 131 | assert(Block != -1 && "We ran out of Blocks!"); |
| 132 | |
| 133 | uint32_t NextBlock = static_cast<uint32_t>(Block); |
| 134 | Blocks[I++] = NextBlock; |
| 135 | FreeBlocks.reset(NextBlock); |
| 136 | Block = FreeBlocks.find_next(Block); |
| 137 | } while (--NumBlocks > 0); |
| 138 | return Error::success(); |
| 139 | } |
| 140 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 141 | uint32_t MSFBuilder::getNumUsedBlocks() const { |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 142 | return getTotalBlockCount() - getNumFreeBlocks(); |
| 143 | } |
| 144 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 145 | uint32_t MSFBuilder::getNumFreeBlocks() const { return FreeBlocks.count(); } |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 146 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 147 | uint32_t MSFBuilder::getTotalBlockCount() const { return FreeBlocks.size(); } |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 148 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 149 | bool MSFBuilder::isBlockFree(uint32_t Idx) const { return FreeBlocks[Idx]; } |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 150 | |
Zachary Turner | 620961d | 2016-09-14 23:00:02 +0000 | [diff] [blame] | 151 | Expected<uint32_t> MSFBuilder::addStream(uint32_t Size, |
| 152 | ArrayRef<uint32_t> Blocks) { |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 153 | // Add a new stream mapped to the specified blocks. Verify that the specified |
| 154 | // blocks are both necessary and sufficient for holding the requested number |
| 155 | // of bytes, and verify that all requested blocks are free. |
| 156 | uint32_t ReqBlocks = bytesToBlocks(Size, BlockSize); |
| 157 | if (ReqBlocks != Blocks.size()) |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 158 | return make_error<MSFError>( |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 159 | msf_error_code::invalid_format, |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 160 | "Incorrect number of blocks for requested stream size"); |
| 161 | for (auto Block : Blocks) { |
| 162 | if (Block >= FreeBlocks.size()) |
| 163 | FreeBlocks.resize(Block + 1, true); |
| 164 | |
| 165 | if (!FreeBlocks.test(Block)) |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 166 | return make_error<MSFError>( |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 167 | msf_error_code::unspecified, |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 168 | "Attempt to re-use an already allocated block"); |
| 169 | } |
| 170 | // Mark all the blocks occupied by the new stream as not free. |
| 171 | for (auto Block : Blocks) { |
| 172 | FreeBlocks.reset(Block); |
| 173 | } |
| 174 | StreamData.push_back(std::make_pair(Size, Blocks)); |
Zachary Turner | 620961d | 2016-09-14 23:00:02 +0000 | [diff] [blame] | 175 | return StreamData.size() - 1; |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Zachary Turner | 620961d | 2016-09-14 23:00:02 +0000 | [diff] [blame] | 178 | Expected<uint32_t> MSFBuilder::addStream(uint32_t Size) { |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 179 | uint32_t ReqBlocks = bytesToBlocks(Size, BlockSize); |
| 180 | std::vector<uint32_t> NewBlocks; |
| 181 | NewBlocks.resize(ReqBlocks); |
| 182 | if (auto EC = allocateBlocks(ReqBlocks, NewBlocks)) |
Zachary Turner | 620961d | 2016-09-14 23:00:02 +0000 | [diff] [blame] | 183 | return std::move(EC); |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 184 | StreamData.push_back(std::make_pair(Size, NewBlocks)); |
Zachary Turner | 620961d | 2016-09-14 23:00:02 +0000 | [diff] [blame] | 185 | return StreamData.size() - 1; |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 188 | Error MSFBuilder::setStreamSize(uint32_t Idx, uint32_t Size) { |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 189 | uint32_t OldSize = getStreamSize(Idx); |
| 190 | if (OldSize == Size) |
| 191 | return Error::success(); |
| 192 | |
| 193 | uint32_t NewBlocks = bytesToBlocks(Size, BlockSize); |
| 194 | uint32_t OldBlocks = bytesToBlocks(OldSize, BlockSize); |
| 195 | |
| 196 | if (NewBlocks > OldBlocks) { |
| 197 | uint32_t AddedBlocks = NewBlocks - OldBlocks; |
| 198 | // If we're growing, we have to allocate new Blocks. |
| 199 | std::vector<uint32_t> AddedBlockList; |
| 200 | AddedBlockList.resize(AddedBlocks); |
| 201 | if (auto EC = allocateBlocks(AddedBlocks, AddedBlockList)) |
| 202 | return EC; |
| 203 | auto &CurrentBlocks = StreamData[Idx].second; |
| 204 | CurrentBlocks.insert(CurrentBlocks.end(), AddedBlockList.begin(), |
| 205 | AddedBlockList.end()); |
| 206 | } else if (OldBlocks > NewBlocks) { |
| 207 | // For shrinking, free all the Blocks in the Block map, update the stream |
| 208 | // data, then shrink the directory. |
| 209 | uint32_t RemovedBlocks = OldBlocks - NewBlocks; |
| 210 | auto CurrentBlocks = ArrayRef<uint32_t>(StreamData[Idx].second); |
| 211 | auto RemovedBlockList = CurrentBlocks.drop_front(NewBlocks); |
| 212 | for (auto P : RemovedBlockList) |
| 213 | FreeBlocks[P] = true; |
| 214 | StreamData[Idx].second = CurrentBlocks.drop_back(RemovedBlocks); |
| 215 | } |
| 216 | |
| 217 | StreamData[Idx].first = Size; |
| 218 | return Error::success(); |
| 219 | } |
| 220 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 221 | uint32_t MSFBuilder::getNumStreams() const { return StreamData.size(); } |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 222 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 223 | uint32_t MSFBuilder::getStreamSize(uint32_t StreamIdx) const { |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 224 | return StreamData[StreamIdx].first; |
| 225 | } |
| 226 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 227 | ArrayRef<uint32_t> MSFBuilder::getStreamBlocks(uint32_t StreamIdx) const { |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 228 | return StreamData[StreamIdx].second; |
| 229 | } |
| 230 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 231 | uint32_t MSFBuilder::computeDirectoryByteSize() const { |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 232 | // The directory has the following layout, where each item is a ulittle32_t: |
| 233 | // NumStreams |
| 234 | // StreamSizes[NumStreams] |
| 235 | // StreamBlocks[NumStreams][] |
| 236 | uint32_t Size = sizeof(ulittle32_t); // NumStreams |
| 237 | Size += StreamData.size() * sizeof(ulittle32_t); // StreamSizes |
| 238 | for (const auto &D : StreamData) { |
| 239 | uint32_t ExpectedNumBlocks = bytesToBlocks(D.first, BlockSize); |
| 240 | assert(ExpectedNumBlocks == D.second.size() && |
| 241 | "Unexpected number of blocks"); |
| 242 | Size += ExpectedNumBlocks * sizeof(ulittle32_t); |
| 243 | } |
| 244 | return Size; |
| 245 | } |
| 246 | |
Zachary Turner | 9fb9d71 | 2017-08-02 22:31:39 +0000 | [diff] [blame] | 247 | static void finalizeFpmBlockStatus(uint32_t B, ArrayRef<ulittle32_t> &FpmBlocks, |
| 248 | BitVector &Fpm) { |
| 249 | if (FpmBlocks.empty() || FpmBlocks.front() != B) { |
| 250 | Fpm.set(B); |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | // If the next block in the actual layout is this block, it should *not* be |
| 255 | // free. |
| 256 | assert(!Fpm.test(B)); |
| 257 | FpmBlocks = FpmBlocks.drop_front(); |
| 258 | } |
| 259 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 260 | Expected<MSFLayout> MSFBuilder::build() { |
Zachary Turner | e4a4f33 | 2016-07-22 19:56:33 +0000 | [diff] [blame] | 261 | SuperBlock *SB = Allocator.Allocate<SuperBlock>(); |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 262 | MSFLayout L; |
Zachary Turner | e4a4f33 | 2016-07-22 19:56:33 +0000 | [diff] [blame] | 263 | L.SB = SB; |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 264 | |
Zachary Turner | e4a4f33 | 2016-07-22 19:56:33 +0000 | [diff] [blame] | 265 | std::memcpy(SB->MagicBytes, Magic, sizeof(Magic)); |
| 266 | SB->BlockMapAddr = BlockMapAddr; |
| 267 | SB->BlockSize = BlockSize; |
| 268 | SB->NumDirectoryBytes = computeDirectoryByteSize(); |
| 269 | SB->FreeBlockMapBlock = FreePageMap; |
| 270 | SB->Unknown1 = Unknown1; |
| 271 | |
| 272 | uint32_t NumDirectoryBlocks = bytesToBlocks(SB->NumDirectoryBytes, BlockSize); |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 273 | if (NumDirectoryBlocks > DirectoryBlocks.size()) { |
| 274 | // Our hint wasn't enough to satisfy the entire directory. Allocate |
| 275 | // remaining pages. |
| 276 | std::vector<uint32_t> ExtraBlocks; |
| 277 | uint32_t NumExtraBlocks = NumDirectoryBlocks - DirectoryBlocks.size(); |
| 278 | ExtraBlocks.resize(NumExtraBlocks); |
| 279 | if (auto EC = allocateBlocks(NumExtraBlocks, ExtraBlocks)) |
| 280 | return std::move(EC); |
| 281 | DirectoryBlocks.insert(DirectoryBlocks.end(), ExtraBlocks.begin(), |
| 282 | ExtraBlocks.end()); |
| 283 | } else if (NumDirectoryBlocks < DirectoryBlocks.size()) { |
| 284 | uint32_t NumUnnecessaryBlocks = DirectoryBlocks.size() - NumDirectoryBlocks; |
| 285 | for (auto B : |
| 286 | ArrayRef<uint32_t>(DirectoryBlocks).drop_back(NumUnnecessaryBlocks)) |
| 287 | FreeBlocks[B] = true; |
| 288 | DirectoryBlocks.resize(NumDirectoryBlocks); |
| 289 | } |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 290 | |
| 291 | // Don't set the number of blocks in the file until after allocating Blocks |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 292 | // for the directory, since the allocation might cause the file to need to |
| 293 | // grow. |
Zachary Turner | e4a4f33 | 2016-07-22 19:56:33 +0000 | [diff] [blame] | 294 | SB->NumBlocks = FreeBlocks.size(); |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 295 | |
| 296 | ulittle32_t *DirBlocks = Allocator.Allocate<ulittle32_t>(NumDirectoryBlocks); |
| 297 | std::uninitialized_copy_n(DirectoryBlocks.begin(), NumDirectoryBlocks, |
| 298 | DirBlocks); |
| 299 | L.DirectoryBlocks = ArrayRef<ulittle32_t>(DirBlocks, NumDirectoryBlocks); |
| 300 | |
| 301 | // The stream sizes should be re-allocated as a stable pointer and the stream |
| 302 | // map should have each of its entries allocated as a separate stable pointer. |
Eugene Zelenko | 2db0cfa | 2017-06-23 21:57:40 +0000 | [diff] [blame] | 303 | if (!StreamData.empty()) { |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 304 | ulittle32_t *Sizes = Allocator.Allocate<ulittle32_t>(StreamData.size()); |
| 305 | L.StreamSizes = ArrayRef<ulittle32_t>(Sizes, StreamData.size()); |
| 306 | L.StreamMap.resize(StreamData.size()); |
| 307 | for (uint32_t I = 0; I < StreamData.size(); ++I) { |
| 308 | Sizes[I] = StreamData[I].first; |
| 309 | ulittle32_t *BlockList = |
| 310 | Allocator.Allocate<ulittle32_t>(StreamData[I].second.size()); |
| 311 | std::uninitialized_copy_n(StreamData[I].second.begin(), |
| 312 | StreamData[I].second.size(), BlockList); |
| 313 | L.StreamMap[I] = |
| 314 | ArrayRef<ulittle32_t>(BlockList, StreamData[I].second.size()); |
| 315 | } |
| 316 | } |
| 317 | |
Zachary Turner | 9fb9d71 | 2017-08-02 22:31:39 +0000 | [diff] [blame] | 318 | // FPM blocks occur in pairs at every `BlockLength` interval. While blocks of |
| 319 | // this form are reserved for FPM blocks, not all blocks of this form will |
| 320 | // actually be needed for FPM data because there are more blocks of this form |
| 321 | // than are required to represent a PDB file with a given number of blocks. |
| 322 | // So we need to find out which blocks are *actually* going to be real FPM |
| 323 | // blocks, then mark the reset of the reserved blocks as unallocated. |
| 324 | MSFStreamLayout FpmLayout = msf::getFpmStreamLayout(L, true); |
| 325 | auto FpmBlocks = makeArrayRef(FpmLayout.Blocks); |
| 326 | for (uint32_t B = kFreePageMap0Block; B < SB->NumBlocks; |
| 327 | B += msf::getFpmIntervalLength(L)) { |
| 328 | finalizeFpmBlockStatus(B, FpmBlocks, FreeBlocks); |
| 329 | finalizeFpmBlockStatus(B + 1, FpmBlocks, FreeBlocks); |
| 330 | } |
| 331 | L.FreePageMap = FreeBlocks; |
| 332 | |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 333 | return L; |
| 334 | } |