Move ownership of Pass File's to the MachoLinkingContext.

In trying to fix the leaks in the MachO lld codebase, we need to have
a better model for file and atom ownership.  Having the context own
everything seems like the simplest model, so change all the passes to
allocate File's on the context instead of owning files as a member.

llvm-svn: 264004
This commit is contained in:
Pete Cooper 2016-03-21 23:17:47 +00:00
parent 602f79275d
commit c0c464cac1
7 changed files with 27 additions and 12 deletions

View File

@ -13,6 +13,7 @@
#include "lld/Core/LinkingContext.h"
#include "lld/Core/Reader.h"
#include "lld/Core/Writer.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Support/ErrorHandling.h"
@ -93,6 +94,18 @@ public:
void createImplicitFiles(std::vector<std::unique_ptr<File>> &) override;
/// Creates a new file which is owned by the context. Returns a pointer to
/// the new file.
template <class T, class... Args>
typename std::enable_if<!std::is_array<T>::value, T *>::type
make_file(Args &&... args) const {
auto file = std::unique_ptr<T>(new T(std::forward<Args>(args)...));
auto *filePtr = file.get();
auto *ctx = const_cast<MachOLinkingContext *>(this);
ctx->getNodes().push_back(llvm::make_unique<FileNode>(std::move(file)));
return filePtr;
}
uint32_t getCPUType() const;
uint32_t getCPUSubType() const;

View File

@ -273,7 +273,7 @@ class CompactUnwindPass : public Pass {
public:
CompactUnwindPass(const MachOLinkingContext &context)
: _ctx(context), _archHandler(_ctx.archHandler()),
_file("<mach-o Compact Unwind Pass>"),
_file(*_ctx.make_file<MachOFile>("<mach-o Compact Unwind Pass>")),
_isBig(MachOLinkingContext::isBigEndian(_ctx.arch())) {
_file.setOrdinal(_ctx.getNextOrdinalAndIncrement());
}
@ -567,7 +567,7 @@ private:
const MachOLinkingContext &_ctx;
mach_o::ArchHandler &_archHandler;
MachOFile _file;
MachOFile &_file;
bool _isBig;
};

View File

@ -91,7 +91,7 @@ class GOTPass : public Pass {
public:
GOTPass(const MachOLinkingContext &context)
: _ctx(context), _archHandler(_ctx.archHandler()),
_file("<mach-o GOT Pass>") {
_file(*_ctx.make_file<MachOFile>("<mach-o GOT Pass>")) {
_file.setOrdinal(_ctx.getNextOrdinalAndIncrement());
}
@ -169,7 +169,7 @@ private:
const MachOLinkingContext &_ctx;
mach_o::ArchHandler &_archHandler;
MachOFile _file;
MachOFile &_file;
llvm::DenseMap<const Atom*, const GOTEntryAtom*> _targetToGOT;
};

View File

@ -93,7 +93,7 @@ class ObjCPass : public Pass {
public:
ObjCPass(const MachOLinkingContext &context)
: _ctx(context),
_file("<mach-o objc pass>") {
_file(*_ctx.make_file<MachOFile>("<mach-o objc pass>")) {
_file.setOrdinal(_ctx.getNextOrdinalAndIncrement());
}
@ -113,7 +113,7 @@ private:
}
const MachOLinkingContext &_ctx;
MachOFile _file;
MachOFile &_file;
};

View File

@ -42,7 +42,8 @@ class ShimPass : public Pass {
public:
ShimPass(const MachOLinkingContext &context)
: _ctx(context), _archHandler(_ctx.archHandler()),
_stubInfo(_archHandler.stubInfo()), _file("<mach-o shim pass>") {
_stubInfo(_archHandler.stubInfo()),
_file(*_ctx.make_file<MachOFile>("<mach-o shim pass>")) {
_file.setOrdinal(_ctx.getNextOrdinalAndIncrement());
}
@ -114,7 +115,7 @@ private:
const MachOLinkingContext &_ctx;
mach_o::ArchHandler &_archHandler;
const ArchHandler::StubInfo &_stubInfo;
MachOFile _file;
MachOFile &_file;
llvm::DenseMap<const Atom*, const DefinedAtom*> _targetToShim;
};

View File

@ -200,7 +200,8 @@ class StubsPass : public Pass {
public:
StubsPass(const MachOLinkingContext &context)
: _ctx(context), _archHandler(_ctx.archHandler()),
_stubInfo(_archHandler.stubInfo()), _file("<mach-o Stubs pass>") {
_stubInfo(_archHandler.stubInfo()),
_file(*_ctx.make_file<MachOFile>("<mach-o Stubs pass>")) {
_file.setOrdinal(_ctx.getNextOrdinalAndIncrement());
}
@ -356,7 +357,7 @@ private:
const MachOLinkingContext &_ctx;
mach_o::ArchHandler &_archHandler;
const ArchHandler::StubInfo &_stubInfo;
MachOFile _file;
MachOFile &_file;
TargetToUses _targetToUses;
};

View File

@ -65,7 +65,7 @@ class TLVPass : public Pass {
public:
TLVPass(const MachOLinkingContext &context)
: _ctx(context), _archHandler(_ctx.archHandler()),
_file("<mach-o TLV Pass>") {
_file(*_ctx.make_file<MachOFile>("<mach-o TLV pass>")) {
_file.setOrdinal(_ctx.getNextOrdinalAndIncrement());
}
@ -126,7 +126,7 @@ private:
const MachOLinkingContext &_ctx;
mach_o::ArchHandler &_archHandler;
MachOFile _file;
MachOFile &_file;
llvm::DenseMap<const Atom*, const TLVPEntryAtom*> _targetToTLVP;
};