[lld] r285748 - Replace GAlloc with a template function.
Rafael Espindola via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 1 14:06:41 PDT 2016
Author: rafael
Date: Tue Nov 1 16:06:40 2016
New Revision: 285748
URL: http://llvm.org/viewvc/llvm-project?rev=285748&view=rev
Log:
Replace GAlloc with a template function.
This patch replaces GAlloc<ELFT>::<sometype>.Allocate() with
alloc<sometype<ELFT>>().
Patch by Rui!
Modified:
lld/trunk/ELF/Driver.cpp
lld/trunk/ELF/InputFiles.cpp
lld/trunk/ELF/InputFiles.h
lld/trunk/ELF/Memory.cpp
lld/trunk/ELF/Memory.h
Modified: lld/trunk/ELF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.cpp?rev=285748&r1=285747&r2=285748&view=diff
==============================================================================
--- lld/trunk/ELF/Driver.cpp (original)
+++ lld/trunk/ELF/Driver.cpp Tue Nov 1 16:06:40 2016
@@ -54,7 +54,6 @@ bool elf::link(ArrayRef<const char *> Ar
ScriptConfig = &SC;
Driver->main(Args, CanExitEarly);
- InputFile::freePool();
freeArena();
return !HasError;
}
@@ -132,7 +131,7 @@ void LinkerDriver::addFile(StringRef Pat
MemoryBufferRef MBRef = *Buffer;
if (InBinary) {
- Files.push_back(new BinaryFile(MBRef));
+ Files.push_back(new (alloc<BinaryFile>()) BinaryFile(MBRef));
return;
}
@@ -146,7 +145,7 @@ void LinkerDriver::addFile(StringRef Pat
Files.push_back(createObjectFile(MB, Path));
return;
}
- Files.push_back(new ArchiveFile(MBRef));
+ Files.push_back(new (alloc<ArchiveFile>()) ArchiveFile(MBRef));
return;
case file_magic::elf_shared_object:
if (Config->Relocatable) {
@@ -157,7 +156,7 @@ void LinkerDriver::addFile(StringRef Pat
return;
default:
if (InLib)
- Files.push_back(new LazyObjectFile(MBRef));
+ Files.push_back(new (alloc<LazyObjectFile>()) LazyObjectFile(MBRef));
else
Files.push_back(createObjectFile(MBRef));
}
Modified: lld/trunk/ELF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.cpp?rev=285748&r1=285747&r2=285748&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.cpp (original)
+++ lld/trunk/ELF/InputFiles.cpp Tue Nov 1 16:06:40 2016
@@ -35,7 +35,6 @@ using namespace llvm::sys::fs;
using namespace lld;
using namespace lld::elf;
-std::vector<InputFile *> InputFile::Pool;
namespace {
// In ELF object file all section addresses are zero. If we have multiple
@@ -97,15 +96,6 @@ std::string DIHelper<ELFT>::getLineInfo(
: "";
}
-// Deletes all InputFile instances created so far.
-void InputFile::freePool() {
- // Files are freed in reverse order so that files created
- // from other files (e.g. object files extracted from archives)
- // are freed in the proper order.
- for (int I = Pool.size() - 1; I >= 0; --I)
- delete Pool[I];
-}
-
// Returns "(internal)", "foo.a(bar.o)" or "baz.o".
std::string elf::getFilename(const InputFile *F) {
if (!F)
@@ -415,7 +405,7 @@ elf::ObjectFile<ELFT>::createInputSectio
// If -r is given, we do not interpret or apply relocation
// but just copy relocation sections to output.
if (Config->Relocatable)
- return new (GAlloc<ELFT>::IAlloc.Allocate())
+ return new (alloc<InputSection<ELFT>>())
InputSection<ELFT>(this, &Sec, Name);
// Find the relocation target section and associate this
@@ -458,14 +448,13 @@ elf::ObjectFile<ELFT>::createInputSectio
// .eh_frame_hdr section for runtime. So we handle them with a special
// class. For relocatable outputs, they are just passed through.
if (Name == ".eh_frame" && !Config->Relocatable)
- return new (GAlloc<ELFT>::EHAlloc.Allocate())
+ return new (alloc<EhInputSection<ELFT>>())
EhInputSection<ELFT>(this, &Sec, Name);
if (shouldMerge(Sec))
- return new (GAlloc<ELFT>::MAlloc.Allocate())
+ return new (alloc<MergeInputSection<ELFT>>())
MergeInputSection<ELFT>(this, &Sec, Name);
- return new (GAlloc<ELFT>::IAlloc.Allocate())
- InputSection<ELFT>(this, &Sec, Name);
+ return new (alloc<InputSection<ELFT>>()) InputSection<ELFT>(this, &Sec, Name);
}
template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() {
@@ -834,13 +823,13 @@ static InputFile *createELFFile(MemoryBu
InputFile *Obj;
if (Size == ELFCLASS32 && Endian == ELFDATA2LSB)
- Obj = new T<ELF32LE>(MB);
+ Obj = new (alloc<T<ELF32LE>>()) T<ELF32LE>(MB);
else if (Size == ELFCLASS32 && Endian == ELFDATA2MSB)
- Obj = new T<ELF32BE>(MB);
+ Obj = new (alloc<T<ELF32BE>>()) T<ELF32BE>(MB);
else if (Size == ELFCLASS64 && Endian == ELFDATA2LSB)
- Obj = new T<ELF64LE>(MB);
+ Obj = new (alloc<T<ELF64LE>>()) T<ELF64LE>(MB);
else if (Size == ELFCLASS64 && Endian == ELFDATA2MSB)
- Obj = new T<ELF64BE>(MB);
+ Obj = new (alloc<T<ELF64BE>>()) T<ELF64BE>(MB);
else
fatal("invalid file class: " + MB.getBufferIdentifier());
Modified: lld/trunk/ELF/InputFiles.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.h?rev=285748&r1=285747&r2=285748&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.h (original)
+++ lld/trunk/ELF/InputFiles.h Tue Nov 1 16:06:40 2016
@@ -37,21 +37,6 @@ class InputFile;
namespace lld {
namespace elf {
-template <class ELFT> struct GAlloc {
- static llvm::SpecificBumpPtrAllocator<InputSection<ELFT>> IAlloc;
- static llvm::SpecificBumpPtrAllocator<MergeInputSection<ELFT>> MAlloc;
- static llvm::SpecificBumpPtrAllocator<EhInputSection<ELFT>> EHAlloc;
-};
-
-template <class ELFT>
-llvm::SpecificBumpPtrAllocator<InputSection<ELFT>> GAlloc<ELFT>::IAlloc;
-
-template <class ELFT>
-llvm::SpecificBumpPtrAllocator<MergeInputSection<ELFT>> GAlloc<ELFT>::MAlloc;
-
-template <class ELFT>
-llvm::SpecificBumpPtrAllocator<EhInputSection<ELFT>> GAlloc<ELFT>::EHAlloc;
-
using llvm::object::Archive;
class InputFile;
@@ -78,8 +63,6 @@ private:
// The root class of input files.
class InputFile {
public:
- virtual ~InputFile() = default;
-
enum Kind {
ObjectKind,
SharedKind,
@@ -111,19 +94,11 @@ public:
uint16_t EMachine = llvm::ELF::EM_NONE;
uint8_t OSABI = 0;
- static void freePool();
-
protected:
- InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {
- Pool.push_back(this);
- }
+ InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
private:
const Kind FileKind;
-
- // All InputFile instances are added to the pool
- // and freed all at once on exit by freePool().
- static std::vector<InputFile *> Pool;
};
// Returns "(internal)", "foo.a(bar.o)" or "baz.o".
Modified: lld/trunk/ELF/Memory.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Memory.cpp?rev=285748&r1=285747&r2=285748&view=diff
==============================================================================
--- lld/trunk/ELF/Memory.cpp (original)
+++ lld/trunk/ELF/Memory.cpp Tue Nov 1 16:06:40 2016
@@ -10,10 +10,20 @@
#include "Memory.h"
using namespace llvm;
+using namespace lld;
+using namespace lld::elf;
namespace lld {
BumpPtrAllocator elf::BAlloc;
StringSaver elf::Saver{elf::BAlloc};
-void elf::freeArena() { elf::BAlloc.Reset(); }
+SpecificAllocBase::SpecificAllocBase() { Instances.push_back(this); }
+
+std::vector<SpecificAllocBase *> SpecificAllocBase::Instances;
+
+void elf::freeArena() {
+ for (SpecificAllocBase *Alloc : SpecificAllocBase::Instances)
+ Alloc->reset();
+ BAlloc.Reset();
+}
}
Modified: lld/trunk/ELF/Memory.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Memory.h?rev=285748&r1=285747&r2=285748&view=diff
==============================================================================
--- lld/trunk/ELF/Memory.h (original)
+++ lld/trunk/ELF/Memory.h Tue Nov 1 16:06:40 2016
@@ -24,12 +24,36 @@
#include "llvm/Support/Allocator.h"
#include "llvm/Support/StringSaver.h"
+#include <vector>
namespace lld {
namespace elf {
+
+// Use this arena if your object doesn't have a destructor.
extern llvm::BumpPtrAllocator BAlloc;
extern llvm::StringSaver Saver;
+// These two classes are hack to keep track of all
+// SpecificBumpPtrAllocator instances.
+struct SpecificAllocBase {
+ SpecificAllocBase();
+ virtual ~SpecificAllocBase() = default;
+ virtual void reset() = 0;
+ static std::vector<SpecificAllocBase *> Instances;
+};
+
+template <class T> struct SpecificAlloc : public SpecificAllocBase {
+ void reset() override { Alloc.DestroyAll(); }
+ llvm::SpecificBumpPtrAllocator<T> Alloc;
+};
+
+// Use this arean if your object have a destructor.
+// Your destructor will be invoked from freeArena().
+template <class T> static T *alloc() {
+ static SpecificAlloc<T> Alloc;
+ return Alloc.Alloc.Allocate();
+}
+
void freeArena();
}
}
More information about the llvm-commits
mailing list