[PATCH] D34753: [Support] - Add bad alloc error handler for handling allocation malfunctions

Matthias Braun via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 10 11:34:32 PDT 2017


MatzeB added a comment.

So how about

  #include <new>
  ...
  x = malloc(...);
  if (x == nullptr)
    throw std::bad_alloc();

that way running out of memory in malloc and new would behave the same way.



================
Comment at: include/llvm/Support/ErrorHandling.h:82
+
+/// install_bad_alloc_error_handler - Installs a new bad alloc error handler
+/// that should be used whenever a bad alloc error, e.g. failing malloc/calloc,
----------------
Do not repeat the name of the function in the documentation like this (you see the pattern a lot in older code, but we avoid it in new code).


================
Comment at: lib/Support/ErrorHandling.cpp:130
+  } else {
+    assert(false && "Bad alloc error: Unable to allocate memory.\n");
+  }
----------------
How about:
```
#ifdef __EXCEPTIONS
  throw std::bad_alloc();
#else
  report_fatal_error("Unable to allocate memory.\n");
#endif
```

(__EXCEPTIONS is defined by clang/gcc when exceptions are enabled. An actual patch would probably better put that into llvm/Support/Compiler.h and define some LLVM specific constant like LLVM_ENABLE_EXCEPTIONS so we can add the msvc specific thing later).


================
Comment at: lib/Support/Mutex.cpp:52-54
+  if (mutex == nullptr) {
+    report_bad_alloc_error("Mutex allocation failed");
+  }
----------------
We omit `{}` in llvm in simple if and loop statements.


https://reviews.llvm.org/D34753





More information about the llvm-commits mailing list