Refactoring and fix another instance of asan error

llvm-svn: 252783
This commit is contained in:
Xinliang David Li 2015-11-11 19:31:53 +00:00
parent 0b44dcc44a
commit 4d1bef3f76
1 changed files with 10 additions and 5 deletions

View File

@ -244,12 +244,17 @@ void ValueProfData::deserializeTo(InstrProfRecord &Record,
}
}
static ValueProfData *AllocValueProfData(uint32_t TotalSize) {
void *RawMem = ::operator new(TotalSize);
ValueProfData *VPDMem = new (RawMem) ValueProfData();
return VPDMem;
}
std::unique_ptr<ValueProfData>
ValueProfData::serializeFrom(const InstrProfRecord &Record) {
uint32_t TotalSize = getSize(Record);
void *RawMem = ::operator new(TotalSize);
ValueProfData *VPDMem = new (RawMem) ValueProfData();
std::unique_ptr<ValueProfData> VPD(VPDMem);
std::unique_ptr<ValueProfData> VPD(AllocValueProfData(TotalSize));
VPD->TotalSize = TotalSize;
VPD->NumValueKinds = Record.getNumValueKinds();
@ -285,8 +290,8 @@ ValueProfData::getValueProfData(const unsigned char *D,
if (TotalSize % sizeof(uint64_t))
return instrprof_error::malformed;
std::unique_ptr<ValueProfData> VPD(
reinterpret_cast<ValueProfData *>(new char[TotalSize]));
std::unique_ptr<ValueProfData> VPD(AllocValueProfData(TotalSize));
memcpy(VPD.get(), D, TotalSize);
// Byte swap.
VPD->swapBytesToHost(Endianness);