diff options
author | orivej <orivej@yandex-team.ru> | 2022-02-10 16:45:01 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:01 +0300 |
commit | 2d37894b1b037cf24231090eda8589bbb44fb6fc (patch) | |
tree | be835aa92c6248212e705f25388ebafcf84bc7a1 /contrib/libs/llvm12/lib/DebugInfo/MSF | |
parent | 718c552901d703c502ccbefdfc3c9028d608b947 (diff) | |
download | ydb-2d37894b1b037cf24231090eda8589bbb44fb6fc.tar.gz |
Restoring authorship annotation for <orivej@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'contrib/libs/llvm12/lib/DebugInfo/MSF')
-rw-r--r-- | contrib/libs/llvm12/lib/DebugInfo/MSF/MSFBuilder.cpp | 754 | ||||
-rw-r--r-- | contrib/libs/llvm12/lib/DebugInfo/MSF/MSFCommon.cpp | 164 | ||||
-rw-r--r-- | contrib/libs/llvm12/lib/DebugInfo/MSF/MSFError.cpp | 94 | ||||
-rw-r--r-- | contrib/libs/llvm12/lib/DebugInfo/MSF/MappedBlockStream.cpp | 842 | ||||
-rw-r--r-- | contrib/libs/llvm12/lib/DebugInfo/MSF/ya.make | 42 |
5 files changed, 948 insertions, 948 deletions
diff --git a/contrib/libs/llvm12/lib/DebugInfo/MSF/MSFBuilder.cpp b/contrib/libs/llvm12/lib/DebugInfo/MSF/MSFBuilder.cpp index c9d70d77ce..f946dd4860 100644 --- a/contrib/libs/llvm12/lib/DebugInfo/MSF/MSFBuilder.cpp +++ b/contrib/libs/llvm12/lib/DebugInfo/MSF/MSFBuilder.cpp @@ -1,379 +1,379 @@ -//===- MSFBuilder.cpp -----------------------------------------------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#include "llvm/DebugInfo/MSF/MSFBuilder.h" -#include "llvm/ADT/ArrayRef.h" -#include "llvm/DebugInfo/MSF/MSFError.h" -#include "llvm/DebugInfo/MSF/MappedBlockStream.h" -#include "llvm/Support/BinaryByteStream.h" -#include "llvm/Support/BinaryStreamWriter.h" -#include "llvm/Support/Endian.h" -#include "llvm/Support/Error.h" -#include "llvm/Support/FileOutputBuffer.h" -#include <algorithm> -#include <cassert> -#include <cstdint> -#include <cstring> -#include <memory> -#include <utility> -#include <vector> - -using namespace llvm; -using namespace llvm::msf; -using namespace llvm::support; - -static const uint32_t kSuperBlockBlock = 0; -static const uint32_t kFreePageMap0Block = 1; -static const uint32_t kFreePageMap1Block = 2; -static const uint32_t kNumReservedPages = 3; - -static const uint32_t kDefaultFreePageMap = kFreePageMap1Block; -static const uint32_t kDefaultBlockMapAddr = kNumReservedPages; - -MSFBuilder::MSFBuilder(uint32_t BlockSize, uint32_t MinBlockCount, bool CanGrow, - BumpPtrAllocator &Allocator) - : Allocator(Allocator), IsGrowable(CanGrow), - FreePageMap(kDefaultFreePageMap), BlockSize(BlockSize), - BlockMapAddr(kDefaultBlockMapAddr), FreeBlocks(MinBlockCount, true) { - FreeBlocks[kSuperBlockBlock] = false; - FreeBlocks[kFreePageMap0Block] = false; - FreeBlocks[kFreePageMap1Block] = false; - FreeBlocks[BlockMapAddr] = false; -} - -Expected<MSFBuilder> MSFBuilder::create(BumpPtrAllocator &Allocator, - uint32_t BlockSize, - uint32_t MinBlockCount, bool CanGrow) { - if (!isValidBlockSize(BlockSize)) - return make_error<MSFError>(msf_error_code::invalid_format, - "The requested block size is unsupported"); - - return MSFBuilder(BlockSize, - std::max(MinBlockCount, msf::getMinimumBlockCount()), - CanGrow, Allocator); -} - -Error MSFBuilder::setBlockMapAddr(uint32_t Addr) { - if (Addr == BlockMapAddr) - return Error::success(); - - if (Addr >= FreeBlocks.size()) { - if (!IsGrowable) - return make_error<MSFError>(msf_error_code::insufficient_buffer, - "Cannot grow the number of blocks"); - FreeBlocks.resize(Addr + 1, true); - } - - if (!isBlockFree(Addr)) - return make_error<MSFError>( - msf_error_code::block_in_use, - "Requested block map address is already in use"); - FreeBlocks[BlockMapAddr] = true; - FreeBlocks[Addr] = false; - BlockMapAddr = Addr; - return Error::success(); -} - -void MSFBuilder::setFreePageMap(uint32_t Fpm) { FreePageMap = Fpm; } - -void MSFBuilder::setUnknown1(uint32_t Unk1) { Unknown1 = Unk1; } - -Error MSFBuilder::setDirectoryBlocksHint(ArrayRef<uint32_t> DirBlocks) { - for (auto B : DirectoryBlocks) - FreeBlocks[B] = true; - for (auto B : DirBlocks) { - if (!isBlockFree(B)) { - return make_error<MSFError>(msf_error_code::unspecified, - "Attempt to reuse an allocated block"); - } - FreeBlocks[B] = false; - } - - DirectoryBlocks = DirBlocks; - return Error::success(); -} - -Error MSFBuilder::allocateBlocks(uint32_t NumBlocks, - MutableArrayRef<uint32_t> Blocks) { - if (NumBlocks == 0) - return Error::success(); - - uint32_t NumFreeBlocks = FreeBlocks.count(); - if (NumFreeBlocks < NumBlocks) { - if (!IsGrowable) - return make_error<MSFError>(msf_error_code::insufficient_buffer, - "There are no free Blocks in the file"); - uint32_t AllocBlocks = NumBlocks - NumFreeBlocks; - uint32_t OldBlockCount = FreeBlocks.size(); - uint32_t NewBlockCount = AllocBlocks + OldBlockCount; - uint32_t NextFpmBlock = alignTo(OldBlockCount, BlockSize) + 1; - FreeBlocks.resize(NewBlockCount, true); - // If we crossed over an fpm page, we actually need to allocate 2 extra - // blocks for each FPM group crossed and mark both blocks from the group as - // used. FPM blocks are marked as allocated regardless of whether or not - // they ultimately describe the status of blocks in the file. This means - // that not only are extraneous blocks at the end of the main FPM marked as - // allocated, but also blocks from the alternate FPM are always marked as - // allocated. - while (NextFpmBlock < NewBlockCount) { - NewBlockCount += 2; - FreeBlocks.resize(NewBlockCount, true); - FreeBlocks.reset(NextFpmBlock, NextFpmBlock + 2); - NextFpmBlock += BlockSize; - } - } - - int I = 0; - int Block = FreeBlocks.find_first(); - do { - assert(Block != -1 && "We ran out of Blocks!"); - - uint32_t NextBlock = static_cast<uint32_t>(Block); - Blocks[I++] = NextBlock; - FreeBlocks.reset(NextBlock); - Block = FreeBlocks.find_next(Block); - } while (--NumBlocks > 0); - return Error::success(); -} - -uint32_t MSFBuilder::getNumUsedBlocks() const { - return getTotalBlockCount() - getNumFreeBlocks(); -} - -uint32_t MSFBuilder::getNumFreeBlocks() const { return FreeBlocks.count(); } - -uint32_t MSFBuilder::getTotalBlockCount() const { return FreeBlocks.size(); } - -bool MSFBuilder::isBlockFree(uint32_t Idx) const { return FreeBlocks[Idx]; } - -Expected<uint32_t> MSFBuilder::addStream(uint32_t Size, - ArrayRef<uint32_t> Blocks) { - // Add a new stream mapped to the specified blocks. Verify that the specified - // blocks are both necessary and sufficient for holding the requested number - // of bytes, and verify that all requested blocks are free. - uint32_t ReqBlocks = bytesToBlocks(Size, BlockSize); - if (ReqBlocks != Blocks.size()) - return make_error<MSFError>( - msf_error_code::invalid_format, - "Incorrect number of blocks for requested stream size"); - for (auto Block : Blocks) { - if (Block >= FreeBlocks.size()) - FreeBlocks.resize(Block + 1, true); - - if (!FreeBlocks.test(Block)) - return make_error<MSFError>( - msf_error_code::unspecified, - "Attempt to re-use an already allocated block"); - } - // Mark all the blocks occupied by the new stream as not free. - for (auto Block : Blocks) { - FreeBlocks.reset(Block); - } - StreamData.push_back(std::make_pair(Size, Blocks)); - return StreamData.size() - 1; -} - -Expected<uint32_t> MSFBuilder::addStream(uint32_t Size) { - uint32_t ReqBlocks = bytesToBlocks(Size, BlockSize); - std::vector<uint32_t> NewBlocks; - NewBlocks.resize(ReqBlocks); - if (auto EC = allocateBlocks(ReqBlocks, NewBlocks)) - return std::move(EC); - StreamData.push_back(std::make_pair(Size, NewBlocks)); - return StreamData.size() - 1; -} - -Error MSFBuilder::setStreamSize(uint32_t Idx, uint32_t Size) { - uint32_t OldSize = getStreamSize(Idx); - if (OldSize == Size) - return Error::success(); - - uint32_t NewBlocks = bytesToBlocks(Size, BlockSize); - uint32_t OldBlocks = bytesToBlocks(OldSize, BlockSize); - - if (NewBlocks > OldBlocks) { - uint32_t AddedBlocks = NewBlocks - OldBlocks; - // If we're growing, we have to allocate new Blocks. - std::vector<uint32_t> AddedBlockList; - AddedBlockList.resize(AddedBlocks); - if (auto EC = allocateBlocks(AddedBlocks, AddedBlockList)) - return EC; - auto &CurrentBlocks = StreamData[Idx].second; +//===- MSFBuilder.cpp -----------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm/DebugInfo/MSF/MSFBuilder.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/DebugInfo/MSF/MSFError.h" +#include "llvm/DebugInfo/MSF/MappedBlockStream.h" +#include "llvm/Support/BinaryByteStream.h" +#include "llvm/Support/BinaryStreamWriter.h" +#include "llvm/Support/Endian.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/FileOutputBuffer.h" +#include <algorithm> +#include <cassert> +#include <cstdint> +#include <cstring> +#include <memory> +#include <utility> +#include <vector> + +using namespace llvm; +using namespace llvm::msf; +using namespace llvm::support; + +static const uint32_t kSuperBlockBlock = 0; +static const uint32_t kFreePageMap0Block = 1; +static const uint32_t kFreePageMap1Block = 2; +static const uint32_t kNumReservedPages = 3; + +static const uint32_t kDefaultFreePageMap = kFreePageMap1Block; +static const uint32_t kDefaultBlockMapAddr = kNumReservedPages; + +MSFBuilder::MSFBuilder(uint32_t BlockSize, uint32_t MinBlockCount, bool CanGrow, + BumpPtrAllocator &Allocator) + : Allocator(Allocator), IsGrowable(CanGrow), + FreePageMap(kDefaultFreePageMap), BlockSize(BlockSize), + BlockMapAddr(kDefaultBlockMapAddr), FreeBlocks(MinBlockCount, true) { + FreeBlocks[kSuperBlockBlock] = false; + FreeBlocks[kFreePageMap0Block] = false; + FreeBlocks[kFreePageMap1Block] = false; + FreeBlocks[BlockMapAddr] = false; +} + +Expected<MSFBuilder> MSFBuilder::create(BumpPtrAllocator &Allocator, + uint32_t BlockSize, + uint32_t MinBlockCount, bool CanGrow) { + if (!isValidBlockSize(BlockSize)) + return make_error<MSFError>(msf_error_code::invalid_format, + "The requested block size is unsupported"); + + return MSFBuilder(BlockSize, + std::max(MinBlockCount, msf::getMinimumBlockCount()), + CanGrow, Allocator); +} + +Error MSFBuilder::setBlockMapAddr(uint32_t Addr) { + if (Addr == BlockMapAddr) + return Error::success(); + + if (Addr >= FreeBlocks.size()) { + if (!IsGrowable) + return make_error<MSFError>(msf_error_code::insufficient_buffer, + "Cannot grow the number of blocks"); + FreeBlocks.resize(Addr + 1, true); + } + + if (!isBlockFree(Addr)) + return make_error<MSFError>( + msf_error_code::block_in_use, + "Requested block map address is already in use"); + FreeBlocks[BlockMapAddr] = true; + FreeBlocks[Addr] = false; + BlockMapAddr = Addr; + return Error::success(); +} + +void MSFBuilder::setFreePageMap(uint32_t Fpm) { FreePageMap = Fpm; } + +void MSFBuilder::setUnknown1(uint32_t Unk1) { Unknown1 = Unk1; } + +Error MSFBuilder::setDirectoryBlocksHint(ArrayRef<uint32_t> DirBlocks) { + for (auto B : DirectoryBlocks) + FreeBlocks[B] = true; + for (auto B : DirBlocks) { + if (!isBlockFree(B)) { + return make_error<MSFError>(msf_error_code::unspecified, + "Attempt to reuse an allocated block"); + } + FreeBlocks[B] = false; + } + + DirectoryBlocks = DirBlocks; + return Error::success(); +} + +Error MSFBuilder::allocateBlocks(uint32_t NumBlocks, + MutableArrayRef<uint32_t> Blocks) { + if (NumBlocks == 0) + return Error::success(); + + uint32_t NumFreeBlocks = FreeBlocks.count(); + if (NumFreeBlocks < NumBlocks) { + if (!IsGrowable) + return make_error<MSFError>(msf_error_code::insufficient_buffer, + "There are no free Blocks in the file"); + uint32_t AllocBlocks = NumBlocks - NumFreeBlocks; + uint32_t OldBlockCount = FreeBlocks.size(); + uint32_t NewBlockCount = AllocBlocks + OldBlockCount; + uint32_t NextFpmBlock = alignTo(OldBlockCount, BlockSize) + 1; + FreeBlocks.resize(NewBlockCount, true); + // If we crossed over an fpm page, we actually need to allocate 2 extra + // blocks for each FPM group crossed and mark both blocks from the group as + // used. FPM blocks are marked as allocated regardless of whether or not + // they ultimately describe the status of blocks in the file. This means + // that not only are extraneous blocks at the end of the main FPM marked as + // allocated, but also blocks from the alternate FPM are always marked as + // allocated. + while (NextFpmBlock < NewBlockCount) { + NewBlockCount += 2; + FreeBlocks.resize(NewBlockCount, true); + FreeBlocks.reset(NextFpmBlock, NextFpmBlock + 2); + NextFpmBlock += BlockSize; + } + } + + int I = 0; + int Block = FreeBlocks.find_first(); + do { + assert(Block != -1 && "We ran out of Blocks!"); + + uint32_t NextBlock = static_cast<uint32_t>(Block); + Blocks[I++] = NextBlock; + FreeBlocks.reset(NextBlock); + Block = FreeBlocks.find_next(Block); + } while (--NumBlocks > 0); + return Error::success(); +} + +uint32_t MSFBuilder::getNumUsedBlocks() const { + return getTotalBlockCount() - getNumFreeBlocks(); +} + +uint32_t MSFBuilder::getNumFreeBlocks() const { return FreeBlocks.count(); } + +uint32_t MSFBuilder::getTotalBlockCount() const { return FreeBlocks.size(); } + +bool MSFBuilder::isBlockFree(uint32_t Idx) const { return FreeBlocks[Idx]; } + +Expected<uint32_t> MSFBuilder::addStream(uint32_t Size, + ArrayRef<uint32_t> Blocks) { + // Add a new stream mapped to the specified blocks. Verify that the specified + // blocks are both necessary and sufficient for holding the requested number + // of bytes, and verify that all requested blocks are free. + uint32_t ReqBlocks = bytesToBlocks(Size, BlockSize); + if (ReqBlocks != Blocks.size()) + return make_error<MSFError>( + msf_error_code::invalid_format, + "Incorrect number of blocks for requested stream size"); + for (auto Block : Blocks) { + if (Block >= FreeBlocks.size()) + FreeBlocks.resize(Block + 1, true); + + if (!FreeBlocks.test(Block)) + return make_error<MSFError>( + msf_error_code::unspecified, + "Attempt to re-use an already allocated block"); + } + // Mark all the blocks occupied by the new stream as not free. + for (auto Block : Blocks) { + FreeBlocks.reset(Block); + } + StreamData.push_back(std::make_pair(Size, Blocks)); + return StreamData.size() - 1; +} + +Expected<uint32_t> MSFBuilder::addStream(uint32_t Size) { + uint32_t ReqBlocks = bytesToBlocks(Size, BlockSize); + std::vector<uint32_t> NewBlocks; + NewBlocks.resize(ReqBlocks); + if (auto EC = allocateBlocks(ReqBlocks, NewBlocks)) + return std::move(EC); + StreamData.push_back(std::make_pair(Size, NewBlocks)); + return StreamData.size() - 1; +} + +Error MSFBuilder::setStreamSize(uint32_t Idx, uint32_t Size) { + uint32_t OldSize = getStreamSize(Idx); + if (OldSize == Size) + return Error::success(); + + uint32_t NewBlocks = bytesToBlocks(Size, BlockSize); + uint32_t OldBlocks = bytesToBlocks(OldSize, BlockSize); + + if (NewBlocks > OldBlocks) { + uint32_t AddedBlocks = NewBlocks - OldBlocks; + // If we're growing, we have to allocate new Blocks. + std::vector<uint32_t> AddedBlockList; + AddedBlockList.resize(AddedBlocks); + if (auto EC = allocateBlocks(AddedBlocks, AddedBlockList)) + return EC; + auto &CurrentBlocks = StreamData[Idx].second; llvm::append_range(CurrentBlocks, AddedBlockList); - } else if (OldBlocks > NewBlocks) { - // For shrinking, free all the Blocks in the Block map, update the stream - // data, then shrink the directory. - uint32_t RemovedBlocks = OldBlocks - NewBlocks; - auto CurrentBlocks = ArrayRef<uint32_t>(StreamData[Idx].second); - auto RemovedBlockList = CurrentBlocks.drop_front(NewBlocks); - for (auto P : RemovedBlockList) - FreeBlocks[P] = true; - StreamData[Idx].second = CurrentBlocks.drop_back(RemovedBlocks); - } - - StreamData[Idx].first = Size; - return Error::success(); -} - -uint32_t MSFBuilder::getNumStreams() const { return StreamData.size(); } - -uint32_t MSFBuilder::getStreamSize(uint32_t StreamIdx) const { - return StreamData[StreamIdx].first; -} - -ArrayRef<uint32_t> MSFBuilder::getStreamBlocks(uint32_t StreamIdx) const { - return StreamData[StreamIdx].second; -} - -uint32_t MSFBuilder::computeDirectoryByteSize() const { - // The directory has the following layout, where each item is a ulittle32_t: - // NumStreams - // StreamSizes[NumStreams] - // StreamBlocks[NumStreams][] - uint32_t Size = sizeof(ulittle32_t); // NumStreams - Size += StreamData.size() * sizeof(ulittle32_t); // StreamSizes - for (const auto &D : StreamData) { - uint32_t ExpectedNumBlocks = bytesToBlocks(D.first, BlockSize); - assert(ExpectedNumBlocks == D.second.size() && - "Unexpected number of blocks"); - Size += ExpectedNumBlocks * sizeof(ulittle32_t); - } - return Size; -} - -Expected<MSFLayout> MSFBuilder::generateLayout() { - SuperBlock *SB = Allocator.Allocate<SuperBlock>(); - MSFLayout L; - L.SB = SB; - - std::memcpy(SB->MagicBytes, Magic, sizeof(Magic)); - SB->BlockMapAddr = BlockMapAddr; - SB->BlockSize = BlockSize; - SB->NumDirectoryBytes = computeDirectoryByteSize(); - SB->FreeBlockMapBlock = FreePageMap; - SB->Unknown1 = Unknown1; - - uint32_t NumDirectoryBlocks = bytesToBlocks(SB->NumDirectoryBytes, BlockSize); - if (NumDirectoryBlocks > DirectoryBlocks.size()) { - // Our hint wasn't enough to satisfy the entire directory. Allocate - // remaining pages. - std::vector<uint32_t> ExtraBlocks; - uint32_t NumExtraBlocks = NumDirectoryBlocks - DirectoryBlocks.size(); - ExtraBlocks.resize(NumExtraBlocks); - if (auto EC = allocateBlocks(NumExtraBlocks, ExtraBlocks)) - return std::move(EC); + } else if (OldBlocks > NewBlocks) { + // For shrinking, free all the Blocks in the Block map, update the stream + // data, then shrink the directory. + uint32_t RemovedBlocks = OldBlocks - NewBlocks; + auto CurrentBlocks = ArrayRef<uint32_t>(StreamData[Idx].second); + auto RemovedBlockList = CurrentBlocks.drop_front(NewBlocks); + for (auto P : RemovedBlockList) + FreeBlocks[P] = true; + StreamData[Idx].second = CurrentBlocks.drop_back(RemovedBlocks); + } + + StreamData[Idx].first = Size; + return Error::success(); +} + +uint32_t MSFBuilder::getNumStreams() const { return StreamData.size(); } + +uint32_t MSFBuilder::getStreamSize(uint32_t StreamIdx) const { + return StreamData[StreamIdx].first; +} + +ArrayRef<uint32_t> MSFBuilder::getStreamBlocks(uint32_t StreamIdx) const { + return StreamData[StreamIdx].second; +} + +uint32_t MSFBuilder::computeDirectoryByteSize() const { + // The directory has the following layout, where each item is a ulittle32_t: + // NumStreams + // StreamSizes[NumStreams] + // StreamBlocks[NumStreams][] + uint32_t Size = sizeof(ulittle32_t); // NumStreams + Size += StreamData.size() * sizeof(ulittle32_t); // StreamSizes + for (const auto &D : StreamData) { + uint32_t ExpectedNumBlocks = bytesToBlocks(D.first, BlockSize); + assert(ExpectedNumBlocks == D.second.size() && + "Unexpected number of blocks"); + Size += ExpectedNumBlocks * sizeof(ulittle32_t); + } + return Size; +} + +Expected<MSFLayout> MSFBuilder::generateLayout() { + SuperBlock *SB = Allocator.Allocate<SuperBlock>(); + MSFLayout L; + L.SB = SB; + + std::memcpy(SB->MagicBytes, Magic, sizeof(Magic)); + SB->BlockMapAddr = BlockMapAddr; + SB->BlockSize = BlockSize; + SB->NumDirectoryBytes = computeDirectoryByteSize(); + SB->FreeBlockMapBlock = FreePageMap; + SB->Unknown1 = Unknown1; + + uint32_t NumDirectoryBlocks = bytesToBlocks(SB->NumDirectoryBytes, BlockSize); + if (NumDirectoryBlocks > DirectoryBlocks.size()) { + // Our hint wasn't enough to satisfy the entire directory. Allocate + // remaining pages. + std::vector<uint32_t> ExtraBlocks; + uint32_t NumExtraBlocks = NumDirectoryBlocks - DirectoryBlocks.size(); + ExtraBlocks.resize(NumExtraBlocks); + if (auto EC = allocateBlocks(NumExtraBlocks, ExtraBlocks)) + return std::move(EC); llvm::append_range(DirectoryBlocks, ExtraBlocks); - } else if (NumDirectoryBlocks < DirectoryBlocks.size()) { - uint32_t NumUnnecessaryBlocks = DirectoryBlocks.size() - NumDirectoryBlocks; - for (auto B : - ArrayRef<uint32_t>(DirectoryBlocks).drop_back(NumUnnecessaryBlocks)) - FreeBlocks[B] = true; - DirectoryBlocks.resize(NumDirectoryBlocks); - } - - // Don't set the number of blocks in the file until after allocating Blocks - // for the directory, since the allocation might cause the file to need to - // grow. - SB->NumBlocks = FreeBlocks.size(); - - ulittle32_t *DirBlocks = Allocator.Allocate<ulittle32_t>(NumDirectoryBlocks); - std::uninitialized_copy_n(DirectoryBlocks.begin(), NumDirectoryBlocks, - DirBlocks); - L.DirectoryBlocks = ArrayRef<ulittle32_t>(DirBlocks, NumDirectoryBlocks); - - // The stream sizes should be re-allocated as a stable pointer and the stream - // map should have each of its entries allocated as a separate stable pointer. - if (!StreamData.empty()) { - ulittle32_t *Sizes = Allocator.Allocate<ulittle32_t>(StreamData.size()); - L.StreamSizes = ArrayRef<ulittle32_t>(Sizes, StreamData.size()); - L.StreamMap.resize(StreamData.size()); - for (uint32_t I = 0; I < StreamData.size(); ++I) { - Sizes[I] = StreamData[I].first; - ulittle32_t *BlockList = - Allocator.Allocate<ulittle32_t>(StreamData[I].second.size()); - std::uninitialized_copy_n(StreamData[I].second.begin(), - StreamData[I].second.size(), BlockList); - L.StreamMap[I] = - ArrayRef<ulittle32_t>(BlockList, StreamData[I].second.size()); - } - } - - L.FreePageMap = FreeBlocks; - - return L; -} - -static void commitFpm(WritableBinaryStream &MsfBuffer, const MSFLayout &Layout, - BumpPtrAllocator &Allocator) { - auto FpmStream = - WritableMappedBlockStream::createFpmStream(Layout, MsfBuffer, Allocator); - - // We only need to create the alt fpm stream so that it gets initialized. - WritableMappedBlockStream::createFpmStream(Layout, MsfBuffer, Allocator, - true); - - uint32_t BI = 0; - BinaryStreamWriter FpmWriter(*FpmStream); - while (BI < Layout.SB->NumBlocks) { - uint8_t ThisByte = 0; - for (uint32_t I = 0; I < 8; ++I) { - bool IsFree = - (BI < Layout.SB->NumBlocks) ? Layout.FreePageMap.test(BI) : true; - uint8_t Mask = uint8_t(IsFree) << I; - ThisByte |= Mask; - ++BI; - } - cantFail(FpmWriter.writeObject(ThisByte)); - } - assert(FpmWriter.bytesRemaining() == 0); -} - -Expected<FileBufferByteStream> MSFBuilder::commit(StringRef Path, - MSFLayout &Layout) { - Expected<MSFLayout> L = generateLayout(); - if (!L) - return L.takeError(); - - Layout = std::move(*L); - - uint64_t FileSize = Layout.SB->BlockSize * Layout.SB->NumBlocks; - auto OutFileOrError = FileOutputBuffer::create(Path, FileSize); - if (auto EC = OutFileOrError.takeError()) - return std::move(EC); - - FileBufferByteStream Buffer(std::move(*OutFileOrError), - llvm::support::little); - BinaryStreamWriter Writer(Buffer); - - if (auto EC = Writer.writeObject(*Layout.SB)) - return std::move(EC); - - commitFpm(Buffer, Layout, Allocator); - - uint32_t BlockMapOffset = - msf::blockToOffset(Layout.SB->BlockMapAddr, Layout.SB->BlockSize); - Writer.setOffset(BlockMapOffset); - if (auto EC = Writer.writeArray(Layout.DirectoryBlocks)) - return std::move(EC); - - auto DirStream = WritableMappedBlockStream::createDirectoryStream( - Layout, Buffer, Allocator); - BinaryStreamWriter DW(*DirStream); - if (auto EC = DW.writeInteger<uint32_t>(Layout.StreamSizes.size())) - return std::move(EC); - - if (auto EC = DW.writeArray(Layout.StreamSizes)) - return std::move(EC); - - for (const auto &Blocks : Layout.StreamMap) { - if (auto EC = DW.writeArray(Blocks)) - return std::move(EC); - } - - return std::move(Buffer); -} + } else if (NumDirectoryBlocks < DirectoryBlocks.size()) { + uint32_t NumUnnecessaryBlocks = DirectoryBlocks.size() - NumDirectoryBlocks; + for (auto B : + ArrayRef<uint32_t>(DirectoryBlocks).drop_back(NumUnnecessaryBlocks)) + FreeBlocks[B] = true; + DirectoryBlocks.resize(NumDirectoryBlocks); + } + + // Don't set the number of blocks in the file until after allocating Blocks + // for the directory, since the allocation might cause the file to need to + // grow. + SB->NumBlocks = FreeBlocks.size(); + + ulittle32_t *DirBlocks = Allocator.Allocate<ulittle32_t>(NumDirectoryBlocks); + std::uninitialized_copy_n(DirectoryBlocks.begin(), NumDirectoryBlocks, + DirBlocks); + L.DirectoryBlocks = ArrayRef<ulittle32_t>(DirBlocks, NumDirectoryBlocks); + + // The stream sizes should be re-allocated as a stable pointer and the stream + // map should have each of its entries allocated as a separate stable pointer. + if (!StreamData.empty()) { + ulittle32_t *Sizes = Allocator.Allocate<ulittle32_t>(StreamData.size()); + L.StreamSizes = ArrayRef<ulittle32_t>(Sizes, StreamData.size()); + L.StreamMap.resize(StreamData.size()); + for (uint32_t I = 0; I < StreamData.size(); ++I) { + Sizes[I] = StreamData[I].first; + ulittle32_t *BlockList = + Allocator.Allocate<ulittle32_t>(StreamData[I].second.size()); + std::uninitialized_copy_n(StreamData[I].second.begin(), + StreamData[I].second.size(), BlockList); + L.StreamMap[I] = + ArrayRef<ulittle32_t>(BlockList, StreamData[I].second.size()); + } + } + + L.FreePageMap = FreeBlocks; + + return L; +} + +static void commitFpm(WritableBinaryStream &MsfBuffer, const MSFLayout &Layout, + BumpPtrAllocator &Allocator) { + auto FpmStream = + WritableMappedBlockStream::createFpmStream(Layout, MsfBuffer, Allocator); + + // We only need to create the alt fpm stream so that it gets initialized. + WritableMappedBlockStream::createFpmStream(Layout, MsfBuffer, Allocator, + true); + + uint32_t BI = 0; + BinaryStreamWriter FpmWriter(*FpmStream); + while (BI < Layout.SB->NumBlocks) { + uint8_t ThisByte = 0; + for (uint32_t I = 0; I < 8; ++I) { + bool IsFree = + (BI < Layout.SB->NumBlocks) ? Layout.FreePageMap.test(BI) : true; + uint8_t Mask = uint8_t(IsFree) << I; + ThisByte |= Mask; + ++BI; + } + cantFail(FpmWriter.writeObject(ThisByte)); + } + assert(FpmWriter.bytesRemaining() == 0); +} + +Expected<FileBufferByteStream> MSFBuilder::commit(StringRef Path, + MSFLayout &Layout) { + Expected<MSFLayout> L = generateLayout(); + if (!L) + return L.takeError(); + + Layout = std::move(*L); + + uint64_t FileSize = Layout.SB->BlockSize * Layout.SB->NumBlocks; + auto OutFileOrError = FileOutputBuffer::create(Path, FileSize); + if (auto EC = OutFileOrError.takeError()) + return std::move(EC); + + FileBufferByteStream Buffer(std::move(*OutFileOrError), + llvm::support::little); + BinaryStreamWriter Writer(Buffer); + + if (auto EC = Writer.writeObject(*Layout.SB)) + return std::move(EC); + + commitFpm(Buffer, Layout, Allocator); + + uint32_t BlockMapOffset = + msf::blockToOffset(Layout.SB->BlockMapAddr, Layout.SB->BlockSize); + Writer.setOffset(BlockMapOffset); + if (auto EC = Writer.writeArray(Layout.DirectoryBlocks)) + return std::move(EC); + + auto DirStream = WritableMappedBlockStream::createDirectoryStream( + Layout, Buffer, Allocator); + BinaryStreamWriter DW(*DirStream); + if (auto EC = DW.writeInteger<uint32_t>(Layout.StreamSizes.size())) + return std::move(EC); + + if (auto EC = DW.writeArray(Layout.StreamSizes)) + return std::move(EC); + + for (const auto &Blocks : Layout.StreamMap) { + if (auto EC = DW.writeArray(Blocks)) + return std::move(EC); + } + + return std::move(Buffer); +} diff --git a/contrib/libs/llvm12/lib/DebugInfo/MSF/MSFCommon.cpp b/contrib/libs/llvm12/lib/DebugInfo/MSF/MSFCommon.cpp index 52b6bdc320..fb4f070005 100644 --- a/contrib/libs/llvm12/lib/DebugInfo/MSF/MSFCommon.cpp +++ b/contrib/libs/llvm12/lib/DebugInfo/MSF/MSFCommon.cpp @@ -1,82 +1,82 @@ -//===- MSFCommon.cpp - Common types and functions for MSF files -----------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#include "llvm/DebugInfo/MSF/MSFCommon.h" -#include "llvm/DebugInfo/MSF/MSFError.h" -#include "llvm/Support/Endian.h" -#include "llvm/Support/Error.h" -#include <cstdint> -#include <cstring> - -using namespace llvm; -using namespace llvm::msf; - -Error llvm::msf::validateSuperBlock(const SuperBlock &SB) { - // Check the magic bytes. - if (std::memcmp(SB.MagicBytes, Magic, sizeof(Magic)) != 0) - return make_error<MSFError>(msf_error_code::invalid_format, - "MSF magic header doesn't match"); - - if (!isValidBlockSize(SB.BlockSize)) - return make_error<MSFError>(msf_error_code::invalid_format, - "Unsupported block size."); - - // We don't support directories whose sizes aren't a multiple of four bytes. - if (SB.NumDirectoryBytes % sizeof(support::ulittle32_t) != 0) - return make_error<MSFError>(msf_error_code::invalid_format, - "Directory size is not multiple of 4."); - - // The number of blocks which comprise the directory is a simple function of - // the number of bytes it contains. - uint64_t NumDirectoryBlocks = - bytesToBlocks(SB.NumDirectoryBytes, SB.BlockSize); - - // The directory, as we understand it, is a block which consists of a list of - // block numbers. It is unclear what would happen if the number of blocks - // couldn't fit on a single block. - if (NumDirectoryBlocks > SB.BlockSize / sizeof(support::ulittle32_t)) - return make_error<MSFError>(msf_error_code::invalid_format, - "Too many directory blocks."); - - if (SB.BlockMapAddr == 0) - return make_error<MSFError>(msf_error_code::invalid_format, - "Block 0 is reserved"); - - if (SB.BlockMapAddr >= SB.NumBlocks) - return make_error<MSFError>(msf_error_code::invalid_format, - "Block map address is invalid."); - - if (SB.FreeBlockMapBlock != 1 && SB.FreeBlockMapBlock != 2) - return make_error<MSFError>( - msf_error_code::invalid_format, - "The free block map isn't at block 1 or block 2."); - - return Error::success(); -} - -MSFStreamLayout llvm::msf::getFpmStreamLayout(const MSFLayout &Msf, - bool IncludeUnusedFpmData, - bool AltFpm) { - MSFStreamLayout FL; - uint32_t NumFpmIntervals = - getNumFpmIntervals(Msf, IncludeUnusedFpmData, AltFpm); - - uint32_t FpmBlock = AltFpm ? Msf.alternateFpmBlock() : Msf.mainFpmBlock(); - - for (uint32_t I = 0; I < NumFpmIntervals; ++I) { - FL.Blocks.push_back(support::ulittle32_t(FpmBlock)); - FpmBlock += msf::getFpmIntervalLength(Msf); - } - - if (IncludeUnusedFpmData) - FL.Length = NumFpmIntervals * Msf.SB->BlockSize; - else - FL.Length = divideCeil(Msf.SB->NumBlocks, 8); - - return FL; -} +//===- MSFCommon.cpp - Common types and functions for MSF files -----------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm/DebugInfo/MSF/MSFCommon.h" +#include "llvm/DebugInfo/MSF/MSFError.h" +#include "llvm/Support/Endian.h" +#include "llvm/Support/Error.h" +#include <cstdint> +#include <cstring> + +using namespace llvm; +using namespace llvm::msf; + +Error llvm::msf::validateSuperBlock(const SuperBlock &SB) { + // Check the magic bytes. + if (std::memcmp(SB.MagicBytes, Magic, sizeof(Magic)) != 0) + return make_error<MSFError>(msf_error_code::invalid_format, + "MSF magic header doesn't match"); + + if (!isValidBlockSize(SB.BlockSize)) + return make_error<MSFError>(msf_error_code::invalid_format, + "Unsupported block size."); + + // We don't support directories whose sizes aren't a multiple of four bytes. + if (SB.NumDirectoryBytes % sizeof(support::ulittle32_t) != 0) + return make_error<MSFError>(msf_error_code::invalid_format, + "Directory size is not multiple of 4."); + + // The number of blocks which comprise the directory is a simple function of + // the number of bytes it contains. + uint64_t NumDirectoryBlocks = + bytesToBlocks(SB.NumDirectoryBytes, SB.BlockSize); + + // The directory, as we understand it, is a block which consists of a list of + // block numbers. It is unclear what would happen if the number of blocks + // couldn't fit on a single block. + if (NumDirectoryBlocks > SB.BlockSize / sizeof(support::ulittle32_t)) + return make_error<MSFError>(msf_error_code::invalid_format, + "Too many directory blocks."); + + if (SB.BlockMapAddr == 0) + return make_error<MSFError>(msf_error_code::invalid_format, + "Block 0 is reserved"); + + if (SB.BlockMapAddr >= SB.NumBlocks) + return make_error<MSFError>(msf_error_code::invalid_format, + "Block map address is invalid."); + + if (SB.FreeBlockMapBlock != 1 && SB.FreeBlockMapBlock != 2) + return make_error<MSFError>( + msf_error_code::invalid_format, + "The free block map isn't at block 1 or block 2."); + + return Error::success(); +} + +MSFStreamLayout llvm::msf::getFpmStreamLayout(const MSFLayout &Msf, + bool IncludeUnusedFpmData, + bool AltFpm) { + MSFStreamLayout FL; + uint32_t NumFpmIntervals = + getNumFpmIntervals(Msf, IncludeUnusedFpmData, AltFpm); + + uint32_t FpmBlock = AltFpm ? Msf.alternateFpmBlock() : Msf.mainFpmBlock(); + + for (uint32_t I = 0; I < NumFpmIntervals; ++I) { + FL.Blocks.push_back(support::ulittle32_t(FpmBlock)); + FpmBlock += msf::getFpmIntervalLength(Msf); + } + + if (IncludeUnusedFpmData) + FL.Length = NumFpmIntervals * Msf.SB->BlockSize; + else + FL.Length = divideCeil(Msf.SB->NumBlocks, 8); + + return FL; +} diff --git a/contrib/libs/llvm12/lib/DebugInfo/MSF/MSFError.cpp b/contrib/libs/llvm12/lib/DebugInfo/MSF/MSFError.cpp index d58721110f..b368b802c5 100644 --- a/contrib/libs/llvm12/lib/DebugInfo/MSF/MSFError.cpp +++ b/contrib/libs/llvm12/lib/DebugInfo/MSF/MSFError.cpp @@ -1,47 +1,47 @@ -//===- MSFError.cpp - Error extensions for MSF files ------------*- C++ -*-===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#include "llvm/DebugInfo/MSF/MSFError.h" -#include "llvm/Support/ErrorHandling.h" -#include "llvm/Support/ManagedStatic.h" - -using namespace llvm; -using namespace llvm::msf; - -namespace { -// FIXME: This class is only here to support the transition to llvm::Error. It -// will be removed once this transition is complete. Clients should prefer to -// deal with the Error value directly, rather than converting to error_code. -class MSFErrorCategory : public std::error_category { -public: - const char *name() const noexcept override { return "llvm.msf"; } - std::string message(int Condition) const override { - switch (static_cast<msf_error_code>(Condition)) { - case msf_error_code::unspecified: - return "An unknown error has occurred."; - case msf_error_code::insufficient_buffer: - return "The buffer is not large enough to read the requested number of " - "bytes."; - case msf_error_code::not_writable: - return "The specified stream is not writable."; - case msf_error_code::no_stream: - return "The specified stream does not exist."; - case msf_error_code::invalid_format: - return "The data is in an unexpected format."; - case msf_error_code::block_in_use: - return "The block is already in use."; - } - llvm_unreachable("Unrecognized msf_error_code"); - } -}; -} // namespace - -static llvm::ManagedStatic<MSFErrorCategory> MSFCategory; -const std::error_category &llvm::msf::MSFErrCategory() { return *MSFCategory; } - -char MSFError::ID; +//===- MSFError.cpp - Error extensions for MSF files ------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm/DebugInfo/MSF/MSFError.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/ManagedStatic.h" + +using namespace llvm; +using namespace llvm::msf; + +namespace { +// FIXME: This class is only here to support the transition to llvm::Error. It +// will be removed once this transition is complete. Clients should prefer to +// deal with the Error value directly, rather than converting to error_code. +class MSFErrorCategory : public std::error_category { +public: + const char *name() const noexcept override { return "llvm.msf"; } + std::string message(int Condition) const override { + switch (static_cast<msf_error_code>(Condition)) { + case msf_error_code::unspecified: + return "An unknown error has occurred."; + case msf_error_code::insufficient_buffer: + return "The buffer is not large enough to read the requested number of " + "bytes."; + case msf_error_code::not_writable: + return "The specified stream is not writable."; + case msf_error_code::no_stream: + return "The specified stream does not exist."; + case msf_error_code::invalid_format: + return "The data is in an unexpected format."; + case msf_error_code::block_in_use: + return "The block is already in use."; + } + llvm_unreachable("Unrecognized msf_error_code"); + } +}; +} // namespace + +static llvm::ManagedStatic<MSFErrorCategory> MSFCategory; +const std::error_category &llvm::msf::MSFErrCategory() { return *MSFCategory; } + +char MSFError::ID; diff --git a/contrib/libs/llvm12/lib/DebugInfo/MSF/MappedBlockStream.cpp b/contrib/libs/llvm12/lib/DebugInfo/MSF/MappedBlockStream.cpp index aa831ce327..5dc9c86b34 100644 --- a/contrib/libs/llvm12/lib/DebugInfo/MSF/MappedBlockStream.cpp +++ b/contrib/libs/llvm12/lib/DebugInfo/MSF/MappedBlockStream.cpp @@ -1,421 +1,421 @@ -//===- MappedBlockStream.cpp - Reads stream data from an MSF file ---------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#include "llvm/DebugInfo/MSF/MappedBlockStream.h" -#include "llvm/ADT/ArrayRef.h" -#include "llvm/ADT/STLExtras.h" -#include "llvm/DebugInfo/MSF/MSFCommon.h" -#include "llvm/Support/BinaryStreamWriter.h" -#include "llvm/Support/Endian.h" -#include "llvm/Support/Error.h" -#include "llvm/Support/MathExtras.h" -#include <algorithm> -#include <cassert> -#include <cstdint> -#include <cstring> -#include <utility> -#include <vector> - -using namespace llvm; -using namespace llvm::msf; - -namespace { - -template <typename Base> class MappedBlockStreamImpl : public Base { -public: - template <typename... Args> - MappedBlockStreamImpl(Args &&... Params) - : Base(std::forward<Args>(Params)...) {} -}; - -} // end anonymous namespace - -using Interval = std::pair<uint32_t, uint32_t>; - -static Interval intersect(const Interval &I1, const Interval &I2) { - return std::make_pair(std::max(I1.first, I2.first), - std::min(I1.second, I2.second)); -} - -MappedBlockStream::MappedBlockStream(uint32_t BlockSize, - const MSFStreamLayout &Layout, - BinaryStreamRef MsfData, - BumpPtrAllocator &Allocator) - : BlockSize(BlockSize), StreamLayout(Layout), MsfData(MsfData), - Allocator(Allocator) {} - -std::unique_ptr<MappedBlockStream> MappedBlockStream::createStream( - uint32_t BlockSize, const MSFStreamLayout &Layout, BinaryStreamRef MsfData, - BumpPtrAllocator &Allocator) { - return std::make_unique<MappedBlockStreamImpl<MappedBlockStream>>( - BlockSize, Layout, MsfData, Allocator); -} - -std::unique_ptr<MappedBlockStream> MappedBlockStream::createIndexedStream( - const MSFLayout &Layout, BinaryStreamRef MsfData, uint32_t StreamIndex, - BumpPtrAllocator &Allocator) { - assert(StreamIndex < Layout.StreamMap.size() && "Invalid stream index"); - MSFStreamLayout SL; - SL.Blocks = Layout.StreamMap[StreamIndex]; - SL.Length = Layout.StreamSizes[StreamIndex]; - return std::make_unique<MappedBlockStreamImpl<MappedBlockStream>>( - Layout.SB->BlockSize, SL, MsfData, Allocator); -} - -std::unique_ptr<MappedBlockStream> -MappedBlockStream::createDirectoryStream(const MSFLayout &Layout, - BinaryStreamRef MsfData, - BumpPtrAllocator &Allocator) { - MSFStreamLayout SL; - SL.Blocks = Layout.DirectoryBlocks; - SL.Length = Layout.SB->NumDirectoryBytes; - return createStream(Layout.SB->BlockSize, SL, MsfData, Allocator); -} - -std::unique_ptr<MappedBlockStream> -MappedBlockStream::createFpmStream(const MSFLayout &Layout, - BinaryStreamRef MsfData, - BumpPtrAllocator &Allocator) { - MSFStreamLayout SL(getFpmStreamLayout(Layout)); - return createStream(Layout.SB->BlockSize, SL, MsfData, Allocator); -} - -Error MappedBlockStream::readBytes(uint32_t Offset, uint32_t Size, - ArrayRef<uint8_t> &Buffer) { - // Make sure we aren't trying to read beyond the end of the stream. - if (auto EC = checkOffsetForRead(Offset, Size)) - return EC; - - if (tryReadContiguously(Offset, Size, Buffer)) - return Error::success(); - - auto CacheIter = CacheMap.find(Offset); - if (CacheIter != CacheMap.end()) { - // Try to find an alloc that was large enough for this request. - for (auto &Entry : CacheIter->second) { - if (Entry.size() >= Size) { - Buffer = Entry.slice(0, Size); - return Error::success(); - } - } - } - - // We couldn't find a buffer that started at the correct offset (the most - // common scenario). Try to see if there is a buffer that starts at some - // other offset but overlaps the desired range. - for (auto &CacheItem : CacheMap) { - Interval RequestExtent = std::make_pair(Offset, Offset + Size); - - // We already checked this one on the fast path above. - if (CacheItem.first == Offset) - continue; - // If the initial extent of the cached item is beyond the ending extent - // of the request, there is no overlap. - if (CacheItem.first >= Offset + Size) - continue; - - // We really only have to check the last item in the list, since we append - // in order of increasing length. - if (CacheItem.second.empty()) - continue; - - auto CachedAlloc = CacheItem.second.back(); - // If the initial extent of the request is beyond the ending extent of - // the cached item, there is no overlap. - Interval CachedExtent = - std::make_pair(CacheItem.first, CacheItem.first + CachedAlloc.size()); - if (RequestExtent.first >= CachedExtent.first + CachedExtent.second) - continue; - - Interval Intersection = intersect(CachedExtent, RequestExtent); - // Only use this if the entire request extent is contained in the cached - // extent. - if (Intersection != RequestExtent) - continue; - - uint32_t CacheRangeOffset = - AbsoluteDifference(CachedExtent.first, Intersection.first); - Buffer = CachedAlloc.slice(CacheRangeOffset, Size); - return Error::success(); - } - - // Otherwise allocate a large enough buffer in the pool, memcpy the data - // into it, and return an ArrayRef to that. Do not touch existing pool - // allocations, as existing clients may be holding a pointer which must - // not be invalidated. - uint8_t *WriteBuffer = static_cast<uint8_t *>(Allocator.Allocate(Size, 8)); - if (auto EC = readBytes(Offset, MutableArrayRef<uint8_t>(WriteBuffer, Size))) - return EC; - - if (CacheIter != CacheMap.end()) { - CacheIter->second.emplace_back(WriteBuffer, Size); - } else { - std::vector<CacheEntry> List; - List.emplace_back(WriteBuffer, Size); - CacheMap.insert(std::make_pair(Offset, List)); - } - Buffer = ArrayRef<uint8_t>(WriteBuffer, Size); - return Error::success(); -} - -Error MappedBlockStream::readLongestContiguousChunk(uint32_t Offset, - ArrayRef<uint8_t> &Buffer) { - // Make sure we aren't trying to read beyond the end of the stream. - if (auto EC = checkOffsetForRead(Offset, 1)) - return EC; - - uint32_t First = Offset / BlockSize; - uint32_t Last = First; - - while (Last < getNumBlocks() - 1) { - if (StreamLayout.Blocks[Last] != StreamLayout.Blocks[Last + 1] - 1) - break; - ++Last; - } - - uint32_t OffsetInFirstBlock = Offset % BlockSize; - uint32_t BytesFromFirstBlock = BlockSize - OffsetInFirstBlock; - uint32_t BlockSpan = Last - First + 1; - uint32_t ByteSpan = BytesFromFirstBlock + (BlockSpan - 1) * BlockSize; - - ArrayRef<uint8_t> BlockData; - uint32_t MsfOffset = blockToOffset(StreamLayout.Blocks[First], BlockSize); - if (auto EC = MsfData.readBytes(MsfOffset, BlockSize, BlockData)) - return EC; - - BlockData = BlockData.drop_front(OffsetInFirstBlock); - Buffer = ArrayRef<uint8_t>(BlockData.data(), ByteSpan); - return Error::success(); -} - -uint32_t MappedBlockStream::getLength() { return StreamLayout.Length; } - -bool MappedBlockStream::tryReadContiguously(uint32_t Offset, uint32_t Size, - ArrayRef<uint8_t> &Buffer) { - if (Size == 0) { - Buffer = ArrayRef<uint8_t>(); - return true; - } - // Attempt to fulfill the request with a reference directly into the stream. - // This can work even if the request crosses a block boundary, provided that - // all subsequent blocks are contiguous. For example, a 10k read with a 4k - // block size can be filled with a reference if, from the starting offset, - // 3 blocks in a row are contiguous. - uint32_t BlockNum = Offset / BlockSize; - uint32_t OffsetInBlock = Offset % BlockSize; - uint32_t BytesFromFirstBlock = std::min(Size, BlockSize - OffsetInBlock); - uint32_t NumAdditionalBlocks = - alignTo(Size - BytesFromFirstBlock, BlockSize) / BlockSize; - - uint32_t RequiredContiguousBlocks = NumAdditionalBlocks + 1; - uint32_t E = StreamLayout.Blocks[BlockNum]; - for (uint32_t I = 0; I < RequiredContiguousBlocks; ++I, ++E) { - if (StreamLayout.Blocks[I + BlockNum] != E) - return false; - } - - // Read out the entire block where the requested offset starts. Then drop - // bytes from the beginning so that the actual starting byte lines up with - // the requested starting byte. Then, since we know this is a contiguous - // cross-block span, explicitly resize the ArrayRef to cover the entire - // request length. - ArrayRef<uint8_t> BlockData; - uint32_t FirstBlockAddr = StreamLayout.Blocks[BlockNum]; - uint32_t MsfOffset = blockToOffset(FirstBlockAddr, BlockSize); - if (auto EC = MsfData.readBytes(MsfOffset, BlockSize, BlockData)) { - consumeError(std::move(EC)); - return false; - } - BlockData = BlockData.drop_front(OffsetInBlock); - Buffer = ArrayRef<uint8_t>(BlockData.data(), Size); - return true; -} - -Error MappedBlockStream::readBytes(uint32_t Offset, - MutableArrayRef<uint8_t> Buffer) { - uint32_t BlockNum = Offset / BlockSize; - uint32_t OffsetInBlock = Offset % BlockSize; - - // Make sure we aren't trying to read beyond the end of the stream. - if (auto EC = checkOffsetForRead(Offset, Buffer.size())) - return EC; - - uint32_t BytesLeft = Buffer.size(); - uint32_t BytesWritten = 0; - uint8_t *WriteBuffer = Buffer.data(); - while (BytesLeft > 0) { - uint32_t StreamBlockAddr = StreamLayout.Blocks[BlockNum]; - - ArrayRef<uint8_t> BlockData; - uint32_t Offset = blockToOffset(StreamBlockAddr, BlockSize); - if (auto EC = MsfData.readBytes(Offset, BlockSize, BlockData)) - return EC; - - const uint8_t *ChunkStart = BlockData.data() + OffsetInBlock; - uint32_t BytesInChunk = std::min(BytesLeft, BlockSize - OffsetInBlock); - ::memcpy(WriteBuffer + BytesWritten, ChunkStart, BytesInChunk); - - BytesWritten += BytesInChunk; - BytesLeft -= BytesInChunk; - ++BlockNum; - OffsetInBlock = 0; - } - - return Error::success(); -} - -void MappedBlockStream::invalidateCache() { CacheMap.shrink_and_clear(); } - -void MappedBlockStream::fixCacheAfterWrite(uint32_t Offset, - ArrayRef<uint8_t> Data) const { - // If this write overlapped a read which previously came from the pool, - // someone may still be holding a pointer to that alloc which is now invalid. - // Compute the overlapping range and update the cache entry, so any - // outstanding buffers are automatically updated. - for (const auto &MapEntry : CacheMap) { - // If the end of the written extent precedes the beginning of the cached - // extent, ignore this map entry. - if (Offset + Data.size() < MapEntry.first) - continue; - for (const auto &Alloc : MapEntry.second) { - // If the end of the cached extent precedes the beginning of the written - // extent, ignore this alloc. - if (MapEntry.first + Alloc.size() < Offset) - continue; - - // If we get here, they are guaranteed to overlap. - Interval WriteInterval = std::make_pair(Offset, Offset + Data.size()); - Interval CachedInterval = - std::make_pair(MapEntry.first, MapEntry.first + Alloc.size()); - // If they overlap, we need to write the new data into the overlapping - // range. - auto Intersection = intersect(WriteInterval, CachedInterval); - assert(Intersection.first <= Intersection.second); - - uint32_t Length = Intersection.second - Intersection.first; - uint32_t SrcOffset = - AbsoluteDifference(WriteInterval.first, Intersection.first); - uint32_t DestOffset = - AbsoluteDifference(CachedInterval.first, Intersection.first); - ::memcpy(Alloc.data() + DestOffset, Data.data() + SrcOffset, Length); - } - } -} - -WritableMappedBlockStream::WritableMappedBlockStream( - uint32_t BlockSize, const MSFStreamLayout &Layout, - WritableBinaryStreamRef MsfData, BumpPtrAllocator &Allocator) - : ReadInterface(BlockSize, Layout, MsfData, Allocator), - WriteInterface(MsfData) {} - -std::unique_ptr<WritableMappedBlockStream> -WritableMappedBlockStream::createStream(uint32_t BlockSize, - const MSFStreamLayout &Layout, - WritableBinaryStreamRef MsfData, - BumpPtrAllocator &Allocator) { - return std::make_unique<MappedBlockStreamImpl<WritableMappedBlockStream>>( - BlockSize, Layout, MsfData, Allocator); -} - -std::unique_ptr<WritableMappedBlockStream> -WritableMappedBlockStream::createIndexedStream(const MSFLayout &Layout, - WritableBinaryStreamRef MsfData, - uint32_t StreamIndex, - BumpPtrAllocator &Allocator) { - assert(StreamIndex < Layout.StreamMap.size() && "Invalid stream index"); - MSFStreamLayout SL; - SL.Blocks = Layout.StreamMap[StreamIndex]; - SL.Length = Layout.StreamSizes[StreamIndex]; - return createStream(Layout.SB->BlockSize, SL, MsfData, Allocator); -} - -std::unique_ptr<WritableMappedBlockStream> -WritableMappedBlockStream::createDirectoryStream( - const MSFLayout &Layout, WritableBinaryStreamRef MsfData, - BumpPtrAllocator &Allocator) { - MSFStreamLayout SL; - SL.Blocks = Layout.DirectoryBlocks; - SL.Length = Layout.SB->NumDirectoryBytes; - return createStream(Layout.SB->BlockSize, SL, MsfData, Allocator); -} - -std::unique_ptr<WritableMappedBlockStream> -WritableMappedBlockStream::createFpmStream(const MSFLayout &Layout, - WritableBinaryStreamRef MsfData, - BumpPtrAllocator &Allocator, - bool AltFpm) { - // We only want to give the user a stream containing the bytes of the FPM that - // are actually valid, but we want to initialize all of the bytes, even those - // that come from reserved FPM blocks where the entire block is unused. To do - // this, we first create the full layout, which gives us a stream with all - // bytes and all blocks, and initialize everything to 0xFF (all blocks in the - // file are unused). Then we create the minimal layout (which contains only a - // subset of the bytes previously initialized), and return that to the user. - MSFStreamLayout MinLayout(getFpmStreamLayout(Layout, false, AltFpm)); - - MSFStreamLayout FullLayout(getFpmStreamLayout(Layout, true, AltFpm)); - auto Result = - createStream(Layout.SB->BlockSize, FullLayout, MsfData, Allocator); - if (!Result) - return Result; - std::vector<uint8_t> InitData(Layout.SB->BlockSize, 0xFF); - BinaryStreamWriter Initializer(*Result); - while (Initializer.bytesRemaining() > 0) - cantFail(Initializer.writeBytes(InitData)); - return createStream(Layout.SB->BlockSize, MinLayout, MsfData, Allocator); -} - -Error WritableMappedBlockStream::readBytes(uint32_t Offset, uint32_t Size, - ArrayRef<uint8_t> &Buffer) { - return ReadInterface.readBytes(Offset, Size, Buffer); -} - -Error WritableMappedBlockStream::readLongestContiguousChunk( - uint32_t Offset, ArrayRef<uint8_t> &Buffer) { - return ReadInterface.readLongestContiguousChunk(Offset, Buffer); -} - -uint32_t WritableMappedBlockStream::getLength() { - return ReadInterface.getLength(); -} - -Error WritableMappedBlockStream::writeBytes(uint32_t Offset, - ArrayRef<uint8_t> Buffer) { - // Make sure we aren't trying to write beyond the end of the stream. - if (auto EC = checkOffsetForWrite(Offset, Buffer.size())) - return EC; - - uint32_t BlockNum = Offset / getBlockSize(); - uint32_t OffsetInBlock = Offset % getBlockSize(); - - uint32_t BytesLeft = Buffer.size(); - uint32_t BytesWritten = 0; - while (BytesLeft > 0) { - uint32_t StreamBlockAddr = getStreamLayout().Blocks[BlockNum]; - uint32_t BytesToWriteInChunk = - std::min(BytesLeft, getBlockSize() - OffsetInBlock); - - const uint8_t *Chunk = Buffer.data() + BytesWritten; - ArrayRef<uint8_t> ChunkData(Chunk, BytesToWriteInChunk); - uint32_t MsfOffset = blockToOffset(StreamBlockAddr, getBlockSize()); - MsfOffset += OffsetInBlock; - if (auto EC = WriteInterface.writeBytes(MsfOffset, ChunkData)) - return EC; - - BytesLeft -= BytesToWriteInChunk; - BytesWritten += BytesToWriteInChunk; - ++BlockNum; - OffsetInBlock = 0; - } - - ReadInterface.fixCacheAfterWrite(Offset, Buffer); - - return Error::success(); -} - -Error WritableMappedBlockStream::commit() { return WriteInterface.commit(); } +//===- MappedBlockStream.cpp - Reads stream data from an MSF file ---------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm/DebugInfo/MSF/MappedBlockStream.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/DebugInfo/MSF/MSFCommon.h" +#include "llvm/Support/BinaryStreamWriter.h" +#include "llvm/Support/Endian.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/MathExtras.h" +#include <algorithm> +#include <cassert> +#include <cstdint> +#include <cstring> +#include <utility> +#include <vector> + +using namespace llvm; +using namespace llvm::msf; + +namespace { + +template <typename Base> class MappedBlockStreamImpl : public Base { +public: + template <typename... Args> + MappedBlockStreamImpl(Args &&... Params) + : Base(std::forward<Args>(Params)...) {} +}; + +} // end anonymous namespace + +using Interval = std::pair<uint32_t, uint32_t>; + +static Interval intersect(const Interval &I1, const Interval &I2) { + return std::make_pair(std::max(I1.first, I2.first), + std::min(I1.second, I2.second)); +} + +MappedBlockStream::MappedBlockStream(uint32_t BlockSize, + const MSFStreamLayout &Layout, + BinaryStreamRef MsfData, + BumpPtrAllocator &Allocator) + : BlockSize(BlockSize), StreamLayout(Layout), MsfData(MsfData), + Allocator(Allocator) {} + +std::unique_ptr<MappedBlockStream> MappedBlockStream::createStream( + uint32_t BlockSize, const MSFStreamLayout &Layout, BinaryStreamRef MsfData, + BumpPtrAllocator &Allocator) { + return std::make_unique<MappedBlockStreamImpl<MappedBlockStream>>( + BlockSize, Layout, MsfData, Allocator); +} + +std::unique_ptr<MappedBlockStream> MappedBlockStream::createIndexedStream( + const MSFLayout &Layout, BinaryStreamRef MsfData, uint32_t StreamIndex, + BumpPtrAllocator &Allocator) { + assert(StreamIndex < Layout.StreamMap.size() && "Invalid stream index"); + MSFStreamLayout SL; + SL.Blocks = Layout.StreamMap[StreamIndex]; + SL.Length = Layout.StreamSizes[StreamIndex]; + return std::make_unique<MappedBlockStreamImpl<MappedBlockStream>>( + Layout.SB->BlockSize, SL, MsfData, Allocator); +} + +std::unique_ptr<MappedBlockStream> +MappedBlockStream::createDirectoryStream(const MSFLayout &Layout, + BinaryStreamRef MsfData, + BumpPtrAllocator &Allocator) { + MSFStreamLayout SL; + SL.Blocks = Layout.DirectoryBlocks; + SL.Length = Layout.SB->NumDirectoryBytes; + return createStream(Layout.SB->BlockSize, SL, MsfData, Allocator); +} + +std::unique_ptr<MappedBlockStream> +MappedBlockStream::createFpmStream(const MSFLayout &Layout, + BinaryStreamRef MsfData, + BumpPtrAllocator &Allocator) { + MSFStreamLayout SL(getFpmStreamLayout(Layout)); + return createStream(Layout.SB->BlockSize, SL, MsfData, Allocator); +} + +Error MappedBlockStream::readBytes(uint32_t Offset, uint32_t Size, + ArrayRef<uint8_t> &Buffer) { + // Make sure we aren't trying to read beyond the end of the stream. + if (auto EC = checkOffsetForRead(Offset, Size)) + return EC; + + if (tryReadContiguously(Offset, Size, Buffer)) + return Error::success(); + + auto CacheIter = CacheMap.find(Offset); + if (CacheIter != CacheMap.end()) { + // Try to find an alloc that was large enough for this request. + for (auto &Entry : CacheIter->second) { + if (Entry.size() >= Size) { + Buffer = Entry.slice(0, Size); + return Error::success(); + } + } + } + + // We couldn't find a buffer that started at the correct offset (the most + // common scenario). Try to see if there is a buffer that starts at some + // other offset but overlaps the desired range. + for (auto &CacheItem : CacheMap) { + Interval RequestExtent = std::make_pair(Offset, Offset + Size); + + // We already checked this one on the fast path above. + if (CacheItem.first == Offset) + continue; + // If the initial extent of the cached item is beyond the ending extent + // of the request, there is no overlap. + if (CacheItem.first >= Offset + Size) + continue; + + // We really only have to check the last item in the list, since we append + // in order of increasing length. + if (CacheItem.second.empty()) + continue; + + auto CachedAlloc = CacheItem.second.back(); + // If the initial extent of the request is beyond the ending extent of + // the cached item, there is no overlap. + Interval CachedExtent = + std::make_pair(CacheItem.first, CacheItem.first + CachedAlloc.size()); + if (RequestExtent.first >= CachedExtent.first + CachedExtent.second) + continue; + + Interval Intersection = intersect(CachedExtent, RequestExtent); + // Only use this if the entire request extent is contained in the cached + // extent. + if (Intersection != RequestExtent) + continue; + + uint32_t CacheRangeOffset = + AbsoluteDifference(CachedExtent.first, Intersection.first); + Buffer = CachedAlloc.slice(CacheRangeOffset, Size); + return Error::success(); + } + + // Otherwise allocate a large enough buffer in the pool, memcpy the data + // into it, and return an ArrayRef to that. Do not touch existing pool + // allocations, as existing clients may be holding a pointer which must + // not be invalidated. + uint8_t *WriteBuffer = static_cast<uint8_t *>(Allocator.Allocate(Size, 8)); + if (auto EC = readBytes(Offset, MutableArrayRef<uint8_t>(WriteBuffer, Size))) + return EC; + + if (CacheIter != CacheMap.end()) { + CacheIter->second.emplace_back(WriteBuffer, Size); + } else { + std::vector<CacheEntry> List; + List.emplace_back(WriteBuffer, Size); + CacheMap.insert(std::make_pair(Offset, List)); + } + Buffer = ArrayRef<uint8_t>(WriteBuffer, Size); + return Error::success(); +} + +Error MappedBlockStream::readLongestContiguousChunk(uint32_t Offset, + ArrayRef<uint8_t> &Buffer) { + // Make sure we aren't trying to read beyond the end of the stream. + if (auto EC = checkOffsetForRead(Offset, 1)) + return EC; + + uint32_t First = Offset / BlockSize; + uint32_t Last = First; + + while (Last < getNumBlocks() - 1) { + if (StreamLayout.Blocks[Last] != StreamLayout.Blocks[Last + 1] - 1) + break; + ++Last; + } + + uint32_t OffsetInFirstBlock = Offset % BlockSize; + uint32_t BytesFromFirstBlock = BlockSize - OffsetInFirstBlock; + uint32_t BlockSpan = Last - First + 1; + uint32_t ByteSpan = BytesFromFirstBlock + (BlockSpan - 1) * BlockSize; + + ArrayRef<uint8_t> BlockData; + uint32_t MsfOffset = blockToOffset(StreamLayout.Blocks[First], BlockSize); + if (auto EC = MsfData.readBytes(MsfOffset, BlockSize, BlockData)) + return EC; + + BlockData = BlockData.drop_front(OffsetInFirstBlock); + Buffer = ArrayRef<uint8_t>(BlockData.data(), ByteSpan); + return Error::success(); +} + +uint32_t MappedBlockStream::getLength() { return StreamLayout.Length; } + +bool MappedBlockStream::tryReadContiguously(uint32_t Offset, uint32_t Size, + ArrayRef<uint8_t> &Buffer) { + if (Size == 0) { + Buffer = ArrayRef<uint8_t>(); + return true; + } + // Attempt to fulfill the request with a reference directly into the stream. + // This can work even if the request crosses a block boundary, provided that + // all subsequent blocks are contiguous. For example, a 10k read with a 4k + // block size can be filled with a reference if, from the starting offset, + // 3 blocks in a row are contiguous. + uint32_t BlockNum = Offset / BlockSize; + uint32_t OffsetInBlock = Offset % BlockSize; + uint32_t BytesFromFirstBlock = std::min(Size, BlockSize - OffsetInBlock); + uint32_t NumAdditionalBlocks = + alignTo(Size - BytesFromFirstBlock, BlockSize) / BlockSize; + + uint32_t RequiredContiguousBlocks = NumAdditionalBlocks + 1; + uint32_t E = StreamLayout.Blocks[BlockNum]; + for (uint32_t I = 0; I < RequiredContiguousBlocks; ++I, ++E) { + if (StreamLayout.Blocks[I + BlockNum] != E) + return false; + } + + // Read out the entire block where the requested offset starts. Then drop + // bytes from the beginning so that the actual starting byte lines up with + // the requested starting byte. Then, since we know this is a contiguous + // cross-block span, explicitly resize the ArrayRef to cover the entire + // request length. + ArrayRef<uint8_t> BlockData; + uint32_t FirstBlockAddr = StreamLayout.Blocks[BlockNum]; + uint32_t MsfOffset = blockToOffset(FirstBlockAddr, BlockSize); + if (auto EC = MsfData.readBytes(MsfOffset, BlockSize, BlockData)) { + consumeError(std::move(EC)); + return false; + } + BlockData = BlockData.drop_front(OffsetInBlock); + Buffer = ArrayRef<uint8_t>(BlockData.data(), Size); + return true; +} + +Error MappedBlockStream::readBytes(uint32_t Offset, + MutableArrayRef<uint8_t> Buffer) { + uint32_t BlockNum = Offset / BlockSize; + uint32_t OffsetInBlock = Offset % BlockSize; + + // Make sure we aren't trying to read beyond the end of the stream. + if (auto EC = checkOffsetForRead(Offset, Buffer.size())) + return EC; + + uint32_t BytesLeft = Buffer.size(); + uint32_t BytesWritten = 0; + uint8_t *WriteBuffer = Buffer.data(); + while (BytesLeft > 0) { + uint32_t StreamBlockAddr = StreamLayout.Blocks[BlockNum]; + + ArrayRef<uint8_t> BlockData; + uint32_t Offset = blockToOffset(StreamBlockAddr, BlockSize); + if (auto EC = MsfData.readBytes(Offset, BlockSize, BlockData)) + return EC; + + const uint8_t *ChunkStart = BlockData.data() + OffsetInBlock; + uint32_t BytesInChunk = std::min(BytesLeft, BlockSize - OffsetInBlock); + ::memcpy(WriteBuffer + BytesWritten, ChunkStart, BytesInChunk); + + BytesWritten += BytesInChunk; + BytesLeft -= BytesInChunk; + ++BlockNum; + OffsetInBlock = 0; + } + + return Error::success(); +} + +void MappedBlockStream::invalidateCache() { CacheMap.shrink_and_clear(); } + +void MappedBlockStream::fixCacheAfterWrite(uint32_t Offset, + ArrayRef<uint8_t> Data) const { + // If this write overlapped a read which previously came from the pool, + // someone may still be holding a pointer to that alloc which is now invalid. + // Compute the overlapping range and update the cache entry, so any + // outstanding buffers are automatically updated. + for (const auto &MapEntry : CacheMap) { + // If the end of the written extent precedes the beginning of the cached + // extent, ignore this map entry. + if (Offset + Data.size() < MapEntry.first) + continue; + for (const auto &Alloc : MapEntry.second) { + // If the end of the cached extent precedes the beginning of the written + // extent, ignore this alloc. + if (MapEntry.first + Alloc.size() < Offset) + continue; + + // If we get here, they are guaranteed to overlap. + Interval WriteInterval = std::make_pair(Offset, Offset + Data.size()); + Interval CachedInterval = + std::make_pair(MapEntry.first, MapEntry.first + Alloc.size()); + // If they overlap, we need to write the new data into the overlapping + // range. + auto Intersection = intersect(WriteInterval, CachedInterval); + assert(Intersection.first <= Intersection.second); + + uint32_t Length = Intersection.second - Intersection.first; + uint32_t SrcOffset = + AbsoluteDifference(WriteInterval.first, Intersection.first); + uint32_t DestOffset = + AbsoluteDifference(CachedInterval.first, Intersection.first); + ::memcpy(Alloc.data() + DestOffset, Data.data() + SrcOffset, Length); + } + } +} + +WritableMappedBlockStream::WritableMappedBlockStream( + uint32_t BlockSize, const MSFStreamLayout &Layout, + WritableBinaryStreamRef MsfData, BumpPtrAllocator &Allocator) + : ReadInterface(BlockSize, Layout, MsfData, Allocator), + WriteInterface(MsfData) {} + +std::unique_ptr<WritableMappedBlockStream> +WritableMappedBlockStream::createStream(uint32_t BlockSize, + const MSFStreamLayout &Layout, + WritableBinaryStreamRef MsfData, + BumpPtrAllocator &Allocator) { + return std::make_unique<MappedBlockStreamImpl<WritableMappedBlockStream>>( + BlockSize, Layout, MsfData, Allocator); +} + +std::unique_ptr<WritableMappedBlockStream> +WritableMappedBlockStream::createIndexedStream(const MSFLayout &Layout, + WritableBinaryStreamRef MsfData, + uint32_t StreamIndex, + BumpPtrAllocator &Allocator) { + assert(StreamIndex < Layout.StreamMap.size() && "Invalid stream index"); + MSFStreamLayout SL; + SL.Blocks = Layout.StreamMap[StreamIndex]; + SL.Length = Layout.StreamSizes[StreamIndex]; + return createStream(Layout.SB->BlockSize, SL, MsfData, Allocator); +} + +std::unique_ptr<WritableMappedBlockStream> +WritableMappedBlockStream::createDirectoryStream( + const MSFLayout &Layout, WritableBinaryStreamRef MsfData, + BumpPtrAllocator &Allocator) { + MSFStreamLayout SL; + SL.Blocks = Layout.DirectoryBlocks; + SL.Length = Layout.SB->NumDirectoryBytes; + return createStream(Layout.SB->BlockSize, SL, MsfData, Allocator); +} + +std::unique_ptr<WritableMappedBlockStream> +WritableMappedBlockStream::createFpmStream(const MSFLayout &Layout, + WritableBinaryStreamRef MsfData, + BumpPtrAllocator &Allocator, + bool AltFpm) { + // We only want to give the user a stream containing the bytes of the FPM that + // are actually valid, but we want to initialize all of the bytes, even those + // that come from reserved FPM blocks where the entire block is unused. To do + // this, we first create the full layout, which gives us a stream with all + // bytes and all blocks, and initialize everything to 0xFF (all blocks in the + // file are unused). Then we create the minimal layout (which contains only a + // subset of the bytes previously initialized), and return that to the user. + MSFStreamLayout MinLayout(getFpmStreamLayout(Layout, false, AltFpm)); + + MSFStreamLayout FullLayout(getFpmStreamLayout(Layout, true, AltFpm)); + auto Result = + createStream(Layout.SB->BlockSize, FullLayout, MsfData, Allocator); + if (!Result) + return Result; + std::vector<uint8_t> InitData(Layout.SB->BlockSize, 0xFF); + BinaryStreamWriter Initializer(*Result); + while (Initializer.bytesRemaining() > 0) + cantFail(Initializer.writeBytes(InitData)); + return createStream(Layout.SB->BlockSize, MinLayout, MsfData, Allocator); +} + +Error WritableMappedBlockStream::readBytes(uint32_t Offset, uint32_t Size, + ArrayRef<uint8_t> &Buffer) { + return ReadInterface.readBytes(Offset, Size, Buffer); +} + +Error WritableMappedBlockStream::readLongestContiguousChunk( + uint32_t Offset, ArrayRef<uint8_t> &Buffer) { + return ReadInterface.readLongestContiguousChunk(Offset, Buffer); +} + +uint32_t WritableMappedBlockStream::getLength() { + return ReadInterface.getLength(); +} + +Error WritableMappedBlockStream::writeBytes(uint32_t Offset, + ArrayRef<uint8_t> Buffer) { + // Make sure we aren't trying to write beyond the end of the stream. + if (auto EC = checkOffsetForWrite(Offset, Buffer.size())) + return EC; + + uint32_t BlockNum = Offset / getBlockSize(); + uint32_t OffsetInBlock = Offset % getBlockSize(); + + uint32_t BytesLeft = Buffer.size(); + uint32_t BytesWritten = 0; + while (BytesLeft > 0) { + uint32_t StreamBlockAddr = getStreamLayout().Blocks[BlockNum]; + uint32_t BytesToWriteInChunk = + std::min(BytesLeft, getBlockSize() - OffsetInBlock); + + const uint8_t *Chunk = Buffer.data() + BytesWritten; + ArrayRef<uint8_t> ChunkData(Chunk, BytesToWriteInChunk); + uint32_t MsfOffset = blockToOffset(StreamBlockAddr, getBlockSize()); + MsfOffset += OffsetInBlock; + if (auto EC = WriteInterface.writeBytes(MsfOffset, ChunkData)) + return EC; + + BytesLeft -= BytesToWriteInChunk; + BytesWritten += BytesToWriteInChunk; + ++BlockNum; + OffsetInBlock = 0; + } + + ReadInterface.fixCacheAfterWrite(Offset, Buffer); + + return Error::success(); +} + +Error WritableMappedBlockStream::commit() { return WriteInterface.commit(); } diff --git a/contrib/libs/llvm12/lib/DebugInfo/MSF/ya.make b/contrib/libs/llvm12/lib/DebugInfo/MSF/ya.make index 49655ced98..a9daa7d1aa 100644 --- a/contrib/libs/llvm12/lib/DebugInfo/MSF/ya.make +++ b/contrib/libs/llvm12/lib/DebugInfo/MSF/ya.make @@ -1,34 +1,34 @@ -# Generated by devtools/yamaker. - -LIBRARY() - +# Generated by devtools/yamaker. + +LIBRARY() + OWNER( orivej g:cpp-contrib ) - + LICENSE(Apache-2.0 WITH LLVM-exception) LICENSE_TEXTS(.yandex_meta/licenses.list.txt) -PEERDIR( +PEERDIR( contrib/libs/llvm12 contrib/libs/llvm12/lib/Support -) - +) + ADDINCL( contrib/libs/llvm12/lib/DebugInfo/MSF ) - -NO_COMPILER_WARNINGS() - -NO_UTIL() - -SRCS( - MSFBuilder.cpp - MSFCommon.cpp - MSFError.cpp - MappedBlockStream.cpp -) - -END() + +NO_COMPILER_WARNINGS() + +NO_UTIL() + +SRCS( + MSFBuilder.cpp + MSFCommon.cpp + MSFError.cpp + MappedBlockStream.cpp +) + +END() |