[llvm] r365915 - [Support] Move the static initializer install_out_memory_new_handler to InitLLVM

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 12 09:23:25 PDT 2019


Author: maskray
Date: Fri Jul 12 09:23:25 2019
New Revision: 365915

URL: http://llvm.org/viewvc/llvm-project?rev=365915&view=rev
Log:
[Support] Move the static initializer install_out_memory_new_handler to InitLLVM

An application linking against LLVMSupport should not get the gratuitous
set::std_new_handler call.

Reviewed By: jfb

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

Modified:
    llvm/trunk/include/llvm/Support/InitLLVM.h
    llvm/trunk/lib/Support/ErrorHandling.cpp
    llvm/trunk/lib/Support/InitLLVM.cpp

Modified: llvm/trunk/include/llvm/Support/InitLLVM.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/InitLLVM.h?rev=365915&r1=365914&r2=365915&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/InitLLVM.h (original)
+++ llvm/trunk/include/llvm/Support/InitLLVM.h Fri Jul 12 09:23:25 2019
@@ -19,7 +19,10 @@
 //  1. Setting up a signal handler so that pretty stack trace is printed out
 //     if a process crashes.
 //
-//  2. If running on Windows, obtain command line arguments using a
+//  2. Set up the global new-handler which is called when a memory allocation
+//     attempt fails.
+//
+//  3. If running on Windows, obtain command line arguments using a
 //     multibyte character-aware API and convert arguments into UTF-8
 //     encoding, so that you can assume that command line arguments are
 //     always encoded in UTF-8 on any platform.

Modified: llvm/trunk/lib/Support/ErrorHandling.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/ErrorHandling.cpp?rev=365915&r1=365914&r2=365915&view=diff
==============================================================================
--- llvm/trunk/lib/Support/ErrorHandling.cpp (original)
+++ llvm/trunk/lib/Support/ErrorHandling.cpp Fri Jul 12 09:23:25 2019
@@ -186,25 +186,13 @@ static void out_of_memory_new_handler()
   llvm::report_bad_alloc_error("Allocation failed");
 }
 
-// Installs new handler that causes crash on allocation failure. It does not
-// need to be called explicitly, if this file is linked to application, because
-// in this case it is called during construction of 'new_handler_installer'.
+// Installs new handler that causes crash on allocation failure. It is called by
+// InitLLVM.
 void llvm::install_out_of_memory_new_handler() {
-  static bool out_of_memory_new_handler_installed = false;
-  if (!out_of_memory_new_handler_installed) {
-    std::set_new_handler(out_of_memory_new_handler);
-    out_of_memory_new_handler_installed = true;
-  }
+  std::new_handler old = std::set_new_handler(out_of_memory_new_handler);
+  (void)old;
+  assert(old == nullptr && "new-handler already installed");
 }
-
-// Static object that causes installation of 'out_of_memory_new_handler' before
-// execution of 'main'.
-static class NewHandlerInstaller {
-public:
-  NewHandlerInstaller() {
-    install_out_of_memory_new_handler();
-  }
-} new_handler_installer;
 #endif
 
 void llvm::llvm_unreachable_internal(const char *msg, const char *file,

Modified: llvm/trunk/lib/Support/InitLLVM.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/InitLLVM.cpp?rev=365915&r1=365914&r2=365915&view=diff
==============================================================================
--- llvm/trunk/lib/Support/InitLLVM.cpp (original)
+++ llvm/trunk/lib/Support/InitLLVM.cpp Fri Jul 12 09:23:25 2019
@@ -23,6 +23,7 @@ using namespace llvm::sys;
 
 InitLLVM::InitLLVM(int &Argc, const char **&Argv) : StackPrinter(Argc, Argv) {
   sys::PrintStackTraceOnErrorSignal(Argv[0]);
+  install_out_of_memory_new_handler();
 
 #ifdef _WIN32
   // We use UTF-8 as the internal character encoding. On Windows,




More information about the llvm-commits mailing list