blob: 34d16712271e103dd96a107277cbe14254db2f7d [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"
11#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000012#include "llvm/DebugInfo/PDB/Raw/RawError.h"
Zachary Turner6ba65de2016-04-29 17:22:58 +000013
14using namespace llvm;
Zachary Turner2f09b502016-04-29 17:28:47 +000015using namespace llvm::pdb;
Zachary Turner6ba65de2016-04-29 17:22:58 +000016
Zachary Turner90b8b8d2016-05-31 22:41:52 +000017MappedBlockStream::MappedBlockStream(uint32_t StreamIdx, const IPDBFile &File)
18 : Pdb(File) {
Zachary Turner96e60f72016-05-24 20:31:48 +000019 if (StreamIdx >= Pdb.getNumStreams()) {
20 StreamLength = 0;
21 } else {
22 StreamLength = Pdb.getStreamByteSize(StreamIdx);
23 BlockList = Pdb.getStreamBlockList(StreamIdx);
24 }
Zachary Turner6ba65de2016-04-29 17:22:58 +000025}
26
Zachary Turner8dbe3622016-05-27 01:54:44 +000027Error MappedBlockStream::readBytes(uint32_t Offset, uint32_t Size,
28 ArrayRef<uint8_t> &Buffer) const {
29 // Make sure we aren't trying to read beyond the end of the stream.
Zachary Turner7dd42592016-05-27 20:17:33 +000030 if (Size > StreamLength)
Zachary Turner8dbe3622016-05-27 01:54:44 +000031 return make_error<RawError>(raw_error_code::insufficient_buffer);
Zachary Turner7dd42592016-05-27 20:17:33 +000032 if (Offset > StreamLength - Size)
Zachary Turner8dbe3622016-05-27 01:54:44 +000033 return make_error<RawError>(raw_error_code::insufficient_buffer);
34
35 if (tryReadContiguously(Offset, Size, Buffer))
36 return Error::success();
37
38 auto CacheIter = CacheMap.find(Offset);
39 if (CacheIter != CacheMap.end()) {
40 // In a more general solution, we would need to guarantee that the
41 // cached allocation is at least the requested size. In practice, since
42 // these are CodeView / PDB records, we know they are always formatted
43 // the same way and never change, so we should never be requesting two
44 // allocations from the same address with different sizes.
45 Buffer = ArrayRef<uint8_t>(CacheIter->second, Size);
46 return Error::success();
47 }
48
49 // Otherwise allocate a large enough buffer in the pool, memcpy the data
50 // into it, and return an ArrayRef to that.
51 uint8_t *WriteBuffer = Pool.Allocate<uint8_t>(Size);
52
53 if (auto EC = readBytes(Offset, MutableArrayRef<uint8_t>(WriteBuffer, Size)))
54 return EC;
55 CacheMap.insert(std::make_pair(Offset, WriteBuffer));
56 Buffer = ArrayRef<uint8_t>(WriteBuffer, Size);
57 return Error::success();
58}
59
60bool MappedBlockStream::tryReadContiguously(uint32_t Offset, uint32_t Size,
61 ArrayRef<uint8_t> &Buffer) const {
62 // Attempt to fulfill the request with a reference directly into the stream.
63 // This can work even if the request crosses a block boundary, provided that
64 // all subsequent blocks are contiguous. For example, a 10k read with a 4k
65 // block size can be filled with a reference if, from the starting offset,
66 // 3 blocks in a row are contiguous.
67 uint32_t BlockNum = Offset / Pdb.getBlockSize();
68 uint32_t OffsetInBlock = Offset % Pdb.getBlockSize();
69 uint32_t BytesFromFirstBlock =
70 std::min(Size, Pdb.getBlockSize() - OffsetInBlock);
71 uint32_t NumAdditionalBlocks =
72 llvm::alignTo(Size - BytesFromFirstBlock, Pdb.getBlockSize()) /
73 Pdb.getBlockSize();
74
75 uint32_t RequiredContiguousBlocks = NumAdditionalBlocks + 1;
76 uint32_t E = BlockList[BlockNum];
77 for (uint32_t I = 0; I < RequiredContiguousBlocks; ++I, ++E) {
78 if (BlockList[I + BlockNum] != E)
79 return false;
80 }
81
82 uint32_t FirstBlockAddr = BlockList[BlockNum];
83 StringRef Str = Pdb.getBlockData(FirstBlockAddr, Pdb.getBlockSize());
84 Str = Str.drop_front(OffsetInBlock);
85 Buffer =
86 ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(Str.data()), Size);
87 return true;
88}
89
Zachary Turner819e77d2016-05-06 20:51:57 +000090Error MappedBlockStream::readBytes(uint32_t Offset,
91 MutableArrayRef<uint8_t> Buffer) const {
Zachary Turner6ba65de2016-04-29 17:22:58 +000092 uint32_t BlockNum = Offset / Pdb.getBlockSize();
93 uint32_t OffsetInBlock = Offset % Pdb.getBlockSize();
94
95 // Make sure we aren't trying to read beyond the end of the stream.
96 if (Buffer.size() > StreamLength)
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 if (Offset > StreamLength - Buffer.size())
Zachary Turner819e77d2016-05-06 20:51:57 +000099 return make_error<RawError>(raw_error_code::insufficient_buffer);
Zachary Turner6ba65de2016-04-29 17:22:58 +0000100
101 uint32_t BytesLeft = Buffer.size();
102 uint32_t BytesWritten = 0;
103 uint8_t *WriteBuffer = Buffer.data();
104 while (BytesLeft > 0) {
105 uint32_t StreamBlockAddr = BlockList[BlockNum];
106
107 StringRef Data = Pdb.getBlockData(StreamBlockAddr, Pdb.getBlockSize());
108
109 const char *ChunkStart = Data.data() + OffsetInBlock;
110 uint32_t BytesInChunk =
111 std::min(BytesLeft, Pdb.getBlockSize() - OffsetInBlock);
112 ::memcpy(WriteBuffer + BytesWritten, ChunkStart, BytesInChunk);
113
114 BytesWritten += BytesInChunk;
115 BytesLeft -= BytesInChunk;
116 ++BlockNum;
117 OffsetInBlock = 0;
118 }
119
Zachary Turner819e77d2016-05-06 20:51:57 +0000120 return Error::success();
Zachary Turnerf5c59652016-05-03 00:28:21 +0000121
Zachary Turnerf5c59652016-05-03 00:28:21 +0000122}
Zachary Turner90b8b8d2016-05-31 22:41:52 +0000123
124uint32_t MappedBlockStream::getNumBytesCopied() const {
125 return static_cast<uint32_t>(Pool.getBytesAllocated());
126}