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

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


On Wed, 18 Feb 2026 16:25:36 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:
> 
>   change second example to use int type

I like the concept, but hate the name. But I haven't come up with a better name
yet.  Hopefully someone else will think of something.

src/hotspot/share/utilities/globalDefinitions.hpp line 181:

> 179: }
> 180: 
> 181: // Returns the size (in bytes) of the expression as an unsigned type that is as small as possible.

How about something like this:

Yields the size (in bytes) of the operand, using the smallest unsigned type
that can represent the size value. The operand may be an expression, which is
an unevaluated operand, or it may be a type. All of the restrictions for
sizeof operands apply to the operand. The result is a constant expression.

This is modeled on the description of sizeof and the terminology from the
"Integral conversions" section. "unevaluated operand" is defined (rather
circularly) early in the "Expressions" chapter.

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

PR Review: https://git.openjdk.org/jdk/pull/29716#pullrequestreview-3820389117
PR Review Comment: https://git.openjdk.org/jdk/pull/29716#discussion_r2824442092


More information about the hotspot-dev mailing list