Remove lld/Support/Memory.h.
authorRui Ueyama <ruiu@google.com>
Sun, 18 Dec 2016 14:06:06 +0000 (14:06 +0000)
committerRui Ueyama <ruiu@google.com>
Sun, 18 Dec 2016 14:06:06 +0000 (14:06 +0000)
I thought for a while about how to remove it, but it looks like we
can just copy the file for now. Of course I'm not happy about that,
but it's just less than 50 lines of code, and we already have
duplicate code in Error.h and some other places. I want to solve
them all at once later.

Differential Revision: https://reviews.llvm.org/D27819

llvm-svn: 290062

27 files changed:
lld/COFF/CMakeLists.txt
lld/COFF/Driver.cpp
lld/COFF/DriverUtils.cpp
lld/COFF/InputFiles.cpp
lld/COFF/Memory.h [new file with mode: 0644]
lld/COFF/ModuleDef.cpp
lld/COFF/SymbolTable.cpp
lld/COFF/Symbols.cpp
lld/COFF/Symbols.h
lld/COFF/Writer.cpp
lld/ELF/CMakeLists.txt
lld/ELF/Driver.cpp
lld/ELF/DriverUtils.cpp
lld/ELF/InputFiles.cpp
lld/ELF/InputSection.cpp
lld/ELF/LinkerScript.cpp
lld/ELF/Memory.h [new file with mode: 0644]
lld/ELF/OutputSections.cpp
lld/ELF/SymbolTable.cpp
lld/ELF/SyntheticSections.cpp
lld/ELF/Target.cpp
lld/ELF/Thunks.cpp
lld/ELF/Writer.cpp
lld/include/lld/Support/Memory.h
lld/lib/CMakeLists.txt
lld/lib/Support/CMakeLists.txt [deleted file]
lld/lib/Support/Memory.cpp [deleted file]

