Page MenuHomeFreeBSD

Have a type-agnostic macro to determine the [u]int maximum value
Needs ReviewPublic

Authored by rscheff on Jan 13 2023, 11:02 AM.
Tags
None
Referenced Files
Unknown Object (File)
Sat, Oct 25, 7:11 AM
Unknown Object (File)
Wed, Oct 22, 3:08 PM
Unknown Object (File)
Wed, Oct 22, 3:08 PM
Unknown Object (File)
Wed, Oct 22, 2:24 AM
Unknown Object (File)
Thu, Oct 9, 1:59 AM
Unknown Object (File)
Sep 11 2025, 3:34 PM
Unknown Object (File)
Sep 11 2025, 12:58 AM
Unknown Object (File)
Sep 7 2025, 3:25 AM
Subscribers
None

Details

Summary

Currently, when the size of a variable is changed, any
comparisons with INT_MAX, CHAR_MAX and so on have to be manually adjusted.

While these hardcoded values have some portability as they map to
machine-specific values, the case of changing the type of a variable is
not covered.

This provides a (semi) portable way of determining the maximum value
a variable of a given size can hold.

The assumption is to run on a 8-bit per byte architecture, and it will
not work for bit-fields.

Other than this, the entire range of UCHAR_MAX, UINT_MAX, USHRT_MAX,
ULONG_MAX, ULLONG_MAX could be replaced with UTYPE_MAX(variable)

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Passed
Unit
No Test Coverage
Build Status
Buildable 49079
Build 45968: arc lint + arc unit

Event Timeline

rscheff created this revision.
sys/sys/limits.h
109

U for unsigned
S for signed

UTYPE_MAX
STYPE_MAX

Then also declare the minimum variant?

UTYPE_MIN
STYPE_MIN

Thinking more about this, maybe follow the style of min_t, like

U_MAX_T(type)
S_MAX_T(type)
U_MIN_T(type)
S_MIN_T(type)

It's more readable?