[lld] r285760 - Provide a convenient function to allocate and initialize objects.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 1 15:53:19 PDT 2016


Author: ruiu
Date: Tue Nov  1 17:53:18 2016
New Revision: 285760

URL: http://llvm.org/viewvc/llvm-project?rev=285760&view=rev
Log:
Provide a convenient function to allocate and initialize objects.

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

Modified:
    lld/trunk/ELF/Driver.cpp
    lld/trunk/ELF/InputFiles.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=285760&r1=285759&r2=285760&view=diff
==============================================================================
--- lld/trunk/ELF/Driver.cpp (original)
+++ lld/trunk/ELF/Driver.cpp Tue Nov  1 17:53:18 2016
@@ -131,7 +131,7 @@ void LinkerDriver::addFile(StringRef Pat
   MemoryBufferRef MBRef = *Buffer;
 
   if (InBinary) {
-    Files.push_back(new (alloc<BinaryFile>()) BinaryFile(MBRef));
+    Files.push_back(make<BinaryFile>(MBRef));
     return;
   }
 
@@ -145,7 +145,7 @@ void LinkerDriver::addFile(StringRef Pat
         Files.push_back(createObjectFile(MB, Path));
       return;
     }
-    Files.push_back(new (alloc<ArchiveFile>()) ArchiveFile(MBRef));
+    Files.push_back(make<ArchiveFile>(MBRef));
     return;
   case file_magic::elf_shared_object:
     if (Config->Relocatable) {
@@ -156,7 +156,7 @@ void LinkerDriver::addFile(StringRef Pat
     return;
   default:
     if (InLib)
-      Files.push_back(new (alloc<LazyObjectFile>()) LazyObjectFile(MBRef));
+      Files.push_back(make<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=285760&r1=285759&r2=285760&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.cpp (original)
+++ lld/trunk/ELF/InputFiles.cpp Tue Nov  1 17:53:18 2016
@@ -407,8 +407,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 (alloc<InputSection<ELFT>>())
-          InputSection<ELFT>(this, &Sec, Name);
+      return make<InputSection<ELFT>>(this, &Sec, Name);
 
     // Find the relocation target section and associate this
     // section with it.
@@ -450,13 +449,11 @@ 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 (alloc<EhInputSection<ELFT>>())
-        EhInputSection<ELFT>(this, &Sec, Name);
+    return make<EhInputSection<ELFT>>(this, &Sec, Name);
 
   if (shouldMerge(Sec))
-    return new (alloc<MergeInputSection<ELFT>>())
-        MergeInputSection<ELFT>(this, &Sec, Name);
-  return new (alloc<InputSection<ELFT>>()) InputSection<ELFT>(this, &Sec, Name);
+    return make<MergeInputSection<ELFT>>(this, &Sec, Name);
+  return make<InputSection<ELFT>>(this, &Sec, Name);
 }
 
 template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() {
@@ -825,13 +822,13 @@ static InputFile *createELFFile(MemoryBu
 
   InputFile *Obj;
   if (Size == ELFCLASS32 && Endian == ELFDATA2LSB)
-    Obj = new (alloc<T<ELF32LE>>()) T<ELF32LE>(MB);
+    Obj = make<T<ELF32LE>>(MB);
   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)
-    Obj = new (alloc<T<ELF64LE>>()) T<ELF64LE>(MB);
+    Obj = make<T<ELF64LE>>(MB);
   else if (Size == ELFCLASS64 && Endian == ELFDATA2MSB)
-    Obj = new (alloc<T<ELF64BE>>()) T<ELF64BE>(MB);
+    Obj = make<T<ELF64BE>>(MB);
   else
     fatal("invalid file class: " + MB.getBufferIdentifier());
 

Modified: lld/trunk/ELF/Memory.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Memory.h?rev=285760&r1=285759&r2=285760&view=diff
==============================================================================
--- lld/trunk/ELF/Memory.h (original)
+++ lld/trunk/ELF/Memory.h Tue Nov  1 17:53:18 2016
@@ -49,9 +49,9 @@ template <class T> struct SpecificAlloc
 
 // Use this arean if your object have a destructor.
 // 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;
-  return Alloc.Alloc.Allocate();
+  return new (Alloc.Alloc.Allocate()) T(std::forward<U>(Args)...);
 }
 
 void freeArena();




More information about the llvm-commits mailing list