RFR: 8377895: Create sizeof_auto, to reduce narrowing conversions [v6]
Kim Barrett
kbarrett at openjdk.org
Thu Feb 19 21:52:38 UTC 2026
On Thu, 19 Feb 2026 11:13:26 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:
>
> suggestion from Kim
Just a couple of optional comment suggestions at this point. Otherwise looks good.
src/hotspot/share/utilities/globalDefinitions.hpp line 189:
> 187: // Example of correct usage of sizeof/sizeof_auto:
> 188: // size_t size = std::numeric_limits<uint32_t>::max() * sizeof(uint16_t); // this will wrap using sizeof_auto, use sizeof to ensure computation using size_t
> 189: // int count = 42 / sizeof_auto(uint16_t); // implicit narrowing conversion or compiler warning/error using stricter compiler flags when using sizeof
Could you shorten these (very) long lines? Just move the description of each snippet
before the snippet, rather than as a same-line comment.
-------------
Marked as reviewed by kbarrett (Reviewer).
PR Review: https://git.openjdk.org/jdk/pull/29716#pullrequestreview-3828418197
PR Review Comment: https://git.openjdk.org/jdk/pull/29716#discussion_r2830069466
More information about the hotspot-dev
mailing list