Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 1 | //===- MSFBuilder.cpp - MSF Directory & Metadata Builder --------*- C++ -*-===// |
| 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 | |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame^] | 10 | #include "llvm/DebugInfo/Msf/MsfBuilder.h" |
| 11 | #include "llvm/DebugInfo/Msf/MsfError.h" |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 12 | |
| 13 | using namespace llvm; |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame^] | 14 | using namespace llvm::msf; |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 15 | using namespace llvm::support; |
| 16 | |
| 17 | namespace { |
| 18 | const uint32_t kSuperBlockBlock = 0; |
Zachary Turner | b927e02 | 2016-07-15 22:17:19 +0000 | [diff] [blame] | 19 | const uint32_t kFreePageMap0Block = 1; |
| 20 | const uint32_t kFreePageMap1Block = 2; |
| 21 | const uint32_t kNumReservedPages = 3; |
| 22 | |
| 23 | const uint32_t kDefaultBlockMapAddr = kNumReservedPages; |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | MsfBuilder::MsfBuilder(uint32_t BlockSize, uint32_t MinBlockCount, bool CanGrow, |
| 27 | BumpPtrAllocator &Allocator) |
Saleem Abdulrasool | ea6a4fe | 2016-07-15 21:10:31 +0000 | [diff] [blame] | 28 | : Allocator(Allocator), IsGrowable(CanGrow), BlockSize(BlockSize), |
| 29 | MininumBlocks(MinBlockCount), BlockMapAddr(kDefaultBlockMapAddr), |
Zachary Turner | b927e02 | 2016-07-15 22:17:19 +0000 | [diff] [blame] | 30 | FreeBlocks(MinBlockCount, true) { |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 31 | FreeBlocks[kSuperBlockBlock] = false; |
Zachary Turner | b927e02 | 2016-07-15 22:17:19 +0000 | [diff] [blame] | 32 | FreeBlocks[kFreePageMap0Block] = false; |
| 33 | FreeBlocks[kFreePageMap1Block] = false; |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 34 | FreeBlocks[BlockMapAddr] = false; |
| 35 | } |
| 36 | |
| 37 | Expected<MsfBuilder> MsfBuilder::create(BumpPtrAllocator &Allocator, |
| 38 | uint32_t BlockSize, |
| 39 | uint32_t MinBlockCount, bool CanGrow) { |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame^] | 40 | if (!isValidBlockSize(BlockSize)) |
| 41 | return make_error<MsfError>(msf_error_code::invalid_format, |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 42 | "The requested block size is unsupported"); |
| 43 | |
Zachary Turner | b927e02 | 2016-07-15 22:17:19 +0000 | [diff] [blame] | 44 | return MsfBuilder(BlockSize, |
| 45 | std::max(MinBlockCount, msf::getMinimumBlockCount()), |
| 46 | CanGrow, Allocator); |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | Error MsfBuilder::setBlockMapAddr(uint32_t Addr) { |
| 50 | if (Addr == BlockMapAddr) |
| 51 | return Error::success(); |
| 52 | |
| 53 | if (Addr >= FreeBlocks.size()) { |
| 54 | if (!IsGrowable) |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame^] | 55 | return make_error<MsfError>(msf_error_code::insufficient_buffer, |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 56 | "Cannot grow the number of blocks"); |
| 57 | FreeBlocks.resize(Addr + 1); |
| 58 | } |
| 59 | |
| 60 | if (!isBlockFree(Addr)) |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame^] | 61 | return make_error<MsfError>( |
| 62 | msf_error_code::block_in_use, |
| 63 | "Requested block map address is already in use"); |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 64 | FreeBlocks[BlockMapAddr] = true; |
| 65 | FreeBlocks[Addr] = false; |
| 66 | BlockMapAddr = Addr; |
| 67 | return Error::success(); |
| 68 | } |
| 69 | |
Zachary Turner | b927e02 | 2016-07-15 22:17:19 +0000 | [diff] [blame] | 70 | void MsfBuilder::setFreePageMap(uint32_t Fpm) { FreePageMap = Fpm; } |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 71 | |
| 72 | void MsfBuilder::setUnknown1(uint32_t Unk1) { Unknown1 = Unk1; } |
| 73 | |
| 74 | Error MsfBuilder::setDirectoryBlocksHint(ArrayRef<uint32_t> DirBlocks) { |
| 75 | for (auto B : DirectoryBlocks) |
| 76 | FreeBlocks[B] = true; |
| 77 | for (auto B : DirBlocks) { |
| 78 | if (!isBlockFree(B)) { |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame^] | 79 | return make_error<MsfError>(msf_error_code::unspecified, |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 80 | "Attempt to reuse an allocated block"); |
| 81 | } |
| 82 | FreeBlocks[B] = false; |
| 83 | } |
| 84 | |
| 85 | DirectoryBlocks = DirBlocks; |
| 86 | return Error::success(); |
| 87 | } |
| 88 | |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 89 | Error MsfBuilder::allocateBlocks(uint32_t NumBlocks, |
| 90 | MutableArrayRef<uint32_t> Blocks) { |
| 91 | if (NumBlocks == 0) |
| 92 | return Error::success(); |
| 93 | |
| 94 | uint32_t NumFreeBlocks = FreeBlocks.count(); |
| 95 | if (NumFreeBlocks < NumBlocks) { |
| 96 | if (!IsGrowable) |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame^] | 97 | return make_error<MsfError>(msf_error_code::insufficient_buffer, |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 98 | "There are no free Blocks in the file"); |
| 99 | uint32_t AllocBlocks = NumBlocks - NumFreeBlocks; |
| 100 | FreeBlocks.resize(AllocBlocks + FreeBlocks.size(), true); |
| 101 | } |
| 102 | |
| 103 | int I = 0; |
| 104 | int Block = FreeBlocks.find_first(); |
| 105 | do { |
| 106 | assert(Block != -1 && "We ran out of Blocks!"); |
| 107 | |
| 108 | uint32_t NextBlock = static_cast<uint32_t>(Block); |
| 109 | Blocks[I++] = NextBlock; |
| 110 | FreeBlocks.reset(NextBlock); |
| 111 | Block = FreeBlocks.find_next(Block); |
| 112 | } while (--NumBlocks > 0); |
| 113 | return Error::success(); |
| 114 | } |
| 115 | |
| 116 | uint32_t MsfBuilder::getNumUsedBlocks() const { |
| 117 | return getTotalBlockCount() - getNumFreeBlocks(); |
| 118 | } |
| 119 | |
| 120 | uint32_t MsfBuilder::getNumFreeBlocks() const { return FreeBlocks.count(); } |
| 121 | |
| 122 | uint32_t MsfBuilder::getTotalBlockCount() const { return FreeBlocks.size(); } |
| 123 | |
| 124 | bool MsfBuilder::isBlockFree(uint32_t Idx) const { return FreeBlocks[Idx]; } |
| 125 | |
| 126 | Error MsfBuilder::addStream(uint32_t Size, ArrayRef<uint32_t> Blocks) { |
| 127 | // Add a new stream mapped to the specified blocks. Verify that the specified |
| 128 | // blocks are both necessary and sufficient for holding the requested number |
| 129 | // of bytes, and verify that all requested blocks are free. |
| 130 | uint32_t ReqBlocks = bytesToBlocks(Size, BlockSize); |
| 131 | if (ReqBlocks != Blocks.size()) |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame^] | 132 | return make_error<MsfError>( |
| 133 | msf_error_code::invalid_format, |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 134 | "Incorrect number of blocks for requested stream size"); |
| 135 | for (auto Block : Blocks) { |
| 136 | if (Block >= FreeBlocks.size()) |
| 137 | FreeBlocks.resize(Block + 1, true); |
| 138 | |
| 139 | if (!FreeBlocks.test(Block)) |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame^] | 140 | return make_error<MsfError>( |
| 141 | msf_error_code::unspecified, |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 142 | "Attempt to re-use an already allocated block"); |
| 143 | } |
| 144 | // Mark all the blocks occupied by the new stream as not free. |
| 145 | for (auto Block : Blocks) { |
| 146 | FreeBlocks.reset(Block); |
| 147 | } |
| 148 | StreamData.push_back(std::make_pair(Size, Blocks)); |
| 149 | return Error::success(); |
| 150 | } |
| 151 | |
| 152 | Error MsfBuilder::addStream(uint32_t Size) { |
| 153 | uint32_t ReqBlocks = bytesToBlocks(Size, BlockSize); |
| 154 | std::vector<uint32_t> NewBlocks; |
| 155 | NewBlocks.resize(ReqBlocks); |
| 156 | if (auto EC = allocateBlocks(ReqBlocks, NewBlocks)) |
| 157 | return EC; |
| 158 | StreamData.push_back(std::make_pair(Size, NewBlocks)); |
| 159 | return Error::success(); |
| 160 | } |
| 161 | |
| 162 | Error MsfBuilder::setStreamSize(uint32_t Idx, uint32_t Size) { |
| 163 | uint32_t OldSize = getStreamSize(Idx); |
| 164 | if (OldSize == Size) |
| 165 | return Error::success(); |
| 166 | |
| 167 | uint32_t NewBlocks = bytesToBlocks(Size, BlockSize); |
| 168 | uint32_t OldBlocks = bytesToBlocks(OldSize, BlockSize); |
| 169 | |
| 170 | if (NewBlocks > OldBlocks) { |
| 171 | uint32_t AddedBlocks = NewBlocks - OldBlocks; |
| 172 | // If we're growing, we have to allocate new Blocks. |
| 173 | std::vector<uint32_t> AddedBlockList; |
| 174 | AddedBlockList.resize(AddedBlocks); |
| 175 | if (auto EC = allocateBlocks(AddedBlocks, AddedBlockList)) |
| 176 | return EC; |
| 177 | auto &CurrentBlocks = StreamData[Idx].second; |
| 178 | CurrentBlocks.insert(CurrentBlocks.end(), AddedBlockList.begin(), |
| 179 | AddedBlockList.end()); |
| 180 | } else if (OldBlocks > NewBlocks) { |
| 181 | // For shrinking, free all the Blocks in the Block map, update the stream |
| 182 | // data, then shrink the directory. |
| 183 | uint32_t RemovedBlocks = OldBlocks - NewBlocks; |
| 184 | auto CurrentBlocks = ArrayRef<uint32_t>(StreamData[Idx].second); |
| 185 | auto RemovedBlockList = CurrentBlocks.drop_front(NewBlocks); |
| 186 | for (auto P : RemovedBlockList) |
| 187 | FreeBlocks[P] = true; |
| 188 | StreamData[Idx].second = CurrentBlocks.drop_back(RemovedBlocks); |
| 189 | } |
| 190 | |
| 191 | StreamData[Idx].first = Size; |
| 192 | return Error::success(); |
| 193 | } |
| 194 | |
| 195 | uint32_t MsfBuilder::getNumStreams() const { return StreamData.size(); } |
| 196 | |
| 197 | uint32_t MsfBuilder::getStreamSize(uint32_t StreamIdx) const { |
| 198 | return StreamData[StreamIdx].first; |
| 199 | } |
| 200 | |
| 201 | ArrayRef<uint32_t> MsfBuilder::getStreamBlocks(uint32_t StreamIdx) const { |
| 202 | return StreamData[StreamIdx].second; |
| 203 | } |
| 204 | |
| 205 | uint32_t MsfBuilder::computeDirectoryByteSize() const { |
| 206 | // The directory has the following layout, where each item is a ulittle32_t: |
| 207 | // NumStreams |
| 208 | // StreamSizes[NumStreams] |
| 209 | // StreamBlocks[NumStreams][] |
| 210 | uint32_t Size = sizeof(ulittle32_t); // NumStreams |
| 211 | Size += StreamData.size() * sizeof(ulittle32_t); // StreamSizes |
| 212 | for (const auto &D : StreamData) { |
| 213 | uint32_t ExpectedNumBlocks = bytesToBlocks(D.first, BlockSize); |
| 214 | assert(ExpectedNumBlocks == D.second.size() && |
| 215 | "Unexpected number of blocks"); |
| 216 | Size += ExpectedNumBlocks * sizeof(ulittle32_t); |
| 217 | } |
| 218 | return Size; |
| 219 | } |
| 220 | |
| 221 | Expected<Layout> MsfBuilder::build() { |
| 222 | Layout L; |
| 223 | L.SB = Allocator.Allocate<SuperBlock>(); |
| 224 | std::memcpy(L.SB->MagicBytes, Magic, sizeof(Magic)); |
| 225 | L.SB->BlockMapAddr = BlockMapAddr; |
| 226 | L.SB->BlockSize = BlockSize; |
| 227 | L.SB->NumDirectoryBytes = computeDirectoryByteSize(); |
Zachary Turner | b927e02 | 2016-07-15 22:17:19 +0000 | [diff] [blame] | 228 | L.SB->FreeBlockMapBlock = FreePageMap; |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 229 | L.SB->Unknown1 = Unknown1; |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 230 | |
| 231 | uint32_t NumDirectoryBlocks = |
| 232 | bytesToBlocks(L.SB->NumDirectoryBytes, BlockSize); |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 233 | if (NumDirectoryBlocks > DirectoryBlocks.size()) { |
| 234 | // Our hint wasn't enough to satisfy the entire directory. Allocate |
| 235 | // remaining pages. |
| 236 | std::vector<uint32_t> ExtraBlocks; |
| 237 | uint32_t NumExtraBlocks = NumDirectoryBlocks - DirectoryBlocks.size(); |
| 238 | ExtraBlocks.resize(NumExtraBlocks); |
| 239 | if (auto EC = allocateBlocks(NumExtraBlocks, ExtraBlocks)) |
| 240 | return std::move(EC); |
| 241 | DirectoryBlocks.insert(DirectoryBlocks.end(), ExtraBlocks.begin(), |
| 242 | ExtraBlocks.end()); |
| 243 | } else if (NumDirectoryBlocks < DirectoryBlocks.size()) { |
| 244 | uint32_t NumUnnecessaryBlocks = DirectoryBlocks.size() - NumDirectoryBlocks; |
| 245 | for (auto B : |
| 246 | ArrayRef<uint32_t>(DirectoryBlocks).drop_back(NumUnnecessaryBlocks)) |
| 247 | FreeBlocks[B] = true; |
| 248 | DirectoryBlocks.resize(NumDirectoryBlocks); |
| 249 | } |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 250 | |
| 251 | // 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^] | 252 | // for the directory, since the allocation might cause the file to need to |
| 253 | // grow. |
Zachary Turner | f52a899 | 2016-07-15 20:43:38 +0000 | [diff] [blame] | 254 | L.SB->NumBlocks = FreeBlocks.size(); |
| 255 | |
| 256 | ulittle32_t *DirBlocks = Allocator.Allocate<ulittle32_t>(NumDirectoryBlocks); |
| 257 | std::uninitialized_copy_n(DirectoryBlocks.begin(), NumDirectoryBlocks, |
| 258 | DirBlocks); |
| 259 | L.DirectoryBlocks = ArrayRef<ulittle32_t>(DirBlocks, NumDirectoryBlocks); |
| 260 | |
| 261 | // The stream sizes should be re-allocated as a stable pointer and the stream |
| 262 | // map should have each of its entries allocated as a separate stable pointer. |
| 263 | if (StreamData.size() > 0) { |
| 264 | ulittle32_t *Sizes = Allocator.Allocate<ulittle32_t>(StreamData.size()); |
| 265 | L.StreamSizes = ArrayRef<ulittle32_t>(Sizes, StreamData.size()); |
| 266 | L.StreamMap.resize(StreamData.size()); |
| 267 | for (uint32_t I = 0; I < StreamData.size(); ++I) { |
| 268 | Sizes[I] = StreamData[I].first; |
| 269 | ulittle32_t *BlockList = |
| 270 | Allocator.Allocate<ulittle32_t>(StreamData[I].second.size()); |
| 271 | std::uninitialized_copy_n(StreamData[I].second.begin(), |
| 272 | StreamData[I].second.size(), BlockList); |
| 273 | L.StreamMap[I] = |
| 274 | ArrayRef<ulittle32_t>(BlockList, StreamData[I].second.size()); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | return L; |
| 279 | } |