blob: 03462b388630702f73dfae6b2b6c8b7ef242050a [file] [log] [blame]
Zachary Turner6ba65de2016-04-29 17:22:58 +00001//===- MappedBlockStream.cpp - Reads stream data from a PDBFile -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/DebugInfo/PDB/Raw/MappedBlockStream.h"
Zachary Turnerd8447992016-06-07 05:28:55 +000011#include "llvm/DebugInfo/PDB/Raw/IPDBStreamData.h"
Zachary Turner6ba65de2016-04-29 17:22:58 +000012#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000013#include "llvm/DebugInfo/PDB/Raw/RawError.h"
Zachary Turner6ba65de2016-04-29 17:22:58 +000014
15using namespace llvm;
Zachary Turner2f09b502016-04-29 17:28:47 +000016using namespace llvm::pdb;
Zachary Turner6ba65de2016-04-29 17:22:58 +000017
Zachary Turnerd8447992016-06-07 05:28:55 +000018MappedBlockStream::MappedBlockStream(std::unique_ptr<IPDBStreamData> Data,
19 const IPDBFile &Pdb)
20 : Pdb(Pdb), Data(std::move(Data)) {}
Zachary Turner6ba65de2016-04-29 17:22:58 +000021
Zachary Turner8dbe3622016-05-27 01:54:44 +000022Error MappedBlockStream::readBytes(uint32_t Offset, uint32_t Size,
23 ArrayRef<uint8_t> &Buffer) const {
24 // Make sure we aren't trying to read beyond the end of the stream.
Zachary Turnerd8447992016-06-07 05:28:55 +000025 if (Size > Data->getLength())
Zachary Turner8dbe3622016-05-27 01:54:44 +000026 return make_error<RawError>(raw_error_code::insufficient_buffer);
Zachary Turnerd8447992016-06-07 05:28:55 +000027 if (Offset > Data->getLength() - Size)
Zachary Turner8dbe3622016-05-27 01:54:44 +000028 return make_error<RawError>(raw_error_code::insufficient_buffer);
29
30 if (tryReadContiguously(Offset, Size, Buffer))
31 return Error::success();
32
33 auto CacheIter = CacheMap.find(Offset);
34 if (CacheIter != CacheMap.end()) {
35 // In a more general solution, we would need to guarantee that the
36 // cached allocation is at least the requested size. In practice, since
37 // these are CodeView / PDB records, we know they are always formatted
38 // the same way and never change, so we should never be requesting two
39 // allocations from the same address with different sizes.
40 Buffer = ArrayRef<uint8_t>(CacheIter->second, Size);
41 return Error::success();
42 }
43
44 // Otherwise allocate a large enough buffer in the pool, memcpy the data
45 // into it, and return an ArrayRef to that.
46 uint8_t *WriteBuffer = Pool.Allocate<uint8_t>(Size);
47
48 if (auto EC = readBytes(Offset, MutableArrayRef<uint8_t>(WriteBuffer, Size)))
49 return EC;
50 CacheMap.insert(std::make_pair(Offset, WriteBuffer));
51 Buffer = ArrayRef<uint8_t>(WriteBuffer, Size);
52 return Error::success();
53}
54
Zachary Turnerd8447992016-06-07 05:28:55 +000055uint32_t MappedBlockStream::getLength() const { return Data->getLength(); }
56
Zachary Turner8dbe3622016-05-27 01:54:44 +000057bool MappedBlockStream::tryReadContiguously(uint32_t Offset, uint32_t Size,
58 ArrayRef<uint8_t> &Buffer) const {
59 // Attempt to fulfill the request with a reference directly into the stream.
60 // This can work even if the request crosses a block boundary, provided that
61 // all subsequent blocks are contiguous. For example, a 10k read with a 4k
62 // block size can be filled with a reference if, from the starting offset,
63 // 3 blocks in a row are contiguous.
64 uint32_t BlockNum = Offset / Pdb.getBlockSize();
65 uint32_t OffsetInBlock = Offset % Pdb.getBlockSize();
66 uint32_t BytesFromFirstBlock =
67 std::min(Size, Pdb.getBlockSize() - OffsetInBlock);
68 uint32_t NumAdditionalBlocks =
69 llvm::alignTo(Size - BytesFromFirstBlock, Pdb.getBlockSize()) /
70 Pdb.getBlockSize();
71
Zachary Turnerd8447992016-06-07 05:28:55 +000072 auto BlockList = Data->getStreamBlocks();
Zachary Turner8dbe3622016-05-27 01:54:44 +000073 uint32_t RequiredContiguousBlocks = NumAdditionalBlocks + 1;
74 uint32_t E = BlockList[BlockNum];
75 for (uint32_t I = 0; I < RequiredContiguousBlocks; ++I, ++E) {
76 if (BlockList[I + BlockNum] != E)
77 return false;
78 }
79
80 uint32_t FirstBlockAddr = BlockList[BlockNum];
81 StringRef Str = Pdb.getBlockData(FirstBlockAddr, Pdb.getBlockSize());
82 Str = Str.drop_front(OffsetInBlock);
83 Buffer =
84 ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(Str.data()), Size);
85 return true;
86}
87
Zachary Turner819e77d2016-05-06 20:51:57 +000088Error MappedBlockStream::readBytes(uint32_t Offset,
89 MutableArrayRef<uint8_t> Buffer) const {
Zachary Turner6ba65de2016-04-29 17:22:58 +000090 uint32_t BlockNum = Offset / Pdb.getBlockSize();
91 uint32_t OffsetInBlock = Offset % Pdb.getBlockSize();
92
93 // Make sure we aren't trying to read beyond the end of the stream.
Zachary Turnerd8447992016-06-07 05:28:55 +000094 if (Buffer.size() > Data->getLength())
Zachary Turner819e77d2016-05-06 20:51:57 +000095 return make_error<RawError>(raw_error_code::insufficient_buffer);
Zachary Turnerd8447992016-06-07 05:28:55 +000096 if (Offset > Data->getLength() - Buffer.size())
Zachary Turner819e77d2016-05-06 20:51:57 +000097 return make_error<RawError>(raw_error_code::insufficient_buffer);
Zachary Turner6ba65de2016-04-29 17:22:58 +000098
99 uint32_t BytesLeft = Buffer.size();
100 uint32_t BytesWritten = 0;
101 uint8_t *WriteBuffer = Buffer.data();
Zachary Turnerd8447992016-06-07 05:28:55 +0000102 auto BlockList = Data->getStreamBlocks();
Zachary Turner6ba65de2016-04-29 17:22:58 +0000103 while (BytesLeft > 0) {
104 uint32_t StreamBlockAddr = BlockList[BlockNum];
105
106 StringRef Data = Pdb.getBlockData(StreamBlockAddr, Pdb.getBlockSize());
107
108 const char *ChunkStart = Data.data() + OffsetInBlock;
109 uint32_t BytesInChunk =
110 std::min(BytesLeft, Pdb.getBlockSize() - OffsetInBlock);
111 ::memcpy(WriteBuffer + BytesWritten, ChunkStart, BytesInChunk);
112
113 BytesWritten += BytesInChunk;
114 BytesLeft -= BytesInChunk;
115 ++BlockNum;
116 OffsetInBlock = 0;
117 }
118
Zachary Turner819e77d2016-05-06 20:51:57 +0000119 return Error::success();
Zachary Turnerf5c59652016-05-03 00:28:21 +0000120
Zachary Turnerf5c59652016-05-03 00:28:21 +0000121}
Zachary Turner90b8b8d2016-05-31 22:41:52 +0000122
123uint32_t MappedBlockStream::getNumBytesCopied() const {
124 return static_cast<uint32_t>(Pool.getBytesAllocated());
125}