RFR: 8255082: HotSpot Style Guide should permit noexcept [v3]
Vladimir Kozlov
kvn at openjdk.org
Fri Jun 20 16:56:30 UTC 2025
On Fri, 20 Jun 2025 15:56:47 GMT, Kim Barrett <kbarrett at openjdk.org> wrote:
>> doc/hotspot-style.md line 1140:
>>
>>> 1138: result. If an allocation function is not declared `noexcept` then the compiler
>>> 1139: may elide that checking and handling for a `new` expression calling that
>>> 1140: function.
>>
>> This implies that compiler may generate a `nullptr` check if `noexcept` is specified. Is it true? Is it static (during compilation) check or it can generate runtime check?
>>
>> We usually have explicit checks in such places to catch allocation failure. We are missing check in some places which may lead to crashes (reference through `nullptr`). Can compiler helps here?
>
> An allocation function may be declared as non-throwing (via `noexcept`).
>
> If not declared non-throwing (so "potentially throwing", even though in a
> no-exceptions build environment it won't ever actually throw, and will instead
> probably terminate the program), the compiler's implementation of a using
> `new` expression can assume the allocation function will never return null,
> and does not need to generate any code to handle that possibility.
>
> If declared non-throwing, a using `new` expression must account for the
> possibility that the allocation function might return null. It needs to test
> the result of the allocation function. If it is null then the `new` expression
> must not call the constructor for the type and must return null as its result.
> (Of course, usual compiler optimizations apply, and the null handling code can
> be elided if the compiler can prove the allocation does not in fact ever
> return null, regardless of any exception specifications. That's probably
> pretty much never the case though.)
>
> Because of that, an allocation function that can return null *must* be
> declared non-throwing. And an allocation function that never returns null
> shouldn't be declared non-throwing, as that's just degrading performance for
> no reason.
So we need to have explicit `nullptr` checks or `assert(p != nullprt,"")` in our code.
For example, for next `new()` we can add `assert(p != nullprt,"")` because `AllocateHeap` will exit VM if it can't allocate memory. And we don't need `noexcept` here or we can use `noexcept(false)`. Right?
ALWAYSINLINE void* operator new(size_t size, MemTag mem_tag) {
return AllocateHeap(size, mem_tag);
}
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/25574#discussion_r2159374542
More information about the hotspot-dev
mailing list