blob: 0b378db51ffb1d5d3c9145d57b02f62487b7474c [file] [log] [blame]
Zachary Turnerf52a8992016-07-15 20:43:38 +00001//
2// The LLVM Compiler Infrastructure
3//
4// This file is distributed under the University of Illinois Open Source
5// License. See LICENSE.TXT for details.
6//
7//===----------------------------------------------------------------------===//
8
Zachary Turnera3225b02016-07-29 20:56:36 +00009#include "llvm/DebugInfo/MSF/MSFBuilder.h"
10#include "llvm/DebugInfo/MSF/MSFError.h"
Zachary Turnerf52a8992016-07-15 20:43:38 +000011
12using namespace llvm;
Zachary Turnerbac69d32016-07-22 19:56:05 +000013using namespace llvm::msf;
Zachary Turnerf52a8992016-07-15 20:43:38 +000014using namespace llvm::support;
15
16namespace {
17const uint32_t kSuperBlockBlock = 0;
Zachary Turnerb927e022016-07-15 22:17:19 +000018const uint32_t kFreePageMap0Block = 1;
19const uint32_t kFreePageMap1Block = 2;
20const uint32_t kNumReservedPages = 3;
21
22const uint32_t kDefaultBlockMapAddr = kNumReservedPages;
Zachary Turnerf52a8992016-07-15 20:43:38 +000023}
24
Zachary Turnera3225b02016-07-29 20:56:36 +000025MSFBuilder::MSFBuilder(uint32_t BlockSize, uint32_t MinBlockCount, bool CanGrow,
Zachary Turnerf52a8992016-07-15 20:43:38 +000026 BumpPtrAllocator &Allocator)
Saleem Abdulrasoolea6a4fe2016-07-15 21:10:31 +000027 : Allocator(Allocator), IsGrowable(CanGrow), BlockSize(BlockSize),
28 MininumBlocks(MinBlockCount), BlockMapAddr(kDefaultBlockMapAddr),
Zachary Turnerb927e022016-07-15 22:17:19 +000029 FreeBlocks(MinBlockCount, true) {
Zachary Turnerf52a8992016-07-15 20:43:38 +000030 FreeBlocks[kSuperBlockBlock] = false;
Zachary Turnerb927e022016-07-15 22:17:19 +000031 FreeBlocks[kFreePageMap0Block] = false;
32 FreeBlocks[kFreePageMap1Block] = false;
Zachary Turnerf52a8992016-07-15 20:43:38 +000033 FreeBlocks[BlockMapAddr] = false;
34}
35
Zachary Turnera3225b02016-07-29 20:56:36 +000036Expected<MSFBuilder> MSFBuilder::create(BumpPtrAllocator &Allocator,
Zachary Turnerf52a8992016-07-15 20:43:38 +000037 uint32_t BlockSize,
38 uint32_t MinBlockCount, bool CanGrow) {
Zachary Turnerbac69d32016-07-22 19:56:05 +000039 if (!isValidBlockSize(BlockSize))
Zachary Turnera3225b02016-07-29 20:56:36 +000040 return make_error<MSFError>(msf_error_code::invalid_format,
Zachary Turnerf52a8992016-07-15 20:43:38 +000041 "The requested block size is unsupported");
42
Zachary Turnera3225b02016-07-29 20:56:36 +000043 return MSFBuilder(BlockSize,
Zachary Turnerb927e022016-07-15 22:17:19 +000044 std::max(MinBlockCount, msf::getMinimumBlockCount()),
45 CanGrow, Allocator);
Zachary Turnerf52a8992016-07-15 20:43:38 +000046}
47
Zachary Turnera3225b02016-07-29 20:56:36 +000048Error MSFBuilder::setBlockMapAddr(uint32_t Addr) {
Zachary Turnerf52a8992016-07-15 20:43:38 +000049 if (Addr == BlockMapAddr)
50 return Error::success();
51
52 if (Addr >= FreeBlocks.size()) {
53 if (!IsGrowable)
Zachary Turnera3225b02016-07-29 20:56:36 +000054 return make_error<MSFError>(msf_error_code::insufficient_buffer,
Zachary Turnerf52a8992016-07-15 20:43:38 +000055 "Cannot grow the number of blocks");
Rui Ueyama4ee7f3c2016-08-02 21:56:37 +000056 FreeBlocks.resize(Addr + 1, true);
Zachary Turnerf52a8992016-07-15 20:43:38 +000057 }
58
59 if (!isBlockFree(Addr))
Zachary Turnera3225b02016-07-29 20:56:36 +000060 return make_error<MSFError>(
Zachary Turnerbac69d32016-07-22 19:56:05 +000061 msf_error_code::block_in_use,
62 "Requested block map address is already in use");
Zachary Turnerf52a8992016-07-15 20:43:38 +000063 FreeBlocks[BlockMapAddr] = true;
64 FreeBlocks[Addr] = false;
65 BlockMapAddr = Addr;
66 return Error::success();
67}
68
Zachary Turnera3225b02016-07-29 20:56:36 +000069void MSFBuilder::setFreePageMap(uint32_t Fpm) { FreePageMap = Fpm; }
Zachary Turnerfaa554b2016-07-15 22:16:56 +000070
Zachary Turnera3225b02016-07-29 20:56:36 +000071void MSFBuilder::setUnknown1(uint32_t Unk1) { Unknown1 = Unk1; }
Zachary Turnerfaa554b2016-07-15 22:16:56 +000072
Zachary Turnera3225b02016-07-29 20:56:36 +000073Error MSFBuilder::setDirectoryBlocksHint(ArrayRef<uint32_t> DirBlocks) {
Zachary Turnerfaa554b2016-07-15 22:16:56 +000074 for (auto B : DirectoryBlocks)
75 FreeBlocks[B] = true;
76 for (auto B : DirBlocks) {
77 if (!isBlockFree(B)) {
Zachary Turnera3225b02016-07-29 20:56:36 +000078 return make_error<MSFError>(msf_error_code::unspecified,
Zachary Turnerfaa554b2016-07-15 22:16:56 +000079 "Attempt to reuse an allocated block");
80 }
81 FreeBlocks[B] = false;
82 }
83
84 DirectoryBlocks = DirBlocks;
85 return Error::success();
86}
87
Zachary Turnera3225b02016-07-29 20:56:36 +000088Error MSFBuilder::allocateBlocks(uint32_t NumBlocks,
Zachary Turnerf52a8992016-07-15 20:43:38 +000089 MutableArrayRef<uint32_t> Blocks) {
90 if (NumBlocks == 0)
91 return Error::success();
92
93 uint32_t NumFreeBlocks = FreeBlocks.count();
94 if (NumFreeBlocks < NumBlocks) {
95 if (!IsGrowable)
Zachary Turnera3225b02016-07-29 20:56:36 +000096 return make_error<MSFError>(msf_error_code::insufficient_buffer,
Zachary Turnerf52a8992016-07-15 20:43:38 +000097 "There are no free Blocks in the file");
98 uint32_t AllocBlocks = NumBlocks - NumFreeBlocks;
99 FreeBlocks.resize(AllocBlocks + FreeBlocks.size(), true);
100 }
101
102 int I = 0;
103 int Block = FreeBlocks.find_first();
104 do {
105 assert(Block != -1 && "We ran out of Blocks!");
106
107 uint32_t NextBlock = static_cast<uint32_t>(Block);
108 Blocks[I++] = NextBlock;
109 FreeBlocks.reset(NextBlock);
110 Block = FreeBlocks.find_next(Block);
111 } while (--NumBlocks > 0);
112 return Error::success();
113}
114
Zachary Turnera3225b02016-07-29 20:56:36 +0000115uint32_t MSFBuilder::getNumUsedBlocks() const {
Zachary Turnerf52a8992016-07-15 20:43:38 +0000116 return getTotalBlockCount() - getNumFreeBlocks();
117}
118
Zachary Turnera3225b02016-07-29 20:56:36 +0000119uint32_t MSFBuilder::getNumFreeBlocks() const { return FreeBlocks.count(); }
Zachary Turnerf52a8992016-07-15 20:43:38 +0000120
Zachary Turnera3225b02016-07-29 20:56:36 +0000121uint32_t MSFBuilder::getTotalBlockCount() const { return FreeBlocks.size(); }
Zachary Turnerf52a8992016-07-15 20:43:38 +0000122
Zachary Turnera3225b02016-07-29 20:56:36 +0000123bool MSFBuilder::isBlockFree(uint32_t Idx) const { return FreeBlocks[Idx]; }
Zachary Turnerf52a8992016-07-15 20:43:38 +0000124
Zachary Turner620961d2016-09-14 23:00:02 +0000125Expected<uint32_t> MSFBuilder::addStream(uint32_t Size,
126 ArrayRef<uint32_t> Blocks) {
Zachary Turnerf52a8992016-07-15 20:43:38 +0000127 // 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 Turnera3225b02016-07-29 20:56:36 +0000132 return make_error<MSFError>(
Zachary Turnerbac69d32016-07-22 19:56:05 +0000133 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 Turnera3225b02016-07-29 20:56:36 +0000140 return make_error<MSFError>(
Zachary Turnerbac69d32016-07-22 19:56:05 +0000141 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));
Zachary Turner620961d2016-09-14 23:00:02 +0000149 return StreamData.size() - 1;
Zachary Turnerf52a8992016-07-15 20:43:38 +0000150}
151
Zachary Turner620961d2016-09-14 23:00:02 +0000152Expected<uint32_t> MSFBuilder::addStream(uint32_t Size) {
Zachary Turnerf52a8992016-07-15 20:43:38 +0000153 uint32_t ReqBlocks = bytesToBlocks(Size, BlockSize);
154 std::vector<uint32_t> NewBlocks;
155 NewBlocks.resize(ReqBlocks);
156 if (auto EC = allocateBlocks(ReqBlocks, NewBlocks))
Zachary Turner620961d2016-09-14 23:00:02 +0000157 return std::move(EC);
Zachary Turnerf52a8992016-07-15 20:43:38 +0000158 StreamData.push_back(std::make_pair(Size, NewBlocks));
Zachary Turner620961d2016-09-14 23:00:02 +0000159 return StreamData.size() - 1;
Zachary Turnerf52a8992016-07-15 20:43:38 +0000160}
161
Zachary Turnera3225b02016-07-29 20:56:36 +0000162Error MSFBuilder::setStreamSize(uint32_t Idx, uint32_t Size) {
Zachary Turnerf52a8992016-07-15 20:43:38 +0000163 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
Zachary Turnera3225b02016-07-29 20:56:36 +0000195uint32_t MSFBuilder::getNumStreams() const { return StreamData.size(); }
Zachary Turnerf52a8992016-07-15 20:43:38 +0000196
Zachary Turnera3225b02016-07-29 20:56:36 +0000197uint32_t MSFBuilder::getStreamSize(uint32_t StreamIdx) const {
Zachary Turnerf52a8992016-07-15 20:43:38 +0000198 return StreamData[StreamIdx].first;
199}
200
Zachary Turnera3225b02016-07-29 20:56:36 +0000201ArrayRef<uint32_t> MSFBuilder::getStreamBlocks(uint32_t StreamIdx) const {
Zachary Turnerf52a8992016-07-15 20:43:38 +0000202 return StreamData[StreamIdx].second;
203}
204
Zachary Turnera3225b02016-07-29 20:56:36 +0000205uint32_t MSFBuilder::computeDirectoryByteSize() const {
Zachary Turnerf52a8992016-07-15 20:43:38 +0000206 // 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
Zachary Turnera3225b02016-07-29 20:56:36 +0000221Expected<MSFLayout> MSFBuilder::build() {
Zachary Turnere4a4f332016-07-22 19:56:33 +0000222 SuperBlock *SB = Allocator.Allocate<SuperBlock>();
Zachary Turnera3225b02016-07-29 20:56:36 +0000223 MSFLayout L;
Zachary Turnere4a4f332016-07-22 19:56:33 +0000224 L.SB = SB;
Zachary Turnerf52a8992016-07-15 20:43:38 +0000225
Zachary Turnere4a4f332016-07-22 19:56:33 +0000226 std::memcpy(SB->MagicBytes, Magic, sizeof(Magic));
227 SB->BlockMapAddr = BlockMapAddr;
228 SB->BlockSize = BlockSize;
229 SB->NumDirectoryBytes = computeDirectoryByteSize();
230 SB->FreeBlockMapBlock = FreePageMap;
231 SB->Unknown1 = Unknown1;
232
233 uint32_t NumDirectoryBlocks = bytesToBlocks(SB->NumDirectoryBytes, BlockSize);
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000234 if (NumDirectoryBlocks > DirectoryBlocks.size()) {
235 // Our hint wasn't enough to satisfy the entire directory. Allocate
236 // remaining pages.
237 std::vector<uint32_t> ExtraBlocks;
238 uint32_t NumExtraBlocks = NumDirectoryBlocks - DirectoryBlocks.size();
239 ExtraBlocks.resize(NumExtraBlocks);
240 if (auto EC = allocateBlocks(NumExtraBlocks, ExtraBlocks))
241 return std::move(EC);
242 DirectoryBlocks.insert(DirectoryBlocks.end(), ExtraBlocks.begin(),
243 ExtraBlocks.end());
244 } else if (NumDirectoryBlocks < DirectoryBlocks.size()) {
245 uint32_t NumUnnecessaryBlocks = DirectoryBlocks.size() - NumDirectoryBlocks;
246 for (auto B :
247 ArrayRef<uint32_t>(DirectoryBlocks).drop_back(NumUnnecessaryBlocks))
248 FreeBlocks[B] = true;
249 DirectoryBlocks.resize(NumDirectoryBlocks);
250 }
Zachary Turnerf52a8992016-07-15 20:43:38 +0000251
252 // Don't set the number of blocks in the file until after allocating Blocks
Zachary Turnerbac69d32016-07-22 19:56:05 +0000253 // for the directory, since the allocation might cause the file to need to
254 // grow.
Zachary Turnere4a4f332016-07-22 19:56:33 +0000255 SB->NumBlocks = FreeBlocks.size();
Zachary Turnerf52a8992016-07-15 20:43:38 +0000256
257 ulittle32_t *DirBlocks = Allocator.Allocate<ulittle32_t>(NumDirectoryBlocks);
258 std::uninitialized_copy_n(DirectoryBlocks.begin(), NumDirectoryBlocks,
259 DirBlocks);
260 L.DirectoryBlocks = ArrayRef<ulittle32_t>(DirBlocks, NumDirectoryBlocks);
261
262 // The stream sizes should be re-allocated as a stable pointer and the stream
263 // map should have each of its entries allocated as a separate stable pointer.
264 if (StreamData.size() > 0) {
265 ulittle32_t *Sizes = Allocator.Allocate<ulittle32_t>(StreamData.size());
266 L.StreamSizes = ArrayRef<ulittle32_t>(Sizes, StreamData.size());
267 L.StreamMap.resize(StreamData.size());
268 for (uint32_t I = 0; I < StreamData.size(); ++I) {
269 Sizes[I] = StreamData[I].first;
270 ulittle32_t *BlockList =
271 Allocator.Allocate<ulittle32_t>(StreamData[I].second.size());
272 std::uninitialized_copy_n(StreamData[I].second.begin(),
273 StreamData[I].second.size(), BlockList);
274 L.StreamMap[I] =
275 ArrayRef<ulittle32_t>(BlockList, StreamData[I].second.size());
276 }
277 }
278
279 return L;
280}