[libcxx-commits] [PATCH] D146379: [libc++] These throwing allocation functions shouldn't return null.

Betzalel Ganot via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue May 2 05:32:32 PDT 2023


BetzalelG added a comment.

@DianQK I just looked at the implementation in the github mirror of gcc (libstdc++ v3):
https://github.com/gcc-mirror/gcc/tree/master/libstdc%2B%2B-v3
Particularly this file:
https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/libsupc%2B%2B/new_op.cc
Other operator new functions are in the other files in the folder, such as:
https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/libsupc%2B%2B/new_opnt.cc

The relevant line used in operator new is `_GLIBCXX_THROW_OR_ABORT(bad_alloc());`
My understanding of this implementation is:

- `_GLIBCXX_THROW_OR_ABORT` is defined to throw the given exception when `__cpp_exceptions` is defined, and abort otherwise
- `__try` and `__catch` are used in the nothrow version. (`__try` and `__catch` are defined so that if `__cpp_exceptions` isn't defined, they will be defined as `if(true)` and `if(false)` respectively)

So in summary:
When exceptions are defined - regular new will throw, nothrow will return null
When exceptions are undefined - regular new will abort, nothrow will abort as well


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146379/new/

https://reviews.llvm.org/D146379



More information about the libcxx-commits mailing list