llvm.org GIT mirror llvm / d77ce81
[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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@365915 91177308-0d34-0410-b5e6-96231b3b80d8 Fangrui Song 4 months ago
3 changed file(s) with 11 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
1818 // 1. Setting up a signal handler so that pretty stack trace is printed out
1919 // if a process crashes.
2020 //
21 // 2. If running on Windows, obtain command line arguments using a
21 // 2. Set up the global new-handler which is called when a memory allocation
22 // attempt fails.
23 //
24 // 3. If running on Windows, obtain command line arguments using a
2225 // multibyte character-aware API and convert arguments into UTF-8
2326 // encoding, so that you can assume that command line arguments are
2427 // always encoded in UTF-8 on any platform.
185185 llvm::report_bad_alloc_error("Allocation failed");
186186 }
187187
188 // Installs new handler that causes crash on allocation failure. It does not
189 // need to be called explicitly, if this file is linked to application, because
190 // in this case it is called during construction of 'new_handler_installer'.
188 // Installs new handler that causes crash on allocation failure. It is called by
189 // InitLLVM.
191190 void llvm::install_out_of_memory_new_handler() {
192 static bool out_of_memory_new_handler_installed = false;
193 if (!out_of_memory_new_handler_installed) {
194 std::set_new_handler(out_of_memory_new_handler);
195 out_of_memory_new_handler_installed = true;
196 }
197 }
198
199 // Static object that causes installation of 'out_of_memory_new_handler' before
200 // execution of 'main'.
201 static class NewHandlerInstaller {
202 public:
203 NewHandlerInstaller() {
204 install_out_of_memory_new_handler();
205 }
206 } new_handler_installer;
191 std::new_handler old = std::set_new_handler(out_of_memory_new_handler);
192 (void)old;
193 assert(old == nullptr && "new-handler already installed");
194 }
207195 #endif
208196
209197 void llvm::llvm_unreachable_internal(const char *msg, const char *file,
2222
2323 InitLLVM::InitLLVM(int &Argc, const char **&Argv) : StackPrinter(Argc, Argv) {
2424 sys::PrintStackTraceOnErrorSignal(Argv[0]);
25 install_out_of_memory_new_handler();
2526
2627 #ifdef _WIN32
2728 // We use UTF-8 as the internal character encoding. On Windows,