LLVM  4.0.0
MappedBlockStream.h
Go to the documentation of this file.
1 //===- MappedBlockStream.h - Discontiguous stream data in an MSF -*- C++
2 //-*-===//
3 //
4 // The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 
11 #ifndef LLVM_DEBUGINFO_MSF_MAPPEDBLOCKSTREAM_H
12 #define LLVM_DEBUGINFO_MSF_MAPPEDBLOCKSTREAM_H
13 
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/Support/Allocator.h"
20 #include "llvm/Support/Endian.h"
21 #include "llvm/Support/Error.h"
22 #include <cstdint>
23 #include <vector>
24 
25 namespace llvm {
26 namespace msf {
27 
28 struct MSFLayout;
29 
30 /// MappedBlockStream represents data stored in an MSF file into chunks of a
31 /// particular size (called the Block Size), and whose chunks may not be
32 /// necessarily contiguous. The arrangement of these chunks MSF the file
33 /// is described by some other metadata contained within the MSF file. In
34 /// the case of a standard MSF Stream, the layout of the stream's blocks
35 /// is described by the MSF "directory", but in the case of the directory
36 /// itself, the layout is described by an array at a fixed location within
37 /// the MSF. MappedBlockStream provides methods for reading from and writing
38 /// to one of these streams transparently, as if it were a contiguous sequence
39 /// of bytes.
42 public:
43  static std::unique_ptr<MappedBlockStream>
45  const MSFStreamLayout &Layout, const ReadableStream &MsfData);
46 
47  static std::unique_ptr<MappedBlockStream>
48  createIndexedStream(const MSFLayout &Layout, const ReadableStream &MsfData,
49  uint32_t StreamIndex);
50 
51  static std::unique_ptr<MappedBlockStream>
52  createFpmStream(const MSFLayout &Layout, const ReadableStream &MsfData);
53 
54  static std::unique_ptr<MappedBlockStream>
55  createDirectoryStream(const MSFLayout &Layout, const ReadableStream &MsfData);
56 
58  ArrayRef<uint8_t> &Buffer) const override;
60  ArrayRef<uint8_t> &Buffer) const override;
61 
62  uint32_t getLength() const override;
63 
65 
67 
68  void invalidateCache();
69 
70  uint32_t getBlockSize() const { return BlockSize; }
71  uint32_t getNumBlocks() const { return NumBlocks; }
72  uint32_t getStreamLength() const { return StreamLayout.Length; }
73 
74 protected:
76  const MSFStreamLayout &StreamLayout,
77  const ReadableStream &MsfData);
78 
79 private:
80  const MSFStreamLayout &getStreamLayout() const { return StreamLayout; }
81  void fixCacheAfterWrite(uint32_t Offset, ArrayRef<uint8_t> Data) const;
82 
84  bool tryReadContiguously(uint32_t Offset, uint32_t Size,
85  ArrayRef<uint8_t> &Buffer) const;
86 
87  const uint32_t BlockSize;
88  const uint32_t NumBlocks;
89  const MSFStreamLayout StreamLayout;
90  const ReadableStream &MsfData;
91 
92  typedef MutableArrayRef<uint8_t> CacheEntry;
93  mutable llvm::BumpPtrAllocator Pool;
95 };
96 
98 public:
99  static std::unique_ptr<WritableMappedBlockStream>
101  const MSFStreamLayout &Layout, const WritableStream &MsfData);
102 
103  static std::unique_ptr<WritableMappedBlockStream>
104  createIndexedStream(const MSFLayout &Layout, const WritableStream &MsfData,
105  uint32_t StreamIndex);
106 
107  static std::unique_ptr<WritableMappedBlockStream>
108  createDirectoryStream(const MSFLayout &Layout, const WritableStream &MsfData);
109 
110  static std::unique_ptr<WritableMappedBlockStream>
111  createFpmStream(const MSFLayout &Layout, const WritableStream &MsfData);
112 
114  ArrayRef<uint8_t> &Buffer) const override;
116  ArrayRef<uint8_t> &Buffer) const override;
117  uint32_t getLength() const override;
118 
119  Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Buffer) const override;
120 
121  Error commit() const override;
122 
124  return ReadInterface.getStreamLayout();
125  }
126  uint32_t getBlockSize() const { return ReadInterface.getBlockSize(); }
127  uint32_t getNumBlocks() const { return ReadInterface.getNumBlocks(); }
128  uint32_t getStreamLength() const { return ReadInterface.getStreamLength(); }
129 
130 protected:
131  WritableMappedBlockStream(uint32_t BlockSize, uint32_t NumBlocks,
132  const MSFStreamLayout &StreamLayout,
133  const WritableStream &MsfData);
134 
135 private:
136  MappedBlockStream ReadInterface;
137 
138  const WritableStream &WriteInterface;
139 };
140 
141 } // end namespace pdb
142 } // end namespace llvm
143 
144 #endif // LLVM_DEBUGINFO_MSF_MAPPEDBLOCKSTREAM_H
static std::unique_ptr< MappedBlockStream > createStream(uint32_t BlockSize, uint32_t NumBlocks, const MSFStreamLayout &Layout, const ReadableStream &MsfData)
Error writeBytes(uint32_t Offset, ArrayRef< uint8_t > Buffer) const override
static std::unique_ptr< WritableMappedBlockStream > createDirectoryStream(const MSFLayout &Layout, const WritableStream &MsfData)
uint32_t getLength() const override
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
Describes the layout of a stream in an MSF layout.
Error readLongestContiguousChunk(uint32_t Offset, ArrayRef< uint8_t > &Buffer) const override
Error readBytes(uint32_t Offset, uint32_t Size, ArrayRef< uint8_t > &Buffer) const override
static std::unique_ptr< WritableMappedBlockStream > createFpmStream(const MSFLayout &Layout, const WritableStream &MsfData)
static std::unique_ptr< MappedBlockStream > createDirectoryStream(const MSFLayout &Layout, const ReadableStream &MsfData)
static std::unique_ptr< WritableMappedBlockStream > createStream(uint32_t BlockSize, uint32_t NumBlocks, const MSFStreamLayout &Layout, const WritableStream &MsfData)
static std::unique_ptr< MappedBlockStream > createFpmStream(const MSFLayout &Layout, const ReadableStream &MsfData)
Error readBytes(uint32_t Offset, uint32_t Size, ArrayRef< uint8_t > &Buffer) const override
MappedBlockStream(uint32_t BlockSize, uint32_t NumBlocks, const MSFStreamLayout &StreamLayout, const ReadableStream &MsfData)
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:138
uint32_t Offset
static const int BlockSize
Definition: TarWriter.cpp:34
WritableMappedBlockStream(uint32_t BlockSize, uint32_t NumBlocks, const MSFStreamLayout &StreamLayout, const WritableStream &MsfData)
static std::unique_ptr< MappedBlockStream > createIndexedStream(const MSFLayout &Layout, const ReadableStream &MsfData, uint32_t StreamIndex)
uint32_t getStreamLength() const
Error readLongestContiguousChunk(uint32_t Offset, ArrayRef< uint8_t > &Buffer) const override
static std::unique_ptr< WritableMappedBlockStream > createIndexedStream(const MSFLayout &Layout, const WritableStream &MsfData, uint32_t StreamIndex)
MappedBlockStream represents data stored in an MSF file into chunks of a particular size (called the ...
Lightweight error class with error context and mandatory checking.
llvm::BumpPtrAllocator & getAllocator()
const MSFStreamLayout & getStreamLayout() const