RFR: 8377895: Create sizeof_auto, to reduce narrowing conversions [v3]

Kim Barrett kbarrett at openjdk.org
Wed Feb 18 21:06:29 UTC 2026


On Wed, 18 Feb 2026 13:02:03 GMT, Leo Korinth <lkorinth at openjdk.org> wrote:

>> `sizeof_auto` is created so that we can reduce the amount of casting and thus make the code more type safe. The normal `sizeof` will return a `size_t` although the size of most stuff can be represented in a `uint8_t`. `sizeof_auto` will return the size in an as small unsigned type as is possible. The result is that expressions that uses `sizeof_auto` can convert to most integral types, and no explicit narrowing cast will be needed.
>> 
>> The change in `cpCache.hpp` is an example where we would get an implicit narrowing warning (if we had those enabled) using normal `sizeof`, but where `sizeof_auto` will not make the expression a `size_t` .
>
> Leo Korinth has updated the pull request incrementally with one additional commit since the last revision:
> 
>   using Kim's suggestions

test/hotspot/gtest/utilities/test_globalDefinitions.cpp line 354:

> 352:   static_assert(sizeof(sizeof_auto(char[std::numeric_limits<uint16_t>::max() + 1LL])) == sizeof(uint32_t));
> 353: #if defined(_LP64) && !defined(_WINDOWS)
> 354:   // char array sometimes limited to 2 gig length on 32 bit platforms (signed), and on windows (even 64 bit!!!)

Does it work to have an array of some larger type (like a struct with a bunch of members)
to get a larger sizeof?  Or is it that somehow the array byte size is limited?  The latter would
seem pretty surprising to me, but I don't know windows at all well.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/29716#discussion_r2822771456


More information about the hotspot-dev mailing list