Simplify memory ownership with std::unique_ptr.

llvm-svn: 215567
This commit is contained in:
Rafael Espindola 2014-08-13 18:59:01 +00:00
parent 5f2bb7d9b8
commit bb415eac2e
1 changed files with 6 additions and 14 deletions

View File

@ -25,16 +25,6 @@ class TestObjectCache : public ObjectCache {
public: public:
TestObjectCache() : DuplicateInserted(false) { } TestObjectCache() : DuplicateInserted(false) { }
virtual ~TestObjectCache() {
// Free any buffers we've allocated.
SmallVectorImpl<MemoryBuffer *>::iterator it, end;
end = AllocatedBuffers.end();
for (it = AllocatedBuffers.begin(); it != end; ++it) {
delete *it;
}
AllocatedBuffers.clear();
}
virtual void notifyObjectCompiled(const Module *M, const MemoryBuffer *Obj) { virtual void notifyObjectCompiled(const Module *M, const MemoryBuffer *Obj) {
// If we've seen this module before, note that. // If we've seen this module before, note that.
const std::string ModuleID = M->getModuleIdentifier(); const std::string ModuleID = M->getModuleIdentifier();
@ -75,14 +65,16 @@ public:
private: private:
MemoryBuffer *copyBuffer(const MemoryBuffer *Buf) { MemoryBuffer *copyBuffer(const MemoryBuffer *Buf) {
// Create a local copy of the buffer. // Create a local copy of the buffer.
MemoryBuffer *NewBuffer = MemoryBuffer::getMemBufferCopy(Buf->getBuffer()); std::unique_ptr<MemoryBuffer> NewBuffer(
AllocatedBuffers.push_back(NewBuffer); MemoryBuffer::getMemBufferCopy(Buf->getBuffer()));
return NewBuffer; MemoryBuffer *Ret = NewBuffer.get();
AllocatedBuffers.push_back(std::move(NewBuffer));
return Ret;
} }
StringMap<const MemoryBuffer *> ObjMap; StringMap<const MemoryBuffer *> ObjMap;
StringSet<> ModulesLookedUp; StringSet<> ModulesLookedUp;
SmallVector<MemoryBuffer *, 2> AllocatedBuffers; SmallVector<std::unique_ptr<MemoryBuffer>, 2> AllocatedBuffers;
bool DuplicateInserted; bool DuplicateInserted;
}; };