Provide a convenient function to allocate and initialize objects.

You can now write make<T>(Args) instead of new (alloc<T>()) T(Args).

llvm-svn: 285760
This commit is contained in:
Rui Ueyama 2016-11-01 22:53:18 +00:00
parent 05c03845e7
commit d52adb3917
3 changed files with 13 additions and 16 deletions

View File

@ -131,7 +131,7 @@ void LinkerDriver::addFile(StringRef Path) {
MemoryBufferRef MBRef = *Buffer; MemoryBufferRef MBRef = *Buffer;
if (InBinary) { if (InBinary) {
Files.push_back(new (alloc<BinaryFile>()) BinaryFile(MBRef)); Files.push_back(make<BinaryFile>(MBRef));
return; return;
} }
@ -145,7 +145,7 @@ void LinkerDriver::addFile(StringRef Path) {
Files.push_back(createObjectFile(MB, Path)); Files.push_back(createObjectFile(MB, Path));
return; return;
} }
Files.push_back(new (alloc<ArchiveFile>()) ArchiveFile(MBRef)); Files.push_back(make<ArchiveFile>(MBRef));
return; return;
case file_magic::elf_shared_object: case file_magic::elf_shared_object:
if (Config->Relocatable) { if (Config->Relocatable) {
@ -156,7 +156,7 @@ void LinkerDriver::addFile(StringRef Path) {
return; return;
default: default:
if (InLib) if (InLib)
Files.push_back(new (alloc<LazyObjectFile>()) LazyObjectFile(MBRef)); Files.push_back(make<LazyObjectFile>(MBRef));
else else
Files.push_back(createObjectFile(MBRef)); Files.push_back(createObjectFile(MBRef));
} }

View File

@ -407,8 +407,7 @@ elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec,
// If -r is given, we do not interpret or apply relocation // If -r is given, we do not interpret or apply relocation
// but just copy relocation sections to output. // but just copy relocation sections to output.
if (Config->Relocatable) if (Config->Relocatable)
return new (alloc<InputSection<ELFT>>()) return make<InputSection<ELFT>>(this, &Sec, Name);
InputSection<ELFT>(this, &Sec, Name);
// Find the relocation target section and associate this // Find the relocation target section and associate this
// section with it. // section with it.
@ -450,13 +449,11 @@ elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec,
// .eh_frame_hdr section for runtime. So we handle them with a special // .eh_frame_hdr section for runtime. So we handle them with a special
// class. For relocatable outputs, they are just passed through. // class. For relocatable outputs, they are just passed through.
if (Name == ".eh_frame" && !Config->Relocatable) if (Name == ".eh_frame" && !Config->Relocatable)
return new (alloc<EhInputSection<ELFT>>()) return make<EhInputSection<ELFT>>(this, &Sec, Name);
EhInputSection<ELFT>(this, &Sec, Name);
if (shouldMerge(Sec)) if (shouldMerge(Sec))
return new (alloc<MergeInputSection<ELFT>>()) return make<MergeInputSection<ELFT>>(this, &Sec, Name);
MergeInputSection<ELFT>(this, &Sec, Name); return make<InputSection<ELFT>>(this, &Sec, Name);
return new (alloc<InputSection<ELFT>>()) InputSection<ELFT>(this, &Sec, Name);
} }
template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() { template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() {
@ -825,13 +822,13 @@ static InputFile *createELFFile(MemoryBufferRef MB) {
InputFile *Obj; InputFile *Obj;
if (Size == ELFCLASS32 && Endian == ELFDATA2LSB) if (Size == ELFCLASS32 && Endian == ELFDATA2LSB)
Obj = new (alloc<T<ELF32LE>>()) T<ELF32LE>(MB); Obj = make<T<ELF32LE>>(MB);
else if (Size == ELFCLASS32 && Endian == ELFDATA2MSB) else if (Size == ELFCLASS32 && Endian == ELFDATA2MSB)
Obj = new (alloc<T<ELF32BE>>()) T<ELF32BE>(MB); Obj = make<T<ELF32BE>>(MB);
else if (Size == ELFCLASS64 && Endian == ELFDATA2LSB) else if (Size == ELFCLASS64 && Endian == ELFDATA2LSB)
Obj = new (alloc<T<ELF64LE>>()) T<ELF64LE>(MB); Obj = make<T<ELF64LE>>(MB);
else if (Size == ELFCLASS64 && Endian == ELFDATA2MSB) else if (Size == ELFCLASS64 && Endian == ELFDATA2MSB)
Obj = new (alloc<T<ELF64BE>>()) T<ELF64BE>(MB); Obj = make<T<ELF64BE>>(MB);
else else
fatal("invalid file class: " + MB.getBufferIdentifier()); fatal("invalid file class: " + MB.getBufferIdentifier());

View File

@ -49,9 +49,9 @@ template <class T> struct SpecificAlloc : public SpecificAllocBase {
// Use this arean if your object have a destructor. // Use this arean if your object have a destructor.
// Your destructor will be invoked from freeArena(). // Your destructor will be invoked from freeArena().
template <class T> static T *alloc() { template <typename T, typename... U> static T *make(U &&... Args) {
static SpecificAlloc<T> Alloc; static SpecificAlloc<T> Alloc;
return Alloc.Alloc.Allocate(); return new (Alloc.Alloc.Allocate()) T(std::forward<U>(Args)...);
} }
void freeArena(); void freeArena();