diff options
author | Fangrui Song <i@maskray.me> | 2021-12-22 14:36:14 -0800 |
---|---|---|
committer | Fangrui Song <i@maskray.me> | 2021-12-22 14:36:14 -0800 |
commit | 5fc4323eda60ee15ef4c87d989d468cd0e6d77c1 (patch) | |
tree | f21bbf91aa71a177d51b10d4ed9d1b6b2a0868c8 | |
parent | 3cc2161c897ca6c9f1ff09fbc610a6ae40b3b566 (diff) | |
download | llvm-5fc4323eda60ee15ef4c87d989d468cd0e6d77c1.zip llvm-5fc4323eda60ee15ef4c87d989d468cd0e6d77c1.tar.gz llvm-5fc4323eda60ee15ef4c87d989d468cd0e6d77c1.tar.bz2 |
[ELF] Change some global pointers to unique_ptr
Currently the singleton `config` is assigned by `config = make<Configuration>()`
and (if `canExitEarly` is false) destroyed by `lld::freeArena`.
`make<Configuration>` allocates a stab with `malloc(4096)`. This both wastes
memory and bloats the executable (every type instantiates `BumpPtrAllocator`
which costs more than 1KiB code on x86-64).
(No need to worry about `clang::no_destroy`. Regular invocations (`canExitEarly`
is true) call `_Exit` via llvm::sys::Process::ExitNoCleanup.)
Reviewed By: lichray
Differential Revision: https://reviews.llvm.org/D116143
-rw-r--r-- | lld/ELF/Config.h | 3 | ||||
-rw-r--r-- | lld/ELF/Driver.cpp | 12 | ||||
-rw-r--r-- | lld/ELF/Driver.h | 2 | ||||
-rw-r--r-- | lld/ELF/LinkerScript.cpp | 2 | ||||
-rw-r--r-- | lld/ELF/LinkerScript.h | 2 | ||||
-rw-r--r-- | lld/ELF/SymbolTable.cpp | 2 | ||||
-rw-r--r-- | lld/ELF/SymbolTable.h | 2 |
7 files changed, 13 insertions, 12 deletions
diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h index 0c8ce62..b3d5219 100644 --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -22,6 +22,7 @@ #include "llvm/Support/GlobPattern.h" #include "llvm/Support/PrettyStackTrace.h" #include <atomic> +#include <memory> #include <vector> namespace lld { @@ -343,7 +344,7 @@ struct Configuration { }; // The only instance of Configuration struct. -extern Configuration *config; +extern std::unique_ptr<Configuration> config; // The first two elements of versionDefinitions represent VER_NDX_LOCAL and // VER_NDX_GLOBAL. This helper returns other elements. diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index d000744..05cc4db 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -71,8 +71,8 @@ using namespace llvm::support; using namespace lld; using namespace lld::elf; -Configuration *elf::config; -LinkerDriver *elf::driver; +std::unique_ptr<Configuration> elf::config; +std::unique_ptr<LinkerDriver> elf::driver; static void setConfigs(opt::InputArgList &args); static void readConfigs(opt::InputArgList &args); @@ -111,10 +111,10 @@ bool elf::link(ArrayRef<const char *> args, bool canExitEarly, errorHandler().exitEarly = canExitEarly; stderrOS.enable_colors(stderrOS.has_colors()); - config = make<Configuration>(); - driver = make<LinkerDriver>(); - script = make<LinkerScript>(); - symtab = make<SymbolTable>(); + config = std::make_unique<Configuration>(); + driver = std::make_unique<LinkerDriver>(); + script = std::make_unique<LinkerScript>(); + symtab = std::make_unique<SymbolTable>(); partitions = {Partition()}; diff --git a/lld/ELF/Driver.h b/lld/ELF/Driver.h index 96d0400..5961e1f 100644 --- a/lld/ELF/Driver.h +++ b/lld/ELF/Driver.h @@ -22,7 +22,7 @@ namespace lld { namespace elf { -extern class LinkerDriver *driver; +extern std::unique_ptr<class LinkerDriver> driver; class LinkerDriver { public: diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index 782a658f..38b1b90 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -47,7 +47,7 @@ using namespace llvm::support::endian; using namespace lld; using namespace lld::elf; -LinkerScript *elf::script; +std::unique_ptr<LinkerScript> elf::script; static bool isSectionPrefix(StringRef prefix, StringRef name) { return name.consume_front(prefix) && (name.empty() || name[0] == '.'); diff --git a/lld/ELF/LinkerScript.h b/lld/ELF/LinkerScript.h index badc4d1..5e9a354 100644 --- a/lld/ELF/LinkerScript.h +++ b/lld/ELF/LinkerScript.h @@ -366,7 +366,7 @@ public: std::vector<const InputSectionBase *> orphanSections; }; -extern LinkerScript *script; +extern std::unique_ptr<LinkerScript> script; } // end namespace elf } // end namespace lld diff --git a/lld/ELF/SymbolTable.cpp b/lld/ELF/SymbolTable.cpp index ce77f92..5c7b8b0 100644 --- a/lld/ELF/SymbolTable.cpp +++ b/lld/ELF/SymbolTable.cpp @@ -29,7 +29,7 @@ using namespace llvm::ELF; using namespace lld; using namespace lld::elf; -SymbolTable *elf::symtab; +std::unique_ptr<SymbolTable> elf::symtab; void SymbolTable::wrap(Symbol *sym, Symbol *real, Symbol *wrap) { // Redirect __real_foo to the original foo and foo to the original __wrap_foo. diff --git a/lld/ELF/SymbolTable.h b/lld/ELF/SymbolTable.h index 54c4b11..568e455 100644 --- a/lld/ELF/SymbolTable.h +++ b/lld/ELF/SymbolTable.h @@ -91,7 +91,7 @@ private: llvm::Optional<llvm::StringMap<std::vector<Symbol *>>> demangledSyms; }; -extern SymbolTable *symtab; +extern std::unique_ptr<SymbolTable> symtab; } // namespace elf } // namespace lld |