blob: e6f15d8877e792f4d1d4537929bae8cd4b0652c0 [file] [log] [blame]
Zachary Turnerf52a8992016-07-15 20:43:38 +00001//===- 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 Turnerbac69d32016-07-22 19:56:05 +000010#include "llvm/DebugInfo/Msf/MsfBuilder.h"
11#include "llvm/DebugInfo/Msf/MsfError.h"
Zachary Turnerf52a8992016-07-15 20:43:38 +000012
13using namespace llvm;
Zachary Turnerbac69d32016-07-22 19:56:05 +000014using namespace llvm::msf;
Zachary Turnerf52a8992016-07-15 20:43:38 +000015using namespace llvm::support;
16
17namespace {
18const uint32_t kSuperBlockBlock = 0;
Zachary Turnerb927e022016-07-15 22:17:19 +000019const uint32_t kFreePageMap0Block = 1;
20const uint32_t kFreePageMap1Block = 2;
21const uint32_t kNumReservedPages = 3;
22
23const uint32_t kDefaultBlockMapAddr = kNumReservedPages;
Zachary Turnerf52a8992016-07-15 20:43:38 +000024}
25
26MsfBuilder::MsfBuilder(uint32_t BlockSize, uint32_t MinBlockCount, bool CanGrow,
27 BumpPtrAllocator &Allocator)
Saleem Abdulrasoolea6a4fe2016-07-15 21:10:31 +000028 : Allocator(Allocator), IsGrowable(CanGrow), BlockSize(BlockSize),
29 MininumBlocks(MinBlockCount), BlockMapAddr(kDefaultBlockMapAddr),
Zachary Turnerb927e022016-07-15 22:17:19 +000030 FreeBlocks(MinBlockCount, true) {
Zachary Turnerf52a8992016-07-15 20:43:38 +000031 FreeBlocks[kSuperBlockBlock] = false;
Zachary Turnerb927e022016-07-15 22:17:19 +000032 FreeBlocks[kFreePageMap0Block] = false;
33 FreeBlocks[kFreePageMap1Block] = false;
Zachary Turnerf52a8992016-07-15 20:43:38 +000034 FreeBlocks[BlockMapAddr] = false;
35}
36
37Expected<MsfBuilder> MsfBuilder::create(BumpPtrAllocator &Allocator,
38 uint32_t BlockSize,
39 uint32_t MinBlockCount, bool CanGrow) {
Zachary Turnerbac69d32016-07-22 19:56:05 +000040 if (!isValidBlockSize(BlockSize))
41 return make_error<MsfError>(msf_error_code::invalid_format,
Zachary Turnerf52a8992016-07-15 20:43:38 +000042 "The requested block size is unsupported");
43
Zachary Turnerb927e022016-07-15 22:17:19 +000044 return MsfBuilder(BlockSize,
45 std::max(MinBlockCount, msf::getMinimumBlockCount()),
46 CanGrow, Allocator);
Zachary Turnerf52a8992016-07-15 20:43:38 +000047}
48
49Error MsfBuilder::setBlockMapAddr(uint32_t Addr) {
50 if (Addr == BlockMapAddr)
51 return Error::success();
52
53 if (Addr >= FreeBlocks.size()) {
54 if (!IsGrowable)
Zachary Turnerbac69d32016-07-22 19:56:05 +000055 return make_error<MsfError>(msf_error_code::insufficient_buffer,
Zachary Turnerf52a8992016-07-15 20:43:38 +000056 "Cannot grow the number of blocks");
57 FreeBlocks.resize(Addr + 1);
58 }
59
60 if (!isBlockFree(Addr))
Zachary Turnerbac69d32016-07-22 19:56:05 +000061 return make_error<MsfError>(
62 msf_error_code::block_in_use,
63 "Requested block map address is already in use");
Zachary Turnerf52a8992016-07-15 20:43:38 +000064 FreeBlocks[BlockMapAddr] = true;
65 FreeBlocks[Addr] = false;
66 BlockMapAddr = Addr;
67 return Error::success();
68}
69
Zachary Turnerb927e022016-07-15 22:17:19 +000070void MsfBuilder::setFreePageMap(uint32_t Fpm) { FreePageMap = Fpm; }
Zachary Turnerfaa554b2016-07-15 22:16:56 +000071
72void MsfBuilder::setUnknown1(uint32_t Unk1) { Unknown1 = Unk1; }
73
74Error 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 Turnerbac69d32016-07-22 19:56:05 +000079 return make_error<MsfError>(msf_error_code::unspecified,
Zachary Turnerfaa554b2016-07-15 22:16:56 +000080 "Attempt to reuse an allocated block");
81 }
82 FreeBlocks[B] = false;
83 }
84
85 DirectoryBlocks = DirBlocks;
86 return Error::success();
87}
88
Zachary Turnerf52a8992016-07-15 20:43:38 +000089Error 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 Turnerbac69d32016-07-22 19:56:05 +000097 return make_error<MsfError>(msf_error_code::insufficient_buffer,
Zachary Turnerf52a8992016-07-15 20:43:38 +000098 "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
116uint32_t MsfBuilder::getNumUsedBlocks() const {
117 return getTotalBlockCount() - getNumFreeBlocks();
118}
119
120uint32_t MsfBuilder::getNumFreeBlocks() const { return FreeBlocks.count(); }
121
122uint32_t MsfBuilder::getTotalBlockCount() const { return FreeBlocks.size(); }
123
124bool MsfBuilder::isBlockFree(uint32_t Idx) const { return FreeBlocks[Idx]; }
125
126Error 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 Turnerbac69d32016-07-22 19:56:05 +0000132 return make_error<MsfError>(
133 msf_error_code::invalid_format,
Zachary Turnerf52a8992016-07-15 20:43:38 +0000134 "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 Turnerbac69d32016-07-22 19:56:05 +0000140 return make_error<MsfError>(
141 msf_error_code::unspecified,
Zachary Turnerf52a8992016-07-15 20:43:38 +0000142 "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
152Error 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
162Error 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
195uint32_t MsfBuilder::getNumStreams() const { return StreamData.size(); }
196
197uint32_t MsfBuilder::getStreamSize(uint32_t StreamIdx) const {
198 return StreamData[StreamIdx].first;
199}
200
201ArrayRef<uint32_t> MsfBuilder::getStreamBlocks(uint32_t StreamIdx) const {
202 return StreamData[StreamIdx].second;
203}
204
205uint32_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
221Expected<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 Turnerb927e022016-07-15 22:17:19 +0000228 L.SB->FreeBlockMapBlock = FreePageMap;
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000229 L.SB->Unknown1 = Unknown1;
Zachary Turnerf52a8992016-07-15 20:43:38 +0000230
231 uint32_t NumDirectoryBlocks =
232 bytesToBlocks(L.SB->NumDirectoryBytes, BlockSize);
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000233 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 Turnerf52a8992016-07-15 20:43:38 +0000250
251 // Don't set the number of blocks in the file until after allocating Blocks
Zachary Turnerbac69d32016-07-22 19:56:05 +0000252 // for the directory, since the allocation might cause the file to need to
253 // grow.
Zachary Turnerf52a8992016-07-15 20:43:38 +0000254 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}