40 : Allocator(Allocator), IsGrowable(CanGrow),
41 FreePageMap(kDefaultFreePageMap), BlockSize(BlockSize),
42 BlockMapAddr(kDefaultBlockMapAddr), FreeBlocks(MinBlockCount,
true) {
46 FreeBlocks[BlockMapAddr] =
false;
51 uint32_t MinBlockCount,
bool CanGrow) {
54 "The requested block size is unsupported");
62 if (Addr == BlockMapAddr)
65 if (Addr >= FreeBlocks.
size()) {
68 "Cannot grow the number of blocks");
69 FreeBlocks.
resize(Addr + 1,
true);
73 return make_error<MSFError>(
75 "Requested block map address is already in use");
76 FreeBlocks[BlockMapAddr] =
true;
77 FreeBlocks[Addr] =
false;
87 for (
auto B : DirectoryBlocks)
89 for (
auto B : DirBlocks) {
92 "Attempt to reuse an allocated block");
94 FreeBlocks[
B] =
false;
97 DirectoryBlocks = DirBlocks;
107 if (NumFreeBlocks < NumBlocks) {
110 "There are no free Blocks in the file");
111 uint32_t AllocBlocks = NumBlocks - NumFreeBlocks;
113 uint32_t NewBlockCount = AllocBlocks + OldBlockCount;
115 FreeBlocks.
resize(NewBlockCount,
true);
123 while (NextFpmBlock < NewBlockCount) {
125 FreeBlocks.
resize(NewBlockCount,
true);
126 FreeBlocks.
reset(NextFpmBlock, NextFpmBlock + 2);
127 NextFpmBlock += BlockSize;
134 assert(Block != -1 &&
"We ran out of Blocks!");
137 Blocks[I++] = NextBlock;
138 FreeBlocks.
reset(NextBlock);
140 }
while (--NumBlocks > 0);
160 if (ReqBlocks != Blocks.
size())
161 return make_error<MSFError>(
163 "Incorrect number of blocks for requested stream size");
164 for (
auto Block : Blocks) {
165 if (Block >= FreeBlocks.
size())
166 FreeBlocks.
resize(Block + 1,
true);
168 if (!FreeBlocks.
test(Block))
169 return make_error<MSFError>(
171 "Attempt to re-use an already allocated block");
174 for (
auto Block : Blocks) {
175 FreeBlocks.
reset(Block);
177 StreamData.push_back(std::make_pair(Size, Blocks));
178 return StreamData.size() - 1;
183 std::vector<uint32_t> NewBlocks;
184 NewBlocks.resize(ReqBlocks);
185 if (
auto EC = allocateBlocks(ReqBlocks, NewBlocks))
186 return std::move(EC);
187 StreamData.push_back(std::make_pair(Size, NewBlocks));
188 return StreamData.size() - 1;
199 if (NewBlocks > OldBlocks) {
200 uint32_t AddedBlocks = NewBlocks - OldBlocks;
202 std::vector<uint32_t> AddedBlockList;
203 AddedBlockList.resize(AddedBlocks);
204 if (
auto EC = allocateBlocks(AddedBlocks, AddedBlockList))
206 auto &CurrentBlocks = StreamData[Idx].second;
207 CurrentBlocks.insert(CurrentBlocks.end(), AddedBlockList.begin(),
208 AddedBlockList.end());
209 }
else if (OldBlocks > NewBlocks) {
212 uint32_t RemovedBlocks = OldBlocks - NewBlocks;
214 auto RemovedBlockList = CurrentBlocks.
drop_front(NewBlocks);
215 for (
auto P : RemovedBlockList)
216 FreeBlocks[
P] =
true;
217 StreamData[Idx].second = CurrentBlocks.drop_back(RemovedBlocks);
220 StreamData[Idx].first =
Size;
227 return StreamData[StreamIdx].first;
231 return StreamData[StreamIdx].second;
234 uint32_t MSFBuilder::computeDirectoryByteSize()
const {
241 for (
const auto &
D : StreamData) {
243 assert(ExpectedNumBlocks ==
D.second.size() &&
244 "Unexpected number of blocks");
263 if (NumDirectoryBlocks > DirectoryBlocks.size()) {
266 std::vector<uint32_t> ExtraBlocks;
267 uint32_t NumExtraBlocks = NumDirectoryBlocks - DirectoryBlocks.size();
268 ExtraBlocks.resize(NumExtraBlocks);
269 if (
auto EC = allocateBlocks(NumExtraBlocks, ExtraBlocks))
270 return std::move(EC);
271 DirectoryBlocks.insert(DirectoryBlocks.end(), ExtraBlocks.begin(),
273 }
else if (NumDirectoryBlocks < DirectoryBlocks.size()) {
274 uint32_t NumUnnecessaryBlocks = DirectoryBlocks.size() - NumDirectoryBlocks;
277 FreeBlocks[
B] =
true;
278 DirectoryBlocks.resize(NumDirectoryBlocks);
287 std::uninitialized_copy_n(DirectoryBlocks.begin(), NumDirectoryBlocks,
293 if (!StreamData.empty()) {
296 L.StreamMap.resize(StreamData.size());
298 Sizes[
I] = StreamData[
I].first;
301 std::uninitialized_copy_n(StreamData[
I].
second.begin(),
302 StreamData[
I].second.size(), BlockList);
308 L.FreePageMap = FreeBlocks;
324 while (BI < Layout.SB->NumBlocks) {
325 uint8_t ThisByte = 0;
329 uint8_t
Mask = uint8_t(IsFree) <<
I;
344 Layout = std::move(*L);
348 if (
auto EC = OutFileOrError.takeError())
349 return std::move(EC);
356 return std::move(EC);
364 return std::move(EC);
367 Layout, Buffer, Allocator);
370 return std::move(EC);
373 return std::move(EC);
375 for (
const auto &Blocks : Layout.
StreamMap) {
377 return std::move(EC);
380 return std::move(Buffer);
Error writeObject(const T &Obj)
Writes the object Obj to the underlying stream, as if by using memcpy.
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
char MagicBytes[sizeof(Magic)]
void setFreePageMap(uint32_t Fpm)
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
static std::unique_ptr< WritableMappedBlockStream > createDirectoryStream(const MSFLayout &Layout, WritableBinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
static 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...
This class represents lattice values for constants.
static const uint32_t kFreePageMap0Block
Error setBlockMapAddr(uint32_t Addr)
Request the block map to be at a specific block address.
uint64_t blockToOffset(uint64_t BlockNumber, uint64_t BlockSize)
bool test(unsigned Idx) const
uint32_t getMinimumBlockCount()
block Block Frequency true
Error takeError()
Take ownership of the stored error.
Expected< MSFLayout > generateLayout()
Finalize the layout and build the headers and structures that describe the MSF layout and can be writ...
static const uint32_t kNumReservedPages
static const uint32_t kDefaultFreePageMap
int find_first() const
find_first - Returns the index of the first set bit, -1 if none of the bits are set.
int find_next(unsigned Prev) const
find_next - Returns the index of the next set bit following the "Prev" bit.
uint32_t getNumFreeBlocks() const
Get the total number of blocks that exist in the MSF file but are not allocated to any valid data...
Tagged union holding either a T or a Error.
static const uint32_t kSuperBlockBlock
Error setDirectoryBlocksHint(ArrayRef< uint32_t > DirBlocks)
support::ulittle32_t BlockSize
Error writeArray(ArrayRef< T > Array)
Writes an array of objects of type T to the underlying stream, as if by using memcpy.
static void commitFpm(WritableBinaryStream &MsfBuffer, const MSFLayout &Layout, BumpPtrAllocator &Allocator)
uint32_t getNumUsedBlocks() const
Get the total number of blocks that will be allocated to actual data in this MSF file.
support::ulittle32_t BlockMapAddr
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
uint32_t getTotalBlockCount() const
Get the total number of blocks in the MSF file.
detail::packed_endian_specific_integral< uint32_t, little, unaligned > ulittle32_t
Allocate memory in an ever growing pool, as if by bump-pointer.
size_t size() const
size - Get the array size.
bool isBlockFree(uint32_t Idx) const
Check whether a particular block is allocated or free.
Provides write only access to a subclass of WritableBinaryStream.
static const char Magic[]
Error writeInteger(T Value)
Write the integer Value to the underlying stream in the specified endianness.
support::ulittle32_t Unknown1
uint32_t getStreamSize(uint32_t StreamIdx) const
Get the size of a stream by index.
uint32_t bytesRemaining() const
Error setStreamSize(uint32_t Idx, uint32_t Size)
Update the size of an existing stream.
static const int BlockSize
static const uint32_t kDefaultBlockMapAddr
static ErrorSuccess success()
Create a success value.
size_type count() const
count - Returns the number of bits which are set.
Align max(MaybeAlign Lhs, Align Rhs)
ArrayRef< support::ulittle32_t > DirectoryBlocks
static std::unique_ptr< WritableMappedBlockStream > createFpmStream(const MSFLayout &Layout, WritableBinaryStreamRef MsfData, BumpPtrAllocator &Allocator, bool AltFpm=false)
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 GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
Expected< FileBufferByteStream > commit(StringRef Path, MSFLayout &Layout)
Write the MSF layout to the underlying file.
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, Align Alignment)
Allocate space at the specified alignment.
void setOffset(uint32_t Off)
uint32_t getNumStreams() const
Get the total number of streams in the MSF layout.
uint64_t bytesToBlocks(uint64_t NumBytes, uint64_t BlockSize)
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
static const uint32_t kFreePageMap1Block
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
void setUnknown1(uint32_t Unk1)
size_type size() const
size - Returns the number of bits in this bitvector.
std::vector< ArrayRef< support::ulittle32_t > > StreamMap
ArrayRef< uint32_t > getStreamBlocks(uint32_t StreamIdx) const
Get the list of blocks allocated to a particular stream.
ArrayRef< support::ulittle32_t > StreamSizes
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
support::ulittle32_t FreeBlockMapBlock
Lightweight error class with error context and mandatory checking.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
bool isValidBlockSize(uint32_t Size)
An implementation of WritableBinaryStream backed by an llvm FileOutputBuffer.
StringRef - Represent a constant reference to a string, i.e.
support::ulittle32_t NumDirectoryBytes
support::ulittle32_t NumBlocks
A BinaryStream which can be read from as well as written to.