index a0ff9c5..70a33b9 100644 (file)
@@ -40,7 +40,6 @@ add_lld_library(lldCOFF
 
   LINK_LIBS
   lldCore
-  lldSupport
   ${PTHREAD_LIB}
 
   DEPENDS
index e2b5e49..dc3a00b 100644 (file)
 #include "Config.h"
 #include "Error.h"
 #include "InputFiles.h"
+#include "Memory.h"
 #include "SymbolTable.h"
 #include "Symbols.h"
 #include "Writer.h"
 #include "lld/Driver/Driver.h"
-#include "lld/Support/Memory.h"
 #include "llvm/ADT/Optional.h"
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/LibDriver/LibDriver.h"
@@ -50,6 +50,10 @@ namespace coff {
 Configuration *Config;
 LinkerDriver *Driver;
 
+BumpPtrAllocator BAlloc;
+StringSaver Saver{BAlloc};
+std::vector<SpecificAllocBase *> SpecificAllocBase::Instances;
+
 bool link(ArrayRef<const char *> Args) {
   Config = make<Configuration>();
   Driver = make<LinkerDriver>();
index cca185d..14dd004 100644 (file)
@@ -16,8 +16,8 @@
 #include "Config.h"
 #include "Driver.h"
 #include "Error.h"
+#include "Memory.h"
 #include "Symbols.h"
-#include "lld/Support/Memory.h"
 #include "llvm/ADT/Optional.h"
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/Object/COFF.h"
index 1484645..0a97c21 100644 (file)
@@ -7,14 +7,15 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "InputFiles.h"
 #include "Chunks.h"
 #include "Config.h"
 #include "Driver.h"
 #include "Error.h"
-#include "InputFiles.h"
+#include "Memory.h"
 #include "SymbolTable.h"
 #include "Symbols.h"
-#include "lld/Support/Memory.h"
+#include "llvm-c/lto.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/Triple.h"
 #include "llvm/ADT/Twine.h"
@@ -29,7 +30,6 @@
 #include "llvm/Support/ErrorOr.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Target/TargetOptions.h"
-#include "llvm-c/lto.h"
 #include <cstring>
 #include <system_error>
 #include <utility>
diff --git a/lld/COFF/Memory.h b/lld/COFF/Memory.h
new file mode 100644 (file)
index 0000000..526f113
--- /dev/null
@@ -0,0 +1,52 @@
+//===- Memory.h -------------------------------------------------*- C++ -*-===//
+//
+//                             The LLVM Linker
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// See ELF/Memory.h
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLD_COFF_MEMORY_H
+#define LLD_COFF_MEMORY_H
+
+#include "llvm/Support/Allocator.h"
+#include "llvm/Support/StringSaver.h"
+#include <vector>
+
+namespace lld {
+namespace coff {
+
+extern llvm::BumpPtrAllocator BAlloc;
+extern llvm::StringSaver Saver;
+
+struct SpecificAllocBase {
+  SpecificAllocBase() { Instances.push_back(this); }
+  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;
+};
+
+template <typename T, typename... U> T *make(U &&... Args) {
+  static SpecificAlloc<T> Alloc;
+  return new (Alloc.Alloc.Allocate()) T(std::forward<U>(Args)...);
+}
+
+inline void freeArena() {
+  for (SpecificAllocBase *Alloc : SpecificAllocBase::Instances)
+    Alloc->reset();
+  BAlloc.Reset();
+}
+}
+}
+
+#endif
index 6c8ef71..a273b6f 100644 (file)
@@ -18,7 +18,7 @@
 
 #include "Config.h"
 #include "Error.h"
-#include "lld/Support/Memory.h"
+#include "Memory.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/Support/StringSaver.h"
index a39bcd2..9cc0b75 100644 (file)
@@ -11,8 +11,8 @@
 #include "Config.h"
 #include "Driver.h"
 #include "Error.h"
+#include "Memory.h"
 #include "Symbols.h"
-#include "lld/Support/Memory.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/LTO/legacy/LTOCodeGenerator.h"
 #include "llvm/Support/Debug.h"
index a8ec4ff..6de85d5 100644 (file)
@@ -10,8 +10,8 @@
 #include "Symbols.h"
 #include "Error.h"
 #include "InputFiles.h"
+#include "Memory.h"
 #include "Strings.h"
-#include "lld/Support/Memory.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/raw_ostream.h"
index 506489b..bc9ad4a 100644 (file)
@@ -12,8 +12,8 @@
 
 #include "Chunks.h"
 #include "Config.h"
+#include "Memory.h"
 #include "lld/Core/LLVM.h"
-#include "lld/Support/Memory.h"
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/Object/Archive.h"
 #include "llvm/Object/COFF.h"
index 98fd6c4..ae18eb4 100644 (file)
 #include "DLL.h"
 #include "Error.h"
 #include "InputFiles.h"
+#include "Memory.h"
 #include "PDB.h"
 #include "SymbolTable.h"
 #include "Symbols.h"
 #include "lld/Core/Parallel.h"
-#include "lld/Support/Memory.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/StringSwitch.h"
index c7c2bf5..2e9d2b9 100644 (file)
@@ -53,7 +53,6 @@ add_lld_library(lldELF
   LINK_LIBS
   lldConfig
   lldCore
-  lldSupport
   ${PTHREAD_LIB}
 
   DEPENDS
index 67f0678..521b618 100644 (file)
@@ -14,6 +14,7 @@
 #include "InputFiles.h"
 #include "InputSection.h"
 #include "LinkerScript.h"
+#include "Memory.h"
 #include "Strings.h"
 #include "SymbolTable.h"
 #include "Target.h"
@@ -21,7 +22,6 @@
 #include "Writer.h"
 #include "lld/Config/Version.h"
 #include "lld/Driver/Driver.h"
-#include "lld/Support/Memory.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/Support/CommandLine.h"
@@ -42,6 +42,10 @@ using namespace lld::elf;
 Configuration *elf::Config;
 LinkerDriver *elf::Driver;
 
+BumpPtrAllocator elf::BAlloc;
+StringSaver elf::Saver{BAlloc};
+std::vector<SpecificAllocBase *> elf::SpecificAllocBase::Instances;
+
 bool elf::link(ArrayRef<const char *> Args, bool CanExitEarly,
                raw_ostream &Error) {
   ErrorCount = 0;
index 179dfd3..6a270a6 100644 (file)
 
 #include "Driver.h"
 #include "Error.h"
+#include "Memory.h"
 #include "ScriptParser.h"
 #include "lld/Config/Version.h"
 #include "lld/Core/Reproduce.h"
-#include "lld/Support/Memory.h"
 #include "llvm/ADT/Optional.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/Triple.h"
index b7f1aea..2a86599 100644 (file)
 #include "Error.h"
 #include "InputSection.h"
 #include "LinkerScript.h"
+#include "Memory.h"
 #include "SymbolTable.h"
 #include "Symbols.h"
 #include "SyntheticSections.h"
-#include "lld/Support/Memory.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/Bitcode/BitcodeReader.h"
 #include "llvm/CodeGen/Analysis.h"
index 8382a6b..963995e 100644 (file)
 #include "Error.h"
 #include "InputFiles.h"
 #include "LinkerScript.h"
+#include "Memory.h"
 #include "OutputSections.h"
 #include "Relocations.h"
 #include "SyntheticSections.h"
 #include "Target.h"
 #include "Thunks.h"
-#include "lld/Support/Memory.h"
 #include "llvm/Support/Compression.h"
 #include "llvm/Support/Endian.h"
 #include <mutex>
index 7387c9c..ab6435c 100644 (file)
@@ -15,6 +15,7 @@
 #include "Config.h"
 #include "Driver.h"
 #include "InputSection.h"
+#include "Memory.h"
 #include "OutputSections.h"
 #include "ScriptParser.h"
 #include "Strings.h"
@@ -23,7 +24,6 @@
 #include "SyntheticSections.h"
 #include "Target.h"
 #include "Writer.h"
-#include "lld/Support/Memory.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringRef.h"
diff --git a/lld/ELF/Memory.h b/lld/ELF/Memory.h
new file mode 100644 (file)
index 0000000..e5a04ed
--- /dev/null
@@ -0,0 +1,67 @@
+//===- Memory.h -------------------------------------------------*- C++ -*-===//
+//
+//                             The LLVM Linker
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines arena allocators.
+//
+// Almost all large objects, such as files, sections or symbols, are
+// used for the entire lifetime of the linker once they are created.
+// This usage characteristic makes arena allocator an attractive choice
+// where the entire linker is one arena. With an arena, newly created
+// objects belong to the arena and freed all at once when everything is done.
+// Arena allocators are efficient and easy to understand.
+// Most objects are allocated using the arena allocators defined by this file.
+//
+// If you edit this file, please edit COFF/Memory.h too.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLD_ELF_MEMORY_H
+#define LLD_ELF_MEMORY_H
+
+#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() { Instances.push_back(this); }
+  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 arena if your object has a destructor.
+// Your destructor will be invoked from freeArena().
+template <typename T, typename... U> T *make(U &&... Args) {
+  static SpecificAlloc<T> Alloc;
+  return new (Alloc.Alloc.Allocate()) T(std::forward<U>(Args)...);
+}
+
+inline void freeArena() {
+  for (SpecificAllocBase *Alloc : SpecificAllocBase::Instances)
+    Alloc->reset();
+  BAlloc.Reset();
+}
+}
+}
+
+#endif
index f8daa57..cfb8ac9 100644 (file)
 #include "Config.h"
 #include "EhFrame.h"
 #include "LinkerScript.h"
+#include "Memory.h"
 #include "Strings.h"
 #include "SymbolTable.h"
 #include "SyntheticSections.h"
 #include "Target.h"
 #include "Threads.h"
-#include "lld/Support/Memory.h"
 #include "llvm/Support/Dwarf.h"
 #include "llvm/Support/MD5.h"
 #include "llvm/Support/MathExtras.h"
index 2d72735..2578f6e 100644 (file)
@@ -18,8 +18,8 @@
 #include "Config.h"
 #include "Error.h"
 #include "LinkerScript.h"
+#include "Memory.h"
 #include "Symbols.h"
-#include "lld/Support/Memory.h"
 #include "llvm/ADT/STLExtras.h"
 
 using namespace llvm;
index 96a4f70..ea5e924 100644 (file)
@@ -19,6 +19,7 @@
 #include "Error.h"
 #include "InputFiles.h"
 #include "LinkerScript.h"
+#include "Memory.h"
 #include "OutputSections.h"
 #include "Strings.h"
 #include "SymbolTable.h"
@@ -26,7 +27,6 @@
 #include "Threads.h"
 #include "Writer.h"
 #include "lld/Config/Version.h"
-#include "lld/Support/Memory.h"
 #include "llvm/Support/Dwarf.h"
 #include "llvm/Support/Endian.h"
 #include "llvm/Support/MD5.h"
index cbb70d4..9e292dc 100644 (file)
 #include "Target.h"
 #include "Error.h"
 #include "InputFiles.h"
+#include "Memory.h"
 #include "OutputSections.h"
 #include "Symbols.h"
 #include "SyntheticSections.h"
 #include "Thunks.h"
 #include "Writer.h"
-#include "lld/Support/Memory.h"
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/Object/ELF.h"
 #include "llvm/Support/ELF.h"
index e7a1efe..34b630a 100644 (file)
 #include "Config.h"
 #include "Error.h"
 #include "InputSection.h"
+#include "Memory.h"
 #include "OutputSections.h"
 #include "Symbols.h"
 #include "Target.h"
-#include "lld/Support/Memory.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/ELF.h"
 #include "llvm/Support/Endian.h"
index d1c1ecd..e8d517d 100644 (file)
 #include "Writer.h"
 #include "Config.h"
 #include "LinkerScript.h"
+#include "Memory.h"
 #include "OutputSections.h"
 #include "Relocations.h"
 #include "Strings.h"
 #include "SymbolTable.h"
 #include "SyntheticSections.h"
 #include "Target.h"
-#include "lld/Support/Memory.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/Support/FileOutputBuffer.h"
index 1ed5ec9..46db4a3 100644 (file)
@@ -35,7 +35,7 @@ extern llvm::StringSaver Saver;
 // These two classes are hack to keep track of all
 // SpecificBumpPtrAllocator instances.
 struct SpecificAllocBase {
-  SpecificAllocBase();
+  SpecificAllocBase() { Instances.push_back(this); }
   virtual ~SpecificAllocBase() = default;
   virtual void reset() = 0;
   static std::vector<SpecificAllocBase *> Instances;
@@ -53,7 +53,11 @@ template <typename T, typename... U> inline T *make(U &&... Args) {
   return new (Alloc.Alloc.Allocate()) T(std::forward<U>(Args)...);
 }
 
-void freeArena();
+inline void freeArena() {
+  for (SpecificAllocBase *Alloc : SpecificAllocBase::Instances)
+    Alloc->reset();
+  BAlloc.Reset();
+}
 }
 
 #endif
index acd5322..699f5e9 100644 (file)
@@ -2,4 +2,3 @@ add_subdirectory(Config)
 add_subdirectory(Core)
 add_subdirectory(Driver)
 add_subdirectory(ReaderWriter)
-add_subdirectory(Support)
diff --git a/lld/lib/Support/CMakeLists.txt b/lld/lib/Support/CMakeLists.txt
deleted file mode 100644 (file)
index 18dccdb..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-add_lld_library(lldSupport
-  Memory.cpp
-
-  ADDITIONAL_HEADER_DIRS
-  ${LLD_INCLUDE_DIR}/lld/Support
-
-  LINK_LIBS
-  LLVMSupport
-)
diff --git a/lld/lib/Support/Memory.cpp b/lld/lib/Support/Memory.cpp
deleted file mode 100644 (file)
index 9bdcdcc..0000000
+++ /dev/null
@@ -1,27 +0,0 @@
-//===- Memory.cpp -----------------------------------------------*- C++ -*-===//
-//
-//                             The LLVM Linker
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-
-#include "lld/Support/Memory.h"
-
-using namespace llvm;
-
-namespace lld {
-BumpPtrAllocator BAlloc;
-StringSaver Saver{BAlloc};
-
-SpecificAllocBase::SpecificAllocBase() { Instances.push_back(this); }
-
-std::vector<SpecificAllocBase *> SpecificAllocBase::Instances;
-
-void freeArena() {
-  for (SpecificAllocBase *Alloc : SpecificAllocBase::Instances)
-    Alloc->reset();
-  BAlloc.Reset();
-}
-}