MappedBlockStream.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //==- MappedBlockStream.h - Discontiguous stream data in an MSF --*- C++ -*-==//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_DEBUGINFO_MSF_MAPPEDBLOCKSTREAM_H
  14. #define LLVM_DEBUGINFO_MSF_MAPPEDBLOCKSTREAM_H
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/DebugInfo/MSF/MSFCommon.h"
  18. #include "llvm/Support/Allocator.h"
  19. #include "llvm/Support/BinaryStream.h"
  20. #include "llvm/Support/BinaryStreamRef.h"
  21. #include "llvm/Support/Endian.h"
  22. #include "llvm/Support/Error.h"
  23. #include <cstdint>
  24. #include <memory>
  25. #include <vector>
  26. namespace llvm {
  27. namespace msf {
  28. /// MappedBlockStream represents data stored in an MSF file into chunks of a
  29. /// particular size (called the Block Size), and whose chunks may not be
  30. /// necessarily contiguous. The arrangement of these chunks MSF the file
  31. /// is described by some other metadata contained within the MSF file. In
  32. /// the case of a standard MSF Stream, the layout of the stream's blocks
  33. /// is described by the MSF "directory", but in the case of the directory
  34. /// itself, the layout is described by an array at a fixed location within
  35. /// the MSF. MappedBlockStream provides methods for reading from and writing
  36. /// to one of these streams transparently, as if it were a contiguous sequence
  37. /// of bytes.
  38. class MappedBlockStream : public BinaryStream {
  39. friend class WritableMappedBlockStream;
  40. public:
  41. static std::unique_ptr<MappedBlockStream>
  42. createStream(uint32_t BlockSize, const MSFStreamLayout &Layout,
  43. BinaryStreamRef MsfData, BumpPtrAllocator &Allocator);
  44. static std::unique_ptr<MappedBlockStream>
  45. createIndexedStream(const MSFLayout &Layout, BinaryStreamRef MsfData,
  46. uint32_t StreamIndex, BumpPtrAllocator &Allocator);
  47. static std::unique_ptr<MappedBlockStream>
  48. createFpmStream(const MSFLayout &Layout, BinaryStreamRef MsfData,
  49. BumpPtrAllocator &Allocator);
  50. static std::unique_ptr<MappedBlockStream>
  51. createDirectoryStream(const MSFLayout &Layout, BinaryStreamRef MsfData,
  52. BumpPtrAllocator &Allocator);
  53. support::endianness getEndian() const override {
  54. return support::little;
  55. }
  56. Error readBytes(uint32_t Offset, uint32_t Size,
  57. ArrayRef<uint8_t> &Buffer) override;
  58. Error readLongestContiguousChunk(uint32_t Offset,
  59. ArrayRef<uint8_t> &Buffer) override;
  60. uint32_t getLength() override;
  61. BumpPtrAllocator &getAllocator() { return Allocator; }
  62. void invalidateCache();
  63. uint32_t getBlockSize() const { return BlockSize; }
  64. uint32_t getNumBlocks() const { return StreamLayout.Blocks.size(); }
  65. uint32_t getStreamLength() const { return StreamLayout.Length; }
  66. protected:
  67. MappedBlockStream(uint32_t BlockSize, const MSFStreamLayout &StreamLayout,
  68. BinaryStreamRef MsfData, BumpPtrAllocator &Allocator);
  69. private:
  70. const MSFStreamLayout &getStreamLayout() const { return StreamLayout; }
  71. void fixCacheAfterWrite(uint32_t Offset, ArrayRef<uint8_t> Data) const;
  72. Error readBytes(uint32_t Offset, MutableArrayRef<uint8_t> Buffer);
  73. bool tryReadContiguously(uint32_t Offset, uint32_t Size,
  74. ArrayRef<uint8_t> &Buffer);
  75. const uint32_t BlockSize;
  76. const MSFStreamLayout StreamLayout;
  77. BinaryStreamRef MsfData;
  78. using CacheEntry = MutableArrayRef<uint8_t>;
  79. // We just store the allocator by reference. We use this to allocate
  80. // contiguous memory for things like arrays or strings that cross a block
  81. // boundary, and this memory is expected to outlive the stream. For example,
  82. // someone could create a stream, read some stuff, then close the stream, and
  83. // we would like outstanding references to fields to remain valid since the
  84. // entire file is mapped anyway. Because of that, the user must supply the
  85. // allocator to allocate broken records from.
  86. BumpPtrAllocator &Allocator;
  87. DenseMap<uint32_t, std::vector<CacheEntry>> CacheMap;
  88. };
  89. class WritableMappedBlockStream : public WritableBinaryStream {
  90. public:
  91. static std::unique_ptr<WritableMappedBlockStream>
  92. createStream(uint32_t BlockSize, const MSFStreamLayout &Layout,
  93. WritableBinaryStreamRef MsfData, BumpPtrAllocator &Allocator);
  94. static std::unique_ptr<WritableMappedBlockStream>
  95. createIndexedStream(const MSFLayout &Layout, WritableBinaryStreamRef MsfData,
  96. uint32_t StreamIndex, BumpPtrAllocator &Allocator);
  97. static std::unique_ptr<WritableMappedBlockStream>
  98. createDirectoryStream(const MSFLayout &Layout,
  99. WritableBinaryStreamRef MsfData,
  100. BumpPtrAllocator &Allocator);
  101. static std::unique_ptr<WritableMappedBlockStream>
  102. createFpmStream(const MSFLayout &Layout, WritableBinaryStreamRef MsfData,
  103. BumpPtrAllocator &Allocator, bool AltFpm = false);
  104. support::endianness getEndian() const override {
  105. return support::little;
  106. }
  107. Error readBytes(uint32_t Offset, uint32_t Size,
  108. ArrayRef<uint8_t> &Buffer) override;
  109. Error readLongestContiguousChunk(uint32_t Offset,
  110. ArrayRef<uint8_t> &Buffer) override;
  111. uint32_t getLength() override;
  112. Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Buffer) override;
  113. Error commit() override;
  114. const MSFStreamLayout &getStreamLayout() const {
  115. return ReadInterface.getStreamLayout();
  116. }
  117. uint32_t getBlockSize() const { return ReadInterface.getBlockSize(); }
  118. uint32_t getNumBlocks() const { return ReadInterface.getNumBlocks(); }
  119. uint32_t getStreamLength() const { return ReadInterface.getStreamLength(); }
  120. protected:
  121. WritableMappedBlockStream(uint32_t BlockSize,
  122. const MSFStreamLayout &StreamLayout,
  123. WritableBinaryStreamRef MsfData,
  124. BumpPtrAllocator &Allocator);
  125. private:
  126. MappedBlockStream ReadInterface;
  127. WritableBinaryStreamRef WriteInterface;
  128. };
  129. } // end namespace pdb
  130. } // end namespace llvm
  131. #endif // LLVM_DEBUGINFO_MSF_MAPPEDBLOCKSTREAM_H
  132. #ifdef __GNUC__
  133. #pragma GCC diagnostic pop
  134. #endif