teak-llvm/llvm/lib/DebugInfo/CodeView/ByteStream.cpp
Zachary Turner 8dbe3629a0 [codeview,pdb] Try really hard to conserve memory when reading.
PDBs can be extremely large.  We're already mapping the entire
PDB into the process's address space, but to make matters worse
the blocks of the PDB are not arranged contiguously.  So, when
we have something like an array or a string embedded into the
stream, we have to make a copy.  Since it's convenient to use
traditional data structures to iterate and manipulate these
records, we need the memory to be contiguous.

As a result of this, we were using roughly twice as much memory
as the file size of the PDB, because every stream was copied
out and re-stitched together contiguously.

This patch addresses this by improving the MappedBlockStream
to allocate from a BumpPtrAllocator only when a read requires
a discontiguous read.  Furthermore, it introduces some data
structures backed by a stream which can iterate over both
fixed and variable length records of a PDB.  Since everything
is backed by a stream and not a buffer, we can read almost
everything from the PDB with zero copies.

Differential Revision: http://reviews.llvm.org/D20654
Reviewed By: ruiu

llvm-svn: 270951
2016-05-27 01:54:44 +00:00

66 lines
1.9 KiB
C++

//===- ByteStream.cpp - Reads stream data from a byte sequence ------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/DebugInfo/CodeView/ByteStream.h"
#include "llvm/DebugInfo/CodeView/CodeViewError.h"
#include "llvm/DebugInfo/CodeView/StreamReader.h"
#include <cstring>
using namespace llvm;
using namespace llvm::codeview;
ByteStream::ByteStream() {}
ByteStream::ByteStream(MutableArrayRef<uint8_t> Data) : Data(Data) {}
ByteStream::~ByteStream() {}
void ByteStream::reset() {
Ownership.reset();
Data = MutableArrayRef<uint8_t>();
}
void ByteStream::load(uint32_t Length) {
reset();
if (Length > 0)
Data = MutableArrayRef<uint8_t>(new uint8_t[Length], Length);
Ownership.reset(Data.data());
}
Error ByteStream::load(StreamReader &Reader, uint32_t Length) {
load(Length);
auto EC = Reader.readBytes(Data);
if (EC)
reset();
return EC;
}
Error ByteStream::readBytes(uint32_t Offset,
MutableArrayRef<uint8_t> Buffer) const {
if (Data.size() < Buffer.size() + Offset)
return make_error<CodeViewError>(cv_error_code::insufficient_buffer);
::memcpy(Buffer.data() + Offset, Data.data(), Buffer.size());
return Error::success();
}
Error ByteStream::readBytes(uint32_t Offset, uint32_t Size,
ArrayRef<uint8_t> &Buffer) const {
if (Data.size() < Buffer.size() + Offset)
return make_error<CodeViewError>(cv_error_code::insufficient_buffer);
Buffer = Data.slice(Offset, Size);
return Error::success();
}
uint32_t ByteStream::getLength() const { return Data.size(); }
StringRef ByteStream::str() const {
const char *CharData = reinterpret_cast<const char *>(Data.data());
return StringRef(CharData, Data.size());
}