RFR: 8357086: os::xxx functions returning memory size should return size_t [v4]

Kim Barrett kbarrett at openjdk.org
Wed Jun 11 12:25:32 UTC 2025


On Wed, 11 Jun 2025 08:35:30 GMT, Stefan Karlsson <stefank at openjdk.org> wrote:

> Are you up for making an experiment that changes `total_swap_space` and `free_swap_space` to return two values: one the actual value in `size_t` and the other an error code that gets set whenever we hit an error?
> 
> The two proposed alternatives for this would be:
> 
>     1. returning something containing the two values (struct, Pair, Tuple, array, ...)
> 
>     2. Return the size_t and using an an out put parameter to signal an error (or vice versa)
> 
> 
> I think the fan-out of that will not be too bad and the likely outcome is that it is clearer that the code is propagating errors.

I think the concern about the documented range for ssize_t is perhaps
overblown. I think that specification is driven in part by continuing support
for non-two's-complement. I think that for any platform we're going to
support, ssize_t is equivalent to std::make_unsigned_t<size_t>.  We could
ensure that with a static_assert somewhere.

That some functions use -1 for a non-error and perhaps some other negative
value to indicate an error seems not particularly different to me than that
some functions return an error code (usually an int, so who knows whether
positive or negative).

So I would probably be okay with using ssize_t.

I'm not generally a fan of out parameters.

I think there are examples in HotSpot of functions that return a dedicated
struct containing a result and an error indication. The standard uses
std::pair for that sort of thing (until C++23's std::expected<T, E>, which is
likely a long way off for us). I prefer the dedicated struct approach, as it
provides meaningful names. That's also why I'm not a fan of tuples or arrays
for this purpose.

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

PR Comment: https://git.openjdk.org/jdk/pull/25450#issuecomment-2962482033


More information about the hotspot-dev mailing list