[lld] 5fc4323 - [ELF] Change some global pointers to unique_ptr
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Wed Dec 22 14:36:20 PST 2021
Author: Fangrui Song
Date: 2021-12-22T14:36:14-08:00
New Revision: 5fc4323eda60ee15ef4c87d989d468cd0e6d77c1
URL: https://github.com/llvm/llvm-project/commit/5fc4323eda60ee15ef4c87d989d468cd0e6d77c1
DIFF: https://github.com/llvm/llvm-project/commit/5fc4323eda60ee15ef4c87d989d468cd0e6d77c1.diff
LOG: [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
Added:
Modified:
lld/ELF/Config.h
lld/ELF/Driver.cpp
lld/ELF/Driver.h
lld/ELF/LinkerScript.cpp
lld/ELF/LinkerScript.h
lld/ELF/SymbolTable.cpp
lld/ELF/SymbolTable.h
Removed:
################################################################################
diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h
index 0c8ce62c566a1..b3d5219ff57b7 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 d0007449a2b14..05cc4db5af749 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 96d040041c5ab..5961e1f694729 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 782a658fdaeda..38b1b9093cd55 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 badc4d126be8f..5e9a3542c4268 100644
--- a/lld/ELF/LinkerScript.h
+++ b/lld/ELF/LinkerScript.h
@@ -366,7 +366,7 @@ class LinkerScript final {
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 ce77f9233feaf..5c7b8b00a6eea 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 54c4b1169ed1a..568e4559ec346 100644
--- a/lld/ELF/SymbolTable.h
+++ b/lld/ELF/SymbolTable.h
@@ -91,7 +91,7 @@ class SymbolTable {
llvm::Optional<llvm::StringMap<std::vector<Symbol *>>> demangledSyms;
};
-extern SymbolTable *symtab;
+extern std::unique_ptr<SymbolTable> symtab;
} // namespace elf
} // namespace lld
More information about the llvm-commits
mailing list