blob: acc92f90d26ac2cd185e586071fa26eda63704f0 [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
17MappedBlockStream::MappedBlockStream(uint32_t StreamIdx, const PDBFile &File) : Pdb(File) {
Zachary Turner96e60f72016-05-24 20:31:48 +000018 if (StreamIdx >= Pdb.getNumStreams()) {
19 StreamLength = 0;
20 } else {
21 StreamLength = Pdb.getStreamByteSize(StreamIdx);
22 BlockList = Pdb.getStreamBlockList(StreamIdx);
23 }
Zachary Turner6ba65de2016-04-29 17:22:58 +000024}
25
Zachary Turner8dbe3622016-05-27 01:54:44 +000026Error MappedBlockStream::readBytes(uint32_t Offset, uint32_t Size,
27 ArrayRef<uint8_t> &Buffer) const {
28 // Make sure we aren't trying to read beyond the end of the stream.
Zachary Turner7dd42592016-05-27 20:17:33 +000029 if (Size > StreamLength)
Zachary Turner8dbe3622016-05-27 01:54:44 +000030 return make_error<RawError>(raw_error_code::insufficient_buffer);
Zachary Turner7dd42592016-05-27 20:17:33 +000031 if (Offset > StreamLength - Size)
Zachary Turner8dbe3622016-05-27 01:54:44 +000032 return make_error<RawError>(raw_error_code::insufficient_buffer);
33
34 if (tryReadContiguously(Offset, Size, Buffer))
35 return Error::success();
36
37 auto CacheIter = CacheMap.find(Offset);
38 if (CacheIter != CacheMap.end()) {
39 // In a more general solution, we would need to guarantee that the
40 // cached allocation is at least the requested size. In practice, since
41 // these are CodeView / PDB records, we know they are always formatted
42 // the same way and never change, so we should never be requesting two
43 // allocations from the same address with different sizes.
44 Buffer = ArrayRef<uint8_t>(CacheIter->second, Size);
45 return Error::success();
46 }
47
48 // Otherwise allocate a large enough buffer in the pool, memcpy the data
49 // into it, and return an ArrayRef to that.
50 uint8_t *WriteBuffer = Pool.Allocate<uint8_t>(Size);
51
52 if (auto EC = readBytes(Offset, MutableArrayRef<uint8_t>(WriteBuffer, Size)))
53 return EC;
54 CacheMap.insert(std::make_pair(Offset, WriteBuffer));
55 Buffer = ArrayRef<uint8_t>(WriteBuffer, Size);
56 return Error::success();
57}
58
59bool MappedBlockStream::tryReadContiguously(uint32_t Offset, uint32_t Size,
60 ArrayRef<uint8_t> &Buffer) const {
61 // Attempt to fulfill the request with a reference directly into the stream.
62 // This can work even if the request crosses a block boundary, provided that
63 // all subsequent blocks are contiguous. For example, a 10k read with a 4k
64 // block size can be filled with a reference if, from the starting offset,
65 // 3 blocks in a row are contiguous.
66 uint32_t BlockNum = Offset / Pdb.getBlockSize();
67 uint32_t OffsetInBlock = Offset % Pdb.getBlockSize();
68 uint32_t BytesFromFirstBlock =
69 std::min(Size, Pdb.getBlockSize() - OffsetInBlock);
70 uint32_t NumAdditionalBlocks =
71 llvm::alignTo(Size - BytesFromFirstBlock, Pdb.getBlockSize()) /
72 Pdb.getBlockSize();
73
74 uint32_t RequiredContiguousBlocks = NumAdditionalBlocks + 1;
75 uint32_t E = BlockList[BlockNum];
76 for (uint32_t I = 0; I < RequiredContiguousBlocks; ++I, ++E) {
77 if (BlockList[I + BlockNum] != E)
78 return false;
79 }
80
81 uint32_t FirstBlockAddr = BlockList[BlockNum];
82 StringRef Str = Pdb.getBlockData(FirstBlockAddr, Pdb.getBlockSize());
83 Str = Str.drop_front(OffsetInBlock);
84 Buffer =
85 ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(Str.data()), Size);
86 return true;
87}
88
Zachary Turner819e77d2016-05-06 20:51:57 +000089Error MappedBlockStream::readBytes(uint32_t Offset,
90 MutableArrayRef<uint8_t> Buffer) const {
Zachary Turner6ba65de2016-04-29 17:22:58 +000091 uint32_t BlockNum = Offset / Pdb.getBlockSize();
92 uint32_t OffsetInBlock = Offset % Pdb.getBlockSize();
93
94 // Make sure we aren't trying to read beyond the end of the stream.
95 if (Buffer.size() > StreamLength)
Zachary Turner819e77d2016-05-06 20:51:57 +000096 return make_error<RawError>(raw_error_code::insufficient_buffer);
Zachary Turner6ba65de2016-04-29 17:22:58 +000097 if (Offset > StreamLength - Buffer.size())
Zachary Turner819e77d2016-05-06 20:51:57 +000098 return make_error<RawError>(raw_error_code::insufficient_buffer);
Zachary Turner6ba65de2016-04-29 17:22:58 +000099
100 uint32_t BytesLeft = Buffer.size();
101 uint32_t BytesWritten = 0;
102 uint8_t *WriteBuffer = Buffer.data();
103 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}